-
Notifications
You must be signed in to change notification settings - Fork 3
/
manifest2label.js
executable file
·228 lines (206 loc) · 6.4 KB
/
manifest2label.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
#!/usr/bin/env node
'use strict'
const exec = require('child_process').exec
const fs = require('fs')
const extract = require('extract-zip')
const tar = require('tar')
const jsonDiff = require('json-diff')
let zipFile = process.argv[2]
const deleteArtifacts = process.argv[3] ? JSON.parse(process.argv[3]) : false
const pushToRepository = process.argv[4] ? JSON.parse(process.argv[4]) : false
const registerHost = process.argv[5] ? process.argv[5] : 'localhost'
if (!zipFile) {
console.log('Call this script as: ' + process.argv[1] + ' <zipFile> [<deleteArtifacts>] [<push2Repos>] [<registryHost>]')
process.exit(1)
}
if (!zipFile.startsWith('/')) {
zipFile = process.cwd() + '/' + zipFile
}
// Utility functions:
let unpack = function (zipFile) {
return new Promise((resolve, reject) => {
extract(zipFile, {
dir: zipFile + '_unpacked'
}, (err) => {
if (!err) {
try {
let manifest = JSON.parse(fs.readFileSync(zipFile + '_unpacked/manifest.json', 'utf-8'))
resolve(manifest)
} catch (error) {
reject(error)
}
} else {
reject(err)
}
})
})
}
let imageList = function (binfile) {
return new Promise((resolve, reject) => {
tar.x({ file: binfile }, ['manifest.json']).then(_ => {
let dockerManifest = JSON.parse(fs.readFileSync('manifest.json', 'utf-8'))
if (dockerManifest) {
exec('rm -rf manifest.json', (error, stdout, stderr) => {
if (error) {
console.log('Failed to remove docker manifest.', error, stderr)
}
})
let result = []
dockerManifest.map((entry) => {
if (entry.RepoTags) {
result.push(entry.RepoTags)
}
})
resolve(result)
} else {
reject(new Error('docker manifest not found'))
}
}).catch((err) => { reject(err) })
})
}
let load = function (binfile) {
return new Promise((resolve, reject) => {
exec('docker load < ' + binfile, (error, stdout, stderr) => {
if (!error) {
console.log('Load into Docker done.')
resolve(stdout)
} else {
reject(error, stderr)
}
})
})
}
let getLabels = function (dockerImage) {
return new Promise((resolve, reject) => {
let labelCommand = 'docker image inspect ' + dockerImage
exec(labelCommand, (error, stdout, stderr) => {
if (!error) {
let labels = JSON.parse(stdout)[0].Config.Labels
resolve(labels)
} else {
reject(error, stderr)
}
})
})
}
let compareLabels = function (manifest, labels) {
let result = Object.assign({}, labels)
Object.keys(manifest).map((key) => {
let origKey = key.replace('__added', '')
if (origKey === 'binaryFile') return
if (!origKey.startsWith('vf-OS')) {
result['vf-OS.' + origKey] = manifest[origKey]
} else {
result[origKey] = manifest[origKey]
}
})
return result
}
let createUpdatedImage = function (dockerImage, basename, diff) {
return new Promise((resolve, reject) => {
let text = []
text.push('FROM ' + dockerImage)
Object.keys(diff).map((key) => {
text.push('LABEL ' + key + '="' + diff[key]['__new'] + '"')
})
// write Dockerfile
fs.mkdirSync('tempBuild')
fs.writeFileSync('tempBuild/Dockerfile', text.join('\n'))
// Run docker build -t imageName:extended
let buildCommand = 'docker build tempBuild -t ' + basename + ':extended'
exec(buildCommand, (error, stdout, stderr) => {
if (error) {
reject(error)
return
}
exec('rm -rf tempBuild', (error, stdout, stderr) => {
if (error) {
reject(error)
return
}
resolve()
})
})
})
}
let tag = function (original, newTag) {
return new Promise((resolve, reject) => {
exec('docker image tag ' + original + ' ' + newTag, (error, stdout, stderr) => {
if (!error) {
resolve(stdout)
} else {
reject(error, stderr)
}
})
})
}
let push = function (dockerImage, basename) {
return new Promise(async (resolve, reject) => {
await tag(dockerImage, registerHost + ':5000/' + basename + ':latest')
exec('docker image push ' + registerHost + ':5000/' + basename + ':latest', (error, stdout, stderr) => {
if (!error) {
resolve()
} else {
reject(error, stderr)
}
})
})
}
let getFullImageName = function (imageFile, list) {
let result = list[0][0]
list.map((item) => {
if (item[0].includes(imageFile)) {
result = item[0]
}
})
return result
}
// Main program:
let main = async function () {
try {
let manifest = await unpack(zipFile)
console.log('Read manifest from zipfile:\n', manifest)
let list = await imageList(zipFile + '_unpacked/' + manifest.binaryFile)
console.log('Images to read into Docker repository:\n', list)
await load(zipFile + '_unpacked/' + manifest.binaryFile)
let fullImageName = getFullImageName(manifest.binaryFile, list)
let labels = await getLabels(fullImageName)
let newLabels = compareLabels(manifest, labels)
let diff = jsonDiff.diff(labels, newLabels)
if (diff) { // returns undefined on no changes
console.log('Changed labels, rebuilding image:', jsonDiff.diffString(labels, newLabels))
await createUpdatedImage(fullImageName, manifest.binaryFile, diff)
fullImageName = manifest.binaryFile + ':extended'
} else {
console.log('No labels changed, using currently loaded image.')
}
if (pushToRepository) {
await push(fullImageName, manifest.binaryFile)
for (let i = 0; i < list.length; i++) {
await push(list[i][0], list[i][0].replace(/:.*/, ''))
}
}
if (deleteArtifacts) {
console.log('Cleaning up behind me.')
exec('rm -rf ' + zipFile + '_unpacked', (error, stdout, stderr) => {
if (error) {
console.log(error, stderr)
} else {
if (pushToRepository) {
// Also cleanup local images
exec('docker image rm ' + fullImageName + ' ' + registerHost + ':5000/' + manifest.binaryFile + ':latest',
(error, stdout, stderr) => {
if (error) {
console.log(error, stderr)
} else {
console.log('Also cleaned images.')
}
})
}
console.log('Done cleanup.')
}
})
}
} catch (err) { console.error('Error', err) }
}
main()