-
Notifications
You must be signed in to change notification settings - Fork 8
/
manager.js
247 lines (224 loc) · 9.19 KB
/
manager.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const fs = require('fs');
const pm2 = require('pm2');
const { exec } = require('child_process');
const apiPath = require.resolve('cast-web-api');
console.log(apiPath);
class Manager {
static start(argument) {
return new Promise((resolve, reject) => {
Manager.isReady().then(ready => {
pm2.start({
script: apiPath,
name: 'cast-web-api',
args: argument,
}, (err, proc) => {
pm2.disconnect();
if (err) {
reject(err);
} else {
Manager.status()
.then(status => resolve(status))
.catch(err => resolve(proc));
}
});
});
});
}
static stop() {
return new Promise((resolve, reject) => {
Manager.isReady().then(ready => {
pm2.stop('cast-web-api', (err, proc) => {
pm2.disconnect(); //TODO: better solution
if (err) {
reject(err);
}
resolve(proc);
});
});
});
}
static status() {
return new Promise(resolve => {
let promises = [];
Manager.getProcessDescriptionList()
.then(processDescriptionList => {
processDescriptionList.forEach(pd => promises.push(Manager.sendToProcessDesc(pd)));
Promise.all(promises)
.then(addresses => {
resolve(addresses);
pm2.disconnect();
}); //TODO: add timeout here
})
.catch(err => {
console.error(err);
pm2.disconnect();
resolve([]);
});
});
}
static startup() {
return new Promise((resolve, reject) => {
let windows = process.platform === "win32";
Manager.save(windows)
.then(() => {
if (windows) resolve(Manager.startupWin());
else resolve(Manager.startupPm2());
})
.catch(error => {
reject({error: {message: "Couldn't save pm2 processes"}, stdout: "", stderr: error});
})
})
}
static startupPm2() {
return new Promise((resolve, reject) => {
let cmd = require.resolve('pm2').replace('index.js', 'bin/pm2');
exec(`${cmd} startup`, (error, stdout, stderr) => {
if (error || stderr) {
if (stdout && stdout.includes("sudo env")) {
reject({error: {message:"Permissions required. To do this, just copy/paste and run this command: \n"}, stdout: stdout, stderr: stderr});
} else {
reject({error: error, stdout: stdout, stderr: stderr});
}
}
resolve(stdout);
});
});
// platform, opts, cb //TODO: documentation parameters (platform, errback) broke, this param hack works for now
// pm2.startup(false, { args:[0, { name: function () { return "startup"; }}] }, (err, result) => {})
}
static startupWin() {
return new Promise((resolve, reject) => {
let pm2WindowsStartupPath = require.resolve('pm2-windows-startup');
Manager.fixWinResurrectBat(pm2WindowsStartupPath.replace('index.js', 'pm2_resurrect.cmd'))
.then(()=>{
reject({error: {message: "Windows, to auto start, just copy/paste and run the command below: \n"}, stdout: `node ${pm2WindowsStartupPath} install`, stderr: ""});
})
.catch(error => {reject(error)});
});
}
static unstartup() {
let windows = process.platform === "win32";
if (windows) return Manager.unstartupWin();
else return Manager.unstartupPm2();
}
static unstartupPm2() {
return new Promise((resolve, reject) => {
let cmd = require.resolve('pm2').replace('index.js', 'bin/pm2');
exec(`${cmd} unstartup`, (error, stdout, stderr) => {
if (error || stderr) {
if (stdout && stdout.includes("sudo env")) {
reject({error: {message:"Permissions required. To do this, just copy/paste and run this command: \n"}, stdout: stdout, stderr: stderr});
} else {
reject({error: error, stdout: stdout, stderr: stderr});
}
}
resolve(stdout);
});
});
}
static unstartupWin() {
return new Promise((resolve, reject) => {
let cmd = `${require.resolve('pm2-windows-startup')} uninstall`;
reject({error: {message: "Windows, to stop auto start, just copy/paste and run the command below: \n"}, stdout: `node ${cmd}`, stderr: ""});
});
}
static save(windows) {
return new Promise((resolve, reject) => {
let cmd = `${require.resolve('pm2').replace('index.js', 'bin/pm2')} save`;
if (windows) cmd = `node ${require.resolve('pm2').replace('index.js', 'bin\\pm2')} save`;
exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
reject({error: error, stdout: stdout, stderr: stderr});
}
resolve(stdout);
});
});
}
static fixWinResurrectBat(resurrectBatPath) {
return new Promise((resolve, reject) => {
fs.readFile(resurrectBatPath, 'utf8', (err, data) => {
if (err) reject(err); //TODO: adapt to custom object format
if (!data.includes('\\pm2')) {
let newPM2Path = `node ${require.resolve('pm2').replace('index.js', 'bin\\pm2')}`;
let newResurrectBat = data.replace('pm2', newPM2Path);
fs.writeFile(resurrectBatPath, newResurrectBat, 'utf8', err => {
if (err) reject(err); //TODO: adapt to custom object format
else resolve(true);
});
} else {
resolve(true);
}
});
});
}
static getProcessDescriptionList() {
return new Promise((resolve, reject) => {
Manager.isReady().then(ready => {
pm2.describe('cast-web-api', (err, processDescriptionList) => {
// pm2.disconnect(); //TODO: better solution
if (err) {
reject(err);
}
resolve(processDescriptionList);
});
});
});
}
static sendToProcessDesc(processDescription) {
return new Promise(resolve => {
if (processDescription.pm2_env.status === 'online') {
try {
pm2.sendDataToProcessId(processDescription.pm_id, {
type : 'process:msg',
data : {
some : 'data',
hello : true
},
topic: 'some topic'
}, function(err, res) {
if (err) {
console.error(err);
resolve(false);
}
});
} catch (e) {
console.error(e);
resolve(false);
}
pm2.launchBus((err, bus) => {
bus.on('process:msg', (packet) => {
processDescription.address = packet.data.address;
resolve(processDescription);
});
});
} else {
resolve(false);
}
})
}
static isReady() {
return new Promise((resolve, reject) => {
pm2.connect(err => {
if (err) {
reject(err); //TODO: do sth with the error
process.exit(2);
}
resolve(true);
});
});
}
static fixPermission() {
return new Promise((resolve, reject) => {
let windows = process.platform === "win32";
let Config = require('cast-web-api/lib/config/config');
let user = process.env.USER || process.env.USERNAME;
let path = Config.getAbsolutePath();
if (!windows) {
reject({error: {message: "To change permissions, just copy/paste and run the commands below. Change the username if necessary: \n"}, stdout: `chmod -R 0600 ${path}/config\nchown -R ${user} ${path}/config`, stderr: ""});
} else {
reject({error: {message: "To change permissions, just copy/paste and run the command below. Change the username if necessary: \n"}, stdout: `icacls ${path}\\config /grant ${user}:M`, stderr: ""});
}
});
}
}
module.exports = Manager;