forked from all-of-us/curation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curation.sh
executable file
·146 lines (120 loc) · 3.82 KB
/
curation.sh
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
#!/usr/bin/env bash
# don't suppress errors
set -e
# suppress execution printing
set +x
## FUNCTIONS
function in_ci() {
if [[ -n "${CI}" ]] && [[ "${CI}" == "true" ]]; then
return 0
fi
return 1
}
function missing_required_env() {
echo Required environment variable \""${1}"\" is missing or empty
exit 1
}
## ENVIRONMENT
# set buildx envvars
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
## EXECUTION
# determine user from environment
uid=$(id -u)
gid=$(id -g)
whoiis=$(whoami)
echo Running as user "${whoiis}" \("${uid}:${gid}"\)...
dkr_run_args=(
"run"
"-v"
"$(pwd)/.git:/home/curation/project/curation/.git:z"
"-v"
"$(pwd)/data_steward:/home/curation/project/curation/data_steward:z"
"-v"
"$(pwd)/tests:/home/curation/project/curation/tests:z"
"-v"
"$(pwd)/tools:/home/curation/project/curation/tools:z"
"-v"
"$(pwd)/.circleci:/home/curation/project/curation/.circleci:z"
)
# If running specific tests using env var
if [[ -n "${CURATION_TESTS_FILEPATH}" && -s "${CURATION_TESTS_FILEPATH}" ]]; then
dkr_run_args+=("-v")
dkr_run_args+=("$(realpath "${CURATION_TESTS_FILEPATH}"):/tests-to-run.txt:ro")
dkr_run_args+=("-e")
dkr_run_args+=("CURATION_TESTS_FILEPATH=/tests-to-run.txt")
fi
# when run on a developer's machine, we need to do some extra things like:
# 1. ensure base container image is up to date
# 2. ensure they have credentials we can use
# 3. utilize compose v2 syntax (i.e. "docker compose" vs. "docker-compose")
# 3a. this can probably be changed with updates to CI env.
# 4. when in ci, ensure host ${BASH_ENV} file is brought into container and used
if ! in_ci; then
echo Running outside CI.
# when run locally, ensure we have google app creds to provide inside container
if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
missing_required_env "GOOGLE_APPLICATION_CREDENTIALS"
fi
# when run on a developer machine, utilize compose v2
COMPOSE_EXEC="docker compose"
echo Ensuring image is up to date...
build_args=(
"build"
"--build-arg"
"UID=${uid}"
"--build-arg"
"GID=${gid}"
"--quiet"
"develop"
)
# execute develop image build
set +e
build_res=$(${COMPOSE_EXEC} "${build_args[@]}" 2>&1)
set -e
# verify build succeeded before proceeding
if [[ -n "${build_res}" ]]; then
echo "Build step failed"
echo "${build_res}"
exit 1
fi
# add google cred volume to arg list
dkr_run_args+=("-v")
dkr_run_args+=("${GOOGLE_APPLICATION_CREDENTIALS}:/home/curation/project/curation/aou-res-curation-test.json")
else
echo Running in CI.
# when operating in CI, utilize compose v1
COMPOSE_EXEC="docker-compose"
echo "Adding CI \$BASH_ENV to image: ${BASH_ENV}"
dkr_run_args+=("-v" "${BASH_ENV}:/ci.env:ro")
fi
# define script arg array for use below
script_args=("$@")
# finally, add any / all remaining args provided to this script as args to pass into docker
# If the arg list contains "--", assume this to be the separation point between flags to send to
# docker compose, and the container entrypoint command.
#
# Otherwise, assume entire list is to be sent to container entrypoint
#
# This is necessary as we need to inject the name of the service defined within docker-compose.yaml that we want to
# run in-between the flags intended for `docker compose run` and container entrypoint.
if [[ "${script_args[*]}" =~ ([[:space:]]'--'[[:space:]]) ]]; then
# this will be flipped to 1 (true) when we reach "--"
at_command=0
for v in "${script_args[@]}"; do
if [[ "${v}" == "--" ]] && [[ "${at_command}" -eq 0 ]]; then
at_command=1
dkr_run_args+=("develop")
else
dkr_run_args+=("${v}")
fi
done
else
dkr_run_args+=("develop")
for v in "${script_args[@]}"; do
dkr_run_args+=("${v}")
done
fi
echo "Run cmd: ${dkr_run_args[*]}"
# run service
exec ${COMPOSE_EXEC} "${dkr_run_args[@]}"