forked from nextstrain/nextclade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-dev
executable file
·502 lines (463 loc) · 12 KB
/
docker-dev
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env bash
# Runs various day-to-day dev commands inside dev docker container.
#
# The container includes all the tools required for maintaining the project. But this container and script exist
# for convenience only and are not required for operations. The usual `cargo` and `yarn` workflows can still be used
# (see dev guide).
#
# See the full list of possible commands in the long `case` block halfway through the script.
# Here are some of the most useful ones:
#
# * Build nextclade binary in debug mode:
# ./docker-dev b nextclade
#
# * Build nextclade binary in release mode:
# ./docker-dev br nextclade
#
# * Build and run nextclade binary in debug mode, with arguments:
# ./docker-dev r nextclade -- dataset list --help
#
# * Build and run nextclade binary in release mode, with arguments:
# ./docker-dev rr nextclade -- dataset list --help
#
# * Cross-compile nextclade binary for Windows in release mode:
# CROSS=x86_64-pc-windows-gnu ./docker-dev br
#
# * Build WebAssembly module in debug mode:
# ./docker-dev W
#
# * Build WebAssembly module in release mode:
# ./docker-dev Wr
#
# * Build and run web application in debug mode:
# ./docker-dev a
#
# * Build and run web application in release mode:
# ./docker-dev war
#
# * Lint Rust code:
# ./docker-dev l
#
# * Lint Rust code, apply automatic fixes:
# ./docker-dev lf
#
# * Format Rust code:
# ./docker-dev f
#
# * Shell into the container:
# ./docker-dev bash
#
# * Run arbitrary command inside the container:
# ./docker-dev cargo --version
#
# * Run arbitrary yarn command, in the context of web app, inside the container:
# ./docker-dev a yarn add --dev react
#
set -euo pipefail
function abspath() {
readlink -m "$1"
}
export THIS_DIR=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
pwd
)
SUCCESS="({ set +x; } 2> /dev/null && echo '🟩 Success' && exit 0)"
FAILURE="({ set +x; } 2> /dev/null && echo '🟥 Failure' && exit 1)"
CANCELLED="({ set +x; } 2> /dev/null && echo '🟦 Cancelled' && exit 0)"
SUCCESS_OR_FAILURE="&& ${SUCCESS} || ${FAILURE}"
HANDLE_SIGINT="trap \"${CANCELLED}; exit 0\" INT"
export PACKAGE_NAME="nextclade"
export PACKAGE_DIR_REL=""
export PROJECT_ROOT_DIR="$(abspath "${THIS_DIR}")"
export PACKAGE_DIR="$(abspath "${PROJECT_ROOT_DIR}/${PACKAGE_DIR_REL}")"
export DOCKER_REPO="nextstrain/nextclade_builder"
export DOCKER_IMAGE_NAME_SAFE="${DOCKER_REPO//\//-}"
export DOCKER_CONTAINER_NAME="${DOCKER_IMAGE_NAME_SAFE}-$(date +%s)"
export USER=user
export GROUP=user
export BUILD_DIR_REL=".build/docker${CROSS:+-$CROSS}/${PACKAGE_DIR_REL}"
export BUILD_DIR="$(abspath "${PACKAGE_DIR}/${BUILD_DIR_REL}")"
export BUILD_DIR_TEST="${BUILD_DIR}/test"
export CACHE_DIR_REL=".cache/docker/${PACKAGE_DIR_REL}"
export CACHE_DIR="$(abspath "${PACKAGE_DIR}/${CACHE_DIR_REL}")"
export NICE="nice -14 ionice -c2 -n3"
export TIME="/usr/bin/time --quiet -f \"Cmd : %C\nTime: %E\nMem : %M KB\n\""
export RUST_BACKTRACE="full"
export COLORBT_SHOW_HIDDEN="0"
export RUST_LOG="warn"
export CARGO_BUILD_TARGET_DIR="${BUILD_DIR_REL}"
. "${PROJECT_ROOT_DIR}/.env.example"
if [ -f "${PROJECT_ROOT_DIR}/.env" ]; then
. "${PROJECT_ROOT_DIR}/.env"
fi
. "${THIS_DIR}/scripts/is_ci.sh"
export EXTENSIONS_TO_WATCH="rs,toml,lock"
export FILES_TO_WATCH="\
-w 'Cargo.lock' \
-w 'Cargo.toml' \
-w 'clippy.toml' \
-w 'packages_rs/nextclade-web/src' \
-w 'packages_rs/nextclade-web/Cargo.toml' \
-w 'packages_rs/nextclade/src' \
-w 'packages_rs/nextclade/Cargo.toml' \
-w 'rust-toolchain.toml' \
-w 'rustfmt.toml' \
"
export FILES_TO_IGNORE="\
-i '.build' \
-i '.cache' \
-i '.vscode' \
-i 'target/' \
-i 'packages_rs/nextclade-web/src/gen/' \
"
export LINT_FIX="--fix --allow-staged"
FORCE_DOCKER_IMAGE_BUILD=${FORCE_DOCKER_IMAGE_BUILD:-}
DOCKER_IMAGE_PUSH=0
BUILD=0
RUN=0
WASM=0
WEB=0
RELEASE=
WATCH=0
TEST=0
TEST_FLAGS=
BENCH=0
EXAMPLE=0
LINT=0
FORMAT=0
FIX=
case "${1:-}" in
"docker-image-build" | "d")
shift
FORCE_DOCKER_IMAGE_BUILD=1
;;
"docker-image-build-push" | "dp")
shift
FORCE_DOCKER_IMAGE_BUILD=1
DOCKER_IMAGE_PUSH=1
;;
"build" | "b")
shift
BUILD=1
;;
"build-release" | "br")
shift
BUILD=1
RELEASE="--release"
;;
"run" | "r")
shift
RUN=1
;;
"run-release" | "rr")
shift
RUN=1
RELEASE="--release"
;;
"watch-run" | "wr")
shift
RUN=1
WATCH=1
;;
"watch-run-release" | "wrr")
shift
RUN=1
RELEASE="--release"
WATCH=1
;;
"wasm" | "W")
shift
WASM=1
RELEASE="dev"
;;
"wasm-release" | "Wr")
shift
WASM=1
RELEASE="prod"
;;
"watch-wasm" | "wW")
shift
WASM=1
RELEASE="dev"
WATCH=1
;;
"watch-wasm-release" | "wWr")
shift
WASM=1
RELEASE="prod"
WATCH=1
;;
"web" | "a")
shift
WEB=1
RELEASE="dev"
WATCH=0
;;
"web-release" | "ar")
shift
WEB=1
RELEASE="prod:build"
WATCH=0
;;
"watch-web-release" | "war")
shift
WEB=1
RELEASE="prod:watch"
WATCH=0
;;
"test" | "t")
shift
TEST=1
WATCH=0
;;
"watch-test" | "wt")
shift
TEST=1
WATCH=1
;;
"test-unit" | "tu")
shift
TEST=1
TEST_FLAGS="--lib"
WATCH=0
;;
"watch-test-unit" | "wtu")
shift
TEST=1
TEST_FLAGS="--lib"
WATCH=1
;;
"test-integration" | "ti")
shift
TEST=1
TEST_FLAGS="--test='*'"
WATCH=0
;;
"watch-test-integration" | "wti")
shift
TEST=1
TEST_FLAGS="--test='*'"
WATCH=1
;;
"bench" | "B")
shift
BENCH=1
WATCH=0
;;
"watch-bench" | "wB")
shift
BENCH=1
WATCH=1
;;
"example" | "E")
shift
EXAMPLE=1
;;
"example-release" | "Er")
shift
EXAMPLE=1
RELEASE="--release"
;;
"lint" | "l")
shift
LINT=1
WATCH=0
FIX=
;;
"watch-lint" | "wl")
shift
LINT=1
WATCH=1
FIX=
;;
"lint-fix" | "lf")
shift
LINT=1
WATCH=0
FIX="${LINT_FIX}"
;;
"watch-lint-fix" | "wlf")
shift
LINT=1
WATCH=1
FIX="${LINT_FIX}"
;;
"format" | "f")
shift
FORMAT=1
WATCH=0
FIX=
;;
"format-check" | "fc")
shift
FORMAT=1
WATCH=1
FIX="--check"
;;
"exec" | "e")
shift
RUN=0
;;
esac
if [ "${WASM}" == "1" ]; then
CROSS="wasm32-unknown-unknown"
fi
DOCKER_BASE_IMAGE="${DOCKER_BASE_IMAGE:=ubuntu:20.04}"
CLANG_VERSION="${CLANG_VERSION:=13}"
DOCKER_TARGET="dev"
RUST_TARGET=""
if [ -n "${CROSS:-}" ]; then
if [[ "${CROSS}" == *linux-gnu ]]; then
if [[ "${CROSS}" == x86_64-* ]]; then
DOCKER_BASE_IMAGE="debian:7.11"
CLANG_VERSION="3.5"
else
DOCKER_BASE_IMAGE="debian:9.13"
CLANG_VERSION="13"
fi
fi
DOCKER_TARGET="cross-${CROSS}"
RUST_TARGET="--target=${CROSS}"
fi
DOCKER_IMAGE_VERSION="$(./scripts/docker_build_checksum.sh)"
if ! docker inspect --format '{{.Id}}' "${DOCKER_REPO}:${DOCKER_TARGET}-${DOCKER_IMAGE_VERSION}" &>/dev/null ||
[ -n "${FORCE_DOCKER_IMAGE_BUILD}" ]; then
export DOCKER_BUILDKIT=1
export BUILDKIT_PROGRESS=plain
export PROGRESS_NO_TRUNC=1
ADDITIONAL_DOCKER_BUILD_ARGS=
if [[ "${DOCKER_TARGET}" == *apple* ]]; then
# shellcheck disable=SC2089
ADDITIONAL_DOCKER_BUILD_ARGS="--build-arg OSXCROSS_URL=${OSXCROSS_URL}"
fi
${NICE} docker build -q \
--file="docker/docker-dev.dockerfile" \
--target="${DOCKER_TARGET}" \
--tag="${DOCKER_REPO}:${DOCKER_TARGET}" \
--tag="${DOCKER_REPO}:${DOCKER_TARGET}-${DOCKER_IMAGE_VERSION}" \
--cache-from="${DOCKER_REPO}:dev" \
--cache-from="${DOCKER_REPO}:dev-${DOCKER_IMAGE_VERSION}" \
--cache-from="${DOCKER_REPO}:${DOCKER_TARGET}" \
--cache-from="${DOCKER_REPO}:${DOCKER_TARGET}-${DOCKER_IMAGE_VERSION}" \
--network=host \
--build-arg="DOCKER_BASE_IMAGE=${DOCKER_BASE_IMAGE}" \
--build-arg="CLANG_VERSION=${CLANG_VERSION}" \
--build-arg="UID=$(id -u)" \
--build-arg="GID=$(id -g)" \
--build-arg="USER=user" \
--build-arg="GROUP=user" \
${ADDITIONAL_DOCKER_BUILD_ARGS} \
. \
>/dev/null
if [ "${DOCKER_IMAGE_PUSH}" == "1" ]; then
docker push "${DOCKER_REPO}:${DOCKER_TARGET}"
docker push "${DOCKER_REPO}:${DOCKER_TARGET}-${DOCKER_IMAGE_VERSION}"
fi
if [ -n "${FORCE_DOCKER_IMAGE_BUILD}" ]; then
exit 0;
fi
fi
export RUSTC_FORCE_INCREMENTAL=""
if [ -n "${RELEASE}" ]; then
export RUSTC_FORCE_INCREMENTAL=1
fi
PARAMS=""
COMMAND=${*:-}
PORTS=
if [ "${RUN}" == "1" ]; then
PARAMS="$(echo "${@:-}" | xargs)"
if [ -n "${PARAMS}" ]; then
PARAMS="--bin=${PARAMS}"
fi
if [ "$(is_ci)" == "1" ]; then
PARAMS=" --locked ${PARAMS}"
fi
COMMAND="cargo run -q --target-dir='${BUILD_DIR_REL}' ${RUST_TARGET} ${RELEASE} ${PARAMS}"
elif [ "${EXAMPLE}" == "1" ]; then
PARAMS="$(echo "${@:-}" | xargs)"
if [ -n "${PARAMS}" ]; then
PARAMS="--example=${PARAMS}"
fi
if [ "$(is_ci)" == "1" ]; then
PARAMS=" --locked ${PARAMS}"
fi
COMMAND="cargo run -q --package=nextclade-cli --target-dir='${BUILD_DIR_REL}' ${RUST_TARGET} ${RELEASE} ${PARAMS}"
elif [ "${BUILD}" == "1" ]; then
PARAMS="$(echo "${@:-}" | xargs)"
if [ -n "${PARAMS}" ]; then
PARAMS="--bin=${PARAMS}"
fi
if [ "$(is_ci)" == "1" ]; then
PARAMS=" --locked ${PARAMS}"
fi
COMMAND="cargo build -q --package=nextclade-cli --target-dir='${BUILD_DIR_REL}' ${RUST_TARGET} ${RELEASE} ${PARAMS}"
elif [ "${WASM}" == "1" ]; then
COMMAND="bash -c \"set -euo pipefail && cd packages_rs/nextclade-web && yarn wasm-${RELEASE}\""
elif [ "${WEB}" == "1" ]; then
PORTS="-p 3000:3000"
COMMAND=${COMMAND:=yarn install && yarn ${RELEASE}}
COMMAND="bash -c \"set -euo pipefail && cd packages_rs/nextclade-web && ${COMMAND}\""
elif [ "${TEST}" == "1" ]; then
PRETTY_TEST="( grep --color=always --line-buffered -vP \"running.*tests|\x1b\[32m.\x1b\(B\x1b\[m|^$|test result|TEST START\" || true )"
COMMAND="cargo test --color=always -q --target-dir='${BUILD_DIR_REL}' ${RUST_TARGET} ${TEST_FLAGS} ${*:-} -- --nocapture --color=always | ${PRETTY_TEST} ${SUCCESS_OR_FAILURE}"
elif [ "${BENCH}" == "1" ]; then
PRETTY_BENCH="( grep --color=always --line-buffered -vP \"Benchmarking|Gnuplot not found, using plotters backend\" || true )"
COMMAND="cargo bench --color=always -q --target-dir='${BUILD_DIR_REL}' ${RUST_TARGET} ${*:-} | ${PRETTY_BENCH} ${SUCCESS_OR_FAILURE}"
elif [ "${LINT}" == "1" ]; then
COMMAND="cargo clippy --exclude=3rdparty -q --target-dir=\"${BUILD_DIR_REL}\" --all ${FIX:-}"
elif [ "${FORMAT}" == "1" ]; then
COMMAND="cargo fmt -q --all ${FIX:-}"
else
COMMAND="${NICE} ${TIME} ${COMMAND}"
fi
if [ "${WATCH}" == "1" ]; then
COMMAND="${NICE} watchexec \
--restart \
--shell=bash \
--debounce=10 \
--no-meta \
--no-environment \
--exts=${EXTENSIONS_TO_WATCH} \
${FILES_TO_WATCH} \
${FILES_TO_IGNORE} \
'reset; ${NICE} ${TIME} ${COMMAND}'"
else
COMMAND="${NICE} ${TIME} ${COMMAND}"
fi
mkdir -p "${BUILD_DIR}" "${CACHE_DIR}/.cargo/"{install,registry}
# shellcheck disable=SC2086
${NICE} docker run -it --rm \
--network=host \
--init \
--name="${DOCKER_CONTAINER_NAME}" \
--hostname="${DOCKER_IMAGE_NAME_SAFE}" \
--user="$(id -u):$(id -g)" \
--volume="${PROJECT_ROOT_DIR}:/workdir" \
--volume="${CACHE_DIR}/.cargo/install:/home/${USER}/.cargo/install" \
--volume="${CACHE_DIR}/.cargo/registry:/home/${USER}/.cargo/registry" \
--workdir="/workdir/${PACKAGE_DIR_REL}" \
--env="UID=$(id -u)" \
--env="GID=$(id -g)" \
--env="USER=${USER}" \
--env="GROUP=${GROUP}" \
--env="PS1=\${USER}@\${HOST}" \
--env="RUST_BACKTRACE=${RUST_BACKTRACE}" \
--env="COLORBT_SHOW_HIDDEN=${COLORBT_SHOW_HIDDEN}" \
--env="RUST_LOG=${RUST_LOG}" \
--env="CARGO_BUILD_TARGET_DIR=${CARGO_BUILD_TARGET_DIR}" \
--env="RUSTC_FORCE_INCREMENTAL=${RUSTC_FORCE_INCREMENTAL}" \
--env-file="${PROJECT_ROOT_DIR}/.env" \
--ulimit core=0 \
${PORTS} \
"${DOCKER_REPO}:${DOCKER_TARGET}-${DOCKER_IMAGE_VERSION}" \
bash -c "set -euo pipefail; ${HANDLE_SIGINT}; ${COMMAND} ${SUCCESS_OR_FAILURE}"
if [ -n "${CROSS:-}" ] && [ -n "${RELEASE:-}" ] && { [ "${BUILD:-}" == 1 ] || [ "${RUN:-}" == 1 ]; }; then
mkdir -p .out/
if [[ "${CROSS}" == *windows* ]]; then
cp "${BUILD_DIR}/${CROSS}/release/nextalign.exe" ".out/nextalign-${CROSS}.exe"
cp "${BUILD_DIR}/${CROSS}/release/nextclade.exe" ".out/nextclade-${CROSS}.exe"
else
cp "${BUILD_DIR}/${CROSS}/release/nextalign" ".out/nextalign-${CROSS}"
cp "${BUILD_DIR}/${CROSS}/release/nextclade" ".out/nextclade-${CROSS}"
fi
fi