forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd.js
102 lines (81 loc) · 2.86 KB
/
systemd.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const fs = require('fs');
const execa = require('execa');
const cli = require('../../lib');
const getUid = require('./get-uid');
class SystemdProcessManager extends cli.ProcessManager {
get systemdName() {
return `ghost_${this.instance.name}`;
}
get logSuggestion() {
return 'journalctl -n 50';
}
start() {
this._precheck();
return this.ui.sudo(`systemctl start ${this.systemdName}`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
stop() {
this._precheck();
return this.ui.sudo(`systemctl stop ${this.systemdName}`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
restart() {
this._precheck();
return this.ui.sudo(`systemctl restart ${this.systemdName}`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
isEnabled() {
try {
execa.shellSync(`systemctl is-enabled ${this.systemdName}`);
return true;
} catch (e) {
// Systemd prints out "disabled" if service isn't enabled
// or "failed to get unit file state" if something else goes wrong
if (!e.message.match(/disabled|Failed to get unit file state/)) {
throw e;
}
return false;
}
}
enable() {
return this.ui.sudo(`systemctl enable ${this.systemdName} --quiet`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
disable() {
return this.ui.sudo(`systemctl disable ${this.systemdName} --quiet`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}
isRunning() {
try {
execa.shellSync(`systemctl is-active ${this.systemdName}`);
return true;
} catch (e) {
// Systemd prints out "inactive" if service isn't running
// or "activating" if service hasn't completely started yet
if (!e.message.match(/inactive|activating/)) {
throw e;
}
return false;
}
}
_precheck() {
const uid = getUid(this.instance.dir);
if (!uid) {
throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup linux-user systemd` and try again.')
}
if (fs.existsSync(`/lib/systemd/system/${this.systemdName}.service`)) {
return;
}
throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.');
}
static willRun() {
try {
execa.shellSync('which systemctl', {stdio: 'ignore'});
return true;
} catch (e) {
return false;
}
}
}
module.exports = SystemdProcessManager;