-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-instance.js
More file actions
92 lines (77 loc) · 3.59 KB
/
new-instance.js
File metadata and controls
92 lines (77 loc) · 3.59 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
require('dotenv').config()
const log = require('./logger')(__filename)
const fs = require('fs')
const cloud = require('./cloud/cloud.js')
const deploy = require('./deploy')
const { getNotes, items } = require('./items')
const { getDomain, exec, SSH } = require('./utils')
const configurations = require('../configurations/instances')
const installSSLCertificates = async ({ service, ssh }) => {
const domains = service.domains || [`${service.name}-${service.tier}.${process.env.ALIAJS_DEFAULT_TOP_LEVEL_DOMAIN}`]
for (const domain of domains) {
const name = getDomain({ domain })
const fullchain = getNotes({ items: items.certificates, name: `${name}/fullchain.pem` })
const privkey = getNotes({ items: items.certificates, name: `${name}/privkey.pem` })
await ssh({ command: `sudo echo '${fullchain}' > fullchain.pem`, secrets: [] })
await ssh({ command: `sudo mv fullchain.pem /etc/ssl/certs/${domain}.pem` })
await ssh({ command: `sudo chmod 622 /etc/ssl/certs/${domain}.pem` })
await ssh({ command: `sudo echo '${privkey}' > privkey.pem`, secrets: [] })
await ssh({ command: `sudo mv privkey.pem /etc/ssl/private/${domain}.pem` })
await ssh({ command: `sudo chmod 600 /etc/ssl/private/${domain}.pem` })
}
}
exports.initInstance = async ({ address, instance, refresh, replace, response, temp }) => {
const { imageName, name, services, type } = instance
const keyName = instance.keyName || process.env.ALIAJS_KEY_NAME
const { Reservations } = await cloud.newInstance({ address, imageName, keyName, instance, name: `${name}-${Math.random().toString(36).slice(2, 8)}`, type })
instance.InstanceId = Reservations[0].Instances[0].InstanceId
instance.PublicIpAddress = Reservations[0].Instances[0].PublicIpAddress
const ssh = {
current: SSH({ address: instance.address, keyName, instance, response }),
new: SSH({ address: Reservations[0].Instances[0].PublicIpAddress, keyName, instance: Reservations[0].Instances[0], response }),
}
for (let item of items.operations) {
const name = item.name
if (name.match(/^authorized_keys/)) {
await ssh.new({ command: `echo '${getNotes({ items: items.operations, name })}' >> ~/.ssh/authorized_keys`})
}
}
for (const service of services) {
await installSSLCertificates({ service, ssh: ssh.new })
await deploy[service.type]({
address: Reservations[0].Instances[0].PublicIpAddress,
exec,
initial: true,
instance,
service,
ssh,
})
}
if (replace === true) {
if (typeof instance.address === 'string') {
await cloud.associateAddress({ instance, ssh })
} else {
for (const service of services) {
await cloud.upsertARecord({ instance, name: `${service.name}-${service.tier}`, zone: process.env.ALIAJS_DEFAULT_TOP_LEVEL_DOMAIN })
}
await new Promise(r => setTimeout(r, 5 * 60 * 1000))
}
}
return Reservations
}
exports.initInstances = async ({ address, instances, replace, response }) => {
const temp = (await exec({ command: 'mktemp -d' })).replace(/\s$/, '')
const runningReservations = await cloud.describeInstances()
for (let i = 0; i < instances.length; i++) {
const instance = instances[i]
const Reservations = await exports.initInstance({ address, instance, replace, response, temp })
if (replace === true) {
// Cloud instance deletion is the last thing to run for some reasons.
for (const runningInstance of runningReservations) {
if (new RegExp(`^${instance.name}-[a-z0-9]+$`).test(runningInstance.name)) {
await cloud.deleteInstance({ instance: runningInstance })
}
}
}
}
}