-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdeploy.ts
More file actions
575 lines (488 loc) · 17.5 KB
/
deploy.ts
File metadata and controls
575 lines (488 loc) · 17.5 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import fs from 'node:fs';
import path from 'node:path';
import { promisify } from 'util';
import { Args, Flags, ux as CliUx } from '@oclif/core';
import { Manifest, ManifestValue } from '@subsquid/manifest';
import chalk from 'chalk';
import diff from 'cli-diff';
import { globSync } from 'glob';
import ignore from 'ignore';
import inquirer from 'inquirer';
import { entries, get, pick } from 'lodash';
import prettyBytes from 'pretty-bytes';
import targz from 'targz';
import { deploySquid, OrganizationRequest, Squid, uploadFile } from '../api';
import { SqdFlags, SUCCESS_CHECK_MARK } from '../command';
import { DeployCommand } from '../deploy-command';
import { loadManifestFile } from '../manifest';
import { formatSquidReference, ParsedSquidReference, printSquid } from '../utils';
const compressAsync = promisify(targz.compress);
const SQUID_WORKDIR_DESC = [
`Squid working directory. Could be:`,
` - a relative or absolute path to a local folder (e.g. ".")`,
` - a URL to a .tar.gz archive`,
` - a github URL to a git repo with a branch or commit tag`,
];
export const UPDATE_COLOR = 'cyan';
export const CREATE_COLOR = 'green';
export const DELETE_COLOR = 'red';
export function resolveManifest(
localPath: string,
manifestPath: string,
): { error: string } | { buildDir: string; squidDir: string; manifest: Manifest; manifestRaw: string } {
try {
const { squidDir, manifest, manifestRaw } = loadManifestFile(localPath, manifestPath);
const buildDir = path.join(squidDir, 'builds');
fs.mkdirSync(buildDir, { recursive: true, mode: 0o777 });
return {
squidDir,
buildDir,
manifest,
manifestRaw,
};
} catch (e: any) {
return { error: e.message };
}
}
function example(command: string, description: string) {
// return [chalk.dim(`// ${description}`), command].join('\r\n');
return `${command} ${chalk.dim(`// ${description}`)}`;
}
export default class Deploy extends DeployCommand {
static description = 'Deploy new or update an existing squid in the Cloud';
static examples = [
example('sqd deploy .', 'Create a new squid with name provided in the manifest file'),
example(
'sqd deploy . -n my-squid-override',
'Create a new squid deployment and override it\'s name to "my-squid-override"',
),
example('sqd deploy . -n my-squid -s asmzf5', 'Update the "my-squid" squid with slot "asmzf5"'),
example(
'sqd deploy ./path-to-the-squid -m squid.prod.yaml',
'Use a manifest file located in ./path-to-the-squid/squid.prod.yaml',
),
example(
'sqd deploy /Users/dev/path-to-the-squid -m /Users/dev/path-to-the-squid/squid.prod.yaml',
'Full paths are also fine',
),
];
static help = 'If squid flags are not specified, the they will be retrieved from the manifest or prompted.';
static args = {
source: Args.directory({
description: [
`Squid source. Could be:`,
` - a relative or absolute path to a local folder (e.g. ".")`,
` - a URL to a .tar.gz archive`,
` - a github URL to a git repo with a branch or commit tag`,
].join('\n'),
required: true,
default: '.',
}),
};
static flags = {
org: SqdFlags.org({
required: false,
}),
name: SqdFlags.name({
required: false,
relationships: [],
}),
tag: SqdFlags.tag({
required: false,
dependsOn: [],
}),
slot: SqdFlags.slot({
required: false,
dependsOn: [],
}),
reference: SqdFlags.reference({
required: false,
}),
manifest: Flags.file({
char: 'm',
description: 'Specify the relative local path to a squid manifest file in the squid working directory',
required: false,
default: 'squid.yaml',
helpValue: '<manifest_path>',
}),
'hard-reset': Flags.boolean({
description:
'Perform a hard reset before deploying. This will drop and re-create all squid resources, including the database, causing a short API downtime',
required: false,
default: false,
}),
'stream-logs': Flags.boolean({
description: 'Attach and stream squid logs after the deployment',
required: false,
default: true,
allowNo: true,
}),
'add-tag': Flags.string({
description: 'Add a tag to the deployed squid',
required: false,
}),
'allow-update': Flags.boolean({
description: 'Allow updating an existing squid',
required: false,
default: false,
}),
'allow-tag-reassign': Flags.boolean({
description: 'Allow reassigning an existing tag',
required: false,
default: false,
}),
'allow-manifest-override': Flags.boolean({
description: 'Allow overriding the manifest during deployment',
required: false,
default: false,
}),
};
async run(): Promise<void> {
const {
args: { source },
flags: {
interactive,
manifest: manifestPath,
'hard-reset': hardReset,
'stream-logs': streamLogs,
'add-tag': addTag,
reference,
...flags
},
} = await this.parse(Deploy);
const isUrl = source.startsWith('http://') || source.startsWith('https://');
if (isUrl) {
this.log(`🦑 Releasing the squid from remote`);
return this.error('Not implemented yet');
}
if (interactive && hardReset) {
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: `Are you sure?`,
prefix: `Your squid will be reset, which may potentially result in data loss.`,
},
]);
if (!confirm) return;
}
this.log(`🦑 Releasing the squid from local folder`);
const res = resolveManifest(source, manifestPath);
if ('error' in res) return this.showError(res.error, 'MANIFEST_VALIDATION_FAILED');
const { buildDir, squidDir } = res;
const overrides = reference || (pick(flags, 'slot', 'name', 'tag', 'org') as Partial<ParsedSquidReference>);
let manifest = res.manifest;
// FIXME: it is not possible to override org atm
if (entries(overrides).some(([k, v]) => k !== 'org' && get(manifest, k) !== v)) {
// we need to do it to keep formatting the same
const manifestRaw = Manifest.replace(res.manifestRaw, {});
const newManifestRaw = Manifest.replace(manifestRaw, overrides);
if (!flags['allow-manifest-override']) {
const confirm = await this.promptOverrideConflict(manifestRaw, newManifestRaw, { interactive });
if (!confirm) return;
}
const newRes = Manifest.parse(newManifestRaw);
if (newRes.error) return this.showError(newRes.error.message, 'MANIFEST_VALIDATION_FAILED');
manifest = newRes.value;
}
const organization = await this.promptOrganization(overrides.org, { interactive });
const name = await this.promptSquidName(manifest.squidName(), { interactive });
const slot = manifest.slotName();
const tag = manifest.tag;
this.log(chalk.dim(`Squid directory: ${squidDir}`));
this.log(chalk.dim(`Build directory: ${buildDir}`));
this.log(chalk.dim(`Manifest: ${manifestPath}`));
this.log(chalk.cyan(`-----------------------------`));
this.log(chalk.cyan(`Organization: ${organization.code}`));
this.log(chalk.cyan(`Squid name: ${name}`));
if (slot) {
this.log(chalk.cyan(`Squid slot: ${slot}`));
} else if (tag) {
this.log(chalk.cyan(`Squid tag: ${tag}`));
}
this.log(chalk.cyan(`-----------------------------`));
let target: Squid | null = null;
if (slot) {
target = await this.findSquid({
organization,
squid: { name, slot },
});
} else if (tag) {
target = await this.findOrThrowSquid({
organization,
squid: { name, tag },
});
}
/**
* Squid exists we should check running deploys
*/
if (target) {
const attached = await this.promptAttachToDeploy(target, { interactive });
if (attached) return;
}
/**
* Squid exists we should ask for update
*/
if (target && !flags['allow-update']) {
const update = await this.promptUpdateSquid(target, { interactive, tag });
if (!update) return;
}
/**
* Prevent deploying if the Postgres version has changed (requires hard reset)
*/
const versionMismatch = getPostgresVersionMismatch(target, manifest);
if (!hardReset && versionMismatch) {
this.error(
`The squid ${printSquid(target!)} is currently using Postgres ${versionMismatch.currentVersion}, but the new manifest specifies Postgres ${versionMismatch.newVersion}. ` +
`Changing the Postgres version requires a hard reset. Please do it explicitly using "--hard-reset" flag.`,
);
}
/**
* Warn if the existing squid has a Postgres addon but the new manifest removes it
*/
if (!hardReset && target?.addons?.postgres && !manifest.deploy?.addons?.postgres) {
const confirmed = await this.promptPostgresDeletion(target, { interactive });
if (!confirmed) return;
}
/**
* Squid exists we should check if tag belongs to another squid
*/
const hasTag = !!target?.tags.find((t) => t.name === addTag) || tag === addTag;
if (addTag && !flags['allow-tag-reassign'] && !hasTag) {
const add = await this.promptAddTag({ organization, name, tag: addTag }, { interactive });
if (!add) return;
}
const archiveName = `${slot || tag ? formatSquidReference({ name, slot, tag }) : name}.tar.gz`;
const artifactPath = await this.pack({ buildDir, squidDir, archiveName });
const artifactUrl = await this.upload({ organization, artifactPath });
const deploy = await deploySquid({
organization,
data: {
artifactUrl,
manifestPath,
options: {
hardReset,
overrideName: name,
overrideSlot: target?.slot || slot,
tag: addTag,
},
},
});
const deployment = await this.pollDeploy({ organization, deploy });
if (!deployment || !deployment.squid) return;
if (target) {
this.logDeployResult(UPDATE_COLOR, `The squid ${printSquid(target)} has been successfully updated`);
} else {
this.logDeployResult(
CREATE_COLOR,
`A new squid ${printSquid({ ...deployment.squid, organization: deployment.organization })} has been successfully created`,
);
}
if (streamLogs) {
await this.streamLogs({ organization: deployment.organization, squid: deployment.squid });
}
}
private async promptUpdateSquid(
squid: Squid,
{
using = 'using "--allow-update" flag',
interactive,
tag,
}: {
using?: string;
interactive?: boolean;
tag?: string;
} = {},
) {
const hasOtherTags = (!tag && squid.tags.length > 0) || squid.tags.some((t) => t.name !== tag);
const warning = [
`The squid ${printSquid(squid)} already exists${hasOtherTags ? ` and has one or more tags assigned to it:` : ``}`,
];
if (hasOtherTags) {
warning.push(...squid.tags.map((t) => chalk.dim(` - ${t.name}`)));
}
if (interactive) {
this.warn(warning.join('\n'));
} else {
this.error([...warning, `Please do it explicitly ${using}`].join('\n'));
}
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: 'Are you sure?',
prefix: `The squid ${printSquid(squid)} will be updated.`,
},
]);
return !!confirm;
}
private async promptOverrideConflict(
dest: string,
src: string,
{ using = 'using "--allow--manifest-override" flag', interactive }: { using?: string; interactive?: boolean } = {},
) {
const warning = [
'Conflict detected!',
`A manifest values do not match with specified ones.`,
``,
diff({ content: dest }, { content: src }),
``,
].join('\n');
if (interactive) {
this.warn(warning);
} else {
this.error([warning, `Please do it explicitly ${using}`].join('\n'));
}
this.log(
`If it is intended and you'd like to override them, just skip this message and confirm, the manifest name will be overridden automatically in the Cloud during the deploy.`,
);
const { confirm } = await inquirer.prompt([
{
name: 'confirm',
type: 'confirm',
message: chalk.reset(`Manifest values will be overridden. ${chalk.bold('Are you sure?')}`),
},
]);
return !!confirm;
}
private async promptPostgresDeletion(
squid: Squid,
{ using = 'using "--allow-postgres-deletion" flag', interactive }: { using?: string; interactive?: boolean } = {},
) {
const warning = `The new manifest does not include "addons.postgres", but the squid ${printSquid(squid)} currently has a Postgres database. Deploying will permanently delete the database and all its data.`;
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 you want to continue?',
prefix: `The Postgres database will be permanently deleted.`,
},
]);
return !!confirm;
}
private async promptSquidName(
name?: string | null | undefined,
{ using = 'using "--name" flag', interactive }: { using?: string; interactive?: boolean } = {},
) {
if (name) return name;
const warning = `The squid name is not defined either in the manifest or via CLI command.`;
if (interactive) {
this.warn(warning);
} else {
this.error([warning, `Please specify it explicitly ${using}`].join('\n'));
}
const { input } = await inquirer.prompt([
{
name: 'input',
type: 'input',
message: `Please enter the name of the squid:`,
},
]);
return input as string;
}
private async pack({ buildDir, squidDir, archiveName }: { buildDir: string; squidDir: string; archiveName: string }) {
CliUx.ux.action.start(`◷ Compressing the squid to ${archiveName} `);
const squidIgnore = createSquidIgnore(squidDir);
const squidArtifact = path.join(buildDir, archiveName);
let filesCount = 0;
await compressAsync({
src: squidDir,
dest: squidArtifact,
tar: {
ignore: (name) => {
const relativePath = path.relative(path.resolve(squidDir), path.resolve(name));
if (squidIgnore.ignores(relativePath)) {
this.log(chalk.dim(`-- ignoring ${relativePath}`));
return true;
} else {
this.log(chalk.dim(`adding ${relativePath}`));
filesCount++;
return false;
}
},
},
});
if (filesCount === 0) {
return this.showError(
`0 files were found in ${squidDir}. Please check the squid source, looks like it is empty`,
'PACKING_FAILED',
);
}
const squidArtifactStats = fs.statSync(squidArtifact);
CliUx.ux.action.stop(`${filesCount} files, ${prettyBytes(squidArtifactStats.size)} ${SUCCESS_CHECK_MARK}`);
return squidArtifact;
}
private async upload({ organization, artifactPath }: OrganizationRequest & { artifactPath: string }) {
CliUx.ux.action.start(`◷ Uploading ${path.basename(artifactPath)}`);
const { error, fileUrl: artifactUrl } = await uploadFile({ organization, path: artifactPath });
if (error) {
return this.showError(error);
} else if (!artifactUrl) {
return this.showError('The artifact URL is missing', 'UPLOAD_FAILED');
}
CliUx.ux.action.stop(SUCCESS_CHECK_MARK);
return artifactUrl;
}
}
export function createSquidIgnore(squidDir: string) {
const ig = ignore().add(
// default ignore patterns
['node_modules', '.git'],
);
const ignoreFilePaths = globSync(['.squidignore', '**/.squidignore'], {
cwd: squidDir,
nodir: true,
posix: true,
});
if (!ignoreFilePaths.length) {
return ig.add([
// squid uploaded archives directory
'/builds',
// squid built files
'/lib',
// IDE files
'.idea',
'.vscode',
]);
}
for (const ignoreFilePath of ignoreFilePaths) {
const raw = fs.readFileSync(path.resolve(squidDir, ignoreFilePath)).toString();
const ignoreDir = path.dirname(ignoreFilePath);
const patterns = getIgnorePatterns(ignoreDir, raw);
ig.add(patterns);
}
return ig;
}
export function getIgnorePatterns(ignoreDir: string, raw: string) {
const lines = raw.split('\n');
const patterns: string[] = [];
for (let line of lines) {
line = line.trim();
if (line.length === 0) continue;
if (line.startsWith('#')) continue;
let pattern = line.startsWith('/') || line.startsWith('*/') || line.startsWith('**/') ? line : `**/${line}`;
pattern = ignoreDir === '.' ? pattern : `${toRootPattern(ignoreDir)}${toRootPattern(pattern)}`;
patterns.push(pattern);
}
return patterns;
}
function toRootPattern(pattern: string) {
return pattern.startsWith('/') ? pattern : `/${pattern}`;
}
export function getPostgresVersionMismatch(
target: Squid | null,
manifest: Manifest,
): { currentVersion: string; newVersion: string } | null {
if (!target?.addons?.postgres || !manifest.deploy?.addons?.postgres) return null;
const currentManifest = target.manifest.current as ManifestValue;
const currentVersion = currentManifest.deploy?.addons?.postgres?.version;
const newVersion = manifest.deploy.addons.postgres.version;
if (currentVersion && newVersion && currentVersion !== newVersion) {
return { currentVersion, newVersion };
}
return null;
}