-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlaradose.sh
executable file
·293 lines (218 loc) · 7.84 KB
/
laradose.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
#!/bin/bash
DOWNLOADER=
GITHUB_URL=https://github.com/AdrienPoupa/laradose
VERSION=1.0.1
install() {
if [ -d ./docker ]; then
fatal "Laradose is already installed"
fi
echo "Installing Laradose to the current directory..."
copy_files
generate_ssl_certificate
post_install_commands
configure
}
copy_files() {
verify_download curl || verify_download wget || fatal 'curl or wget are required for downloading files'
verify_command unzip || fatal 'unzip is required for the installation script'
echo "Downloading..."
download laradose.zip ${GITHUB_URL}/archive/master.zip
echo "Unzipping..."
unzip -q laradose.zip -d laradose
echo "Copying files..."
cp -r ./laradose/laradose-master/docker ./docker
cp ./laradose/laradose-master/docker-compose.yml ./
cp ./laradose/laradose-master/laradose.sh ./laradose.sh
cat ./laradose/laradose-master/.env.example >> ./.env.example
cat ./laradose/laradose-master/.env.example >> ./.env
cat ./laradose/laradose-master/webpack.mix.js >> ./webpack.mix.js
rm -rf ./laradose
rm ./laradose.zip
}
generate_ssl_certificate() {
echo "Generating SSL certificate..."
# Create private and public key
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout ./docker/nginx/keys/server.key -out ./docker/nginx/keys/server.crt -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=localhost" > /dev/null 2>&1
# Create csr file
openssl req -new -key ./docker/nginx/keys/server.key -out ./docker/nginx/keys/server.csr -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=localhost" > /dev/null 2>&1
}
update() {
echo "Be aware that this command will overwrite any modifications you made to Laradose configuration or Docker images."
echo "Please make sure that your folder is versioned so you can revert to the previous state if needed."
read -r -p "Press any key to continue..."
echo "Updating Laradose..."
copy_files
exit 0
}
# Menu inspired by https://serverfault.com/a/298312
additional_containers_menu() {
echo "Select the additional containers you want to enable:"
for i in "${!options[@]}"; do
printf "%d. %s %s\n" $((i+1)) "${options[i]}" "${choices[i]:-}"
done
if [[ "$msg" ]]; then echo "$msg"; fi
}
configure() {
echo "Laradose configuration"
# Export the vars in .env into your shell:
export $(grep -E -v '^#' .env | xargs)
options=("Queue" "Redis" "Laravel Horizon" "phpMyAdmin" "MailHog" "Scheduler")
folders=("queue" "redis" "horizon" "phpmyadmin" "mailhog" "scheduler")
# Fill already selected options
for i in "${!folders[@]}"; do
if [[ $COMPOSE_FILE == *${folders[i]}* ]]; then
choices[i]=$'\u2713'
fi
done
prompt="Type the container number (again to uncheck, ENTER when done): "
while additional_containers_menu && read -rp "$prompt" num && [[ "$num" ]]; do
[[ "$num" != *[![:digit:]]* ]] &&
(( num > 0 && num <= ${#options[@]} )) ||
{ msg="Invalid option: $num"; continue; }
((num--)); msg="${options[num]} was ${choices[num]:+un}enabled"
[[ "${choices[num]}" ]] && choices[num]="" || choices[num]=$'\u2713'
done
compose_file_input="docker-compose.yml:"
for i in "${!options[@]}"; do
[[ "${choices[i]}" ]] && compose_file_input=$compose_file_input"docker/${folders[i]}/docker-compose.override.yml":
done
# Remove last :
compose_file_input=${compose_file_input%?}
replace_in_file "s#COMPOSE_PROJECT_NAME=.*#COMPOSE_PROJECT_NAME=${APP_NAME}#" ./.env
replace_in_file "s#DB_HOST=.*#DB_HOST=mysql#" ./.env
replace_in_file "s#DB_PASSWORD=.*#DB_PASSWORD=${DB_USERNAME}#" ./.env
replace_in_file "s#COMPOSE_FILE=.*#COMPOSE_FILE=${compose_file_input}#" ./.env
env_input "NGINX_HTTPS_PORT" "Nginx HTTPS port"
env_input "NGINX_HTTP_PORT" "Nginx HTTP port"
env_input "DB_PORT" "MySQL port"
env_input "WEBPACK_PORT" "Webpack Development Server port"
if [[ $compose_file_input == *"redis"* ]]; then
replace_in_file "s#REDIS_HOST=.*#REDIS_HOST=redis#" ./.env
env_input "REDIS_PORT" "Redis port"
fi
if [[ $compose_file_input == *"phpmyadmin"* ]]; then
env_input "PHPMYADMIN_PORT" "phpMyAdmin port"
fi
if [[ $compose_file_input == *"mailhog"* ]]; then
replace_in_file "s#MAIL_HOST=.*#MAIL_HOST=mailhog#" ./.env
replace_in_file "s#MAIL_MAILER=.*#MAIL_MAILER=smtp#" ./.env
replace_in_file "s#MAIL_PORT=.*#MAIL_PORT=1025#" ./.env
env_input "MAILHOG_PORT" "MailHog port"
fi
env_input "PHP_VERSION" "PHP Version (7.2, 7.3, 7.4)"
env_input "PHP_SERVER_NAME" "PHP Server Name for xdebug"
env_input "MYSQL_VERSION" "MySQL Version (5.7, 8)"
env_input "USER_ID" "Linux User ID for file permissions (current user: $(id -u))"
env_input "GROUP_ID" "Linux Group ID for file permissions (current group: $(id -g))"
env_input "MIX_MODE" "Mix mode can be one of: watch, hot, dev, prod"
env_input "MIX_BROWSERSYNC" "Enable Browsersync (enabled or disabled)"
if [[ $new_value == "enabled" || $new_value == "" && $MIX_BROWSERSYNC == "enabled" ]]; then
env_input "MIX_BROWSERSYNC_PORT" "Browsersync port"
env_input "MIX_BROWSERSYNC_ADMIN_PORT" "Browsersync admin port"
fi
echo "Configuration complete! Restart docker-compose to apply the changes."
exit 0
}
env_input() {
key=$1
description=$2
read -r -p "$description: [${!key}] " new_value
if [ -n "$new_value" ]; then
replace_in_file "s/${key}=.*/${key}=${new_value}/" ./.env
fi
}
# We cant easily use sed since OS X has a different sed implementation than linux. Opting to use perl instead as it
# should be readily available in most systems. See https://stackoverflow.com/a/22247781/3781678
replace_in_file() {
perl -pi -e "$1" "$2"
}
post_install_commands() {
echo "Applying permissions..."
chown -R "$(id -u)":"$(id -g)" .
chmod -R 755 .
chmod +x docker/scheduler/entrypoint.sh
# Add the https argument to the "hot" npm run option,
# since the webpack option passed in webpack is apparently not enough
replace_in_file "s/--disable-host-check/--disable-host-check --https/" package.json
}
uninstall() {
echo "Uninstalling Laradose..."
rm -rf ./docker
rm ./docker-compose.yml
echo "Laradose was uninstalled successfully!"
echo "You can now remove additional entries from your .env and .env.example files"
exit 0
}
show_menus() {
clear
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " L A R A D O S E"
echo
echo "Author: Adrien Poupa"
echo "Version: "${VERSION}
echo "URL: "${GITHUB_URL}
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "1. Configure"
echo "2. Update"
echo "3. Uninstall"
echo "0. Exit"
}
read_options() {
local choice
read -r -p "Enter choice [1 - 3] " choice
case $choice in
1) configure ;;
2) update ;;
3) uninstall ;;
*) exit 0;;
esac
}
# Credits https://raw.githubusercontent.com/rancher/k3s/master/install.sh
verify_download() {
verify_command
# Set verified executable as our downloader program and return success
DOWNLOADER=$1
return 0
}
# --- verify existence of a command executable ---
verify_command() {
# Return failure if it doesn't exist or is no executable
[ -x "$(command -v "$1")" ] || return 1
return 0
}
download() {
[ $# -eq 2 ] || fatal 'download needs exactly 2 arguments'
case $DOWNLOADER in
curl)
curl -o "$1" -sfL "$2"
;;
wget)
wget -qO "$1" "$2"
;;
*)
fatal "Incorrect executable '$DOWNLOADER'"
;;
esac
# Abort if download command failed
[ $? -eq 0 ] || fatal 'Download failed'
}
fatal() {
echo '[ERROR]' "$@" >&2
exit 1
}
if [ "$1" == "--install" ]
then
install
exit 0
fi
while true
do
verify_command docker || fatal 'Docker is required for Laradose'
verify_command docker-compose || fatal 'docker-compose is required for Laradose'
verify_command perl || fatal 'Perl is required for Laradose'
if ! [ -f ./.env ]; then
fatal 'You must have a .env file'
fi
show_menus
read_options
done