-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
223 lines (184 loc) · 7.39 KB
/
index.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
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
const core = require('@actions/core');
const { exec } = require('@actions/exec');
const io = require('@actions/io');
const cache = require('@actions/cache');
const fs = require('fs');
const CACHE_LOCK_PATH = '/hab/cache/artifacts/.cached';
const RESTORE_LOCK_PATH = '/hab/cache/artifacts/.restored';
// gather input
const deps = (core.getInput('deps') || '').split(/\s+/).filter(pkg => Boolean(pkg));
const supervisor = core.getInput('supervisor') == 'true'
? true
: (
!core.getInput('supervisor')
? false
: core.getInput('supervisor').trim().split(/\s*\n\s*/).filter(svc => Boolean(svc))
);
const cacheKey = core.getInput('cache-key') || `hab-artifacts-cache:${process.env.GITHUB_WORKFLOW}`;
// run with error wrapper
try {
module.exports = run();
} catch(err) {
core.setFailed(err.message);
}
async function run() {
core.exportVariable('HAB_NONINTERACTIVE', 'true');
core.exportVariable('STUDIO_TYPE', 'default');
const habEnv = {
HAB_NONINTERACTIVE: 'true', // not effective for hab svc load output pending https://github.com/habitat-sh/habitat/issues/6260
...process.env
};
if (await io.which('hab')) {
core.info('Chef Habitat already installed!');
} else {
// install hab binary and bootstrap /hab environment
try {
core.startGroup('Installing Chef Habitat');
await exec('wget https://raw.githubusercontent.com/habitat-sh/habitat/master/components/hab/install.sh -O /tmp/hab-install.sh');
await exec('sudo bash /tmp/hab-install.sh');
await io.rmRF('/tmp/hab-install.sh');
} catch (err) {
core.setFailed(`Failed to install Chef Habitat: ${err.message}`);
return;
} finally {
core.endGroup();
}
// create hab user and group
try {
core.startGroup('Creating hab user');
await exec('sudo groupadd hab');
await exec('sudo useradd -g hab -G docker hab');
await io.rmRF('/tmp/hab-install.sh');
} catch (err) {
core.setFailed(`Failed to create hab user: ${err.message}`);
return;
} finally {
core.endGroup();
}
// verify installation (and initialize license)
try {
await exec('hab --version');
} catch (err) {
core.setFailed(`Failed to verify hab installation: ${err.message}`);
return;
}
// link user cache directory to global
try {
core.startGroup('Linking ~/.hab/cache to /hab/cache');
await exec(`mkdir -p "${process.env.HOME}/.hab"`);
await exec(`ln -sf /hab/cache "${process.env.HOME}/.hab/"`);
} catch (err) {
core.setFailed(`Failed to link ~/.hab/cache: ${err.message}`);
return;
} finally {
core.endGroup();
}
}
// ensure /hab/cache exists and has correct ownership/permissions before cache is restored
try {
core.startGroup('Updating cache ownership');
await exec(`sudo mkdir -p /hab/cache`);
await exec(`sudo chown -R runner:docker /hab/cache`);
await exec(`sudo chmod g+s /hab/cache`);
} catch (err) {
core.setFailed(`Failed to update cache ownership: ${err.message}`);
return;
} finally {
core.endGroup();
}
// restore cache
if (fs.existsSync(RESTORE_LOCK_PATH)) {
core.info(`Skipping restoring, ${RESTORE_LOCK_PATH} already exists`);
} else {
try {
core.startGroup(`Restoring package cache`);
core.info(`Initializing runner-writable /hab/cache`);
await exec(`mkdir -p /hab/cache/artifacts`);
core.info(`Writing restore lock: ${RESTORE_LOCK_PATH}`);
fs.writeFileSync(RESTORE_LOCK_PATH, '');
console.info(`Calling restoreCache: ${cacheKey}`);
const restoredCache = await cache.restoreCache(['/hab/cache/artifacts'], cacheKey);
core.info(restoredCache ? `Restored cache ${restoredCache}` : 'No cache restored');
core.info(`Re-writing restore lock: ${RESTORE_LOCK_PATH}`);
fs.writeFileSync(RESTORE_LOCK_PATH, '');
// .cached file is written at beginning of caching, and removed after restore to
// guard against multiple post scripts trying to save the same cache
if (fs.existsSync(CACHE_LOCK_PATH)) {
core.info(`Erasing cache lock: ${CACHE_LOCK_PATH}`);
await exec(`rm -v "${CACHE_LOCK_PATH}"`);
}
} catch (err) {
core.setFailed(`Failed to restore package cache: ${err.message}`);
return;
} finally {
core.endGroup();
}
}
// install deps
if (deps.length) {
try {
core.startGroup(`Installing deps: ${deps.join(' ')}`);
await exec('sudo --preserve-env hab pkg install', deps, { env: habEnv });
} catch (err) {
core.setFailed(`Failed to install deps: ${err.message}`);
return;
} finally {
core.endGroup();
}
}
// start supervisor
if (supervisor) {
try {
core.startGroup('Starting supervisor');
await exec(`sudo mkdir -p /hab/sup/default`);
await exec('sudo --preserve-env setsid bash', ['-c', 'hab sup run > /hab/sup/default/sup.log 2>&1 &'], { env: habEnv });
core.info('Waiting for supervisor secret...');
await exec('bash', ['-c', 'until test -f /hab/sup/default/CTL_SECRET; do echo -n "."; sleep .1; done; echo']);
core.info('Enabling sudoless access to supervisor API...');
await exec('sudo chgrp docker /hab/sup/default/CTL_SECRET');
await exec('sudo chmod g+r /hab/sup/default/CTL_SECRET');
core.info('Waiting for supervisor...');
await exec('bash', ['-c', 'until hab svc status; do echo -n "."; sleep .1; done; echo']);
} catch (err) {
core.setFailed(`Failed to start supervisor: ${err.message}`);
return;
} finally {
core.endGroup();
}
if (Array.isArray(supervisor)) {
for (const svc of supervisor) {
try {
core.startGroup(`Loading service: ${svc}`);
await exec(`hab svc load ${svc}`, [], { env: habEnv });
} catch (err) {
core.setFailed(`Failed to load service: ${err.message}`);
return;
} finally {
core.endGroup();
}
}
}
}
// ensure /hab/cache exists and has correct ownership/permissions before cache is restored
try {
core.startGroup('Enabling sudoless package installation');
await exec(`sudo chown -R runner:docker /hab/pkgs`);
await exec(`find /hab/pkgs -maxdepth 3 -type d -exec sudo chmod g+ws {} \;`);
} catch (err) {
core.setFailed(`Failed to enable sudoless package installation: ${err.message}`);
return;
} finally {
core.endGroup();
}
}
async function execOutput(commandLine, args = [], options = {}) {
let stdout = '';
await exec(commandLine, args, {
...options,
listeners: {
...options.listeners,
stdout: buffer => stdout += buffer
}
});
return stdout.trim();
}