-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev
executable file
·318 lines (267 loc) · 7.57 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
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
#!/usr/bin/env bash
set -euxo pipefail
shopt -s inherit_errexit 2>/dev/null || true
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
python=${dir}/venv/bin/python
ENVIRONMENT=${ENVIRONMENT:-}
PORT=${PORT:-8080}
usage() {
local me
me=$(basename "$0")
cat <<-EOF
USAGE: ${me} COMMAND
Run development workflows.
Examples:
# Run the tests once
${me}
# same as above
${me} tests
# install dev dependencies in a local virtualenv
${me} set-up-environment
# add new required packages from requirements.in to requirements.txt
${me} update-requirements
# upgrade versions for requirements
${me} upgrade-requirements
# upgrade version for specific requirement
${me} upgrade-requirements <requirement>
# run flask locally, (Bring Your Own [UAA, CF, opensearch_dashboards])
${me} serve
# run a local Opensearch + Opensearch Dashboards cluster in docker-compose
${me} cluster
# destroy cluster created above
${me} destroy-cluster
# push Opensearch/Dashboards/Proxy cluster to CF (make sure you update cf/secrets.yml first)
${me} cf
# run e2e tests
${me} e2e
EOF
}
pushd () {
# make pushd silent
command pushd "$@" > /dev/null
}
popd () {
# make popd silent
command popd > /dev/null
}
update_requirements() {
export CUSTOM_COMPILE_COMMAND="./dev update-requirements"
$python -m piptools compile \
--quiet \
--output-file=requirements.txt \
pip-tools/requirements.in
$python -m piptools compile \
--quiet \
--output-file=dev-requirements.txt \
pip-tools/dev-requirements.in
}
set_up_ci_environment () {
python3 -m venv venv
# we need to update pip because the version from apt is too old, and breaks
# when we try to install from requirements files later
${python} -m pip install --upgrade pip
# do this first to make sure we have piptools
${python} -m pip install -r dev-requirements.txt
# do this again for syncs that might involve deletions
${python} -m piptools sync requirements.txt dev-requirements.txt
${python} -m playwright install firefox
}
set_up_environment () {
python -m venv venv
source venv/bin/activate
# do this first to make sure we have piptools
${python} -m pip install --trusted-host pypi.org -r dev-requirements.txt
# do this again for syncs that might involve deletions
${python} -m piptools sync requirements.txt dev-requirements.txt
${python} -m playwright install firefox
}
upgrade_requirements() {
if [[ $# -ge 1 ]]; then
if [[ $# -ge 2 ]]; then
echo "can't update more than one package at a time"
fi
${python} -m piptools compile --upgrade-package "$1" --output-file requirements.txt pip-tools/requirements.in
${python} -m piptools compile --upgrade-package "$1" --output-file dev-requirements.txt pip-tools/dev-requirements.in
else
${python} -m piptools compile --upgrade --output-file requirements.txt pip-tools/requirements.in
${python} -m piptools compile --upgrade --output-file dev-requirements.txt pip-tools/dev-requirements.in
fi
}
watch_tests() {
export FLASK_ENV=unit
${python} -m piptools sync requirements.txt dev-requirements.txt
${python} -m pytest_watch --spool=1000 -n -c -w -- -Werror -vv tests "$@"
}
cf_push() {
cf push -f cf/opensearch-manifest.yml --vars-file cf/secrets.yml
cf push -f cf/opensearch-dashboards-manifest.yml --vars-file cf/secrets.yml
cf push -f cf/proxy-manifest.yml --vars-file cf/secrets.yml
}
cf_network() {
if [[ $# -lt 3 ]]; then
echo "Three arguments required: opensearch manager app name, opensearch node app name, dashboards app name, and proxy app name"
exit 1
fi
OPENSEARCH_NODE_APP_NAME="$1"
DASHBOARDS_APP_NAME="$2"
PROXY_APP_NAME="$3"
cf add-network-policy "$DASHBOARDS_APP_NAME" "$OPENSEARCH_NODE_APP_NAME" --protocol tcp --port 9200
cf add-network-policy "$PROXY_APP_NAME" "$DASHBOARDS_APP_NAME" --protocol tcp --port 5601
}
source_env_vars() {
set -o allexport
pushd "${dir}"
case $ENVIRONMENT in
prod|production)
source "prod.env"
;;
dev)
source "dev.env"
;;
*)
source ".env"
;;
esac
popd
set +o allexport
}
set_cf_default_vars() {
CF_ORG_1_NAME=${CF_ORG_1_NAME:-kibana-test-org-1}
CF_ORG_2_NAME=${CF_ORG_2_NAME:-kibana-test-org-2}
export CF_ORG_1_NAME
export CF_ORG_2_NAME
CF_ORG_1_SPACE_1_NAME=${CF_ORG_1_SPACE_1_NAME:-test-kibana-space-1}
CF_ORG_2_SPACE_2_NAME=${CF_ORG_2_SPACE_2_NAME:-test-kibana-space-2}
BOTH_ORGS_SPACE_NAME=${BOTH_ORGS_SPACE_NAME:-both-orgs-space}
export CF_ORG_1_SPACE_1_NAME
export CF_ORG_2_SPACE_2_NAME
export BOTH_ORGS_SPACE_NAME
}
set_cf_env_vars() {
CF_ORG_1_ID=$(cf org "$CF_ORG_1_NAME" --guid)
CF_ORG_2_ID=$(cf org "$CF_ORG_2_NAME" --guid)
export CF_ORG_1_ID
export CF_ORG_2_ID
cf target -o "$CF_ORG_1_NAME"
CF_ORG_1_SPACE_1_ID=$(cf space "$CF_ORG_1_SPACE_1_NAME" --guid)
CF_ORG_1_BOTH_ORGS_SPACE_ID=$(cf space "$BOTH_ORGS_SPACE_NAME" --guid)
export CF_ORG_1_SPACE_1_ID
export CF_ORG_1_BOTH_ORGS_SPACE_ID
cf target -o "$CF_ORG_2_NAME"
CF_ORG_2_SPACE_2_ID=$(cf space "$CF_ORG_2_SPACE_2_NAME" --guid)
CF_ORG_2_BOTH_ORGS_SPACE_ID=$(cf space "$BOTH_ORGS_SPACE_NAME" --guid)
export CF_ORG_2_SPACE_2_ID
export CF_ORG_2_BOTH_ORGS_SPACE_ID
}
seed_opensearch_data() {
bash ./ci/seed-opensearch-data.sh
}
provision_cf_access() {
bash ./ci/provision-cf-access.sh
}
run_e2e_tests() {
${python} -m pytest e2e --browser firefox "$@"
}
main() {
pushd "${dir}"
trap popd exit
local command=$1
shift
case $command in
-h)
usage
;;
update-requirements|update-requirement)
update_requirements "$@"
;;
upgrade-requirements|upgrade_requirement)
upgrade_requirements "$@"
;;
set-up-ci-environment)
set_up_ci_environment
;;
set-up-environment|setup-environment)
set_up_environment
;;
bandit)
${python} -m bandit -r cf_auth_proxy
;;
black)
${python} -m black . "$@"
;;
test|tests)
export FLASK_ENV=unit
${python} -m piptools sync requirements.txt dev-requirements.txt
${python} -m pytest tests
;;
provision-cf-access)
source_env_vars
set_cf_default_vars
provision_cf_access
;;
serve)
source_env_vars
export FLASK_APP="cf_auth_proxy.app:create_app()"
${python} -m gunicorn \
--access-logfile - \
--error-logfile - \
--log-level info \
--timeout 300 \
--workers 4 \
-b "127.0.0.1:$PORT" \
--worker-class eventlet "$FLASK_APP"
;;
start-cluster)
source_env_vars
docker compose -f docker/docker-compose.yml up --force-recreate --build "$@"
;;
destroy-cluster)
pushd docker
docker compose down
popd
;;
cf-push)
cf_push
cf_network "$@"
;;
cf-network)
cf_network "$@"
;;
watch-test|watch-tests)
watch_tests "$@"
;;
e2e-local)
export ENVIRONMENT=local
source_env_vars
set_cf_default_vars
set_cf_env_vars
seed_opensearch_data
run_e2e_tests "$@"
;;
e2e)
if [[ -n "$ENVIRONMENT" ]]; then
source_env_vars
fi
run_e2e_tests "$@"
;;
format)
${python} -m black .
;;
seed-opensearch-data-local)
source_env_vars
set_cf_default_vars
set_cf_env_vars
seed_opensearch_data
;;
seed-opensearch-data)
set_cf_default_vars
set_cf_env_vars
seed_opensearch_data
;;
*)
usage
exit 1
;;
esac
}
main "$@"