-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.sh
290 lines (235 loc) · 8.18 KB
/
setup.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
286
287
288
289
290
#!/usr/bin/env bash
# ======================================================================================
# title : setup.sh
# description : Setting the .env file up for docker-compose
# usage : bash setup.sh [--configure] [--migrate-db] [--collect-static] [--update]
# author : Kevin Yuan
# date : 25.01.2020
# version : 1.0
# notes : <Some notes about this script, dependencies>
# ======================================================================================
# Terminate this bash script as soon as an error occures
set -eu
# ================================== MISC Codebits ====================================
# Setting some colors
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
redBg=$(tput setab 1)
greenBg=$(tput setab 2)
yellowBg=$(tput setab 3)
whiteBg=$(tput setab 7)
reset=$(tput sgr0)
termwidth="$(($(tput cols) / 2))"
# Centered printing with Padding
function center() {
padding="$(printf '%0.1s' ={1..500})"
printf '%*.*s %s %*.*s\n' 0 "$(((termwidth - 2 - ${#1}) / 2))" "$padding" "$1" 0 "$(((termwidth - 1 - ${#1}) / 2))" "$padding"
}
# Checking if flag (command line option) is set
# Taken from here https://stackoverflow.com/questions/2875424/correct-way-to-check-for-a-command-line-flag-in-bash
has_param() {
local term="$1"
shift
for arg; do
if [[ $arg == "$term" ]]; then
return 0
fi
done
return 1
}
wrap_text() {
echo -e "$1" | fold -w $termwidth -s
}
# ================================= Argument Parser ===================================
# Welcome message
center ""
center "BiCoN web setup script"
center ""
echo -e
# Argument flags
run_all=true
configure=false
migrate_db=false
collect_static=false
# Run configuration file generation
if has_param '--configure' "$@"; then
run_all=false
configure=true
fi
# Migrate changes in the new database
if has_param '--migrate-db' "$@"; then
run_all=false
migrate_db=true
fi
# Collect new static files
if has_param '--collect-static' "$@"; then
run_all=false
collect_static=true
fi
# Update == Collect new static files && Migrate changes to the database
if has_param '--update' "$@"; then
run_all=false
migrate_db=true
collect_static=true
fi
# If no option has been found, run everything and print options
if [[ $run_all == "true" ]]; then
configure=true
migrate_db=true
collect_static=true
# Print selection
wrap_text "${yellow}No flag has been set. Running everything (configuring the containers; migrating the database; collecting all the static files).${reset}"
else
echo -e "${yellow}The following operations will be perfomed"
echo -e "- Configuring the containers from BiCoN-web.conf (creating the .env file): ${configure}"
echo -e "- Migrating the database container based on the supplied migrations from django: ${migrate_db}"
echo -e "- Collecting all the static files from the templates into the static directory: ${collect_static}"
echo -e "${reset}"
fi
echo -e
# =========================== Configuring the container =============================
if [ $configure = "true" ]; then
# Print section
center "Configuring the container"
echo -e
# Check if config file exists
echo -e "- Load BiCoN-web.conf"
if ! test -f "BiCoN-web.conf"; then
echo -e "${red}BiCoN-web.conf does not exist."
echo -e "Exiting now...${reset}"
exit 1
fi
# Load the config file (carefull, code in this file will be executed!)
source BiCoN-web.conf
# Backup old .env file if exists
echo -e "- Check if configuration file already exists"
if test -f ".env"; then
echo -e " + Old .env configuration file found, rename it into '.env.bak'"
mv .env .env.bak
fi
echo -e
echo -e "- Creating the new '.env' file:"
: >.env
# ------------ Django & Celery
echo -e "--- Django & Celery:"
echo -e "# --- Django & Celery ---" >>.env
# Check for production
echo -e " + Django production: \c"
if [[ $DJANGO_PRODUCTION == "true" ]]; then
echo "DJANGO_SETTINGS_MODULE=BiCoN_web.settings.production" >>.env
echo -e "${green}true (in production mode)${reset}"
elif [[ $DJANGO_PRODUCTION == "false" ]]; then
echo "DJANGO_SETTINGS_MODULE=BiCoN_web.settings.development" >>.env
echo -e "${yellow}false (in debug mode)${reset}"
else
echo -e "${red}Error! Unknown parameter \"${DJANGO_PRODUCTION}\" - Valid options are \"true\" or \"false\""
echo -e
echo -e "Exiting now ...${reset}"
exit 1
fi
# Allowed hosts
echo -e " + Django allowed hosts: \c"
if [[ "$DJANGO_ALLOWED_HOSTS" =~ ^\[(\"|\').*(\"|\')\]$ ]]; then
echo "DJANGO_ALLOWED_HOSTS=\"${DJANGO_ALLOWED_HOSTS}\"" >>.env
echo -e "${green}${DJANGO_ALLOWED_HOSTS}${reset}"
else
echo -e "${red}Error! Your input: ${DJANGO_ALLOWED_HOSTS} is not valid. Please check if your input is wrapped in quotes"
echo -e
echo -e "Exiting now ...${reset}"
exit 1
fi
# ------------ Postgres
echo -e "--- Postgres:"
echo -e "\n# --- Postgres ---" >>.env
# Username
echo -e " + Postgres username: \c"
if [ -z $POSTGRES_USER ]; then
echo -e "${yellow}postgres (variable not set - default username)${reset}"
POSTGRES_USER=postgres
else
echo -e "${green}${POSTGRES_USER}${reset}"
fi
echo "POSTGRES_USER=${POSTGRES_USER}" >>.env
# Password
echo -e " + Postgres password: \c"
if [ -z $POSTGRES_PASSWORD ]; then
echo -e "${yellow}postgres (variable not set - default password)${reset}"
POSTGRES_PASSWORD=postgres
else
echo -e "${green}*****${reset}"
fi
echo "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" >>.env
# Database
echo -e " + Postgres database: \c"
if [ -z $POSTGRES_DB ]; then
echo -e "${yellow}postgres (variable not set - default database)${reset}"
POSTGRES_DB=postgres
else
echo -e "${green}${POSTGRES_DB}${reset}"
fi
echo "POSTGRES_DB=${POSTGRES_DB}" >>.env
# ------------ RabbitMQ
echo -e "--- RabbitMQ:"
echo -e "\n# --- RabbitMQ ---" >>.env
# Username
echo -e " + RabbitMQ username: \c"
if [ -z $RABBITMQ_DEFAULT_USER ]; then
echo -e "${yellow}admin (variable not set - default username)${reset}"
RABBITMQ_DEFAULT_USER=admin
else
echo -e "${green}${POSTGRES_USER}${reset}"
fi
echo "RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}" >>.env
# Password
echo -e " + RabbitMQ password: \c"
if [ -z $RABBITMQ_DEFAULT_PASS ]; then
echo -e "${yellow}mypass (variable not set - default password)${reset}"
RABBITMQ_DEFAULT_PASS=mypass
else
echo -e "${green}*****${reset}"
fi
echo "RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}" >>.env
# ------------ NGINX
echo -e "--- Nginx:"
echo -e "\n# --- NGINX ---" >>.env
# Published path
echo -e " + Nginx published path: \c"
if [ -z $NGINX_PUBLISHED_PATH ]; then
echo -e "${yellow}/ (variable not set - default path)${reset}"
else
# Remove leading or trailing slashes
NGINX_PUBLISHED_PATH=${NGINX_PUBLISHED_PATH#/}
NGINX_PUBLISHED_PATH=/${NGINX_PUBLISHED_PATH%/}
echo -e "${green}${NGINX_PUBLISHED_PATH}${reset}"
fi
echo "NGINX_PUBLISHED_PATH=${NGINX_PUBLISHED_PATH}" >>.env
# Port
echo -e " + Nginx published port: \c"
if [ -z $NGINX_PUBLISHED_PORT ]; then
echo -e "${yellow}8081 (variable not set - default port)${reset}"
NGINX_PUBLISHED_PORT=8081
else
echo -e "${green}${NGINX_PUBLISHED_PORT}${reset}"
fi
echo "NGINX_PUBLISHED_PORT=${NGINX_PUBLISHED_PORT}" >>.env
# --- END
echo -e "\n${green}Generating .env from config file successfull${reset}"
fi
# =========================== Starting the containers =============================
# Container need to start / been build when we want to reconfigure them
if [[ $migrate_db == "true" ]] || [[ $collect_static == "true" ]]; then
center "Build and start the docker containers"
docker-compose up -d --build
fi
# =========================== Migrate the database =============================
if [[ $migrate_db == "true" ]]; then
center "Migrate the database"
docker-compose exec web python manage.py migrate --noinput
fi
# =========================== Collect all the static files =============================
if [[ $collect_static == "true" ]]; then
center "Collect all the static files"
docker-compose exec web python manage.py collectstatic --no-input
fi