-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.bash
More file actions
275 lines (246 loc) · 6.75 KB
/
init.bash
File metadata and controls
275 lines (246 loc) · 6.75 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
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
#!/bin/bash
# CONSTS
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# HELPER FUNCTIONS
prompt_user() {
local message=$1
# Ask for confirmation
echo -e "${YELLOW}${message}${NC}"
read choice
case "$choice" in
[yY] | [yY][eE][sS])
# operation confirmed
return 0
;;
[nN] | [nN][oO])
# operation canceled
return 1
;;
*)
# invalid choice
return 2
;;
esac
}
log_info() {
echo -e "${GREEN}$1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# MAIN SCRIPT
log_info "Configuring docker"
# install docker
while true; do
prompt_user "Do you want to install docker? [Y/n] "
case $? in
0)
bash ./commands/install_docker.bash
break ;;
1) break ;;
*) continue ;;
esac
done
# install docker compose
while true; do
prompt_user "Do you want to install docker compose? [Y/n] "
case $? in
0)
bash ./commands/install_docker_compose.bash
break ;;
1) break ;;
*) continue ;;
esac
done
# add current user to docker group
while true; do
prompt_user "Do you want to add current user to docker group (for running docker commands without sudo)? [Y/n] "
case $? in
0)
bash ./commands/add_user_to_docker_group.bash $SUDO_USER
break ;;
1) break ;;
*) continue ;;
esac
done
# an empty array to store selected services
declare -A services
# services selection
log_info "Select services"
while true; do
prompt_user "Do you want to add laravel? [Y/n] (no) "
case $? in
0)
services["laravel"]=1
break ;;
*)
services["laravel"]=0
break ;;
esac
done
while true; do
prompt_user "Do you want to add flask? [Y/n] (no) "
case $? in
0)
services["flask"]=1
break ;;
*)
services["flask"]=0
break ;;
esac
done
while true; do
prompt_user "Do you want to add vue? [Y/n] (no) "
case $? in
0)
services["vue"]=1
break ;;
*)
services["vue"]=0
break ;;
esac
done
while true; do
prompt_user "Do you want to add redis? [Y/n] (no) "
case $? in
0)
services["redis"]=1
break ;;
*)
services["redis"]=0
break ;;
esac
done
while true; do
prompt_user "Do you want to add postgres? [Y/n] (no) "
case $? in
0)
services["pgsql"]=1
break ;;
*)
services["pgsql"]=0
break ;;
esac
done
# Getting application name
log_info "Docker configured, creating application..."
read -p "Enter application name (default: app) => " app_name
# If no input is provided, set a default value
if [ -z "$app_name" ]; then
app_name="app"
fi
# Getting application path
while true; do
read -p "Enter application path (default: /home/$SUDO_USER/$app_name) => " app_path
# If no input is provided, set a default value
if [ -z "$app_path" ]; then
app_path="/home/$SUDO_USER/$app_name"
fi
if [ -d "$app_path" ]; then
log_error "Directory already exists."
else
break
fi
done
# Create app path
log_info "Creating $app_path"
mkdir $app_path
log_info "$app_path created"
# Copy config files
# Common
cp ./config/docker-compose.yml "$app_path/docker-compose.yml"
# Nginx
mkdir -p "$app_path/nginx/conf.d"
if [[ "${services["laravel"]}" -eq 1 ]]; then
# Create fresh laravel project
while true; do
prompt_user "Create fresh Laravel project at $app_path/laravel? [Y/n] "
case $? in
0)
log_info "Creating fresh Laravel project"
bash ./commands/create_fresh_laravel_project.bash $app_path
log_info "Fresh Laravel project created"
break ;;
1) break ;;
*) continue ;;
esac
done
# Copy config files
cp ./config/services/laravel/Dockerfile "$app_path/laravel/Dockerfile"
cp ./config/services/laravel/laravel.conf "$app_path/nginx/conf.d/laravel.conf"
cp ./config/services/laravel/docker-compose.yml "$app_path/laravel/docker-compose.yml"
log_info "Laravel configured"
fi
if [[ "${services["flask"]}" -eq 1 ]]; then
# Copy config files
cp -a ./config/services/flask "$app_path/flask"
cp ./config/services/flask/flask.conf "$app_path/nginx/conf.d/flask.conf"
cp ./config/services/flask/docker-compose.yml "$app_path/flask/docker-compose.yml"
log_info "Flask configured"
fi
if [[ "${services["vue"]}" -eq 1 ]]; then
# Create fresh vue project
while true; do
prompt_user "Create fresh Vue.js project at $app_path/vue? [Y/n] "
case $? in
0)
log_info "Creating fresh Vue.js project"
bash ./commands/create_fresh_vue_project.bash $app_path
log_info "Fresh Vue.js project created"
break ;;
1) break ;;
*) continue ;;
esac
done
# Copy config files
cp ./config/services/vue/Dockerfile "$app_path/vue/Dockerfile"
cp ./config/services/vue/nginx.conf "$app_path/vue/nginx.conf"
cp ./config/services/vue/vue.conf "$app_path/nginx/conf.d/vue.conf"
cp ./config/services/vue/docker-compose.yml "$app_path/vue/docker-compose.yml"
log_info "Vue configured"
fi
if [[ "${services["redis"]}" -eq 1 ]]; then
# Copy config files
mkdir "$app_path/redis"
cp ./config/services/redis/.env "$app_path/redis/.env"
cp ./config/services/redis/docker-compose.yml "$app_path/redis/docker-compose.yml"
log_info "Redis configured"
fi
if [[ "${services["pgsql"]}" -eq 1 ]]; then
# Copy config files
mkdir "$app_path/pgsql"
cp ./config/services/pgsql/.env "$app_path/pgsql/.env"
cp ./config/services/pgsql/docker-compose.yml "$app_path/pgsql/docker-compose.yml"
log_info "Postgres configured"
fi
cd $app_path
# Start the containers
log_info "Starting the containers"
# Command to build
compose_cmd="docker compose -f docker-compose.yml"
# Loop through the associative array and add -f parameters for services with value 1
for service in "${!services[@]}"; do
if [[ "${services[$service]}" -eq 1 ]]; then
compose_cmd+=" -f ./$service/docker-compose.yml"
fi
done
# Add the final part of the command
compose_cmd+=" up -d"
# Run the composed command
eval "$compose_cmd"
log_info "Containers started"
if [[ "${services["laravel"]}" -eq 1 ]]; then
# Install Laravel dependencies todo remove from here
log_info "Installing composer dependencies"
docker exec app_laravel composer install
log_info "Composer dependencies installed"
# Run Laravel migrations
log_info "Running migrations"
docker exec app_laravel php artisan migrate
log_info "Migrations ran"
fi
# todo chmod storage test
# todo ssl