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
Expand file tree
/
Copy pathmigration.ts
More file actions
64 lines (58 loc) · 2.18 KB
/
migration.ts
File metadata and controls
64 lines (58 loc) · 2.18 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
import * as AWS from "aws-sdk"
import * as config from "./config"
import * as ec2 from "./ec2"
import { Provisioner } from "./provisioner"
import * as ssm from "./ssm"
// Migration runs Code Ocean data migrations when system version changes.
export const Migration = new Provisioner<string, never>("migration-provisioner", {
dep: config.version.version,
onCreate: runMigration,
changeToken: "",
}, {
dependsOn: [
ec2.servicesInstance,
ec2.servicesDataVolume,
ssm.runMigrationDocument,
],
})
export const delay = (t: number) => new Promise(resolve => setTimeout(resolve, t))
function waitForSsmConnection(servicesInstanceId: string, ssmClient: AWS.SSM, callback: any): void {
var params = {
Target: servicesInstanceId,
}
ssmClient.getConnectionStatus(params, function(err, data) {
if (err || data.Status !== "connected") {
waitForSsmConnection(servicesInstanceId, ssmClient, callback)
} else {
callback()
}
})
}
function runMigration(): Promise<never> {
return new Promise((resolve, reject) => {
const runMigrationDocumentName = ssm.runMigrationDocument.name.get()
const servicesInstanceId = ec2.servicesInstance.id.get()
const ssmClient = new AWS.SSM({region: config.aws.region})
waitForSsmConnection(servicesInstanceId, ssmClient, () => {
ssmClient.sendCommand({
DocumentName: runMigrationDocumentName,
InstanceIds: [servicesInstanceId],
Parameters: {
"dryRun": ["false"],
},
}).promise().then(response => delay(3000).then(() => ssmClient.waitFor("commandExecuted", {
CommandId: response.Command!.CommandId!,
InstanceId: servicesInstanceId,
$waiter: {
delay: 10,
maxAttempts: 60,
},
}).promise().then(response => {
if (response.Status != "Success") {
throw new Error(`Migration failed with status '${response.Status}'`)
}
resolve()
}))).catch(reject)
})
})
}