This repository was archived by the owner on Mar 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_setup.sh
More file actions
142 lines (120 loc) · 6.02 KB
/
run_setup.sh
File metadata and controls
142 lines (120 loc) · 6.02 KB
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
#!/bin/bash
export docker_err=100
export syspath_err=101
# Get the absolution of path
abs_path_file_execute=$(realpath "$0")
abs_path_folder_script=$(dirname "$abs_path_file_execute")
abs_path_folder_root=$(dirname "$(dirname "$abs_path_file_execute")")
abs_path_folder_docker="$abs_path_folder_root""/docker/"
abs_path_folder_src="$abs_path_folder_root""/src/"
abs_path_folder_conf="$abs_path_folder_docker/conf"
# Bash Menu Script Example
echo "---------MENU------------"
PS3='Please enter your choice: '
options=("Setup container" "Destroy container" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Setup container")
# -------------------------- GENERATE SELF_SIGN SSL CERTIFICATE --------------------------
# Go to docker/conf
cd $abs_path_folder_conf
# Check input with domain
read -p "Input domain you want to create with container: " DOMAIN
if [ -z "$DOMAIN" ]; then
echo "Usage: $(basename $0) with <domain>"
exit 11
fi
# Check Error
fail_if_error() {
[ $1 != 0 ] && {
unset PASSPHRASE
exit 10
}
}
# Generate a passphrase to create Password random
export PASSPHRASE=$(head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 128; echo)
# Certificate details; replace items in angle brackets with your own info
subj="
C=VN
ST=blah
O=Blah
localityName=vietnam
commonName=$DOMAIN
organizationalUnitName=Blah
emailAddress=admin@example.com
"
# Generate the server private key
openssl genrsa -des3 -out "$DOMAIN.key" -passout env:PASSPHRASE 2048
fail_if_error $?
# Generate the CSR
openssl req \
-new \
-batch \
-subj "$(echo -n "$subj" | tr "\n" "/")" \
-key "$DOMAIN.key" \
-out "$DOMAIN.csr" \
-passin env:PASSPHRASE
fail_if_error $?
cp "$DOMAIN.key" "$DOMAIN.key.org"
fail_if_error $?
# Strip the password so we don't have to type it every time we restart Apache
openssl rsa -in "$DOMAIN.key.org" -out "$DOMAIN.key" -passin env:PASSPHRASE
fail_if_error $?
# Generate the Certificate (good for 10 years)
openssl x509 -req -days 3650 -in "$DOMAIN.csr" -signkey "$DOMAIN.key" -out "$DOMAIN.crt"
fail_if_error $?
# Go out docker/conf
cd $abs_path_folder_root
# -------------------------- CREATE IMAGE --------------------------
# Get path of function try/catch to catch ERROR
source "$abs_path_folder_script/try_catch.sh"
# Go to docker/
cd "$abs_path_folder_docker" || throw $syspath_err
# Copy src/ into docker/
cp -r "$abs_path_folder_src" . || throw $syspath_err
# Create WEB images with specified name
docker build -t devopsorient.azurecr.io/webpage8001:latest -f Dockerfile.web . || throw $docker_err
docker build -t devopsorient.azurecr.io/webpage8002:latest -f Dockerfile.web . || throw $docker_err
docker build -t devopsorient.azurecr.io/webpage8003:latest -f Dockerfile.web . || throw $docker_err
docker build -t devopsorient.azurecr.io/webpage8004:latest -f Dockerfile.web . || throw $docker_err
# Remove docker/src/
rm -rf src/ || throw $syspath_err
# Create NGINX image
docker build -t devopsorient.azurecr.io/nginx_alb:latest --build-arg domain_key=$DOMAIN.key --build-arg domain_crt=$DOMAIN.crt -f Dockerfile.nginx . || throw $docker_err
# Go to docker/conf to delete SSL after we create IMAGE
cd "$abs_path_folder_conf" || throw $syspath_err
rm $(ls --ignore=nginx.conf) || throwErrors $syspath_err
# Go out docker/conf
cd "$abs_path_folder_root" || throw $syspath_err
# --------------------- DELETE OLD IMAGE IN REGISTRY -------------------
az acr repository delete --name devopsorient --image nginx_alb:latest -y 2> /dev/null || true
az acr repository delete --name devopsorient --image webpage8001:latest -y 2> /dev/null || true
az acr repository delete --name devopsorient --image webpage8002:latest -y 2> /dev/null || true
az acr repository delete --name devopsorient --image webpage8003:latest -y 2> /dev/null || true
az acr repository delete --name devopsorient --image webpage8004:latest -y 2> /dev/null || true
# --------------------- PUSH NEW IMAGE TO REGISTRY ---------------------
docker push devopsorient.azurecr.io/webpage8001:latest || throw $docker_err
docker push devopsorient.azurecr.io/webpage8002:latest || throw $docker_err
docker push devopsorient.azurecr.io/webpage8003:latest || throw $docker_err
docker push devopsorient.azurecr.io/webpage8004:latest || throw $docker_err
docker push devopsorient.azurecr.io/nginx_alb:latest || throw $docker_err
# -------------------------- CREATE CONTAINER --------------------------
docker-compose up -d || true
;;
"Destroy container")
# Remove container & image & network
# docker kill "$(docker ps -a | awk {'print$1'})" || true
docker kill $(docker ps -aq) 2> /dev/null || true
docker container prune --force 2> /dev/null
docker rmi nginx_alb:latest 2> /dev/null || true
docker rmi $(docker image list | grep webpage | awk {'print$1'}) 2> /dev/null || true
docker rmi $(docker image list | grep azurecr | awk {'print$3'}) 2> /dev/null || true
docker network rm $(docker network list | grep my_network | awk {'print$1'}) 2> /dev/null || true
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done