-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.mjs
More file actions
379 lines (332 loc) · 9 KB
/
Copy pathdeploy.mjs
File metadata and controls
379 lines (332 loc) · 9 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
#!/usr/bin/env node
import process from 'node:process'
import { promisify } from 'node:util'
import inquirer from 'inquirer'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts'
import path from 'node:path'
import os from 'node:os'
import { spawn as nativeSpawn, exec } from 'node:child_process'
import { once } from 'node:events'
import { ListTablesCommand } from '@aws-sdk/client-dynamodb'
import deployStrategy from '../.github/actions/deploy-strategy.js'
import ora from 'ora'
const windows = os.platform() === 'win32'
console.log(
'\u001B[93mGitHub is the recommended way to deploy, only use this script if you have to\u001B[0m'
)
try {
const ps = spawn('command', ['-v', 'aws'])
await once(ps, 'close')
if (ps.exitCode !== 0) {
throw new Error('missing aws')
}
} catch {
console.error('\u001B[91mmissing aws cli\u001B[0m')
console.log(
'\u001B[90msee https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html\u001B[0m'
)
process.exit(0)
}
if (!process.env.AWS_ACCESS_KEY_ID) {
console.error('\u001B[91mmissing aws credentials\u001B[0m')
process.exit(1)
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const settings = JSON.parse(
await readFile(new URL('../packages/settings.json', import.meta.url))
)
const sts = new STSClient({ region: 'us-east-1' })
const { Account: accountId } = await sts.send(new GetCallerIdentityCommand())
const awsAccount = settings.awsAccounts[accountId]
if (!awsAccount) {
console.error(
`\u001B[91maccount ${accountId} not found in ../packages/settings.json\u001B[0m`
)
process.exit(0)
}
process.once('exit', () => cursor(true))
process.once('SIGINT', () => cursor(true))
cursor(false)
const { region, stage } = await getStage()
const output = {}
cursor(true)
const { removeOrDeploy } = await inquirer.prompt({
type: 'confirm',
name: 'removeOrDeploy',
default: false,
message: `remove ${stage}`
})
const { removePrompt } =
removeOrDeploy &&
(await inquirer.prompt({
type: 'input',
name: 'removePrompt',
message: 'type "permanently delete" to remove'
}))
if (removeOrDeploy && removePrompt !== 'permanently delete') {
process.exit(0)
}
const remove = removeOrDeploy && removePrompt === 'permanently delete'
if (removePrompt && !remove) {
process.exit(0)
}
cursor(false)
const loadingDeployments = ora({
text: `get deployment strategy for stage ${stage}`,
color: 'gray'
}).start()
await deployStrategy({
core: {
setOutput(name, value) {
output[name] = value
}
},
remove,
stage,
nodePath: 'node',
region,
npmCacheHit: false
})
loadingDeployments.stop()
if (
[
'ordered',
'parallel-stage',
'parallel-backend',
'parallel-frontend',
'parallel-remaining'
].every((deployType) => !output[deployType])
) {
console.log('\u001B[32meverything up-to-date\u001B[0m')
process.exit(0)
}
const strategy = [
'ordered',
'parallel-stage',
'parallel-backend',
'parallel-frontend',
'parallel-remaining'
]
if (remove) {
strategy.shift()
strategy.reverse()
}
console.log(
`\u001B[90mwill ${remove ? 'remove' : 'deploy'} ${strategy
.flatMap((deployType) =>
output[`${deployType}-strategy`]?.matrix?.include.map(
(x) => `${x.stack}(${x.region})`
)
)
.filter(Boolean)
.join(', ')} to ${stage}\u001B[0m`
)
cursor(true)
if (!stage.startsWith('pr-')) {
const { run } = await inquirer.prompt({
type: 'confirm',
name: 'run',
default: false,
message: `It's recommended to use GitHub actions, continue with ${
remove ? 'removal of' : 'deployment to'
} stage ${stage}`
})
if (!run) {
process.exit(0)
}
}
for (const type of strategy) {
if (output[type]) {
const deployment = output[`${type}-strategy`]
const include = [...deployment.matrix.include]
const parallel = deployment['max-parallel']
console.log(
`\u001B[90m${remove ? 'remove' : 'deploy'} ${type} ${include
.map((x) => x.stack)
.join(', ')}\u001B[0m`
)
for (const batch of batchFor({ include, parallel })) {
const processes = batch.map((service) =>
createDeployProcess(service, remove)
)
const pendingExitCodes = Promise.all(
processes.map(({ ps }) => once(ps, 'exit'))
)
await Promise.all(
processes.flatMap(({ ps, stack, region }) =>
['stdout', 'stderr'].map((stream) =>
pipe({ ps, stream, stack, region })
)
)
)
for (const [code, signal] of await pendingExitCodes) {
if (code === null) {
throw new Error(`failed, process was killed by signal ${signal}`)
} else if (code !== 0) {
throw new Error(`failed, process exited with code ${code}`)
}
}
}
}
}
async function pipe({ ps, stream, stack, region }) {
for await (const chunk of ps[stream]) {
for (const line of chunk.toString().split(/[\n\r]/)) {
if (line.trim().length > 0) {
console.log(`\u001B[0m${stack}(${region}) ${line}`)
}
}
}
}
async function assertStageDeployed(stage, region) {
process.env.AWS_REGION = region
process.env.AWS_DEFAULT_REGION = region
const { default: dynamodb } = await import(
path.join(__dirname, '..', 'packages', 'shared', 'dynamodb.js')
)
const { TableNames: tableNames = [] } = await dynamodb.send(
new ListTablesCommand({})
)
if (
!tableNames.some(function findPullRequestStackTable(table) {
return table.includes(stage)
})
) {
console.error(`\u001B[91mno ${stage} environment found\u001B[0m`)
process.exit(0)
}
}
async function getStage() {
if (settings.accountPerStage && awsAccount.stage === 'feature') {
const loadingStage = ora({ text: 'get stage', color: 'gray' }).start()
const ref = await getPullRequestRef()
const stage = `pr-${ref}`
const region = settings.regions[awsAccount.stage]
await assertStageDeployed(stage, region)
loadingStage.stop()
return {
region,
stage
}
} else {
const { stage } = settings.accountPerStage
? { stage: awsAccount.stage }
: await inquirer.prompt({
type: 'list',
name: 'stage',
choices: settings.stages,
message: 'region'
})
const region = settings.regions[stage]
if (stage === 'feature') {
const loadingStage = ora({ text: 'get stage', color: 'gray' }).start()
const ref = await getPullRequestRef()
const prStage = `pr-${ref}`
await assertStageDeployed(prStage, region)
loadingStage.stop()
return {
region,
stage: prStage
}
} else {
return { region, stage }
}
}
}
async function getPullRequestRef() {
const run = promisify(exec)
try {
const { stdout } = await run(`
git ls-remote --refs origin | \
grep $(git rev-parse @{push}) | \
grep -oE 'pull/[0-9]+' | \
sed 's|^pull/||g'`)
const ref = stdout.replaceAll(/[\n\r]/g, '')
if (ref) {
return Number(ref)
}
} catch {
// eslint-disable-next-line no-empty
}
console.error('\u001B[91mno pull request found\u001B[0m')
process.exit(0)
}
function spawn(cmd, args, options) {
if (windows) {
args = [
'/C',
cmd,
...args.map((arg) => {
return typeof arg === 'string' ? arg.replaceAll('^', '^^^^') : arg
})
]
cmd = 'cmd'
}
return nativeSpawn(cmd, args, options)
}
function cursor(show) {
if (show) {
process.stdout.write('\u001B[?25h')
} else {
process.stderr.write('\u001B[?25l')
}
}
function createDeployProcess(service, remove) {
return {
ps: spawn(
'bash',
[
path.join(__dirname, '..', 'scripts', 'sam.sh'),
service.stage,
service.region,
service['package-lock'].toString(),
remove.toString()
],
{
cwd: service.directory,
shell: true,
env: {
FORCE_COLOR: '1',
NODE_OPTIONS: '--unhandled-rejections=strict',
...process.env,
INIT_CWD: undefined,
PATH: `${process.env.PATH}:${path.resolve(
path.join(__dirname, '..', 'node_modules', '.bin')
)}`,
...(windows && {
MSYSTEM: `mingw${os.arch() === 'x64' ? '64' : '32'}`
})
}
}
),
stack: service.stack,
region: service.region
}
}
function* batchFor({ include, parallel }) {
const pending = [...include]
while (pending.length > 0) {
const batch = pending.splice(0, parallel)
if (batch.length === 1) {
yield batch
} else {
const result = []
for (const item of batch) {
const directory = item.directory
const directoryItems = batch.filter((x) => x.directory === directory)
if (directoryItems.length > 1) {
for (const directoryItem of directoryItems) {
yield [directoryItem]
}
} else {
result.push(item)
}
}
if (result.length > 0) {
yield result
}
}
}
}