-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.js
More file actions
91 lines (79 loc) · 2.47 KB
/
Copy pathmanagement.js
File metadata and controls
91 lines (79 loc) · 2.47 KB
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
89
90
91
import util from 'node:util'
import child_process from 'node:child_process'
const exec = util.promisify(child_process.exec)
const containerPrefix = process.env.CONTAINER_PREFIX ?? 'ss13-'
const serversRoot = process.env.SERVERS_ROOT ?? '/servers'
const restarting = new Set()
function getServerPath(server) {
return `${serversRoot}/${server}/game`
}
function serverToContainer(server) {
return `${containerPrefix}${server}`
}
async function getServers() {
const { stdout: containers } = await exec(`docker container ls --format='{{.Names}}'`)
return containers
.trim()
.split('\n')
.filter((i) => i.startsWith(containerPrefix))
.map((i) => i.replace(containerPrefix, ''))
}
async function getStateStatus(server) {
const { stdout } = await exec(
`docker inspect -f '{{.State.Status}}' ${serverToContainer(server)}`
)
return stdout.trim()
}
async function getStateHealth(server) {
const { stdout } = await exec(
`docker inspect -f '{{.State.Health.Status}}' ${serverToContainer(server)}`
)
return stdout.trim()
}
async function getStateStartedAt(server) {
const { stdout } = await exec(
`docker inspect -f '{{.State.StartedAt}}' ${serverToContainer(server)}`
)
return stdout.trim()
}
export async function getServerStatus(server, servers = null) {
if (!servers) {
servers = await getServers()
if (!servers.includes(server)) throw new Error('Server not found')
}
return {
restarting: restarting.has(server),
status: await getStateStatus(server),
health: await getStateHealth(server),
startedAt: await getStateStartedAt(server),
}
}
export async function getAllServersStatus() {
const servers = await getServers()
const ret = {}
for (const server of servers) {
ret[server] = await getServerStatus(server, servers)
}
return ret
}
export async function restartServer(server) {
if (restarting.has(server)) throw new Error('Already restarting')
const servers = await getServers()
if (!servers.includes(server)) throw new Error('Server not found')
const health = await getStateHealth(server)
if (health === 'starting') throw new Error('Currently starting')
restarting.add(server)
const container = serverToContainer(server)
const projectDir = getServerPath(server) + '/tools/server'
child_process.exec(
[
`docker exec ${container} pkill -USR2 DreamDaemon`,
`cd "${projectDir}"`,
`./dc down`,
`./dc up -d`,
].join(' && '),
() => {
restarting.delete(server)
}
)
}