This repository was archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ts
45 lines (41 loc) · 1.56 KB
/
init.ts
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
import * as AWS from "aws-sdk"
import * as config from "./config"
import * as ec2 from "./ec2"
import { delay, Migration } from "./migration"
import { Provisioner } from "./provisioner"
import * as ssm from "./ssm"
// InitSystemData runs Code Ocean system data initialization after each deployments.
export const InitSystemData = new Provisioner<config.AMIConfig, never>("init-system-data-provisioner", {
dep: config.ami,
onCreate: runInitSystemData,
changeToken: "",
}, {
dependsOn: [
ec2.servicesInstance,
Migration,
ssm.runInitSystemDataDocument,
],
})
function runInitSystemData(): Promise<never> {
return new Promise((resolve, reject) => {
const runInitSystemDataDocumentName = ssm.runInitSystemDataDocument.name.get()
const servicesInstanceId = ec2.servicesInstance.id.get()
const ssmClient = new AWS.SSM({region: config.aws.region})
ssmClient.sendCommand({
DocumentName: runInitSystemDataDocumentName,
InstanceIds: [servicesInstanceId],
}).promise().then(response => delay(3000).then(() => ssmClient.waitFor("commandExecuted", {
CommandId: response.Command!.CommandId!,
InstanceId: servicesInstanceId,
$waiter: {
delay: 5,
maxAttempts: 60,
},
}).promise().then(response => {
if (response.Status != "Success") {
throw new Error(`Init system data failed with status '${response.Status}'`)
}
resolve()
}))).catch(reject)
})
}