-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdockerImageUpdate.js
More file actions
277 lines (235 loc) · 8 KB
/
dockerImageUpdate.js
File metadata and controls
277 lines (235 loc) · 8 KB
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
#!/usr/bin/env node
//
// This is a helper script for refreshing the docker images for the running
// stack.
//
// Use when a new docker image is available for our services.
// Works with docker swarm or podman compose
//
require("dotenv").config();
/** @type {{ version: string, services: Object.<string, string> }} */
const version = require("./version");
const { exec } = require("child_process");
const path = require("path");
const fs = require("fs").promises;
const migrationContainer = "update_script_db_migrations"; // ref to clean up the migration container
// const cwd = process.cwd();
const stack = process.env.STACKNAME;
const platform = process.env.PLATFORM === "podman" ? "podman" : "docker";
// const hashServices = {};
// {hash} { imagetag : {serviceData} }
// collect a list of all the running images and their respective containers
// {serviceData} : { ID, NAME, MODE, REPLICAS, IMAGE, PORTS }
/**
* Helper to wait a given number of milliseconds
* @param {number} mS milliseconds to wait
*/
// async function wait(mS) {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve(null);
// }, mS);
// });
// }
/**
* Ensure the stack is up
*/
async function checkUp() {
const command = `${platform} ps | awk '/${stack}.api_sails/ { sum += 1} END {print sum}'`;
const res = await runCommand(command);
return parseInt(res.stdout) > 0;
}
/**
* Reads the version from ./version.json and updates env variables.
* @returns {Promise<string[]>} image names for ab-services with the new tag
*/
async function updateVersion() {
console.log(`Using runtime version ${version.version}`);
const envPath = path.join(__dirname, ".env");
let envContent = await fs.readFile(envPath, { encoding: "utf-8" });
const images = [];
Object.keys(version.services).forEach((service) => {
const tag = version.services[service];
// Image to pull
images.push(`digiserve/ab-${service.replace(/_/g, "-")}:${tag}`);
// Update the process env
const envVar = `AB_${service.toUpperCase()}_VERSION`;
process.env[envVar] = tag;
// Update the .env file content
const regex = new RegExp(`${envVar}=.+`);
if (regex.test(envContent)) {
envContent = envContent.replace(regex, `${envVar}=${tag}`);
} else {
console.log(`Could not match ${regex.toString()}`);
}
});
await fs.writeFile(envPath, envContent, { encoding: "utf-8" });
return images;
}
/** @typedef {{stdout:string, stderr: string}} CommandResponse */
/**
* run a command using child_process.exec
* @param {string} command
* @returns {Promise<CommandResponse>}
*/
function runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve({ /*error,*/ stdout, stderr });
});
});
}
/**
* run database migrations (from ab-migration manager)
* @param {string} branch
*/
async function dbMigrate(branch) {
const response = await runCommand(
`${platform} run --env-file .env --network=${stack}_default --name=${migrationContainer} digiserve/ab-migration-manager:${branch} node app.js`
);
return response;
}
/**
* stop and remove the migration manager image
*/
async function cleanMigrationManager() {
return [
await runCommand(`${platform} stop ${migrationContainer}`),
await runCommand(`${platform} rm ${migrationContainer}`),
].reduce((a, b) => {
return {
stdout: "",
stderr: `${a.stderr}\n${b.stderr}`,
};
});
}
/**
* Pull the images
* @param {string[]} images images to pull
*/
async function updateImage(images) {
const pendings = [];
const response = {
stdout: "",
stderr: "",
};
images.forEach((e) => {
const command = `${platform} pull ${e} && echo "" && ${platform} image ls | grep "${
e.split(":")[0]
}"`;
pendings.push(
runCommand(command)
.then((res) => {
console.log(res.stdout);
if (res.stderr) response.stderr += `${res.stderr}\n`;
})
.catch((err) => (response.stderr += `${err.toString()}\n`))
);
});
await Promise.all(pendings);
return response;
}
/**
* update live images
*/
async function updateServices() {
const command =
platform === "docker"
? `docker stack deploy -c docker-compose.yml -c docker-compose.override.yml ${stack}`
: `podman compose -f docker-compose.yml -f docker-compose.override.yml -p ${stack} up -d`;
return await runCommand(command);
}
/**
* remove old images
* @param {string[]} images
*/
async function cleanOldImages(images) {
const pendings = [];
const response = {
stdout: "",
stderr: "",
};
images.forEach((image) => {
// discard the tag and fomat for regex filter
image = image.split(":")[0].replace("/", ".");
if (image.includes("ab-migration-manager")) return; // no need to remove it
// We need to remove unused images (that still have a tag)
// so filter by .Containers == 0 and .Repository includes image name
const imagesCmd = `${platform} images --format='table {{.ID}}\\t{{.Containers}}\\t{{.Repository}}\\t'`;
const awkCmd = `awk '($2 == 0) && ($3 ~ /${image}/ ) {print $1}'`;
const command = `bash -c "${platform} rmi $(${imagesCmd} | ${awkCmd}) -f"`;
pendings.push(
runCommand(command)
.then((res) => {
console.log(res.stdout);
})
.catch((err) => {
if (!err.message.includes("image name or ID must be specified"))
response.stderr += `${err.toString()}\n`;
})
);
});
await Promise.all(pendings);
return response;
}
/**
* call a function and log it's response to the console
* @param {string} processName Name to log
* @param {(...args: any[]) => Promise<CommandResponse>} callbackFunction
* @param {...*} parameter any arguments to pass to the process function
*/
async function processHandler(processName, callbackFunction, ...parameter) {
if (!processName)
throw Error('The parameter "processName" should be "string" type');
if (!callbackFunction)
throw Error('The parameter "callbackFunction" should be "function" type');
console.log(`${processName}:`);
console.log();
const response = await callbackFunction(...parameter);
console.log(response.stdout);
if (response.stderr) console.log(response.stderr);
console.log();
}
async function Do() {
try {
// Check the stack is up first
const isUp = await checkUp();
if (!isUp) {
console.log(
"This script expects the 'api_sails' service to be running with the stack name '%s'",
stack
);
console.log("We couldn't find it. Try running './UP.sh' first?");
return;
}
// const services = await getServices();
const images = await updateVersion();
if (images.length < 1)
throw new Error("We didn't get any images from updateVersion");
// our migration image also needs to be updated:
let branchMigrate = process.env.AB_MIGRATION_MANAGER_VERSION || "master";
// if (images.length) {
// let ab = images.find((s) => s.indexOf("appbuilder") > -1);
// if (ab) {
// branchMigrate = ab.split(":")[1];
// if (!branchMigrate) branchMigrate = "master";
// }
// }
images.unshift(`digiserve/ab-migration-manager:${branchMigrate}`);
await processHandler("Updating Images", updateImage, images);
await processHandler("DB Migrations", dbMigrate, branchMigrate);
await processHandler("Clean up Migration Manager", cleanMigrationManager);
await processHandler("Updating services", updateServices);
await processHandler("Clean up old images", cleanOldImages, images);
console.log("... done");
console.log();
} catch (error) {
console.error(error);
console.error();
}
}
Do();