-
Notifications
You must be signed in to change notification settings - Fork 3
/
installAsset.js
executable file
·190 lines (179 loc) · 7.13 KB
/
installAsset.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
#!/usr/bin/env node
'use strict'
const exec = require('child_process').exec
const fs = require('fs')
const dockerImage = process.argv[2]
const instanceId = process.argv[3] ? process.argv[3] : dockerImage
const reload = process.argv[4] ? JSON.parse(process.argv[4]) : false
const folder = process.argv[5] ? process.argv[5] : process.cwd() + '/.compose/'
let volumeFolder = process.argv[6] ? process.argv[6] : process.cwd() + '/.persist/'
if (!volumeFolder.endsWith('/')) volumeFolder += '/'
if (!dockerImage && process.argv.length < 3) {
console.log('Call this script as: ' + process.argv[1] + ' <dockerUrl> [<instance_id>] [<reload>] [<targetFolder>] [<volumesFolder>]')
process.exit(1)
}
let imageFile = instanceId.replace(/.*\//gi, '').replace(/:.*/gi, '')
function getOverrides (label) {
switch (label) {
case 'che':
return ['traefik.frontend.entryPoints=che', 'traefik.frontend.rule=PathPrefix:/', 'traefik.port=8081']
default:
console.log('Unknown override requested')
}
return ''
}
function stringToBoolean (string) {
if (!string) return false
switch (string.toLowerCase().trim()) {
case 'true':
case 'yes':
case '1':
return true
case 'false':
case 'no':
case '0':
case null:
return false
default:
return Boolean(string)
}
}
new Promise((resolve, reject) => {
if (dockerImage.includes(':')) {
let pullCommand = 'docker pull ' + dockerImage
exec(pullCommand, (error, stdout, stderr) => {
if (error) {
reject(error, stderr)
} else {
resolve()
}
})
} else {
resolve()
}
}).then(_ => {
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 labelObj = JSON.parse(stdout)[0].Config.Labels
let labels = {}
Object.keys(labelObj).map((key) => {
let keyFields = key.split('.')
let pointer = labels
for (let i = 1; i < keyFields.length; i++) { // Skip highest level item (vf-OS.)
if (!pointer[keyFields[i]]) {
pointer[keyFields[i]] = (i === keyFields.length - 1) ? labelObj[key] : {}
}
pointer = pointer[keyFields[i]]
}
})
let result = []
let id = instanceId || (labels['name'] ? labels['name'] : imageFile)
result[0] = {}
result[0]['id'] = id
result[0]['image'] = dockerImage
try {
if (labels['compose']) {
Object.keys(labels['compose']).map((assetKey, index) => {
let asset = labels['compose'][assetKey]
if (!result[index]) result[index] = {}
if (!result[index]['labels']) result[index]['labels'] = []
if (index > 0) {
if (asset['image']) result[index]['image'] = asset['image']
if (asset['serviceName']) result[index]['id'] = asset['serviceName']
}
if (asset['depends_on']) {
if (!result[index]['depends_on']) result[index]['depends_on'] = []
if (typeof asset['depends_on'] === 'string') {
asset['depends_on'] = { 0: asset['depends_on'] }
}
Object.keys(asset['depends_on']).map((key) => {
result[index]['depends_on'].push(asset['depends_on'][key])
})
}
if (asset['volumes']) {
asset['volume'] = Object.assign(asset['volume'] ? asset['volume'] : {}, asset['volumes'])
}
if (asset['volume']) {
if (!result[index]['volumes']) result[index]['volumes'] = []
Object.keys(asset['volume']).map((key, volumeIndx) => {
if (key === 'shared') {
Object.keys(asset['volume']['shared']).map((innerKey) => {
if (typeof asset['volume']['shared'][innerKey] === 'object') {
Object.keys(asset['volume']['shared'][innerKey]).map((deeperKey) => {
result[index]['volumes'].push(volumeFolder + 'shared_' + innerKey + '_persist/' + deeperKey + ':' + asset['volume']['shared'][innerKey][deeperKey])
})
} else {
result[index]['volumes'].push(volumeFolder + 'shared_' + innerKey + '_persist:' + asset['volume']['shared'][innerKey])
}
})
} else {
result[index]['volumes'].push(volumeFolder + imageFile + '_' + index + '_' + volumeIndx + '_persist:' + asset['volume'][key])
}
})
}
if (asset['environment']) {
if (!result[index]['environment']) result[index]['environment'] = []
Object.keys(asset['environment']).map((key) => {
result[index]['environment'].push(key + '=' + asset['environment'][key])
})
}
if (asset['socket'] && JSON.parse(asset['socket'])) {
if (!result[index]['volumes']) result[index]['volumes'] = []
result[index]['volumes'].push('/var/run/docker.sock:/var/run/docker.sock')
}
if (asset['traefikOverride']) {
result[index]['traefikOverride'] = asset['traefikOverride']
}
if (asset['port']) {
result[index]['labels'].push('traefik.port=' + asset['port'])
}
if (asset['hostname']) {
result[index]['hostname'] = asset['hostname']
}
})
}
} catch (e) { console.log('Had trouble parsing the labels!', e) }
let services = ''
result.map((res, index) => {
if (res['image']) {
let id = res['id'] ? res['id'] : 'unnamed_asset_' + index
if (!res['labels']) {
res['labels'] = []
}
if (!res['traefikOverride']) {
if (!labels['urlprefixReplace'] || stringToBoolean(labels['urlprefixReplace'])) {
res['labels'].push('traefik.frontend.rule=PathPrefix:/' + id + ';ReplacePathRegex: ^/' + id + '/(.*) /$$1')
} else {
res['labels'].push('traefik.frontend.rule=PathPrefix:/' + id)
}
} else {
res['labels'].concat(getOverrides(res['traefikOverride']))
}
delete res['traefikOverride']
delete res['id']
services += ' ' + id + ':\n ' + JSON.stringify(res) + '\n'
} else {
console.log('Missing docker image name for asset:', res, index)
}
})
// Generate docker-compose file for this asset into folder
fs.writeFileSync(folder + '/3_' + imageFile + '_compose.yml', 'version: "3.4"\nservices:\n' + services)
// If parameter: call docker-compose image to reload asset
if (reload) {
exec('docker exec vf_os_platform_exec_control docker-compose up -d', (error, stdout, stderr) => {
if (error) {
console.log('Failed to reload the platform.', stderr)
} else {
console.log('Platform reloaded.', stdout)
}
})
}
}
})
}).catch((e) => {
console.error('failed to pull image', e)
})