This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpg.sh
executable file
·326 lines (255 loc) · 9.26 KB
/
npg.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
#!/bin/bash
#
# NPG: simple web project manager
#
# You nginx vhost directory
NGINX_VHOSTS_DIR="/etc/nginx/sites-available"
# You working directory
WORKING_DIR="/home/nicolas/work"
# The command to restart nginxa
RESTART_NGINX="service nginx restart"
# Username to use when creating directories
USERNAME="nicolas"
# PHP version
PHP_VERSION="7.1"
# Default TLD to use
TLD="localhost"
# Parse arguments
declare -a args=();
while [[ $# -gt 0 ]]
do
key="$1"
args=("${args[@]}" ${key})
case $key in
-h|--help)
HELP=true
shift
;;
*)
# unknown option
;;
esac
shift
done
# Define simple colors helper
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
bold=$'\e[1m'
end=$'\e[0m'
# If no args display help message
if [[ ${#args[@]} = 0 ]]; then
HELP=true
fi
# Print help if request
if [ ${HELP} ]; then
cat << "EOF"
_______ ______ _______
| | | __ \ __|
| | __/ | |
|__|____|___| |_______|
EOF
printf "%s\n\n" "${grn}NPG:${end} NGINX Project Generator"
printf "%s\n" "${yel}Usage:${end}"
printf " %s\n\n" "npg [options] [arguments]"
printf "%s\n" "${yel}Options:${end}"
printf " %-25s %s\n\n" "${grn}-h, --help${end}" "Display this help message"
printf "%s\n" "${yel}Available commands:${end}"
printf " %-25s %s\n" "${grn}list${end}" "List all current projects"
printf " %-25s %s\n" "${grn}test${end}" "Test all projects status"
printf " %-25s %s\n" "${grn}create${end}" "Create a new project"
printf " %-25s %s\n" "${grn}rename${end}" "Rename a project"
printf " %-25s %s\n" "${grn}delete${end}" "Delete a project"
exit
fi
# List
if [[ " ${args[*]} " == *" list "* ]]; then
vhosts=($(ls ${NGINX_VHOSTS_DIR}))
printf "%s\n" "${yel}List of current projects:${end}"
printf " %s\n" "${grn}${vhosts[@]}${end}"
exit
fi
# Test
if [[ " ${args[*]} " == *" test "* ]]; then
printf "%s\n" "${yel}List of current projects:${end}"
printf " ${bold}%-40s %-40s %-40s %-40s${end}\n" "Project" "NGINX vhost" "Working directory" "Status"
sites_enabled=$(ls "$NGINX_VHOSTS_DIR/../sites-enabled")
projects_directory=$(ls "$WORKING_DIR")
for project in $(ls ${NGINX_VHOSTS_DIR}); do
project_name=${project%%.*}
project_enabled='no'
project_directory='no'
if [[ ${sites_enabled[@]} =~ ${project} ]]; then
project_enabled='yes'
fi
if [[ ${projects_directory[@]} =~ ${project_name} ]]; then
project_directory='yes'
fi
status=$(curl -I --stderr /dev/null http://${project} | head -1 | cut -d' ' -f2)
if [ -z "${status}" ]; then
status="Could not resolve host"
fi
[[ ${project_enabled} = 'yes' ]] && c1="${grn}" || c1="${red}"
[[ ${project_directory} = 'yes' ]] && c2="${grn}" || c2="${red}"
[[ ${status} = "200" ]] && c3="${grn}" || c3="${red}"
printf " ${grn}%-40s${end} ${c1}%-40s${end} ${c2}%-40s${end} ${c3}%-40s${end}\n" "${project}" "${project_enabled}" "${project_directory}" "${status}"
done
exit
fi
# Create
if [[ " ${args[*]} " == *" create "* ]]; then
if [ -z "${args[1]}" ]; then
printf "%s\n" "${red}[ERROR] You need to specify a project name.${end}"
printf "%s\n" "${yel}Usage:${end}"
printf " %s\n\n" "npg create project.${TLD}"
printf "%s\n" "Type ${grn}npg --help${end} to display the help message"
exit
fi
projects=($(ls ${NGINX_VHOSTS_DIR}))
project=${args[1]}
if [[ ${projects[@]} =~ ${project} ]]; then
printf "%s\n" "${red}[ERROR] Project ${project} already exist.${end}"
printf "%s\n" "Type ${grn}npg list${end} to list all projects"
exit
fi
printf "%s\n" "${yel}Creating new ${args[1]} project:${end}"
printf " ${grn}%-40s${end} %-40s\n" "Creating vhost:${end}" "${NGINX_VHOSTS_DIR}/${project}"
touch ${NGINX_VHOSTS_DIR}/${project}
cat <<< "
#
# ${args[1]}
#
server {
listen 80;
server_name ${project} *.${project};
root /var/www/work/${project}/public;
index index.php index.html;
# Logging
access_log /var/log/nginx/${project}.access.log;
error_log /var/log/nginx/${project}.error.log;
# Allow access
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
}" > ${NGINX_VHOSTS_DIR}/${project}
printf " ${grn}%-40s${end} %-40s\n" "Activating vhost with symbolic link:${end}" "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
if [[ -h "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}" ]]; then
rm "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
fi
ln -s "${NGINX_VHOSTS_DIR}/${project}" "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
if [[ -d "${WORKING_DIR}/${project}" ]]; then
printf " ${grn}%-40s${end} %-40s\n" "Working directory already exist:${end}" "${WORKING_DIR}/${project}"
else
printf " ${grn}%-40s${end} %-40s\n" "Creation of the working directory:${end}" "${WORKING_DIR}/${project}"
mkdir "${WORKING_DIR}/${project}"
chown -R "${USERNAME}:${USERNAME}" "${WORKING_DIR}/${project}"
fi
printf "${yel}%s${end}\n" "Restarting nginx"
eval ${RESTART_NGINX}
exit
fi
# Delete
if [[ " ${args[*]} " == *" delete "* ]]; then
if [ -z "${args[1]}" ]; then
printf "%s\n" "${red}[ERROR] You need to specify a project name.${end}"
printf "%s\n" "${yel}Usage:${end}"
printf " %s\n\n" "npg delete project.${TLD}"
printf "%s\n" "Type ${grn}npg --help${end} to display the help message"
exit
fi
projects=($(ls ${NGINX_VHOSTS_DIR}))
project=${args[1]}
if [[ ! ${projects[@]} =~ ${project} ]]; then
printf "%s\n" "${red}[ERROR] Couldn't find any project ${project}.${end}"
printf "%s\n" "Type ${grn}npg list${end} to list all projects"
exit
fi
read -p "Are you sure? (Y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
printf "%s\n" "${yel}Deleting ${args[1]} project:${end}"
printf " ${grn}%-40s${end} %-40s\n" "Deleting vhost symbolic link:${end}" "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
unlink "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
printf " ${grn}%-40s${end} %-40s\n" "Deleting vhost:${end}" "${NGINX_VHOSTS_DIR}/${project}"
rm "${NGINX_VHOSTS_DIR}/${project}"
if [[ -d "${WORKING_DIR}/${project}" ]]; then
printf " ${grn}%-40s${end} %-40s\n" "Deleting working directory:${end}" "${WORKING_DIR}/${project}"
rm -rf "${WORKING_DIR}/${project}"
else
printf " ${grn}%-40s${end} %-40s\n" "No working directory:${end}" "${WORKING_DIR}/${project}"
fi
printf "${yel}%s${end}\n" "Restarting nginx"
eval ${RESTART_NGINX}
exit
fi
# Rename
if [[ " ${args[*]} " == *" rename "* ]]; then
if [ -z "${args[1]}" ]; then
printf "%s\n" "${red}[ERROR] You need to specify a project to rename.${end}"
printf "%s\n" "${yel}Usage:${end}"
printf " %s\n\n" "npg create project.${TLD} newproject.${TLD}"
printf "%s\n" "Type ${grn}npg --help${end} to display the help message"
exit
fi
if [ -z "${args[2]}" ]; then
printf "%s\n" "${red}[ERROR] You need to specify the project new name.${end}"
printf "%s\n" "${yel}Usage:${end}"
printf " %s\n\n" "npg create project.${TLD} newproject.${TLD}"
printf "%s\n" "Type ${grn}npg --help${end} to display the help message"
exit
fi
projects=($(ls ${NGINX_VHOSTS_DIR}))
project=${args[1]}
newproject=${args[2]}
if [[ ! ${projects[@]} =~ ${project} ]]; then
printf "%s\n" "${red}[ERROR] Couldn't find any project ${project}.${end}"
printf "%s\n" "Type ${grn}npg list${end} to list all projects"
exit
fi
if [[ ${projects[@]} =~ ${newproject} ]]; then
printf "%s\n" "${red}[ERROR] Project ${newproject} already exist.${end}"
printf "%s\n" "Type ${grn}npg list${end} to list all projects"
exit
fi
printf "%s\n" "${yel}Renaming ${project} into ${newproject}:${end}"
printf " ${grn}%-40s${end} %-40s\n" "Renaming vhost:${end}" "${NGINX_VHOSTS_DIR}/${project}"
mv "${NGINX_VHOSTS_DIR}/${project}" "${NGINX_VHOSTS_DIR}/${newproject}"
printf " ${grn}%-40s${end} %-40s\n" "Updating vhost content:${end}" "${NGINX_VHOSTS_DIR}/${newproject}"
sed -i -e "s/${project}/${newproject}/g" "${NGINX_VHOSTS_DIR}/${newproject}"
printf " ${grn}%-40s${end} %-40s\n" "Update vhost symlink:${end}" "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
if [[ -h "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}" ]]; then
rm "${NGINX_VHOSTS_DIR}/../sites-enabled/${project}"
fi
ln -s "${NGINX_VHOSTS_DIR}/${newproject}" "${NGINX_VHOSTS_DIR}/../sites-enabled/${newproject}"
if [[ -d "${WORKING_DIR}/${project}" ]]; then
printf " ${grn}%-40s${end} %-40s\n" "Updating working directory:${end}" "${WORKING_DIR}/${project}"
mv "${WORKING_DIR}/${project}" "${WORKING_DIR}/${newproject}"
else
printf " ${grn}%-40s${end} %-40s\n" "No working directory:${end}" "${WORKING_DIR}/${project}"
fi
printf "${yel}%s${end}\n" "Restarting nginx"
eval ${RESTART_NGINX}
exit
fi
printf "%s\n" "${red}[ERROR] Unknown command ${args[0]}.${end}"
printf "%s\n" "Type ${grn}npg --help${end} to display the help message"