-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
238 lines (216 loc) · 9.33 KB
/
service.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
const fs = require("fs");
const fetch = require("node-fetch")
const currentConfigFileVersion = 5;
var run = true;
var clck = 0;
var jobFunctions = {};
var lastJobScan = 0;
async function main(docker) {
while (true) {
if (!run) break;
let config = JSON.parse(fs.readFileSync("./nds_config.json"));
// adding non-breaking configuration changes
if (!config.jobs) config.jobs = [];
fs.writeFileSync("./nds_config.json", JSON.stringify(config, null, 4));
// checking if a newer version is available
if (clck % 10 == 0) {
global.latestVersion = await getLatestVersion();
clck = 0;
}
clck += 1;
// garbage manager
let deploymentFolders = getDirectories("./deployments");
deploymentFolders.forEach(folder => {
if (!config.deployments || config.deployments.length === 0) {
console.log("Found a deployment stored locally but could not find deployment in nds_config.json. Deleting deployment folder...");
fs.rmdirSync("./deployments/" + folder, { recursive: true });
} else if (!config.deployments.find(deployment => deployment.id === folder)) {
console.log("Found a deployment stored locally but could not find deployment in nds_config.json. Deleting deployment folder...");
fs.rmdirSync("./deployments/" + folder, { recursive: true });
}
})
// status updater
let deployments = config.deployments || [];
deployments.forEach(deployment => {
let deploymentFolder = "./deployments/" + deployment.id;
fs.existsSync(deploymentFolder) || fs.mkdirSync(deploymentFolder);
var container = docker.getContainer(`nds-container-${deployment.id}`);
container.inspect((err, data) => {
if (err) deployment.status = "waiting for initialization"
else deployment.status = data.State.Status;
if (!config.deployments) return;
let deploymentIndex = config.deployments.findIndex(d => d.id === deployment.id);
config.deployments[deploymentIndex] = deployment;
fs.writeFileSync("./nds_config.json", JSON.stringify(config, null, 4));
})
})
// docker image cleanup
let images = await docker.listImages();
images.forEach(image => {
if (image.RepoTags[0].startsWith("nds-deployment")) {
if (!config.deployments || !config.deployments.find(deployment => deployment.id === image.RepoTags[0].split("-")[2].slice(0, -":latest".length)))
docker.getImage(image.Id).remove({ force: true }, (err, data) => {
if (err) console.log(err);
else {
console.log("Found unused deployment image and removed it")
}
})
}
})
//docker volume cleanup
let volumes = await docker.listVolumes();
volumes = volumes.Volumes;
volumes.forEach(volume => {
if (volume.Name.startsWith("nds-volume")) {
if (!config.volumes || !config.volumes.find(v => v.id === volume.Name.split("-")[2]))
docker.getVolume(volume.Name).remove({ force: true }, (err, data) => {
if (err) console.log(err);
else {
console.log("Found unused volume and removed it")
}
})
}
})
// cleanup temp download files
let tempFiles = fs.readdirSync("./static/tmp-volume-data");
tempFiles.forEach(file => {
if (file !== ".gitkeep") {
if (Date.now() - 1000 * 60 * 5 > fs.statSync("./static/tmp-volume-data/" + file).birthtimeMs) {
fs.rmSync("./static/tmp-volume-data/" + file, { recursive: true, force: true });
}
}
})
await sleep(60000);
}
}
async function jobManager(docker) {
await sleep(1000)
console.log("Starting job manager...")
let config = JSON.parse(fs.readFileSync("./nds_config.json"));
let jobs = config.jobs;
jobs.forEach(job => {
registerJob(job)
})
await sleep(10000);
fs.watch('./nds_config.json', function (event, filename) {
if (event === "change") {
scanForNewJobs();
}
});
function registerJob(job) {
console.log("Registering/Updating job: " + job.name)
jobFunctions[job.id] = async function () {
console.log("Running job")
// checking if a newer version of the job is available
var newConfig = JSON.parse(fs.readFileSync("./nds_config.json"));
var newJob = newConfig.jobs.find(j => j.id === job.id);
if (!newJob) return unregisterJob(job.id);
if (newJob.version !== job.version) {
console.log("There was a newer version of the job found. Updating job...")
return registerJob(newConfig.jobs.find(j => j.id === job.id));
}
if (job.enabled === false) {
console.log("Job is disabled. Skipping...")
return setTimeout(jobFunctions[job.id] || function(){}, job.run_every * 60000);
}
// executing job commands
try {
for (var i in job.actions) {
let action = job.actions[i];
if (action.action === "backup_volume") {
let vid = action.data.volume_id;
let dir = action.data.path;
const volume = VolumeExplorer.volume("nds-volume-" + vid)
try { fs.rmSync(dir, { recursive: true, force: true }); } catch (e) { }
await volume.copyDir("/", dir)
} else if (action.action === "check_deployment") {
let status = newConfig.deployments.find(d => d.id === action.data.container_id).status;
await executeLayer2Actions(action.data[status])
async function executeLayer2Actions(l2Actions) {
for (let p in l2Actions) {
let l2Action = l2Actions[p];
if (l2Action.type === "fetch") {
let body;
if (l2Action.method.toLowerCase() !== "get") body = l2Action.body
await fetch(l2Action.url, {
method: l2Action.method,
body: body
})
} else {
let container = docker.getContainer("nds-container-"+l2Action.container_id);
await container[l2Action.type]()
}
}
}
}
}
} catch (err) {
console.log("An error occured while running job: " + job.name + "\n Error: " + err.toString())
}
// running the job in the future
setTimeout(jobFunctions[job.id] || function(){}, job.run_every * 60000);
}
jobFunctions[job.id]();
}
function unregisterJob(id) {
console.log("Unregistering job: " + id)
delete jobFunctions[id];
}
function scanForNewJobs(){
if (lastJobScan + 100 < Date.now()) return;
let newConfig = JSON.parse(fs.readFileSync("./nds_config.json"));
lastJobScan = Date.now();
newConfig.jobs.forEach(job => {
if (!jobFunctions[job.id]) registerJob(job)
})
}
await sleep(60000);
}
function checkConfigFile() {
let config = JSON.parse(fs.readFileSync("./nds_config.json"));
let configFileVersion = config.configFileVersion;
if (configFileVersion !== currentConfigFileVersion) {
return "The config file's version is not up to date. Please update the config file by running the command: npm run update-config"
} else {
return true;
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + '/' + file).isDirectory();
});
}
async function getLatestVersion() {
let url = "https://api.github.com/repos/joshua-zou/node-deployment-server/releases/latest";
let response = await fetch(url);
let json = await response.json();
let newestVersion = json.tag_name.slice(1);
return newestVersion;
}
module.exports.start = function (docker, errFunction) {
console.log("Starting NDS service...")
async function watchMain() {
try {
await main(docker);
} catch (err) {
errFunction(err.toString());
}
}
watchMain()
async function watchJobManager() {
try {
await jobManager(docker);
} catch (err) {
errFunction(err.toString());
}
}
watchJobManager()
let configGood = checkConfigFile();
if (configGood !== true) errFunction(configGood, false);
}
module.exports.stop = function () {
run = false;
}