-
Notifications
You must be signed in to change notification settings - Fork 10
/
shipfast
executable file
·434 lines (334 loc) · 11.2 KB
/
shipfast
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
#!/bin/bash
set -eu
Show_Help() {
printf "\nSHIPFAST API PROTECTION DEMO\n"
printf "\n Servers:\n"
printf "\t./shipfast up servers\n"
printf "\t./shipfast logs servers\n"
printf "\t./shipfast restart servers\n"
printf "\t./shipfast down servers\n"
printf "\n Shipfast API:\n"
printf "\t./shipfast up api\n"
printf "\t./shipfast logs api\n"
printf "\t./shipfast restart api\n"
printf "\t./shipfast down api\n"
printf "\t./shipfast shell api sh\n"
printf "\n Shipraider Web:\n"
printf "\t./shipfast up web\n"
printf "\t./shipfast logs web\n"
printf "\t./shipfast restart web\n"
printf "\t./shipfast down web\n"
printf "\t./shipfast shell web-api-key sh\n"
printf "\n Android Studio:\n"
printf "\t./shipfast up editor\n"
printf "\t./shipfast logs editor\n"
printf "\t./shipfast restart editor\n"
printf "\t./shipfast down editor\n"
printf "\t./shipfast shell editor zsh\n"
printf "\n Cache:\n"
printf "\t./shipfast cache api delete v1 authorization-token-here\n"
printf "\n Proxy:\n"
printf "\t./shipfast proxy -p 8000\n"
printf "\n Logs:\n"
printf "\t./shipfast logs\n"
printf "\t./shipfast logs -f \n"
printf "\t./shipfast logs --follow \n"
printf "\t./shipfast logs -t 100 \n"
printf "\t./shipfast logs --tail 100 \n"
printf "\t./shipfast logs -f -t 100 \n"
printf "\t./shipfast logs -f -t 100 api \n"
echo
}
Docker_Container_Is_Running() {
sudo docker container ls -a | grep -q "${1? Missing container name.}" -
return $?
}
Build_NodeJS_Server() {
# ShipFast and ShipRaider share the same NodeJS docker image.
${DOCKER_COMPOSE} build api
}
Build_Editor() {
${DOCKER_COMPOSE} build editor
./docker/setup/docker-volumes-dirs.sh "${PROJECT_NAME}" "${PROJECT_HOST_DIR}" "${ANDROID_STUDIO_MAJOR_VERSION}"
}
Start_Editor() {
local _ca_crt_file="./docker/demo/setup/certificates/ProxyCA.crt"
local _ca_pem_dir="/etc/ssl/certs"
local _ca_pem_filename="ProxyCA.pem"
local _ca_pem_file="${_ca_pem_dir}/${_ca_pem_filename}"
local _ca_name="ProxyCA"
./docker/setup/docker-volumes-dirs.sh "${PROJECT_NAME}" "${PROJECT_HOST_DIR}" "${ANDROID_STUDIO_MAJOR_VERSION}"
# KVM needs to change permissions in order for Android Studio can work.
# For some reason the permissions return to original state after we close the editor
# TODO: find a permanent solution
${ROOT_PREFIX} chown ${_USER}:${_USER} /dev/kvm
${DOCKER_COMPOSE} up -d editor
if [ ! -f "${APPROOV_SDK_PATH}" ]; then
printf "\n---> WARNING: Missing the APPROOV SDK ${APPROOV_SDK_PATH} <---\n"
printf "Note: This can be ignored until you need to build the 'approov' product flavour\n"
printf "\n> ADD THE APPROOV SDK WITH: approov sdk -getLibrary ${APPROOV_SDK_PATH}\n"
printf "\n> GET THE APPROOV SDK INITIAL CONFIG: approov sdk -getConfig approov-initial.config\n"
printf "\n> Add the content of the file approov-initial.config to the var APPROOV_INITIAL_CONFIG in the .env file\n\n"
exit 1
fi
}
Restart_Editor() {
Destroy_Editor && Start_Editor
}
Destroy_Editor() {
${DOCKER_COMPOSE} stop editor
${DOCKER_COMPOSE} rm -f editor
}
Start_Api_Server() {
# From the android emulator we must use 10.0.2.2:3333 to call localhost
if [ ! -d "./server/shipfast-api/node_modules" ]; then
${DOCKER_COMPOSE} run api sh -c 'npm install && exit || exit'
fi
${DOCKER_COMPOSE} up -d api
}
Restart_Api_Server() {
Destroy_Api_Server && Start_Api_Server
}
Destroy_Api_Server() {
${DOCKER_COMPOSE} stop api
${DOCKER_COMPOSE} rm -f api
}
Start_Web_Server() {
if [ ! -d "./server/shipraider-rogue-web/node_modules" ]; then
${DOCKER_COMPOSE} run web-api-key sh -c 'npm install && exit || exit'
fi
${DOCKER_COMPOSE} up -d web-api-key
${DOCKER_COMPOSE} up -d web-static-hmac
${DOCKER_COMPOSE} up -d web-dynamic-hmac
${DOCKER_COMPOSE} up -d web-certificate-pinning
${DOCKER_COMPOSE} up -d web-approov
}
Restart_Web_Server() {
Destroy_Web_Server && Start_Web_Server
}
Destroy_Web_Server() {
${DOCKER_COMPOSE} stop web-api-key
${DOCKER_COMPOSE} rm -f web-api-key
${DOCKER_COMPOSE} stop web-static-hmac
${DOCKER_COMPOSE} rm -f web-static-hmac
${DOCKER_COMPOSE} stop web-dynamic-hmac
${DOCKER_COMPOSE} rm -f web-dynamic-hmac
${DOCKER_COMPOSE} stop web-certificate-pinning
${DOCKER_COMPOSE} rm -f web-certificate-pinning
${DOCKER_COMPOSE} stop web-approov
${DOCKER_COMPOSE} rm -f web-approov
}
Logs() {
${DOCKER_COMPOSE} logs ${@}
}
Run_Shell() {
local docker_compose_service="${1? Missing docker composer service name.}"
local shell="${2? Missing shell to use, e.g: sh}"
shift 2
local container_name="dev.${docker_compose_service}"
if Docker_Container_Is_Running "${container_name}"; then
${DOCKER_COMPOSE} exec "${docker_compose_service}" "${shell}" ${@}
return
fi
${DOCKER_COMPOSE} run --rm "${docker_compose_service}" "${shell}" ${@}
}
Start_Proxy() {
${DOCKER_COMPOSE} exec editor mitmproxy ${@}
}
Main() {
local ROOT_PREFIX=""
local DOCKER_COMPOSE=$(which docker-compose)
local _USER="$(id -un)"
local LINES=""
local ANDROID_STUDIO_BUILD="192.6241897"
local ANDROID_STUDIO_VERSION=3.6.1.0
local ANDROID_STUDIO_MAJOR_VERSION=3.6
local PROJECT_DIR="${PWD#*${HOME}/}"
local PROJECT_NAME="${PROJECT_DIR//\//_}"
#PROJECT_HOST_DIR="${HOME}/.docker-shipfast-demo/${PROJECT_NAME//\//_}"
local PROJECT_HOST_DIR="${HOME}/.docker-shipfast-demo"
# Auto detect if we need to use sudo
if type sudo &> /dev/null; then
ROOT_PREFIX=sudo
fi
# Sometimes we may have sudo in our system, but to run `docker-compose` it
# will not find the executable with sudo, I am looking at you AWS EC2, thus
# we can customize it.
if [ -f ./.shipfast.local.vars ]; then
source ./.shipfast.local.vars
fi
DOCKER_COMPOSE="${ROOT_PREFIX} ${DOCKER_COMPOSE}"
if [ ! -f ./.env ]; then
printf "\n---> FATAL ERROR: Missing .env file <---\n"
exit 1
fi
mkdir -p "${PROJECT_HOST_DIR}"
local APPROOV_SDK_PATH=./app/android/kotlin/ShipFast/approov/approov.aar
# Setup X11 server authentication
# @link https://wiki.ros.org/docker/Tutorials/GUI#The_isolated_way
if type xauth &> /dev/null; then
local _XAUTH="${PROJECT_HOST_DIR}"/.docker.X11-unix
touch "${_XAUTH}"
xauth nlist "${DISPLAY}" | sed -e 's/^..../ffff/' | xauth -f "${_XAUTH}" nmerge -
fi
# Updates the env var PROJECT_NAME
if grep -q "PROJECT_NAME" .env && ! grep -q "${PROJECT_NAME}" .env
then
while IFS= read -r line
do
case "${line}" in
"PROJECT_NAME"*) LINES="${LINES}PROJECT_NAME=${PROJECT_NAME}\n" ;;
*) LINES="${LINES}${line}\n" ;;
esac
done < .env
if [ ! -z ${LINES} ]
then
printf "${LINES}" > .env
fi
fi
# Updates the env var PROJECT_HOST_DIR
if grep -q "PROJECT_HOST_DIR" .env && ! grep -q "${PROJECT_HOST_DIR}" .env
then
while IFS= read -r line
do
case "${line}" in
"PROJECT_HOST_DIR"*) LINES="${LINES}PROJECT_HOST_DIR=${PROJECT_HOST_DIR}\n" ;;
*) LINES="${LINES}${line}\n" ;;
esac
done < .env
if [ ! -z ${LINES} ]
then
printf "${LINES}" > .env
fi
fi
# Creates the env var PROJECT_NAME
if ! grep -q "${PROJECT_NAME}" .env
then
printf "\nPROJECT_NAME=${PROJECT_NAME}\n" >> .env
fi
# Creates the env var PROJECT_HOST_DIR
if ! grep -q "${PROJECT_HOST_DIR}" .env
then
printf "\nPROJECT_HOST_DIR=${PROJECT_HOST_DIR}\n" >> .env
fi
local ANDROID_SDK_DIR="${PROJECT_HOST_DIR}/android-studio/${ANDROID_STUDIO_MAJOR_VERSION}/${PROJECT_NAME}/Android/Sdk"
for input in "${@}"; do
case "${input}" in
-h | --help | help )
Show_Help
exit $?
;;
build )
case "${2:-}" in
api )
Build_NodeJS_Server
;;
editor )
Build_Editor
;;
servers )
Build_NodeJS_Server
;;
web )
Build_NodeJS_Server
;;
* )
Show_Help
;;
esac
exit $?
;;
cache )
case "${2:-}" in
api )
shift 2
./server/shipfast-api/bin/cache.sh "${@}"
exit $?
;;
esac
exit $?
;;
down )
case "${2:-}" in
api )
Destroy_Api_Server
;;
editor )
Destroy_Editor
;;
servers )
Destroy_Api_Server
Destroy_Web_Server
;;
web )
Destroy_Web_Server
;;
* )
Show_Help
;;
esac
exit $?
;;
logs )
shift 1
Logs ${@}
exit $?
;;
proxy )
shift 1
Start_Proxy "${@}"
exit $?
;;
restart )
case "${2:-}" in
api )
Restart_Api_Server
;;
editor )
Restart_Editor
;;
servers )
Restart_Api_Server
Restart_Web_Server
;;
web )
Restart_Web_Server
;;
* )
Show_Help
;;
esac
exit $?
;;
shell )
shift 1
Run_Shell ${@}
exit $?
;;
up )
case "${2:-}" in
api )
Start_Api_Server
;;
editor )
Start_Editor
;;
servers )
Start_Api_Server
Start_Web_Server
;;
web )
Start_Web_Server
;;
* )
Show_Help
;;
esac
exit $?
;;
esac
done
Show_Help
exit $?
}
Main ${@}