-
Notifications
You must be signed in to change notification settings - Fork 2
/
dev
executable file
·200 lines (167 loc) · 3.76 KB
/
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
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit 2>/dev/null || true
built_image_name="external-domain-broker-dev"
main() {
[[ $# -eq 0 ]] && usage "Expected command."
if [[ "${DEBUG+x}" ]] ; then
set -x
fi
cd "$(git rev-parse --show-toplevel)"
command="$1"
shift
case $command in
-h)
usage
;;
docker-compose-up | dkc-up)
source_env_vars
cd docker
docker compose up
;;
serve)
build_image
serve "$@"
;;
tests)
build_image
tests "$@"
;;
watch-tests)
build_image
watch-tests "$@"
;;
run)
build_image
run "$@"
;;
new-migration)
build_image
new-migration "$@"
;;
upgrade-requirements)
build_image
upgrade-requirements "$@"
;;
update-requirements)
build_image
update-requirements "$@"
;;
shell)
build_image
shell
;;
*)
usage "Unknown command: $command"
;;
esac
}
source_env_vars() {
set -o allexport
source ".env"
set +o allexport
}
build_image() {
# The build-args are used to create a user with the same UID/GID as
# yourself. That way, any files written to the $PWD mount will be owned by
# you. This user is embedded in the docker image, so the resulting image
# should only be used locally. These arguments are not used in our
# concourse pipeline.
echo "Building image..."
docker build \
--file=docker/Dockerfile.dev \
--tag=${built_image_name} \
--build-arg UID="$(id -u)" \
--build-arg GID="$(id -g)" \
--build-arg USER="$USER" \
--build-arg base_image="ubuntu:22.04" \
.
}
run_docker() {
# the unnamed volume at `/app/venv/` is just to shadow the local
# virtual environment. This is a silly hack so ptw will be a bit faster
docker run \
--rm \
-it \
-v "$PWD:/app" \
-v "/app/venv/" \
"${built_image_name}" \
"$*"
}
run_docker_read_only() {
docker \
run \
--rm \
-it \
-v "$PWD:/app:ro" \
-v "$PWD/tmp:/app/tmp:rw" \
-v "$PWD/logs:/app/logs:rw" \
-v "$PWD/.pytest_cache:/app/.pytest_cache:rw" \
"${built_image_name}" \
"$*"
}
run_docker_exposed() {
docker run \
--rm \
-it \
-v "$PWD:/app" \
-p "0.0.0.0:8000:8000" \
"${built_image_name}" \
"$*"
}
tests() {
run_docker_read_only docker/tests "$@"
}
watch-tests() {
run_docker_read_only docker/tests watch "$@"
}
run() {
run_docker "$@"
}
shell() {
run "./docker/start-servers.sh && bash"
}
serve() {
run_docker_exposed docker/serve "$@"
}
update-requirements() {
run "docker/update-requirements-txt"
}
upgrade-requirements() {
run "docker/upgrade-requirements-txt"
}
new-migration() {
[[ "$#" -eq 1 ]] || usage "Missing migration description"
run "./docker/start-servers.sh && flask db upgrade && flask db migrate -m \"$1\""
}
usage() {
[[ $# -gt 0 ]] && echo "ERROR: $*"
local me=$(basename "$0")
cat <<-EOF
USAGE: $me COMMAND
Run workflows via the development docker image.
This provides a consistent developer experience, and avoids the "works on my
laptop" issue.
Examples:
# Run the tests once
$me
# Same as above
$me tests
# Run the application, binding to port 8000
$me serve
# Continually watch for file changes and runs tests
$me watch-tests
# Start an interactive bash shell in the tests container
$me shell
# Create a new database migration file.
# The new file will be in migrations/versions/
$me new-migration "Add sprocket column to widget table"
# Generate the pip-tools/*requirements.txt files from
# pip-tools/*requirements.in
$me update-requirements
# Run command 'foo' in the container
$me run foo
EOF
exit 1
}
main "$@"