-
Notifications
You must be signed in to change notification settings - Fork 35
/
.studiorc
executable file
·408 lines (351 loc) · 10.8 KB
/
.studiorc
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
#!/bin/bash
RED='\033[0;31m'
NO_COLOR='\033[0m'
set -uo pipefail
export APP_HOSTNAME
export OAUTH_PROVIDER
export OAUTH_USERINFO_URL
export OAUTH_AUTHORIZE_URL
export OAUTH_TOKEN_URL
export OAUTH_CLIENT_ID
export OAUTH_CLIENT_SECRET
export OAUTH_REDIRECT_URL
export GITHUB_API_URL
export GITHUB_APP_ID
export GITHUB_APP_URL
export SSL_CERT_FILE
export DEV_MODE
export HAB_STATS_ADDR
# Wrap a function with this one to ensure that it stops executing if any of its
# commands return nonzero. If so, a highly-visible message is printed which
# should make the error condition more apparent.
stop-on-failure() {
(set -e; "$@");
rv=$?
if [ $rv -ne 0 ]; then
echo -e "${RED}ERROR: $* aborted due to error${NO_COLOR}"
return 1
fi
}
no_install_deps() {
local file="/src/components/${1}/cleanup"
if [ ! -f "$file" ]; then
touch "$file"
else
echo "1"
fi
}
_init-datastore() {
mkdir -p /hab/svc/builder-datastore
cp -f /src/support/builder/datastore.toml /hab/svc/builder-datastore/user.toml
}
init-datastore() { stop-on-failure _init-datastore; }
load_env_config() {
local env_config
env_config="/src/.secrets/habitat-env"
if [[ -f "${env_config}" ]]; then
source "${env_config}"
else
echo -e "${RED}.secrets/habitat-env is required. Please see DEVELOPING.md for getting started instructions.${NO_COLOR}"
exit 1
fi
}
configure() {
load_env_config
/src/support/builder/config.sh
}
install-packages() {
hab pkg install \
core/cacerts \
core/net-tools \
core/procps-ng \
core/shadow \
core/curl \
core/aws-cli \
core/sccache \
-b -c LTS-2024
# workaround for https://github.com/habitat-sh/habitat/issues/6418
hab pkg binlink core/cacerts
hab pkg binlink core/net-tools
hab pkg binlink core/procps-ng
hab pkg binlink core/shadow
hab pkg binlink core/curl
hab pkg binlink core/aws-cli
}
_build-builder-component() {
local component="$1"
stop-builder "$component"
NO_INSTALL_DEPS=$(no_install_deps "builder-$component") \
build "/src/components/builder-$component/habitat-dev"
echo "builder-$component build succeeded"
start-builder "$component"
case "${component}" in
api | worker)
generate_bldr_keys && upload_github_keys;;
*)
;;
esac
}
build-builder-component() { stop-on-failure _build-builder-component "$@"; }
alias bb=build-builder
_build-builder() {
if ! hab sup status > /dev/null 2>&1; then
echo "Starting supervisor"
sup-run
sleep 5
fi
if [[ $(hab sup status) == "No services loaded." ]]; then
start-builder
fi
if [[ "$#" -eq 0 ]]; then
build-builder api jobsrv worker
return $?
fi
for component in "$@"; do
build-builder-component "$component"
done
}
build-builder() { stop-on-failure _build-builder "$@"; }
upload_github_keys() {
if [[ -f "/src/.secrets/builder-github-app.pem" ]]; then
for svc in worker api; do
hab file upload "builder-${svc}.default" "$(date +%s)" "/src/.secrets/builder-github-app.pem"
done
else
echo "Missing GitHub key file. Please follow instructions here: https://github.com/habitat-sh/builder/blob/master/DEVELOPING.md"
sleep 10
fi
}
command-exists() {
command -V "$1" > /dev/null 2>&1
}
_start-builder() {
if [[ "$#" -eq 0 ]]; then
start-builder datastore
configure
start-builder cache
start-builder memcached
start-builder api
start-builder api-proxy
start-builder jobsrv
start-builder worker
start-builder minio
# Provide enough time for minio's reconfigure hook to create the bucket.
# Otherwise, the configure operation will error and abend this function.
sleep 8
configure-minio
upload_github_keys
generate_bldr_keys
echo "Builder Started: Navigate to http://localhost/#/pkgs/core to access the web UI."
echo "Minio login available at http://localhost:9000"
echo "Username: depot"
echo "Password: password"
echo "AWS-CLI ENVVARS have been set with these values"
return $?
fi
for component in "$@"; do
if [ -v "svc_params[$component]" ]; then
# shellcheck disable=SC2086
load-if-not-loaded habitat/builder-$component ${svc_params[$component]}
elif command-exists "start-$component"; then
"start-$component"
else
echo -e "${RED}ERROR: Unknown builder service: $component${NO_COLOR}"
return 1
fi
done
}
start-builder() { stop-on-failure _start-builder "$@"; }
_load-if-not-loaded() {
local pkg_ident
pkg_ident=$1
if hab sup status "$pkg_ident" > /dev/null 2>&1; then
echo "$pkg_ident is already loaded"
else
hab svc load "$@"
fi
}
load-if-not-loaded() { stop-on-failure _load-if-not-loaded "$@"; }
start-datastore() {
echo "Starting datastore"
if hab sup status habitat/builder-datastore > /dev/null 2>&1; then
echo "habitat/builder-datastore is already loaded"
else
init-datastore
hab svc load habitat/builder-datastore
fi
}
configure-minio() {
export AWS_ACCESS_KEY_ID="depot"
export AWS_SECRET_ACCESS_KEY="password"
if aws --endpoint-url http://localhost:9000 s3api list-buckets | grep "habitat-builder-artifact-store.default" > /dev/null; then
echo "Minio already configured"
else
echo "Creating bucket in Minio"
aws --endpoint-url http://localhost:9000 s3api create-bucket --bucket "habitat-builder-artifact-store.default"
fi
}
start-cache() {
load-if-not-loaded core/sccache
}
declare -A svc_params=(
[api]=" -s at-once --bind memcached:builder-memcached.default --bind jobsrv:builder-jobsrv.default"
[api-proxy]=" --bind http:builder-api.default"
[jobsrv]=" -s at-once"
[worker]=" -s at-once --bind jobsrv:builder-jobsrv.default --bind depot:builder-api-proxy.default"
[minio]=" -s at-once"
[memcached]=" -s at-once"
)
_stop-builder() {
if [[ "$#" -eq 0 ]]; then
stop-builder cache api api-proxy datastore jobsrv worker minio memcached
return $?
fi
for component in "$@"; do
if [ $component == cache ]; then
hab svc unload core/sccache
else
echo "Unloading builder-$component"
hab svc unload habitat/builder-$component
fi
done
}
stop-builder() { stop-on-failure _stop-builder "$@"; }
generate_bldr_keys() {
mapfile -t keys < <(find /hab/cache/keys -name "bldr-*.pub")
if [ "${#keys[@]}" -gt 0 ]; then
KEY_NAME=$(echo "${keys[0]}" | grep -Po "bldr-\\d+")
echo "Re-using existing builder key: $KEY_NAME"
else
KEY_NAME=$(hab user key generate bldr | grep -Po "bldr-\\d+")
echo "Generated new builder key: $KEY_NAME"
fi
for svc in api jobsrv worker; do
hab file upload "builder-${svc}.default" "$(date +%s)" "/hab/cache/keys/${KEY_NAME}.pub"
hab file upload "builder-${svc}.default" "$(date +%s)" "/hab/cache/keys/${KEY_NAME}.box.key"
done
}
load_package() {
hab pkg upload --url http://localhost --auth "${HAB_AUTH_TOKEN}" "$@" --channel stable
}
load_packages() {
if [[ -d /src/pkgs ]]; then
for pkg in /src/pkgs/core*.hart ; do
load_package "${pkg}"
done
fi
}
origin() {
local origin=${1:-core}
if curl --fail localhost/v1/depot/origins \
-d "{\"name\":\"${origin}\"}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${HAB_AUTH_TOKEN}";
then
hab origin key generate "${origin}"
hab origin key upload --url http://localhost -z "${HAB_AUTH_TOKEN}" -s "${origin}"
else
echo "Failed to create origin ${origin}"
fi
}
status() {
hab svc status
}
test-builder() {
local preserve_flag logs
logs="/hab/sup/default/debug.log"
if [[ "$#" -gt 0 ]]; then
case "${1}" in
suplogs)
if [[ -f "${logs}" ]]; then
echo "--- TEST-BUILDER LOGS [${logs}] START"
cat "${logs}"
echo "--- TEST-BUILDER LOGS [${logs}] END"
else
echo "No logs. Run \`test-builder\` first."
fi
return;;
preserve)
echo "Setting flag to preserve data following tests."
preserve_flag="-p";;
esac
fi
if hab sup status > /dev/null 2>&1; then
echo "Stopping supervisor"
hab sup term
sleep 8
fi
echo "Starting supervisor in test mode. Logs saved to ${logs}"
HAB_FUNC_TEST=1 RUST_LOG=debug HAB_NONINTERACTIVE=true HAB_NOCOLORING=true hab sup run --no-color > ${logs} 2>&1 &
sleep 8
start-builder
/src/test/builder-api/test.sh "${preserve_flag:-}"
if [[ "${preserve_flag:-}" == "-p" ]]; then
echo "Data has been preserved and supervisor remains running"
echo "To see supervisor logs from the API tests, run: test-builder suplogs"
echo "To inspect Postgres data, run: psql -h 127.0.0.1 builder -U hab -p 5433"
else
echo "Stopping supervisor"
hab sup term
fi
}
function psql() {
local config_dir port
config_dir="/hab/svc/builder-datastore/config"
port=$(grep port $config_dir/postgresql.conf | grep -oE '[[:digit:]]+')
PGPASSWORD=$(cat $config_dir/pwfile) hab pkg exec core/postgresql psql -U hab -h 127.0.0.1 -p "$port" "$@"
}
export -f psql
# Cleanup any development touch files we laid down when checking to see if we could run NO_INSTALL_DEPS
local_cleanup() {
for svc in api api-proxy datastore jobsrv worker minio; do
if [ -f "/src/components/builder-${svc}/cleanup" ]; then
rm "/src/components/builder-${svc}/cleanup"
fi
done
}
alias help=dev_docs
welcome() {
cat <<'WMSG'
Welcome to...
____ _ _ _
| __ ) _ _(_) | __| | ___ _ __
| _ \| | | | | |/ _ |/ _ \ __|
| |_) | |_| | | | (_| | __/ |
|____/ \__,_|_|_|\__,_|\___|_|
Type 'help' for commands
WMSG
}
dev_docs() {
cat <<DOCS
The following commands are available:
build-builder (alias: bb) - Build all services
build-builder [SERVICE] - Build specific service
Valid: api jobsrv worker
Experimental: api-proxy
start-builder - Start all services
start-builder [SERVICE] - Start specific service
Valid: api api-proxy cache datastore
jobsrv worker minio
stop-builder - Stop all services
stop-builder [SERVICE] - Stop specific service
Valid: api api-proxy cache datastore
jobsrv worker minio
psql - Open psql shell to builder DB
origin - Create core origin
origin [ORIGIN] - Create specified origin
status - Run hab svc status
test-builder - Run end-to-end API tests
test-builder suplogs - Display supervisor debug logs from API tests
test-builder preserve - Run end-to-end API tests,
preserving test data in Postgres
sl - Tail the supervisor log
help - Print this list
DOCS
}
install-packages
# Forces the worker to use a bound docker socket
DEV_MODE=true
load_env_config
trap local_cleanup EXIT
welcome