-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
461 lines (430 loc) · 26.2 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const axios = require('axios').default
const ws = require('ws')
const fs = require('fs')
const path = require("path");
const os = require("os");
/**
* @type {import('axios').Axios & {ws: (url) => import('ws').WebSocket}}
*/
const client = new axios.Axios({
socketPath: "/var/lib/incus/unix.socket",
maxBodyLength: Infinity,
maxContentLength: Infinity,
timeout: 9999999
})
client.ws = (url) => {
console.log("new WS", url)
return new ws.WebSocket("ws+unix:///var/lib/incus/unix.socket:" + url)
}
function handleError(err) {
console.log(err)
process.exit(1)
}
const { spawn } = require('child_process');
const downloadBase = require('./downloadBase');
function untar(path, out) {
return new Promise((resolve, reject) => {
// fs.mkdirSync(out)
var tar = spawn('sh', ['-c', 'pv ' + path + ' | tar -C ' + out + " -xz"],
{
env: {
"XZ_DEFAULTS": "-T 8"
}
})
tar.stderr.on('data', (d) => {
console.log(d.toString())
})
tar.stdout.on('data', (d) => {
console.log(d.toString())
})
tar.on('close', (code) => {
//console.log(code)
if (code == 0) {
resolve()
} else {
reject()
}
})
})
}
function tarfiles(cwd, path, out) {
return new Promise((resolve, reject) => {
// fs.mkdirSync(out)
var tar = spawn('sh', ['-c', `tar -cJf ${out} ${path}`], {
env: {
"XZ_DEFAULTS": "-T 8"
},
cwd
})
tar.stderr.on('data', (d) => {
console.log(d.toString())
})
tar.stdout.on('data', (d) => {
console.log(d.toString())
})
tar.on('close', (code) => {
//console.log(code)
if (code == 0) {
resolve()
} else {
reject()
}
})
})
}
function doExport(id) {
return new Promise(async (resolve, reject) => {
try {
var export_data = JSON.parse((await client.post('/1.0/instances/' + id + "/backups", JSON.stringify({
"compression_algorithm": "gzip",
"container_only": false,
"instance_only": false,
"name": id + "-export",
"optimized_storage": false,
}))).data)
await client.get(export_data.operation + "/wait?timeout=99999")
client.get('/1.0/instances/' + id + "/backups/" + id + "-export" + "/export", { responseType: "stream" }).then((s) => {
fs.mkdirSync('./temp/' + id)
var writer = fs.createWriteStream('./temp/' + id + "/backup.tar.xz")
s.data.pipe(writer)
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
//no need to call the reject here, as it will have been called in the
//'error' stream;
});
}).catch(handleError)
} catch (error) {
console.log(error)
}
})
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function findImages() {
let images = [];
for (const image of fs.readdirSync("./images", { withFileTypes: true })) {
if (image.isDirectory()) {
for (const imageVersion of fs.readdirSync("./images/" + image.name, { withFileTypes: true })) {
if (imageVersion.isDirectory()) {
images.push(image.name + "/" + imageVersion.name)
}
}
}
}
return images;
}
async function main() {
await new Promise(async (p, q) => {
var jobs = []
if (!fs.existsSync('./temp')) fs.mkdirSync('./temp');
for (const image of findImages()) {
jobs.push(new Promise((resolve, reject) => {
const config = require('./images/' + image + '/config.json')
if (!config.os || !config.architectures || !config.release || !config.variant || !config.aliases || !config.imageserver || !config.base || !config.commands || !config.files) handleError(new Error("Config file of " + config + " is missing required keys."))
var sysarch = os.arch()
if (sysarch == "x64") sysarch = "amd64";
if (!config.architectures.includes(sysarch)) handleError(new Error("System architecture " + sysarch + " is not supported by " + image + "."))
var d = new Date()
var zero = d.getMonth() < 10 ? "0" : ""
var zeroday = d.getDate() < 10 ? "0" : ""
var zerohours = d.getHours() < 10 ? "0" : ""
var zerominutes = d.getMinutes() < 10 ? "0" : ""
var zeroseconds = d.getSeconds() < 10 ? "0" : ""
var date = `${d.getFullYear()}-${zero + (d.getMonth() + 1)}-${zeroday + d.getDate()}-${zerohours + d.getHours()}${zerominutes + d.getMinutes()}${zeroseconds + d.getSeconds()}`
var id = config.os.replace(' ', '-').replace('.', "") + "-" + config.release.replaceAll(' ', '-').replaceAll('.', "-") + "-" + date
console.log("[Incus] [" + id + "] Initializing instance...")
client.post('/1.0/instances', JSON.stringify({
name: id,
"profiles": [
require('./config.json').default_profile
],
"architecture": sysarch != "arm64" ? "x86_64" : "aarch64",
source: {
"protocol": "simplestreams",
"server": config.imageserver,
"alias": config.base,
"type": "image"
}
})).then(({ data }) => {
data = JSON.parse(data)
//console.log(data)
//console.log(data.operation + "/wait?timeout=9999")
client.get(data.operation + "/wait?timeout=9999").then(async (create_operation) => {
//console.log(create_operation.data)
await sleep(1000)
// console.log('/1.0/instances/' + id + "/state")
client.put('/1.0/instances/' + id + "/state", JSON.stringify({
"action": "start",
"force": false,
"stateful": false,
"timeout": 30
})).then((start_data) => {
//console.log(start_data.data)
//console.log(JSON.parse(start_data.data).operation + "/wait?timeout=9999")
client.get(JSON.parse(start_data.data).operation + "/wait?timeout=9999").then(async (start_operation) => {
//console.log(start_operation.data)
for (const file of config.files) {
try {
await new Promise((re, rej) => {
try {
console.log('[Upload] [' + id + '] Uploading file ' + file.split(':')[0])
const https = require('http')
https.globalAgent.maxSockets = 5000
var opts = {
rejectUnauthorized: false,
method: "POST",
socketPath: "/var/lib/incus/unix.socket",
path: encodeURI("/1.0/instances/" + id + "/files?path=" + file.split(':')[1]),
headers: {
"Content-Type": `application/octet-stream`
},
}
var request = https.request(opts, function (response) {
response.on('error', (err) => {
rej(err)
})
});
request.on("error", (err) => {
console.log(err)
console.log("AHAHA I FOUND THE ISSUE HTTP BOOBOO")
})
console.log(fs.existsSync(`./images/${image}/files/${file.split(':')[0]}`))
var ReadStream = fs.createReadStream(`./images/${image}/files/${file.split(':')[0]}`)
var bytes = 0
var size = fs.lstatSync(ReadStream.path).size;
ReadStream.on('data', (chunk) => {
bytes += chunk.length;
var percent = ((bytes) * 100) / size
var data = {
bytes: {
sent: bytes,
total: size
},
percent: percent
}
console.log('[Upload] [' + id + '] ' + percent + "%")
if (data.percent == 100) {
console.log('[Upload] [' + id + '] Finished')
re()
}
}).pipe(request)
} catch (error) {
console.log(error)
}
}).catch(handleError)
} catch (error) {
console.log(error)
}
}
console.log("[Incus] [" + id + "] Done initializing instance")
for (const command of config.commands) {
await new Promise(async (re, rej) => {
console.log('[Exec] [' + id + '] Running command ' + command)
try {
var exec_data = JSON.parse((await client.post('/1.0/instances/' + id + "/exec", JSON.stringify({
command: ["bash", "-c", command],
interactive: true,
"wait-for-websocket": true
}))).data)
} catch (error) {
console.log("The error is on request websocket (create operation)")
handleError(error)
}
//onsole.log(exec_data.metadata.metadata.fds)
//console.log({
// exec: exec_data.operation + "/websocket?secret=" + exec_data.metadata.metadata.fds['0'],
// control: exec_data.operation + "/websocket?secret=" + exec_data.metadata.metadata.fds['control']
//})
console.log(JSON.stringify(exec_data))
try {
var execws = client.ws(exec_data.operation + "/websocket?secret=" + exec_data.metadata.metadata.fds['0'])
var controlws = client.ws(exec_data.operation + "/websocket?secret=" + exec_data.metadata.metadata.fds['control'])
} catch (error) {
console.log("the error is on WS")
handleError(error);
}
execws.on("error", () => {
console.log("error is on execws event")
})
controlws.on("error", () => {
console.log("error is on controlws event")
})
execws.on('message', (data) => {
if (data.toString() == "") {
re()
execws.close()
controlws.close()
}
if (data.toString() == "\n") return;
//console.log(data.toString().replace('\r\n', "").replace('\n', "").replace('\r', ""))
})
}).catch(() => {
console.log("Error is somewhere in exec command")
})
}
try {
console.log('[Export] [' + id + '] Exporting backup')
await doExport(id)
console.log('[Export] [' + id + '] Extracting backup')
fs.mkdirSync('./temp/' + id + "/backup")
await untar('./temp/' + id + "/backup.tar.xz", './temp/' + id + "/backup")
console.log('[Export] [' + id + '] Moving directories')
//fs.mkdirSync('./temp/' + id + "/backup/rootfs")
//fs.mkdirSync('./temp/' + id + "/backup/meta")
var fsext = require('fs-extra');
fsext.moveSync('./temp/' + id + "/backup/backup/container/rootfs", './temp/' + id + "/backup/rootfs")
var rootfsDir = './temp/' + id + "/backup/rootfs"
fsext.moveSync('./temp/' + id + "/backup/backup/container", './temp/' + id + "/backup/meta")
var metaDir = './temp/' + id + "/backup/meta"
console.log('[Editor] [' + id + '] Edit Metadata')
const yaml = require('yaml')
const yamldata = fs.readFileSync(metaDir + "/metadata.yaml").toString()
var yamlparsed = yaml.parse(yamldata)
console.log('[Editor] [' + id + '] Add Templates')
let tempConfig = Object.keys(config.templates);
for (let i = 0; i < tempConfig.length; i++) {
const temp = tempConfig[i];
console.log('[Editor] [' + id + '] Adding template ' + temp + ' to config');
const tempData = config.templates[temp];
yamlparsed.templates[temp] = tempData;
}
console.log('[Editor] [' + id + '] Edit Properties')
yamlparsed.architecture = sysarch == "amd64" ? "amd64" : "aarch64"
yamlparsed.properties.architecture = sysarch == "amd64" ? "amd64" : "aarch64"
yamlparsed.properties.name = config.os
yamlparsed.properties.os = config.os
yamlparsed.properties.release = config.release
yamlparsed.properties.release = config.release
let key = await downloadBase(config.imageserver, config.base);
let baseYaml = fs.readFileSync(`./temp/${key}/metadata.yaml`).toString();
let baseParsed = yaml.parse(baseYaml);
let baseTemplates = Object.keys(baseParsed.templates);
for (let i = 0; i < baseTemplates.length; i++) {
const temp = baseTemplates[i];
console.log('[Editor] [' + id + '] Adding template ' + temp + ' to config');
const tempData = baseParsed.templates[temp];
yamlparsed.templates[temp] = tempData;
}
fs.writeFileSync(metaDir + "/metadata.yaml", yaml.stringify(yamlparsed))
console.log('[Templating] [' + id + '] Adding templates')
if (fs.existsSync(`./images/${image}/templates`)) {
let templates = fs.readdirSync(`./images/${image}/templates`);
for (const template of templates) {
console.log('[Templating] [' + id + '] Adding template ' + template)
fs.cpSync(`./images/${image}` + "/templates" + "/" + template, metaDir + "/templates" + "/" + template);
}
let baseTemplates = fs.readdirSync(`./temp/${key}/templates`);
for (const template of baseTemplates) {
console.log('[Templating] [' + id + '] Adding template ' + template)
fs.cpSync(`./temp/${key}/templates` + "/" + template, metaDir + "/templates" + "/" + template);
}
}
console.log('[Editor] [' + id + '] Done editing metadata')
console.log('[Archive] [' + id + '] Compressing files')
await tarfiles(metaDir, ".", "../../incus.tar.xz")
await tarfiles(rootfsDir, ".", "../../rootfs.tar.xz")
fs.rmSync('./temp/' + id + "/backup", { recursive: true, force: true })
console.log('[Incus] [' + id + '] Remove build container')
client.put('/1.0/instances/' + id + "/state", JSON.stringify({
"action": "stop",
"force": true,
"stateful": false,
"timeout": 30
})).then((start_data2) => {
//console.log(start_data.data)
//console.log(JSON.parse(start_data.data).operation + "/wait?timeout=9999")
client.get(JSON.parse(start_data2.data).operation + "/wait?timeout=9999").then(async (start_operation2) => {
await client.delete('/1.0/instances/' + id)
console.log('[Incus] [' + id + '] Removed build container')
if (require('./config.json').server['do-upload'] == true) {
console.log('[Remote] [' + id + '] Uploading is enabled')
var e = require('./config.json')
for (const server of e.server.servers) {
await new Promise((resolvee, rejecte) => {
const FormData = require('form-data')
const data = new FormData()
data.append('rootfs', fs.createReadStream('./temp/' + id + "/rootfs.tar.xz"))
data.append('incusmeta', fs.createReadStream('./temp/' + id + "/incus.tar.xz"))
data.append('aliases', config.aliases)
data.append('architecture', sysarch == "amd64" ? "amd64" : "aarch64")
data.append('os', config.os)
data.append('release', config.release)
data.append('releasetitle', config.release)
data.append('variant', config.variant)
data.append("properties", JSON.stringify(config.properties))
console.log('[Remote] [' + id + '] Uploading to ' + server.url)
axios.post(server.url + 'images', data, {
maxBodyLength: Infinity, headers: {
authorization: "Bearer " + server.auth
}
}).then(data => {
//console.log(data.data)
console.log('[Remote] [' + id + '] Built and Uploaded image to ' + server.url)
/* function clean() {
var directory = "./temp"
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(directory, file), (err) => {
if (err) throw err;
});
}
});
}
clean()
console.log('[FS] Done cleaning temp directory') */
resolvee()
}).catch(handleError)
})
}
} else {
console.log('[Done] Built image ' + id)
}
resolve()
}).catch(handleError)
})
} catch (error) {
handleError(error)
}
}).catch(handleError)
})
}).catch(handleError)
}).catch(handleError)
}))
}
const chunkSize = 10;
for (let i = 0; i < jobs.length; i += chunkSize) {
const chunk = jobs.slice(i, i + chunkSize);
console.log(`Run 10 jobs`)
await Promise.all(chunk)
}
p()
})
}
main().catch(console.log)
//what is the structure of config.json for the image builder, not for the images
/*
{
}
*/