-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
333 lines (277 loc) · 8.35 KB
/
index.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as core from "@actions/core";
import fs from "fs";
import fetch from "node-fetch";
function sleep(s) {
return new Promise((resolve) => setTimeout(resolve, s * 1000));
}
class Action {
constructor() {
this.inputs = {
username: core.getInput("username", {
required: true,
trimWhitespace: true,
}),
password: core.getInput("password", {
required: true,
trimWhitespace: true,
}),
project: `paas-project-${core.getInput("project", {
required: false,
trimWhitespace: true,
})}`,
environment: core.getInput("environment", {
required: false,
trimWhitespace: true,
}),
application: core.getInput("application", {
required: false,
trimWhitespace: true,
}),
image: core.getInput("image", { required: false, trimWhitespace: true }),
login_only: core.getBooleanInput("login_only", {
required: false,
trimWhitespace: true,
}),
npmrc_output_dir: core.getInput("npmrc_output_dir", {
required: false,
trimWhitespace: true,
}),
paas_api: core.getInput("paas_api", {
required: false,
trimWhitespace: true,
}),
paas_packages: core.getInput("paas_packages", {
required: false,
trimWhitespace: true,
}),
rollback: core.getBooleanInput("rollback", {
required: false,
trimWhitespace: true,
}),
timeout: parseInt(
core.getInput("timeout", { required: false, trimWhitespace: true })
),
};
}
async waitForApplication() {
let status = "Processing";
let tryCount = 0;
while (status !== "Healthy" && tryCount < this.inputs.timeout) {
const currentAppInfo = await this.getApplicationInfo();
status = currentAppInfo.status;
tryCount++;
await sleep(1);
}
if (tryCount === this.inputs.timeout) {
throw new Error(
`Deployment failed for application ${this.inputs.application}: Timeout. Current application status: ${status}`
);
}
return status;
}
async run() {
await this.login();
if (this.inputs.login_only) {
return;
}
if (!this.inputs.image) {
throw new Error(
`You're attempting to deploy but you didn't provide a Docker image name to do so.`
);
}
if (!this.inputs.project) {
throw new Error(`Project name required for deployment operations.`);
}
const previousAppInfo = await this.getApplicationInfo();
await this.deploy();
await sleep(10); // Wait 10 seconds to let Argo the time to init the deployment process
let status;
try {
status = await this.waitForApplication();
console.log("Deployment succeeded!");
} catch (error) {
console.log("Deployment failed with the following error:\n");
const logs = await this.getApplicationLogs();
if (Array.isArray(logs)) {
logs.forEach((log) => console.log(log));
} else {
console.log(logs);
}
if (!this.inputs.rollback) {
throw new Error(
`Application deployment errored with final status ${status}`
);
}
console.log(
`Rolling back to image ${previousAppInfo.spec.source.helm.values.kuzzle.image.name}:${previousAppInfo.spec.source.helm.values.kuzzle.image.tag}`
);
await this.deploy({
tag: previousAppInfo.spec.source.helm.values.kuzzle.image.tag,
});
await this.waitForApplication();
console.log("Rollback successful! 🥵");
process.exit(1); // To make the Github Action job mark as failed for Github
}
}
async login() {
const { username, password } = this.inputs;
/**
* Login to the Kuzzle PaaS API
*/
try {
const response = await fetch(`${this.inputs.paas_api}/_login/local`, {
method: "post",
body: JSON.stringify({
username,
password,
}),
headers: { "Content-Type": "application/json" },
});
const json = await response.json();
if (response.status !== 200) {
throw new Error(json.error.message);
}
const { result } = json;
this.jwt = result.jwt;
} catch (error) {
console.error(error);
throw new Error("Cannot login to the Kuzzle PaaS services");
}
/**
* Login to the Kuzzle PaaS private NPM registry
*/
try {
const options = {
method: "PUT",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Basic ${Buffer.from(
`${username}:${password}`
).toString("base64")}`,
},
body: JSON.stringify({
name: username,
password,
}),
};
const response = await fetch(
`https://${this.inputs.paas_packages}/-/user/org.couchdb.user:${username}`,
options
);
const json = await response.json();
if (response.status !== 201) {
throw new Error(json.error.message);
}
const { token } = json;
fs.appendFileSync(
`${process.env.GITHUB_WORKSPACE}/${this.inputs.npmrc_output_dir}/.npmrc`,
`@kuzzleio:registry=https://${this.inputs.paas_packages}\n`
);
fs.appendFileSync(
`${process.env.GITHUB_WORKSPACE}/${this.inputs.npmrc_output_dir}/.npmrc`,
`//${this.inputs.paas_packages}/:_authToken=${token}\n`
);
} catch (error) {
console.error(error);
throw new Error("Cannot login to the Kuzzle PaaS private NPM registry");
}
}
async deploy(overrides = {}) {
let [name, tag] = this.inputs.image.split(":");
if (overrides.tag) {
tag = overrides.tag;
}
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.jwt}`,
},
body: JSON.stringify({
image: {
name,
tag,
},
}),
};
try {
console.log(
`Attempting to deploy '${name}:${tag}' for '${this.inputs.application}' the application on the '${this.inputs.environment}' for the '${this.inputs.project}'`
);
const response = await fetch(
`${this.inputs.paas_api}/_/projects/${this.inputs.project}/environments/${this.inputs.environment}/applications/${this.inputs.application}/_deploy`,
options
);
const json = await response.json();
if (json.status !== 200) {
throw new Error(json.error.message);
}
} catch (error) {
throw new Error(`Deployment failed: ${error}`);
}
}
async getApplicationInfo() {
const options = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.jwt}`,
},
};
try {
const response = await fetch(
`${this.inputs.paas_api}/_/projects/${this.inputs.project}/environments/${this.inputs.environment}/applications/${this.inputs.application}`,
options
);
const json = await response.json();
if (json.status !== 200) {
throw new Error(json.error.message);
}
return json.result;
} catch (error) {
throw new Error(
`Failed to fetch '${this.inputs.application}' application information: ${error}`
);
}
}
async getApplicationLogs() {
try {
const url = `${this.inputs.paas_api}/_query`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({
action: "logs",
controller: "application",
applicationId: this.inputs.application,
environmentId: this.inputs.environment,
projectId: this.inputs.project,
}),
headers: {
Authorization: `Bearer ${this.jwt}`,
"Content-Type": "application/json",
},
});
const text = await response.text();
const parsedText = text.split("\n").map((line) => {
try {
const json = JSON.parse(line);
return `${json.podName} ${json.content}`;
} catch (error) {
return line;
}
});
return parsedText.join("\n");
} catch (error) {
throw new Error(
`Failed to fetch '${this.inputs.application}' application logs: ${error}`
);
}
}
}
const action = new Action();
try {
await action.run();
} catch (error) {
core.setFailed(error);
}