-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlabel2manifest.js
executable file
·62 lines (52 loc) · 1.89 KB
/
label2manifest.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
#!/usr/bin/env node
'use strict'
const exec = require('child_process').exec
const fs = require('fs')
const zip = require('yazl')
let dockerImage = process.argv[2]
let deleteArtifacts = process.argv[3] ? JSON.parse(process.argv[3]) : false
let additionalImages = process.argv[4] ? process.argv[4] : ''
if (!dockerImage) {
console.log('Call this script as: ' + process.argv[1] + ' <dockerImage> <deleteArtifacts> ["<additionalImages>"]')
process.exit(1)
}
let imageFile = dockerImage.replace(/.*\//gi, '').replace(/:.*/gi, '')
let result = {
'binaryFile': imageFile
}
let saveCommand = 'docker save -o ' + imageFile + ' ' + dockerImage + ' ' + additionalImages
exec(saveCommand, (error, stdout, stderr) => {
if (error) {
console.log(error, stderr)
} else {
console.log('Exported docker image:', dockerImage)
}
let labelCommand = 'docker image inspect ' + dockerImage
exec(labelCommand, (error, stdout, stderr) => {
if (error) {
console.log(error, stderr)
} else {
console.log('Got metadata from docker image:', dockerImage)
let labels = JSON.parse(stdout)[0].Config.Labels
Object.keys(labels).map((key) => {
result[key.replace('vf-OS.', '')] = labels[key]
})
result['binaryFile'] = imageFile
console.log(labels, result, imageFile)
fs.writeFileSync('manifest.json', JSON.stringify(result))
let zipfile = new zip.ZipFile()
zipfile.addFile('manifest.json', 'manifest.json')
zipfile.addFile(imageFile, imageFile)
zipfile.outputStream.pipe(fs.createWriteStream(imageFile + '.zip')).on('close', function () {
if (deleteArtifacts) {
console.log('done, deleting artifacts')
fs.unlink(imageFile, () => { /* ignore callback */ })
fs.unlink('manifest.json', () => { /* ignore callback */ })
} else {
console.log('done')
}
})
zipfile.end()
}
})
})