-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.sh
executable file
·344 lines (271 loc) · 14.2 KB
/
workflow.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
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
#!/usr/bin/env bash
source ./shell/config.sh
source ./shell/general/os.sh
source ./shell/general/functions.sh
#########################################################
# Command Hierarchy
#
# Level $1
# 'docker' - Docker specific commands
# 'access' - Drop into a container using bash
# 'setup' - Specific logic for setting up the environment
# 'task' - Random One off tasks
#
#########################################################
# Parse Arguments
# e.g. bash workflow.sh tasks test_env
#
if [[ $# -gt 0 ]]; then
if [[ "$1" == "docker" ]]; then
if [[ "$2" == "config" ]]; then
docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" config
elif [[ "$2" == "up" ]]; then
docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" up --force-recreate
elif [[ "$2" == "up-d" ]]; then
docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" up --force-recreate -d
elif [[ "$2" == "clean" ]]; then
if [[ "$3" == "all" ]]; then
echo "Complete clean started..."
docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" down \
&& docker system prune -f \
&& docker volume prune -f \
&& docker ps -a \
&& docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" ps
elif [[ "$3" == "containers" ]]; then
echo "Container cleaning started..."
# shellcheck disable=SC2046
docker rm $(docker ps -aq) -f
docker ps -a
elif [[ "$3" == "images" ]]; then
echo "Image cleaning started..."
# shellcheck disable=SC2046
docker rmi $(docker images -aq) -f
docker images -a
fi
elif [[ "$2" == "status" ]]; then
# shellcheck disable=SC2046
printf "\n"
printf "============================================ \n"
printf "Docker Environment Status \n"
printf "============================================ \n"
printf "\n"
printf "Docker Volumes \n"
printf "============================================ \n"
docker volume ls || true
printf "\n"
printf "Docker Images \n"
printf "============================================ \n"
docker images -a || true
printf "\n"
printf "Docker Stats \n"
printf "============================================ \n"
docker ps -a || true
printf "\n"
printf "Docker Compose Stats \n"
printf "============================================ \n"
docker compose --env-file="$DEFAULT_DOCKER_COMPOSE_ENV_FILE" ps || true
printf "\n"
printf "Docker Stats: System & Hard drive \n"
printf "============================================ \n"
docker system df || true
printf "\n"
printf "Docker Stats: CPU, Mem, Bandwidth \n"
printf "============================================ \n"
# shellcheck disable=SC2046
docker stats --no-stream $(docker ps | awk '{if(NR>1) print $NF}') || true
printf "\n"
printf "Docker IPs: \n"
printf "============================================ \n"
# shellcheck disable=SC2046
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) || true
fi
elif [[ "$1" == "access" ]]; then
if [[ "$2" == "webapp" ]]; then
docker exec -it -w /var/www/html webapp bash
elif [[ "$2" == "redis" ]]; then
docker exec -it redis redis-cli
elif [[ "$2" == "mysql" ]]; then
docker exec -it mysqldb mysql-cli
fi
elif [[ "$1" == "setup" ]]; then
if [[ "$2" == "webapp" ]]; then
# Set file permissions
docker exec -it -w /var/www/html webapp bash file_permissions.sh
# Run setup scripts
docker exec -it -w /var/www/html webapp bash build.sh local
fi
elif [[ "$1" == "tasks" ]]; then
if [[ "$2" == "app" ]]; then
if [[ "$3" == "tail_all" ]]; then
docker exec -it -w /var/www/html webapp tail -f /var/log/nginx/error.log /var/log/nginx/access.log
elif [[ "$3" == "certs_delete" ]]; then
echo "Removing old certs for app"
rm -Rf ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/*
elif [[ "$3" == "certs_create" ]]; then
echo ""
echo "############################################"
echo "Removing old certs for app"
echo "############################################"
rm -Rf ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/*
############################################
# Create a key and certificate pair:
############################################
echo ""
echo "############################################"
echo "Create a key and certificate pair"
echo "############################################"
sudo openssl req \
-x509 -nodes -days 365 -newkey rsa:2048 \
-subj "/CN=*.bysavi.dev" \
-config ~/webapp/cubicle/volumes/data/nginx/webapp/certs/config/v3.cnf \
-keyout ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/self-signed.key \
-out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/self-signed.crt
echo ""
echo "############################################"
echo "Create a Diffie-Hellman Key Pair"
echo "############################################"
sudo openssl dhparam -out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/dhparam.pem 128
############################################
# Check if your cert and key match
############################################
echo ""
echo "############################################"
echo "Check if your certificate and private key belong to each other"
echo "############################################"
openssl rsa -noout -modulus -in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/self-signed.key | openssl md5
openssl x509 -noout -modulus -in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/self-signed.crt | openssl md5
echo ""
echo "############################################"
echo "Add the self-signed certificate to the trusted root store"
echo "############################################"
sudo security add-trusted-cert \
-d -r trustRoot \
-k /Library/Keychains/System.keychain ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/self-signed.crt
elif [[ "$3" == "certs_create_old1" ]]; then
echo ""
echo "############################################"
echo "Removing old certs for app"
echo "############################################"
rm -Rf ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/*
############################################
# Become a Certificate Authority
############################################
echo ""
echo "############################################"
echo "Generate private key"
echo "############################################"
#openssl genrsa -des3 \
openssl genrsa \
-out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.key 2048
echo ""
echo "############################################"
echo "Generate root certificate"
echo "############################################"
openssl req -x509 -new -nodes \
-key ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.key \
-sha256 \
-days 1000 \
-subj "/C=US/ST=CA/L=LA/O=Savi LLC - Employee/OU=Dev Team/CN=Savi LLC - Employee/[email protected]" \
-out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.pem
############################################
# Create CA-signed certs
############################################
echo ""
echo "############################################"
echo "Create a certificate-signing request"
echo "############################################"
openssl req -new -sha256 -nodes \
-out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.csr \
-newkey rsa:2048 \
-subj "/C=US/ST=CA/L=LA/O=Savi LLC - Employee/OU=Dev Team/CN=Savi LLC - Employee/[email protected]" \
-keyout ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.key
echo ""
echo "############################################"
echo "Check if your certificate and private key belong to each other"
echo "############################################"
openssl rsa -noout -modulus -in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.key | openssl md5
openssl req -noout -modulus -in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.csr | openssl md5
openssl x509 -noout -modulus -in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.crt | openssl md5
echo ""
echo "############################################"
echo "Create the signed certificate"
echo "############################################"
openssl x509 -req \
-in ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.csr \
-CA ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.pem \
-CAkey ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.key \
-CAcreateserial \
-out ~/webapp/cubicle/volumes/data/nginx/webapp/certs/actual/ssl.crt \
-days 1000 -sha256 \
-extfile ~/webapp/cubicle/volumes/data/nginx/webapp/certs/config/v3.ext
fi
elif [[ "$2" == "host" ]]; then
if [[ "$3" == "setup_rev_proxy" ]]; then
printf "\n"
echo "======================================================="
echo "Installing and Setting up Nginx on Host for Reverse Proxies"
echo "======================================================="
printf "\n"
if [[ "$(uname)" == "Darwin" ]]; then
# Do something under Mac OS X platform
#########################################################
echo "You are working on a Mac host machine"
echo "It is expected that you have Xcode and Homebrew installed."
printf "\n"
echo "Brew: Installing Nginx."
echo "----------------------------------------"
brew --version
brew analytics off
brew install nginx
nginx -v
nginx -t
printf "\n"
echo "Configuring Nginx."
echo "----------------------------------------"
mkdir -p /home/academystack/PhpstormProjects/work/OgeleIQ/IoT/academystack/technology/ecosystem/docker/environment/v0_2024_03_16/logs/academystack.test || true
cp -a stubs/rev_proxy_nginx.conf.stub /etc/nginx/nginx_academystack.conf
sed -i -e "s/{{DEVICE_HOST_USERNAME}}/$4/g" /etc/nginx/nginx_academystack.conf
sed -i -e "s/{{APP_BY_SAVI_WEB_NGINX_PORT}}/$5/g" /etc/nginx/nginx_academystack.conf
printf "\n"
echo "Brew: Restarting Nginx."
echo "----------------------------------------"
nginx -t
brew services restart nginx
printf "\n"
elif [[ "$(expr substr "$(uname -s)" 1 5)" == "Linux" ]]; then
# Do something under GNU/Linux platform
#########################################################
echo "You are working on a Linux host machine"
printf "\n"
fi
fi
elif [[ "$2" == "test_env" ]]; then
echo Welcome user ${USERNAME} with email: ${EMAIL}.
echo You are currently working in "$(get_current_os)".
printf "\n"
echo "======================================================="
echo "Let's make sure the environment is what we expect it to be"
echo "======================================================="
printf "\n"
printf "===============================\n"
printf "= Configs\n"
printf "===============================\n"
printf "CURRENT_OS = "
get_current_os
printf "CURRENT_OS_FLAVOR = "
get_current_os_flavor
printf "CURRENT_OS_FLAVOR_BIT = "
get_current_os_flavor_bit
printf "CURRENT_OS_FLAVOR_VERSION = "
get_current_os_flavor_version
printf "\n"
printf "===============================\n"
printf "= Next\n"
printf "===============================\n"
printf "thing = "
printf "\n\n"
fi
fi
else
echo "The following top level arguments are valid: docker, access, setup, task"
fi