-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
409 lines (346 loc) · 16.5 KB
/
deploy.js
File metadata and controls
409 lines (346 loc) · 16.5 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const dotenv = require('dotenv')
dotenv.config()
const log = require('./logger')(__filename)
const dns = require('dns')
const { Eta } = require('eta')
const fs = require('fs')
const https = require('https')
const path = require('path')
const util = require('util')
const { getItems, getNotes, items } = require('./items')
const { retry } = require('./utils')
const lookup = util.promisify(dns.lookup)
const eta = new Eta({ views: path.join(__dirname, '..', 'templates'), useWith: true, autoTrim: false })
exports.erpnext = async function ({ address, checkout, domain, exec, initial, home, instance, service, ssh, user }) {
// https://frappeframework.com/docs/user/en/bench/reference/new-site
// https://frappeframework.com/docs/user/en/production-setup
// https://frappeframework.com/docs/user/en/bench/guides/setup-production
// https://frappeframework.com/docs/user/en/installation
// https://frappeframework.com/docs/user/en/tutorial/install-and-setup-bench
// https://frappeframework.com/docs/user/en/basics/sites#site-config
// https://frappeframework.com/docs/user/en/bench/reference/restore
const { domains, locations, remote_repository } = service
const temp = (await exec({ command: 'mktemp -d' })).replace(/\s$/, '')
const repository = `${temp}/repository`
const staticBuilds = `${process.env.ALIAJS_DEFAULT_PATH}/static-builds-${service.tier}`
const unique = `${service.name}-${Date.now()}`
const uniqueBuilds = {}
await exec({ command: `mkdir ${repository} ${temp}/sync` })
if (fs.existsSync(`${staticBuilds}`) === false) {
await exec({ command: `mkdir ${staticBuilds}` })
}
const builds = locations.filter((location) => {
return typeof location.build === 'string'
})
if (builds.length > 0) {
await exec({ command: `git clone ${remote_repository} ${repository}`})
if (checkout === undefined || checkout === '') {
checkout = process.env.ALIAJS_DEFAULT_CHECKOUT
try {
checkout = JSON.parse(await ssh.current({ command: `cat ${staticBuilds}/${service.name}-default.json` })).checkout
} catch (error) {
log.warn(`WARNING! could not parse build info from file, checkout value will be set to ${checkout} (it can be OK)`)
}
}
builds[0].checkout = checkout
if (typeof builds[0].split === 'object') {
builds[0].split[0].checkout = checkout
}
for (let i = 0; i < builds.length; i++) {
builds[i].alias = `${staticBuilds}/$ab/`
if (builds[i].split === undefined) {
builds[i].split = [{ checkout: builds[i].checkout, split: 100 }]
}
for (let j = 0; j < builds[i].split.length; j++) {
const buildIndex = await execBuild({ build: builds[i].split[j], exec, repository, service, staticBuilds })
builds[i].split[j].buildIndex = buildIndex
if (i === 0) {
uniqueBuilds['default'] = buildIndex
fs.writeFileSync(`${staticBuilds}/${service.name}-default.json`, JSON.stringify(builds[0].split[0]))
}
uniqueBuilds[buildIndex] = buildIndex
}
}
}
if (domain === undefined) {
domain = process.env.ALIAJS_DEFAULT_TOP_LEVEL_DOMAIN
}
if (home === undefined) {
home = process.env.ALIAJS_DEFAULT_PATH
}
if (user === undefined) {
user = process.env.ALIAJS_DEFAULT_USER
}
const server_name = `${service.name}-${service.tier}.${domain}`
if (address === undefined) {
address = (await lookup(server_name)).address
}
await exec({ command: `mkdir ${temp}/sync/sites-enabled` })
for (let domain of domains) {
const config = await eta.renderAsync('nginx/frappe', { locations, home, server_name: domain, uniqueBuilds })
fs.writeFileSync(`${temp}/sync/sites-enabled/${domain}`, config)
}
const custom = await eta.renderAsync('nginx/custom', {})
fs.writeFileSync(`${temp}/sync/custom.conf`, custom)
await exec({ command: `rsync -az ${temp}/sync/ ${user}@${address}:${home}/${unique}` })
await exec({ command: `rsync -az ${staticBuilds} ${user}@${address}:${home}` })
await ssh.new({ command: `sudo mv -f ${home}/${unique}/custom.conf /etc/nginx/conf.d/` })
await ssh.new({ command: `sudo cp -f ${home}/${unique}/sites-enabled/* /etc/nginx/sites-enabled/` })
if (initial) {
await setup({ data: { address, aliajs_key_name: process.env.ALIAJS_KEY_NAME, home, instance, server_name, temp }, exec, service, ssh, type: 'initial' })
}
await ssh.new({ command: 'sudo nginx -t' })
await ssh.new({ command: 'sudo service nginx reload' })
}
exports.nginx = async function ({ address, checkout, domain, exec, initial, home, instance, service, ssh, user }) {
const { domains, locations, remote_repository } = service
const temp = (await exec({ command: 'mktemp -d' })).replace(/\s$/, '')
const repository = `${temp}/repository`
const staticBuilds = `${process.env.ALIAJS_DEFAULT_PATH}/static-builds-${service.tier}`
const unique = `${service.name}-${Date.now()}`
const uniqueBuilds = {}
await exec({ command: `mkdir ${repository} ${temp}/sync` })
if (fs.existsSync(`${staticBuilds}`) === false) {
await exec({ command: `mkdir ${staticBuilds}` })
}
const builds = locations.filter((location) => {
return typeof location.build === 'string'
})
if (builds.length > 0) {
await exec({ command: `git clone ${remote_repository} ${repository}`})
if (checkout === undefined || checkout === '') {
checkout = process.env.ALIAJS_DEFAULT_CHECKOUT
try {
checkout = JSON.parse(await ssh.current({ command: `cat ${staticBuilds}/${service.name}-default.json` })).checkout
} catch (error) {
log.warn(`WARNING! could not parse build info from file, checkout value will be set to ${checkout} (it can be OK)`)
}
}
builds[0].checkout = checkout
if (typeof builds[0].split === 'object') {
builds[0].split[0].checkout = checkout
}
for (let i = 0; i < builds.length; i++) {
builds[i].alias = `${staticBuilds}/$ab/`
if (builds[i].split === undefined) {
builds[i].split = [{ checkout: builds[i].checkout, split: 100 }]
}
for (let j = 0; j < builds[i].split.length; j++) {
const buildIndex = await execBuild({ build: builds[i].split[j], exec, repository, service, staticBuilds })
builds[i].split[j].buildIndex = buildIndex
if (i === 0) {
uniqueBuilds['default'] = buildIndex
fs.writeFileSync(`${staticBuilds}/${service.name}-default.json`, JSON.stringify(builds[0].split[0]))
}
uniqueBuilds[buildIndex] = buildIndex
}
}
}
if (domain === undefined) {
domain = process.env.ALIAJS_DEFAULT_TOP_LEVEL_DOMAIN
}
if (home === undefined) {
home = process.env.ALIAJS_DEFAULT_PATH
}
if (user === undefined) {
user = process.env.ALIAJS_DEFAULT_USER
}
const server_name = `${service.name}-${service.tier}.${domain}`
if (address === undefined) {
address = (await lookup(server_name)).address
}
await exec({ command: `mkdir ${temp}/sync/sites-enabled` })
let template = 'server'
if (service.template) {
template = service.template
}
for (let domain of domains) {
const config = await eta.renderAsync(`nginx/${template}`, { locations, home, server_name: domain, uniqueBuilds })
fs.writeFileSync(`${temp}/sync/sites-enabled/${domain}`, config)
}
const custom = await eta.renderAsync('nginx/custom', {})
fs.writeFileSync(`${temp}/sync/custom.conf`, custom)
await exec({ command: `rsync -az ${temp}/sync/ ${user}@${address}:${home}/${unique}` })
await exec({ command: `rsync -az ${staticBuilds} ${user}@${address}:${home}` })
await ssh.new({ command: `sudo mv -f ${home}/${unique}/custom.conf /etc/nginx/conf.d/` })
await ssh.new({ command: `sudo cp -f ${home}/${unique}/sites-enabled/* /etc/nginx/sites-enabled/` })
if (initial) {
await setup({ data: { address, aliajs_key_name: process.env.ALIAJS_KEY_NAME, home, instance, server_name, temp }, exec, service, ssh, type: 'initial' })
}
await ssh.new({ command: 'sudo nginx -t' })
await ssh.new({ command: 'sudo service nginx reload' })
}
exports.nodejs = async function ({ address, checkout, domain, exec, home, initial, instance, service, ssh, user, websocket }) {
if (checkout === undefined || checkout === '') {
checkout = process.env.ALIAJS_DEFAULT_CHECKOUT
try {
const directory = (await ssh.current({ command: `ls -t | grep ^${service.name} | head -n 1` })).replace(/\s$/, '')
const status = await ssh.current({ command: `cd ${directory} && git status` })
checkout = status.split('\n')[0].split(' ').pop()
} catch (error) {
log.warn(`WARNING! ${service.name} on current instance is not set, checkout value will be set to ${checkout} (it can be OK)`)
}
}
if (domain === undefined) {
domain = process.env.ALIAJS_DEFAULT_TOP_LEVEL_DOMAIN
}
if (home === undefined) {
home = process.env.ALIAJS_DEFAULT_PATH
}
if (user === undefined) {
user = process.env.ALIAJS_DEFAULT_USER
}
const server_name = `${service.name}-${service.tier}.${domain}`
const temp = (await exec({ command: 'mktemp -d' })).replace(/\s$/, '')
const unique_service_name = `${service.name}-${new Date().getTime()}`
if (address === undefined) {
address = (await lookup(server_name)).address
}
let services = await ssh.new({ command: 'ls -t /etc/systemd/system' })
services = services.split('\n').filter((folder) => {
return folder.match(new RegExp(`^${service.name}`))
}).reverse()
await exec({ command: `git clone ${service.remote_repository} ${temp}`})
await exec({ command: `cd ${temp} && git checkout ${checkout}` })
await exec({ command: `cd ${temp} && ${command({ service, type: 'packages' })}` })
await exec({ command: `cd ${temp} && ${command({ service, type: 'build' })}` })
let port
await retry(async () => {
port = Math.floor(Math.random() * (65536 - 1024) + 1024)
let used
try {
await exec({ command: `nc -z 127.0.0.1 ${port}` })
used = true
} catch(error) {
// An error means the port is free.
console.log(`Port ${port} is free`)
}
if (used === true) {
throw new Error(`Port ${port} is already in used`)
}
})
let variables = ''
try {
const variablesTemplate = fs.readFileSync(`${__dirname}/../configurations/systemd/variables/${service.name}/${service.tier}.eta`, 'utf8')
variables = await eta.renderStringAsync(variablesTemplate, { main: command({ service, type: 'main' }), run: command({ service, type: 'run' }), service_name: service.name, tier: service.tier, unique_service_name, user, home, domain, port, variables })
} catch (error) {
log.warn(`WARNING! this service has no variable file for tier ${service.tier} (it can be OK)`)
}
const env = dotenv.parse(fs.readFileSync(`${temp}/.env`))
items.development = getItems({ variables: items.operations.variables[0] })
for (let name in env) {
if (env[name] === '') {
variables = `${variables}\nEnvironment=${name}=${getNotes({ items: items.development, name, systemdEscape: true })}`
}
}
const system = await eta.renderAsync('systemd/service', { main: command({ service, type: 'main' }), run: command({ service, type: 'run' }), service_name: service.name, tier: service.tier, unique_service_name, user, home, domain, port, variables })
fs.writeFileSync(`${temp}/service`, system)
const locations = [{ location: '/', proxy_pass: `http://127.0.0.1:${port}` }]
if (service.name === 'aliajs') {
locations[0].proxy_read_timeout = '500s'
fs.appendFileSync(`${temp}/.env`, `\nALIAJS_VARIABLE_0=${process.env.ALIAJS_VARIABLE_0}\nALIAJS_VARIABLE_1=${process.env.ALIAJS_VARIABLE_1}\nALIAJS_VARIABLE_2=${process.env.ALIAJS_VARIABLE_2}`)
}
const domains = service.domains || [server_name]
await exec({ command: `mkdir ${temp}/sites-enabled` })
for (let domain of domains) {
const config = await eta.renderAsync('nginx/server', { locations, server_name: domain })
fs.writeFileSync(`${temp}/nginx`, config)
fs.writeFileSync(`${temp}/sites-enabled/${domain}`, config)
}
const custom = await eta.renderAsync('nginx/custom', {})
fs.writeFileSync(`${temp}/custom`, custom)
await exec({ command: `rsync -az ${temp}/ ${user}@${address}:${home}/${unique_service_name}` })
await ssh.new({ command: `sudo cp -f ${home}/${unique_service_name}/sites-enabled/* /etc/nginx/sites-enabled/` })
await ssh.new({ command: `sudo mv -f ${home}/${unique_service_name}/custom /etc/nginx/conf.d/custom.conf` })
await ssh.new({ command: `sudo mv ${home}/${unique_service_name}/service /etc/systemd/system/${unique_service_name}.service` })
if (initial) {
await ssh.new({ command: `echo '127.0.0.1 ${server_name}' | sudo tee -a /etc/hosts` })
await setup({ data: { address, aliajs_key_name: process.env.ALIAJS_KEY_NAME, home, instance, server_name, temp, unique_service_name }, exec, service, ssh, type: 'initial' })
}
await setup({ data: { instance, unique_service_name }, exec, service, ssh, type: 'post-build' })
await ssh.new({ command: `sudo service ${unique_service_name} start` })
await ssh.new({ command: `sudo systemctl enable ${unique_service_name}` })
await retry(async () => {
const response = await ssh.new({ command: `curl -i http://127.0.0.1:${port}/404-uc3C6` })
if (response.match(/^(\S+)\s404/) === null) {
throw new Error(`Service ${service.name} not responding 404 status`)
}
})
await ssh.new({ command: 'sudo nginx -t' })
await ssh.new({ command: 'sudo service nginx reload' })
await retry(async () => {
const response = await new Promise((resolve, reject) => {
const req = https.request({
host: address,
port: 443,
path: '/404-uc3C6',
method: 'GET',
headers: { Host: server_name },
servername: server_name,
}, resolve)
req.on('error', reject)
req.end()
})
response.resume()
if (response.statusCode !== 404) {
throw new Error(`Service ${service.name} not responding 404 status`)
}
})
if (service.name === 'aliajs') {
await ssh.new({ command: `echo \"0 5 * * 0 cd ${home}/${unique_service_name} && /usr/bin/node ${home}/${unique_service_name}/src/renew-certificates.js > renew-certificates.log 2>&1\n0 6 * * * cd ${home}/${unique_service_name} && /usr/bin/node ${home}/${unique_service_name}/src/new-image.js > new-image.log 2>&1\n30 8 * * * cd ${home}/${unique_service_name} && /usr/bin/node ${home}/${unique_service_name}/src/refresh-instances.js > refresh-instances.log 2>&1\" >> aliajs_cron; crontab aliajs_cron; rm aliajs_cron` })
}
for (let i = 0; i < services.length; i++) {
const service = services[i].replace(/\.service$/, '')
await ssh.new({ command: `sudo service ${service} stop` })
await ssh.new({ command: `sudo systemctl disable ${service}` })
}
await exec({ command: `rm -rf ${temp}` })
}
function command({ data, service, type }) {
const defaults = {
javascript: {
build: "echo 'nothing to build'",
main: '/src/main',
packages: 'npm install',
run: '/usr/bin/node',
},
typescript: {
build: 'npm run build',
main: '/dist/main',
packages: 'npm --production=false install',
run: '/usr/bin/node',
},
}
if (typeof service.setup === 'object' && typeof service.setup[type] === 'string') {
return eta.renderString(service.setup[type], data)
} else {
return eta.renderString(defaults[service.language][type], data)
}
}
async function execBuild({ build, exec, repository, service, staticBuilds }) {
await exec({ command: `cd ${repository} && git checkout --force ${build.checkout}` })
const buildIndex = (await exec({ command: `cd ${repository} && git rev-parse --verify HEAD` })).replace(/\s$/, '')
if (fs.existsSync(`${staticBuilds}/${buildIndex}`) === false) {
await exec({ command: `cd ${repository} && npm --production=false install` })
await exec({ command: `cd ${repository} && npm run build` })
fs.writeFileSync(`${repository}/dist/build.json`, JSON.stringify(build))
await exec({ command: `mv ${repository}/dist ${staticBuilds}/${buildIndex}` })
await exec({ command: `rm -rf ${repository}/node_modules ${repository}/package-lock.json` })
} else {
log.warn(`WARNING! ${staticBuilds}/${buildIndex} already exists, skipping this build (it can be OK)`)
}
return buildIndex
}
async function setup({ data, exec, service, ssh, type }) {
const targets = {
current: ssh.current,
new: ssh.new,
orchestrator: exec
}
if (typeof service.setup === 'object' && typeof service.setup[type] === 'object') {
for (let i = 0; i < service.setup[type].length; i++) {
const command = eta.renderString(service.setup[type][i].command, data)
await targets[service.setup[type][i].target]({ command })
}
}
}