-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
executable file
·234 lines (198 loc) · 4.62 KB
/
setup
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
#!/bin/bash -eu
# CONFIG
## ENV
ENV_FILE="./.env"
VAR_TO_EDIT=(
"ADMIN_USER"
"ADMIN_PASS"
"API42_USERID"
"API42_SECRET"
"API42_REDIRECTURL_PROD"
"API42_REDIRECTURL_DEV"
"NGINX_PORT"
)
JWT_LEN="64"
## ANGULAR ENV
ANGULAR_ENV_DIR="src/angularjs/src/app/environments"
ANGULAR_PROD_ENV="${ANGULAR_ENV_DIR}/environment.common.ts"
ANGULAR_DEV_ENV="${ANGULAR_ENV_DIR}/environment.dev.ts"
## PARSING
ARGS=(${@})
ARGS_LEN="${#ARGS[@]}"
declare -A KNOWN_FILE
KNOWN_FILE[dev]="docker-compose-dev.yaml"
KNOWN_FILE[prod]="docker-compose.yaml"
## PARSE CONFIG
# thanks to the null command (:) and the shell parameters expansion (?)
#+ we can check if all variable are all been set
: ${ENV_FILE?} ${VAR_TO_EDIT?} ${JWT_LEN?} \
${ANGULAR_ENV_DIR?} ${ANGULAR_PROD_ENV?} ${ANGULAR_DEV_ENV?} \
${KNOWN_FILE[dev]?} ${KNOWN_FILE[prod]?}
# FUNCTIONS
## ENV FUNCTION
function install::env::replace_var()
{
local var_name="${1?}"
local var_value="${2?}"
sed -i "s|${var_name}=\"\"|${var_name}=\"${var_value}\"|g" "${ENV_FILE}"
}
function install::env()
{
[ ! -f "${ENV_FILE}" ] && cp "${ENV_FILE}"{.template,}
for var_name in ${VAR_TO_EDIT[@]}; do
[ "$(grep ${var_name}=\"\" ${ENV_FILE})" ] || continue
printf "\x1b[31m%s\x1b[00m > " "${var_name}"
read choice
install::env::replace_var "${var_name}" "${choice}"
done
JWT_PASS="$(head -c${JWT_LEN} /dev/urandom | xxd -ps -c ${JWT_LEN})"
install::env::replace_var "JWT_SECRET" "${JWT_PASS}"
angular::set_after_auth
}
## ANGULAR FUNCTION
function url::encode()
{
local url="${1?}"
local url_len="${#url}"
url_encoded="$(
for ((i = 0; i < url_len; i++)); do
local c="${url:${i}:1}"
case "${c}" in
[a-zA-Z0-9.~_-]) printf "%c" "${c}" ;;
*) printf "%%%02X" "'${c}" ;;
esac
done
)"
}
function angular::env::replace_var()
{
local var_value="${1?}"
local file_path="${2?}"
local var=" after_auth_uri: "
perl -pi -e "s|^${var}\".*?\"|${var}\"${var_value}\"|" "${file_path}"
}
function angular::get_after_auth()
{
local var_name="${1?}"
local user_id
local redirect
source "${ENV_FILE}"; \
user_id="${API42_USERID}" \
redirect="${!var_name}"
url::encode "${redirect}"
redirect="${url_encoded}"
AFTER_AUTH="https://api.intra.42.fr/oauth/authorize?client_id=${user_id}"
AFTER_AUTH+="&redirect_uri=${redirect}&response_type=code"
}
function angular::set_after_auth()
{
angular::get_after_auth "API42_REDIRECTURL_PROD"
angular::env::replace_var "${AFTER_AUTH}" "${ANGULAR_PROD_ENV}"
angular::get_after_auth "API42_REDIRECTURL_DEV"
angular::env::replace_var "${AFTER_AUTH}" "${ANGULAR_DEV_ENV}"
}
## DOCKER
function docker::kill()
{
for file in ${KNOWN_FILE[@]}; do
docker compose -f "./${file}" kill -s SIGINT
docker compose -f "./${file}" down --rmi all
done
}
function docker::clean()
{
sudo rm -rf ./src/angularjs/node_modules
sudo rm -rf ./src/nestjs/node_modules
sudo rm -rf ./volume
}
function docker::run()
{
local mode="${1?}"
docker compose -f "${KNOWN_FILE[${mode}]}" up --build
}
function docker::re()
{
local mode="${1?}"
local do_clean="${2:-0}"
local file
docker::kill
[ "${do_clean}" == "0" ] || docker::clean
docker::run "${mode}"
}
# DEPLOYMENT HELPER
function transc::deploy()
{
rm -f "${ENV_FILE}"
install::env
perl -pi -e "s|^ socket_url: \".*?\"| socket_url: \"${SOCKET_URL}\"|" ./src/angularjs/src/app/environments/environment.common.ts
}
# PARSING
function parse::usage()
{
printf "${0}: [re mode] | [switch mode] | [run mode] | [clean] | [deploy]\n"
printf "\t\n"
printf "\tmode is one of: 'dev' or 'prod'\n"
printf "\tif no arg provided, fill up .env file\n"
printf "\t\n"
printf "\t\tre: clean and then launch in specified mode\n"
printf "\t\trun: run in specified mode\n"
printf "\t\tswitch: remove database and relauch in specified mode\n"
printf "\t\tclean: down all service in compose and remove volume\n"
printf "\t\tdeploy: helper to deploy with github\n"
exit 1
}
function parse::re()
{
local do_clean="0"
[ "${ARGS_LEN}" != "2" ] && parse::usage
case "${ARGS[0]}" in
"re")
do_clean="1"
;;
"switch")
do_clean="0"
;;
esac
install::env
docker::re "${ARGS[1]}" "${do_clean}"
}
function parse::run()
{
local mode
[ "${ARGS_LEN}" != "2" ] && parse::usage
case "${ARGS[1]}" in
"dev"|"prod") : ;;
*) parse::usage ;;
esac
install::env
docker::run "${ARGS[1]}"
}
function parse::arg()
{
if [ "${ARGS_LEN}" -eq 0 ]; then
install::env
return
fi
case "${ARGS[0]}" in
"re"|"switch")
parse::re
;;
"run")
parse::run
;;
"clean")
docker::clean
;;
"deploy")
: ${SOCKET_URL?}
transc::deploy
;;
"help")
parse::usage
;;
*) parse::usage ;;
esac
}
# MAIN
parse::arg
exit $?