forked from bmoers/docker-mid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
292 lines (232 loc) · 9.74 KB
/
app.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
require('dotenv').config();
const request = require('request-promise');
const Promise = require('bluebird');
const { execAsync } = require('async-child-process');
const { MongoClient } = require("mongodb");
const client = new MongoClient(process.env.MONGODB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
var db;
const connect = async () => {
await client.connect();
return client.db();
}
const getCities = () => {
return new Promise((resolve, reject) => {
db.collection("city").find({ active: true }).toArray((err, result) => {
if (err)
return reject(err);
resolve(result.map((city) => city.name));
});
});
}
const getBuilds = (cities) => {
return new Promise((resolve, reject) => {
db.collection("build").find({ city: { $in: cities } }).toArray((err, result) => {
if (err)
return reject(err);
resolve(result);
});
});
}
const addBuild = (build) => {
return new Promise((resolve, reject) => {
db.collection("build").insertOne(build, (err, res) => {
if (err) {
console.log(err);
return reject(err);
}
resolve(res.ops);
});
});
}
const updateBuild = (build) => {
if (!build._id)
return addBuild(build);
return new Promise((resolve, reject) => {
db.collection("build").updateOne({ _id: build._id }, { $set: build }, { upsert: true }, (err, res) => {
if (err) {
console.log(err);
return reject(err);
}
resolve(res.ops);
});
});
}
const dockerBuild = async (command, tags, city, build) => {
console.log(`Building image: ${city}, version: ${build.version}, tags: ${tags}`);
await execAsync(`docker login -u ${process.env.DOCKER_USER_NAME} -p ${process.env.DOCKER_TOKEN}`, { cwd: './docker' })
// build image with all tags
console.log(`build image: ${tags}`)
const { stdout: buildStdout } = await execAsync(command, { cwd: './docker' });
console.log(`\t${buildStdout.split('\n').slice(-2)[0]}`);
console.log("\tbuild done");
// push image with all tags in sequence
await Promise.each(tags, (async (tag) => {
console.log(`push tag: ${tag}`)
const { stdout: pushStdout } = await execAsync(`docker push ${tag}`, { cwd: './docker' });
console.log(`\t${pushStdout.split('\n').slice(-2)[0]}`);
console.log("\ttag done");
}))
// cleanup as my disk is not endless
await Promise.all(tags.map(async (tag) => {
console.log(`remove local tag: ${tag}`);
const { stdout: rmiStdout } = await execAsync(`docker rmi --force ${tag}`, { cwd: './docker' });
console.log(`\t${rmiStdout.split('\n').slice(-2)[0]}`);
}));
}
const getNewBuilds = async (city, existingBuilds = []) => {
console.log(`process city ${city}`);
// get the city patchURLs from the release notes
const patchUrls = [];
try {
const html = await request(`https://docs.servicenow.com/bundle/${city}-release-notes/toc/release-notes/available-versions.html`);
const regex = /(https:\/\/docs\.servicenow\.com\/bundle\/[^\/]+-release-notes\/page\/release-notes\/quality\/[^\/\s]+\.html)/g
let m;
while ((m = regex.exec(html)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
patchUrls.push(m[1])
}
} catch (e) {
console.error(`city ${city} http request failed! (https://docs.servicenow.com/bundle/${city}-release-notes/toc/release-notes/available-versions.html)`, e.message);
}
// get all mid builds from the patchUrl page
let builds = await Promise.mapSeries(patchUrls, async (url) => {
console.log('parsing mid version info from', url);
const out = {
tag: undefined,
date: undefined
};
try {
let html = await request(url);
html = html.replace(/<span[^>]*>/g, '').replace(/<\/span>/g, '');
let regex = />Build tag:\s+(?:glide-)?([^<]+)</i
let m = html.match(regex);
if (m) {
out.tag = m[1];
} else {
console.warn('Tag not found')
}
regex = />Build date:\s+([^<]+)</i
m = html.match(regex);
if (m) {
out.date = m[1];
} else {
console.warn('Date not found')
}
if (!out.tag || !out.date) {
console.warn(`Parsing failed! ${url}`);
console.log(out)
}
} catch (e) {
console.error('Request failed: ', url);
}
return out;
});
// only keep the ones which are correct
builds = builds.filter((build) => build.tag && build.date && build.date.split(/[_-]/).length == 4);
// construct build object
builds = builds.map((build) => {
const dateArray = build.date.split(/[_-]/);
build.id = `${dateArray[2]}${dateArray[0]}${dateArray[1]}${dateArray[3]}`
build.city = city;
build.version = `${build.tag}_${build.date}`;
build.url = `https://install.service-now.com/glide/distribution/builds/package/mid/${dateArray[2]}/${dateArray[0]}/${dateArray[1]}/mid.${build.version}.linux.x86-64.zip`
build.tagname = build.tag.split('__')[1];
build.done = false;
return build;
});
const existingBuildIds = existingBuilds.map((eb) => eb.id);
// find the builds which are not in the db yet
let newBuilds = builds.filter((build) => !existingBuildIds.includes(build.id));
console.log(`newBuilds : ${newBuilds.length}`)
//and check if zip file exists
newBuilds = await Promise.mapSeries(newBuilds, async (build) => {
console.log('check for zip file ', build.url)
try {
await request({ method: 'HEAD', url: build.url });
build.zipExits = true;
} catch (e) {
build.zipExits = false;
console.warn("zip file not found on server for build", build.url);
}
return build;
}).filter((build) => build.zipExits);
return existingBuilds.concat(newBuilds).filter((build) => build.zipExits);;
}
(async () => {
try {
db = await connect();
const cities = (await getCities()).sort();
let existingBuilds = await getBuilds(cities);
if (process.env.FORCE_REFRESH == 'true') {
// force reset build done
console.warn('FORCE REFRESH MODE: replacing all images !')
existingBuilds = existingBuilds.map((build) => {
build.done = false;
return build;
});
}
const versions = existingBuilds.reduce((out, build) => {
if (out[build.city]) {
out[build.city].push(build);
} else {
out[build.city] = [build];
}
return out;
}, {});
//console.log("%j", cities)
const cityBuilds = await Promise.mapSeries(cities, async (city) => {
const builds = await getNewBuilds(city, versions[city]);
return {
city,
builds
}
});
//console.log("%j", cityBuilds)
const buildVersions = {};
cityBuilds.forEach((obj) => {
buildVersions[obj.city] = obj.builds
});
const citiesSorted = Object.keys(buildVersions).sort();
const versionsLen = citiesSorted.length;
console.log('Total number of cities ', versionsLen)
console.log('Cities ', citiesSorted.join(', '));
await Promise.each(citiesSorted, async (city, cityIndex) => {
console.log(`City: '${city}' index: ${cityIndex}`);
const builds = buildVersions[city];
const buildsSorted = builds.sort((a, b) => a.id - b.id);
await Promise.each(buildsSorted, async (build, buildIndex) => {
try {
if (!build.zipExits) {
console.log("zip does not exist, skip ", build.url);
return;
}
if (build.done) {
console.log("this build is done, skip %j", build.tag)
return;
}
const tags = [`moers/mid-server:${city}.${build.date}`]; // `moers/mid-server:${city}.${build.tagname}`,
if (buildIndex == 0) {
tags.push(`moers/mid-server:${city}`);
tags.push(`moers/mid-server:${city}.first`);
}
if (buildIndex == builds.length - 1) {
tags.push(`moers/mid-server:${city}.latest`);
if (cityIndex == versionsLen - 1)
tags.push('moers/mid-server:latest')
}
//console.log(tags);
await dockerBuild(`docker build -f ./Dockerfile --no-cache --build-arg URL=${build.url} ${tags.map((t) => `--tag ${t}`).join(' ')} . `, tags, city, build);
build.done = true;
await updateBuild(build);
} catch (e) {
console.error('somethings wrong', e)
}
});
});
} finally {
client.close();
}
})();