-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdeploy-command.ts
More file actions
236 lines (191 loc) · 6.57 KB
/
deploy-command.ts
File metadata and controls
236 lines (191 loc) · 6.57 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
import { ux as CliUx } from '@oclif/core';
import chalk, { ForegroundColor } from 'chalk';
import inquirer from 'inquirer';
import { Deployment, DeployRequest, getDeploy, Organization, Squid, SquidRequest, streamSquidLogs } from './api';
import { CliCommand, SUCCESS_CHECK_MARK } from './command';
import { doUntil, formatSquidReference, printSquid } from './utils';
export abstract class DeployCommand extends CliCommand {
deploy: Deployment | undefined;
logsPrinted = 0;
async promptAttachToDeploy(squid: Squid, { interactive }: { interactive?: boolean } = {}) {
if (!squid.lastDeploy) return false;
if (squid.status !== 'DEPLOYING') return false;
const warning = `Squid ${printSquid(squid)} is being deploying.
You can not run deploys on the same squid in parallel`;
if (!interactive) {
this.error(warning);
}
this.warn(warning);
switch (squid.lastDeploy.type) {
// we should react only for running deploy
case 'DEPLOY':
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: `Do you want to attach to the running deploy process?`,
},
]);
if (!confirm) return false;
if (squid.organization) {
await this.pollDeploy({
organization: squid.organization,
deploy: squid.lastDeploy,
streamLogs: true,
});
}
return true;
}
}
async promptAddTag(
{ organization, name, tag }: { organization: Pick<Organization, 'code'>; name: string; tag: string },
{ using = 'using "--allow-tag-reassign" flag', interactive }: { using?: string; interactive?: boolean } = {},
) {
const oldSquid = await this.findSquid({
organization,
squid: { name, tag },
});
if (!oldSquid) return true;
const warning = `The tag "${tag}" has already been assigned to ${printSquid(oldSquid)}.`;
if (!interactive) {
this.error([warning, `Please do it explicitly ${using}`].join('\n'));
}
this.warn(warning);
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: 'Are you sure?',
prefix: `The tag will be reassigned.`,
},
]);
return !!confirm;
}
async pollDeploy({
deploy,
organization,
}: DeployRequest & { streamLogs?: boolean }): Promise<Deployment | undefined> {
let lastStatus: string;
let validatedPrinted = false;
await doUntil(
async () => {
this.deploy = await getDeploy({ deploy, organization });
if (!this.deploy) return true;
this.printDebug();
if (this.isFailed()) return this.showError(`An error occurred while deploying the squid`);
if (this.deploy.status === lastStatus) return false;
lastStatus = this.deploy.status;
CliUx.ux.action.stop(SUCCESS_CHECK_MARK);
switch (this.deploy.status) {
case 'UNPACKING':
CliUx.ux.action.start('◷ Preparing the squid');
return false;
case 'RESETTING':
CliUx.ux.action.start('◷ Resetting the squid');
return false;
case 'IMAGE_BUILDING':
CliUx.ux.action.start('◷ Building the squid');
if (!validatedPrinted) {
this.log(
'◷ You may now detach from the build process by pressing Ctrl + C. The Squid deployment will continue uninterrupted.',
);
this.log('◷ The new squid will be available as soon as the deployment is complete.');
validatedPrinted = true;
}
return false;
case 'SQUID_DELETING':
CliUx.ux.action.start('◷ Deleting the squid');
return false;
case 'ADDONS_DELETING':
CliUx.ux.action.start('◷ Deleting the squid addons');
return false;
case 'DEPLOYING':
case 'SQUID_SYNCING':
CliUx.ux.action.start('◷ Syncing the squid');
return false;
case 'ADDONS_SYNCING':
CliUx.ux.action.start('◷ Syncing the squid addons');
return false;
case 'ADDING_INGRESS':
case 'REMOVING_INGRESS':
CliUx.ux.action.start('◷ Configuring ingress');
return false;
case 'OK':
this.log(`Done! ${SUCCESS_CHECK_MARK}`);
return true;
default:
/**
* Just wait if some unexpected status has been received.
* This behavior is more safe for forward compatibility
*/
return false;
}
},
{ pause: 3000 },
);
return this.deploy;
}
async streamLogs({ organization, squid }: SquidRequest) {
CliUx.ux.action.start(`Streaming logs from the squid`);
await streamSquidLogs({
organization,
squid,
onLog: (l) => this.log(l),
});
}
printDebug = () => {
if (!this.deploy) return;
const logs = this.deploy.logs.slice(this.logsPrinted);
if (logs.length === 0) return;
this.logsPrinted += logs.length;
logs
.filter((v) => v)
.forEach(({ severity, message }) => {
switch (severity) {
case 'info':
this.log(chalk.cyan(message));
return;
case 'warn':
this.log(chalk.yellow(message));
return;
case 'error':
this.log(chalk.red(message));
return;
default:
this.log(chalk.dim(message));
}
});
};
showError(text: string, reason?: string): never {
CliUx.ux.action.stop('❌');
reason = reason || this.deploy?.failed || 'UNEXPECTED';
const errors: (string | null)[] = [text];
if (reason === 'UNEXPECTED') {
errors.push(
`------`,
'Please report to SquidDevs https://t.me/HydraDevs',
`${chalk.dim('Deploy:')} ${this.deploy?.id}`,
);
if (this.deploy?.squid) {
errors.push(`${chalk.dim('Squid:')} ${formatSquidReference(this.deploy.squid)}`);
}
}
// FIXME: maybe we should send an error report ourselves here with more details?
this.error(errors.filter(Boolean).join('\n'));
}
isFailed() {
if (!this.deploy) return true;
return this.deploy.failed !== 'NO';
}
logDeployResult(color: typeof ForegroundColor, message: string) {
this.log(
[
'',
chalk[color](`=================================================`),
message,
chalk[color](`=================================================`),
'',
].join('\n'),
);
}
}