-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (106 loc) · 3.17 KB
/
index.js
File metadata and controls
126 lines (106 loc) · 3.17 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
const slab = require('./slab')
const config = require('./config')
const core = require('@actions/core')
const { waitForRunnerRegistered } = require('./gh')
function setOutput(label) {
core.setOutput('label', label)
}
let runnerName
async function cleanup() {
if (runnerName) {
core.info('Stop instance after cancellation')
await slab.stopInstanceRequest(runnerName)
}
}
process.on('SIGINT', async function () {
await cleanup()
process.exit()
})
async function start() {
const provider = config.input.backend
let startInstanceResponse
let waitGithubResponse
for (let i = 1; i <= 3; i++) {
try {
startInstanceResponse = await slab.startInstanceRequest()
waitGithubResponse = await slab.waitForGithub(
startInstanceResponse.task_id,
'configuration_fetching'
)
break
} catch (error) {
core.info('Retrying request now...')
}
if (i === 3) {
core.setFailed(
`${provider} instance start request has failed after 3 attempts (reason: configuration fetching has failed)`
)
return
}
}
runnerName = waitGithubResponse.configuration_fetching.runner_name
setOutput(runnerName)
core.info(
`${provider} instance details: ${waitGithubResponse.configuration_fetching.details}`
)
try {
const waitInstanceResponse = await slab.waitForInstance(
startInstanceResponse.task_id,
'start'
)
const instanceId = waitInstanceResponse.start.instance_id
core.info(`${provider} instance started with ID: ${instanceId}`)
await waitForRunnerRegistered(runnerName)
} catch (error) {
core.info(`Clean up after error, stop ${provider} instance`)
await slab.stopInstanceRequest(runnerName)
core.setFailed(`${provider} instance start has failed`)
}
}
async function stop() {
let stopInstanceResponse
for (let i = 1; i <= 3; i++) {
try {
stopInstanceResponse = await slab.stopInstanceRequest(config.input.label)
break
} catch (error) {
core.info('Retrying request now...')
}
if (i === 3) {
core.setFailed('Instance stop request has failed after 3 attempts')
return
}
}
try {
const waitGithubResponse = await slab.waitForGithub(
stopInstanceResponse.task_id,
'runner_unregister'
)
const taskStatus = waitGithubResponse.runner_unregister.status.toLowerCase()
if (taskStatus === 'done') {
core.info(
`Runner ${config.input.label} unregistered from GitHub successfully`
)
}
} catch (error) {
// Unregistration failure is not critical, so we just log it and continue.
core.warning('An error occurred while unregistering runner, check job logs')
}
try {
await slab.waitForInstance(stopInstanceResponse.task_id, 'stop')
core.info('Instance successfully stopped')
} catch (error) {
// Unregistration failure is not critical, so we just log it and continue.
core.setFailed(
'An error occurred while stopping instance, check for zombie instance in backend provider console.'
)
}
}
async function run() {
try {
config.input.mode === 'start' ? await start() : await stop()
} catch (error) {
core.setFailed(error.message)
}
}
run()