-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploymentRunner.js
169 lines (146 loc) · 3.66 KB
/
deploymentRunner.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
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
const { spawn } = require('child_process')
const process = require('process')
const JobList = require('./fixedList')
require('log-timestamp')
const jobs = new JobList(5)
let isJobRunning = false
function start (deploy) {
if (isJobRunning) {
return false
}
isJobRunning = true
launch(deploy)
return true
}
async function launch (deploy) {
const job = {
id: deploy.id,
status: 'Starting',
stdout: '',
stderr: ''
}
jobs.add(job)
try {
// Clean up after any previous job
job.status = `Cleaning up previous job...`
const args = ['-rf', 'plasma-contracts']
await runJob(job, () => runCommand('rm', args, deploy.cwd))
} catch (err) {
isJobRunning = false
job.status = `Error cleaning up: ${err.code} ${err.error}`
return
}
try {
// Clone the plasma-contracts repo
job.status = `Cloning git repo...`
await runJob(job, () => cloneRepo(deploy.tag, deploy.cwd))
} catch (err) {
isJobRunning = false
job.status = `Error cloning github repo: ${err.code} ${err.error}`
return
}
try {
// Clone the plasma-contracts repo
job.status = `Running npm install...`
await runJob(job, () => runCommand('npm', ['i'], deploy.cwd + '/plasma-contracts'))
} catch (err) {
isJobRunning = false
job.status = `Error running npm install: ${err.code} ${err.error}`
return
}
try {
// truffle migrate
job.status = `Running truffle...`
await runJob(job, () => truffleMigrate(deploy.args, deploy.cwd + '/plasma-contracts', deploy.env))
job.status = `Exited`
} catch (err) {
job.status = `Error running truffle: ${err.code} ${err.error}`
} finally {
isJobRunning = false
}
}
async function runJob (job, fn) {
const result = await fn()
job.code = result.code
job.stdout += result.stdout
job.stderr += result.stderr
if (result.result) {
job.result = result.result
}
if (job.code !== 0) {
throw result
}
}
function cloneRepo (tag, cwd) {
const args = ['clone', 'https://github.com/omisego/plasma-contracts.git', '--depth', '1']
if (tag) {
args.push('--branch')
args.push(tag)
}
return runCommand('git', args, cwd)
}
function truffleMigrate (args, cwd, env) {
return runCommand('npx', ['truffle', ...args], cwd, env)
}
function runCommand (command, args, cwd, env) {
console.log(`${command} ${args}`)
if (env) {
env = Object.assign(process.env, env)
}
const childProc = spawn(command, args, { cwd, env })
const result = {
stdout: '',
stderr: ''
}
return new Promise((resolve, reject) => {
childProc.stdout.on('data', (data) => {
result.stdout += data
if (data.includes('authority_addr')) {
try {
result.result = JSON.parse(data)
} catch (err) {
console.log(`Error parsing result ${data}: ${err}`)
}
}
console.log(`stdout: ${data}`)
})
childProc.stderr.on('data', (data) => {
result.stderr += data
console.error(`stderr: ${data}`)
})
childProc.on('exit', (code) => {
result.code = code
console.log(`Exited: ${code}`)
resolve(result)
})
childProc.on('error', (err) => {
console.error(err)
result.error = err
reject(result)
})
})
}
function find (id) {
const job = jobs.find(id)
if (!job) {
throw new Error(`No job with id ${id}`)
}
return job
}
function status (id) {
const job = find(id)
return job.status
}
function success (id) {
const job = find(id)
return job.code === 0
}
function output (id) {
const job = find(id)
return {
stdout: job.stdout,
stderr: job.stderr,
result: job.result
}
}
module.exports = { start, find, status, success, output }