-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathprovision.js
76 lines (63 loc) · 2.48 KB
/
provision.js
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
const fs = require('fs')
const Linode = require('./linode.js')
const path = require('path')
const SETTINGS = {
SSH_PUB_KEYS: process.env.SSH_PUB_KEYS,
SERVER_ROOT_PASS: process.env.SERVER_ROOT_PASS || 'some_pass',
BRANCH: process.env.BRANCH || 'master',
ACTIVE_SERVER_COUNT: Number(process.env.ACTIVE_SERVER_COUNT) || 3
}
const infraSync = async () => {
console.info(`Deploying: ${SETTINGS.BRANCH}`)
console.info('--- manage script ---')
console.info('[INFO] updating #yacs-rcos script')
const script = await Linode.script.update(639784, {
images: ['linode/ubuntu18.04'],
label: 'yacs-rcos',
is_public: false,
script: fs.readFileSync(path.join(__dirname, './scripts/configure.sh'), 'utf8'),
description: 'Start YACS with some branch'
})
// ------------ VMS
console.info('--- manage vm(s) ---')
console.info('[INFO] fetching all #yacs-rcos vms')
const vms = await Linode.node.list()
console.log(`[INFO] currently, ${vms.length} vms are running/active`)
// fetch current to delete later
const rcosVmIds = vms
.filter(vm => vm.tags.includes('yacs-rcos'))
.sort((a, b) => Date(a.created) - Date(b.created)) // sort by date created, earliest created first
.map(vm => vm.id)
// create a new vm
console.info('[INFO] creating #yacs-rcos node')
const info = await Linode.node.create({
image: 'linode/ubuntu18.04', // or 'linode/ubuntu16.04lts'
root_pass: SETTINGS.SERVER_ROOT_PASS,
authorized_keys: SETTINGS.SSH_PUB_KEYS.split('\n').filter(Boolean),
type: 'g6-nanode-1',
region: 'us-east',
tags: ['yacs-rcos'],
stackscript_id: script.id,
stackscript_data: {
branch: SETTINGS.BRANCH
}
})
// delete existing vm(s) after creation
let amountToRemove = (rcosVmIds.length + 1) - SETTINGS.ACTIVE_SERVER_COUNT // plus one since we just added one
// after drop-all, no need to remove any so will be negative
if (amountToRemove < 0) {
amountToRemove = 0
}
console.info(`[INFO] deleting old vm(s) by tag #yacs-rcos that surpass server allotment of ${SETTINGS.ACTIVE_SERVER_COUNT}`)
console.info(`[INFO] amount to remove: ${amountToRemove}`)
// rcosVmIds is sorted, this allows us to remove the oldest server(s)
for (let i = 0; i < amountToRemove; i++) {
console.info(`[INFO] deleting vm: ${rcosVmIds[i]}`)
await Linode.node.remove(rcosVmIds[i])
}
// print ip
// USED IN GITHUB ACTIONS PIPELINE TO SHOW MESSAGE IN PR
// DO NOT REFORMAT
console.log(`https://${info.ipv4[0]}`)
}
infraSync()