-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.sh
executable file
·328 lines (263 loc) · 8.23 KB
/
boot.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
#!/bin/bash
# Boot - Fast & reliable server manager for Nginx, MySql, and Laravel.
#
# Description:
# Automates nginx tasks: list hosts, add, remove, enable and disable.
# Create Mysql databases, users and grant permissions.
# Craft new laravel projects with .env file ready to go.
#
# Usage: boot [-hlredR] [-a type] <domain>
#
# Options:
# -h : this help
# -l : list hosts
# -r : reload nginx
# -e : enable a host
# -d : disable a host
# -R : remove a host (can really damage your system)
# -a type : create a new host type: site, laravel (default)
#
# Examples:
# boot -e domain.dev : enables domain.dev
# boot -R domain.dev : removes domain.dev
# boot -a abc.dev : creates laravel site at /var/www/abc.dev
# boot -a site xyz.api : creates php site at /var/www/xyz.api
#
# Author: Bruno Torrinha <http://www.torrinha.com>
#
# Inspired on
# - https://github.com/pyghassen/nginx_modsite
# - Laravel Homestead provisioning scripts
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# tab width
tabs 4
# clear
# Include common scripts
. $DIR/inc/messages.sh
# Load variables
while read line; do export "$line";
done < $DIR/boot.env
##########
# Defaults
#######################################
HOST_NAME="$2"
HOST_PATH_FULL="$HOST_PATH/$HOST_NAME"
NGINX_CONF_FILE="$(awk -F= -v RS=' ' '/conf-path/ {print $2}' <<< $(nginx -V 2>&1))"
NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
NGINX_RELOAD_ON_CHANGE=true
###########
# Functions
#######################################
_err () {
_error "$1"
usage "$2"
}
check_root_user() {
if [ "$(whoami)" != 'root' ]; then
_err "Use sudo. You need permission to run $0."
fi
}
load_host() {
# Sanitize DB name
name=${HOST_NAME//_/} # remove underscores
name=${name//./} # remove dots
name=${name//[^a-zA-Z0-9_]/} # remove non alphanumeric or underscores
name=`echo -n $name | tr A-Z a-z` # make lowercase
DB_NAME=$name # DB Name Sanitized
DB_USERNAME="$name"_"$DB_USERNAME" # DB Username
DB_PASSWORD=`pwgen -s 12 1` # DB Password
}
confirm_host_settings() {
echo "Confirm Settings..."
echo ""
echo "➜ Nginx"
echo "+----------------------------------------"
echo "| Host : $HOST_NAME"
echo "| Path : $HOST_PATH_FULL"
echo "+----------------------------------------"
echo " "
echo "➜ MySQL DB"
echo "+----------------------------------------"
echo "| Name : $DB_NAME"
echo "| User : $DB_USERNAME"
echo "| Pass : $DB_PASSWORD"
echo "+----------------------------------------"
echo ""
read -p "➜ Proceed with create? (Y/n): " confirm
if [[ "$confirm" = "n" || "$confirm" = "N" ]]; then
_alert "Configuration aborted"
exit 0
fi
}
save_host_config() {
cat > $DIR/hosts/$HOST_NAME.cnf << EOF
date_added=$(date)
host_name=$HOST_NAME
host_path=$HOST_PATH_FULL
database_name=$DB_NAME
database_user=$DB_USERNAME
database_pass=$DB_PASSWORD
EOF
echo -e "\nConfig saved at $DIR/hosts/$HOST_NAME.cnf\n"
echo -e "Build something great: http://$HOST_NAME\n"
}
#######
# Nginx
#######################################
ngx_sites() {
case "$1" in
available) dir="$NGINX_SITES_AVAILABLE";;
enabled) dir="$NGINX_SITES_ENABLED";;
esac
for file in $dir/*; do
echo -e "\t${file#*$dir/}"
done
}
ngx_reload() {
if [ "$NGINX_RELOAD_ON_CHANGE" = true ]; then
ngx_reload_now
fi
}
ngx_reload_now() {
# invoke-rc.d php7.0-fpm restart
invoke-rc.d nginx reload
_success "Nginx reloaded"
}
ngx_list() {
_success "Available hosts"
ngx_sites "available"
_success "Enabled hosts"
ngx_sites "enabled"
}
ngx_enable_host() {
[[ ! "$HOST_NAME" ]] && _err "Domain was not submitted."
[[ ! -e "$NGINX_SITES_AVAILABLE/$HOST_NAME" ]] &&
_err "$HOST_NAME site does not appear to exist."
[[ -e "$NGINX_SITES_ENABLED/$HOST_NAME" ]] &&
_err "$HOST_NAME site appears to already be enabled"
ln -sf "$NGINX_SITES_AVAILABLE/$HOST_NAME" "$NGINX_SITES_ENABLED/$HOST_NAME"
_success "Site $HOST_NAME enabled"
ngx_reload
}
ngx_disable_host() {
[[ ! "$HOST_NAME" ]] && _err "Domain name not submitted."
[[ ! -e "$NGINX_SITES_AVAILABLE/$HOST_NAME" ]] &&
_err "$HOST_NAME site does not appear to be \'available\'. - Not Removing"
[[ ! -e "$NGINX_SITES_ENABLED/$HOST_NAME" ]] &&
_err "$HOST_NAME site already disabled." 0
rm -rf "$NGINX_SITES_ENABLED/$HOST_NAME"
_success "Site $HOST_NAME disabled"
ngx_reload
}
ngx_add_host() {
sudo -v
#####################
# Prepare Variables #
#####################
# Load host settings
case "$1" in
site|laravel)
HOST_TYPE="$1"
HOST_NAME="$2";;
esac
HOST_PATH_FULL="$HOST_PATH/$HOST_NAME"
load_host
# Check if already installed
[[ -e "$NGINX_SITES_AVAILABLE/$HOST_NAME" ]] &&
_err "$HOST_NAME already exists"
confirm_host_settings
# Create Mysql database, User and assign permission
bash $DIR/scripts/create-mysql.sh "$DB_ADD_DEL" "$DB_NAME" "$DB_USERNAME" "$DB_PASSWORD"
# Create host
if [ "$HOST_FILES_ADD_DEL" ]; then
case "$HOST_TYPE" in
site) bash $DIR/scripts/create-site.sh "$HOST_NAME" "$HOST_PATH" "$NGINX_CONF_DIR";;
laravel) bash $DIR/scripts/create-laravel.sh "$HOST_NAME" "$HOST_PATH" "$NGINX_CONF_DIR";;
esac
fi
# Update .env file variables
sed -e "s/^APP_URL=.*/APP_URL=http:\/\/${HOST_NAME}/" -e "s/^DB_DATABASE=.*/DB_DATABASE=${DB_NAME}/" -e "s/^DB_USERNAME=.*/DB_USERNAME=${DB_USERNAME}/" -e "s/^DB_PASSWORD=.*/DB_PASSWORD=${DB_PASSWORD}/" $HOST_PATH_FULL/.env > $HOST_PATH_FULL/_tmp_env && mv $HOST_PATH_FULL/_tmp_env "$HOST_PATH_FULL/.env"
# Create /etc/hosts
if [ "$HOST_ETC_ADD_DEL" ]; then
bash $DIR/scripts/hosts-etc.sh "add" "$HOST_NAME" "$HOST_IP"
fi
ngx_reload
save_host_config
exit 0
}
ngx_remove_host() {
sudo -v
[[ ! "$HOST_NAME" ]] && _err "Domain name not submitted."
[[ ! -e "$NGINX_SITES_AVAILABLE/$HOST_NAME" ]] &&
_err "$HOST_NAME site is not configured."
# Load host variables
load_host
# Remove host
if [ "$HOST_FILES_ADD_DEL" ]; then
bash $DIR/scripts/remove-host.sh "$HOST_PATH" "$HOST_NAME" "$NGINX_CONF_DIR"
fi
# Remove database
bash $DIR/scripts/create-mysql.sh "$DB_ADD_DEL" "$DB_NAME" "$DB_USERNAME"
# Update hosts
if [ "$HOST_ETC_ADD_DEL" ]; then
bash $DIR/scripts/hosts-etc.sh "remove" "$HOST_NAME" "$HOST_IP"
fi
ngx_reload
_success "Host removed"
exit 0
}
############
# Help About
#######################################
header() {
cat <<"HEADER"
_ _
| | | |
| |__ ___ ___ | |_
| '_ \ / _ \ / _ \| __|
| |_) | (_) | (_) | |_
|_.__/ \___/ \___/ \__|
Fast and reliable web server manager.
Automates nginx tasks: list hosts, add, remove, enable and disable.
Creates Mysql databases, users and grant permissions.
Can bootstrap new laravel projects.
Nginx / Mysql / Laravel
@ 2017 Bruno Torrinha <http://www.github.com/akazorg/laravel-boot>
HEADER
}
usage() {
[[ "$1" = "1" ]] && header;
cat <<"USAGE"
Usage: boot [-hlredR] [-a type] <domain>
Options:
-h : this help
-l : list hosts
-r : reload nginx
-e : enable host
-d : disable host
-R : remove host (can really damage your system)
-a type : create host type: site, laravel (default)
Examples:
boot -e domain.dev : enables domain.dev
boot -R domain.dev : removes domain.dev
boot -a abc.dev : creates laravel site at /var/www/abc.dev
boot -a site xyz.api : creates php site at /var/www/xyz.api
USAGE
exit 1;
}
#############
# Start Block
#######################################
check_root_user
case "$1" in
-h|'') usage 1;;
-r) ngx_reload_now;;
-l) ngx_list;;
-e) ngx_enable_host;;
-d) ngx_disable_host;;
-R) ngx_remove_host;;
-a) ngx_add_host $2 $3 $4;;
*) _err "No options selected" 1;;
esac