-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
88 lines (77 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const Docker = require('dockerode')
async function upgrade (name, version) {
console.info('Connecting to Docker API...')
const dk = new Docker({ socketPath: '/var/run/docker.sock' })
console.info(`Finding ${name} container...`)
const containers = await dk.listContainers({ all: true })
const wiki = containers.find(c => {
return c.Names.some(n => n === `/${name}` || n === name) && (c.Image.startsWith('ghcr.io/requarks/wiki') || c.Image.startsWith('requarks/wiki'))
})
if (!wiki) {
throw new Error(`Could not find ${name} container.`)
}
console.info(`Found ${name} container (ID ${wiki.Id})`)
const wk = dk.getContainer(wiki.Id)
const wkConfig = await wk.inspect()
if (wiki.State !== 'exited') {
console.info('Attempting to stop container...')
await wk.stop()
}
console.info('Container is stopped.')
console.info('Removing container...')
await wk.remove({ v: true })
console.info('Container has been removed.')
console.info('Pulling latest Wiki.js image...')
await new Promise((resolve, reject) => {
dk.pull(`ghcr.io/requarks/wiki:${version}`, (err, stream) => {
if (err) { return reject(err) }
dk.modem.followProgress(stream, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
})
console.info('Recreating container...')
const wkn = await dk.createContainer({
name: name,
Image: `ghcr.io/requarks/wiki:${version}`,
Env: wkConfig.Config.Env,
ExposedPorts: wkConfig.Config.ExposedPorts,
Hostname: name,
HostConfig: wkConfig.HostConfig
})
console.info('Starting container...')
await wkn.start()
console.info(`Container ${wkn.id} started successfully.`)
}
async function main () {
const fastify = require('fastify')({ logger: true })
fastify.get('/', async (request, reply) => {
return { ok: true }
})
fastify.post('/upgrade/:version?', async (request, reply) => {
try {
upgrade(
request.query.container || 'wiki',
request.params.version || '2'
)
return { started: true }
} catch (err) {
console.error(err)
return { started: false, error: err.message}
}
})
try {
await fastify.listen({
port: 80,
host: '0.0.0.0'
})
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
main()