-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpex.sh
executable file
·285 lines (227 loc) · 7.61 KB
/
dpex.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
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
#!/bin/bash
LIBSOURCED="false"
current_dir=$(dirname "$0")
path_ref="./"
# include lib
if [[ -f "./_lib.sh" ]]; then
source "./_lib.sh"
elif [[ -f "./${current_dir}/_lib.sh" ]]; then
source "./${current_dir}/_lib.sh"
path_ref="./${current_dir}/"
else
echo -e "\033[1;31mError:\033[0m \033[0;31m Missing library '_lib.sh' \033[0m"
exit 1
fi;
function usage() {
echo "Please call script by using all params in this order!"
echo " $0 env-file genfiles|clear|compose-comand"
echo "-----------------------------------------------------"
echo
echo " commands: "
echo " genfile > creates an .env and .sec file based on all known vars"
echo " clear > removes all container and images, prunes allocated space"
echo ""
echo " compose-comand > redirected to docker-compose"
echo ""
echo "-----------------------------------------------------"
echo ""
echo " examples: "
echo " $0 demo.env genfiles"
echo " $0 demo.env up --build --detach"
echo " $0 demo.env ps -a"
echo " $0 demo.env down"
echo " $0 demo.env clear"
echo ""
echo "-----------------------------------------------------"
echo
echo "Canceled !!!"
exit 1
}
# All Params are required
if [[ $# -lt 2 ]]; then
echo_error "Missing arguments, see...\n"
usage
fi
function validate_apex_params() {
# check required
check_params "APEX_VERSION" "APEX_FULL_VERSION" "APEX_URL" "APEX_SPACE_NAME" "APEX_INTERNAL_MAIL"
## TableSpace
if [[ "$APEX_CREATE_TSPACE" == true ]]; then
check_params "APEX_CREATE_TSPACE"
fi
## Second PDB
if [[ "$APEX_SECND_PDB" == true ]]; then
check_params "APEX_FIRST_PDB_TBL_SPACE" "APEX_SECND_PDB_TBL_SPACE" "APEX_SECND_PDB_POOL_NAME" "APEX_SECND_PDB_NAME"
fi
## SMTP Password required when APEX_SMTP is true
if [[ "$APEX_SMTP" == true ]]; then
check_params "APEX_SMTP_HOST_ADDRESS" "APEX_SMTP_FROM" "APEX_SMTP_USERNAME"
check_secret "SMTP_PASSWORD"
fi
}
function validate_ords_params() {
check_params "ORDS_FULL_VERSION" "ORDS_VERSION" "ORDS_URL" "ORDS_PORT"
check_secret "ORDS_PASSWORD"
## DynDNS
if [[ "$ORDS_DDNS" == true ]]; then
check_params "ORDS_DDNS_USER" "ORDS_DDNS_URL"
check_secret "ORDS_DDNS_PASSWORD"
fi
}
function validate_tcat_params() {
check_params "TOMCAT_VERSION" "TOMCAT_URL" "TOMCAT_PORT"
}
function validate_aop_params() {
check_params "AOP_PORT"
}
function validate_trfk_params() {
check_params "TRAEFIK_DEFAULT_ROUTE" "TRAEFIK_DOMAIN" "TRAEFIK_DASHBOARD" "TRAEFIK_LETSENCRYPT_EMAIL"
check_secret "TRFK_USERPWD"
}
export DCKAPX_CONF_FILE=${1:-".env"}
# check param file
if [[ ! -f ${DCKAPX_CONF_FILE} ]]; then
if [[ ${2} != "genfiles" ]]; then
echo ${2}
echo_error "Missing configuration file: ${DCKAPX_CONF_FILE}"
exit 1
fi
else
## export configuration
set -a
source ${DCKAPX_CONF_FILE}
DCPAPX_PROJECT_NAME=$(basename "${DCKAPX_CONF_FILE%.*}")
DCKAPX_CONF_FILE=$(pwd)/${DCKAPX_CONF_FILE}
# is there a secret file
DCKAPX_SECRET_FILE="${DCKAPX_CONF_FILE%.*}.sec"
if [[ -f ${DCKAPX_SECRET_FILE} ]]; then
source ${DCKAPX_SECRET_FILE}
fi
## stop export configuration
set +a
fi
DCKAPX_COMP_FILES=""
# check only when argument 2 is not genfiles
if [[ ${2} != "genfiles" ]]; then
# validate global required params
## DB Params are required, when any of DB, APEX, ORDS are set
if [[ "$DB" == true ]] || [[ "$APEX" == true ]] || [[ "$ORDS" == true ]]; then
check_params "DB_USER" "DB_HOST" "DB_PORT" "DB_NAME"
check_secret "DB_PASS"
fi
if [[ "$DB" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}database/docker_compose.yml "
fi
if [[ "$APEX" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}apex/docker_compose.yml "
validate_apex_params
if [[ "$DB" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}apex/docker_compose_db.yml "
fi
if [[ "$ORDS" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}apex/docker_compose_ords.yml "
fi
fi
if [[ "$ORDS" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}ords/docker_compose.yml "
validate_ords_params
if [[ "$DB" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}ords/docker_compose_db.yml "
fi
if [[ "$TRAEFIK" == true ]] && [[ "$TOMCAT" != true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}ords/docker_compose_traefik.yml "
fi
fi
if [[ "$TOMCAT" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}tomcat/docker_compose.yml "
validate_tcat_params
if [[ "$TRAEFIK" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}tomcat/docker_compose_traefik.yml "
fi
fi
if [[ "$AOP" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}aop/docker_compose.yml "
validate_aop_params
if [[ "$DB" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}aop/docker_compose_db.yml "
fi
if [[ "$ORDS" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}aop/docker_compose_ords.yml "
fi
fi
if [[ "$TRAEFIK" == true ]]; then
DCKAPX_COMP_FILES+="-f ${path_ref}traefik/docker_compose.yml "
fi
fi
# arg1 => file, entfernen, den Rest geben wir an compose weiter
shift
DCKAPX_COMMAND="$@"
function clear_containers() {
local arg=${1}
if [[ ${arg} == "-f" ]] || [[ ${arg} == "--force" ]]; then
docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} down --volumes --rmi local
yes | docker system prune
else
ask_with_yes_no "Shutdown all containers?"
if [[ $? -eq 0 ]]; then
docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} down
ask_with_yes_no "Remove volumes?"
if [[ $? -eq 0 ]]; then
docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} down --volumes
fi
ask_with_yes_no "Remove images?"
if [[ $? -eq 0 ]]; then
docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} down --rmi local
fi
ask_with_yes_no "Addionally prune system?"
if [[ $? -eq 0 ]]; then
docker system prune
fi
fi
fi
}
function genfiles() {
local target_file="${1/.env/}"
# export all vars when defined
set -a
source "${path_ref}_template/_defaults.env"
[[ -f "${target_file}.env" ]] && source "${target_file}.env"
[[ -f "${target_file}.sec" ]] && source "${target_file}.sec"
set +a
# create backup
[[ -f "${target_file}.env" ]] && mv "${target_file}.env" "${target_file}.env.old"
[[ -f "${target_file}.sec" ]] && mv "${target_file}.sec" "${target_file}.sec.old"
# write vars to target files
if [[ -z "$ZSH_VERSION" ]]; then
render_template "${path_ref}_template/_template_env" "${target_file}.env"
render_template "${path_ref}_template/_template_sec" "${target_file}.sec"
sed -i '' '1,2d' "${target_file}.env"
sed -i '' '1,2d' "${target_file}.sec"
else
envsubst < "${path_ref}_template/_template_env" > "${target_file}.env"
envsubst < "${path_ref}_template/_template_sec" > "${target_file}.sec"
sed -i '1,2d' "${target_file}.env"
sed -i '1,2d' "${target_file}.sec"
fi
echo_info "${target_file}.env and ${target_file}.sec created"
echo_info "Please define your configuration properties here"
write_line_if_not_exists "# do not commit your secrets" ".gitignore" "\n"
write_line_if_not_exists "${target_file}.sec" ".gitignore"
ls -la ${target_file}.*
}
case ${1} in
'clear')
clear_containers ${2}
;;
'genfiles')
genfiles ${DCKAPX_CONF_FILE}
;;
'-h'|'--help')
usage
;;
*)
echo "docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} ${DCKAPX_COMMAND}"
docker-compose --project-name ${DCPAPX_PROJECT_NAME} ${DCKAPX_COMP_FILES} ${DCKAPX_COMMAND}
;;
esac
#