forked from perusio/nginx_ensite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx-site
executable file
·200 lines (183 loc) · 6.19 KB
/
ngx-site
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
#!/bin/bash
##
# nginx_*site -- Shell script to manage Nginx site configurations.
# Copyright (C) 2012 Michael Lustfield <michaellustfield@ubuntu.com>,
# Cyril Lavier <cyril.lavier@davromaniak.eu>
#
# Authors: Michael Lustfield <michaellustfield@ubuntu.com>
# Cyril Lavier <cyril.lavier@davromaniak.eu>
#
# Based on the work of António P. P. Almeida <appa@perusio.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written authorization.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
##
[[ -r "$HOME/.config/nginx-utils" ]] && . "$HOME/.config/nginx-utils"
sites_enabled="${NGX_CONF_DIR:-/etc/nginx}/${NGX_SITES_ENABLED:-sites-enabled}"
sites_available="${NGX_CONF_DIR:-/etc/nginx}/${NGX_SITES_AVAILABLE:-sites-available}"
warn() {
printf '%s\n' "$1"
}
die() {
printf 'ERROR: %s\n' "$1"
exit 1
}
enable_site() {
for site in "$@"; do
site_available="$sites_available/$site"
site_enabled="$sites_enabled/$site"
[[ -e "$site_enabled" ]] && warn "Error: $1 already enabled"
[[ -r "$site_available" ]] || warn 'Unable to read site to be enabled.'
ln -s "$site_available" "$site_enabled" && (
echo "Site $site succesfully enabled"
[[ "$NGX_RELOAD_CONF" ]] || (review_conf && echo "Run \"service nginx reload\" to apply the changes.")
)
done
}
disable_site() {
for site in "$@"; do
site_enabled="$sites_enabled/$site"
[[ -h "$site_enabled" ]] || warn "$site_enabled is not a symbolic link. Not removing."
rm "$site_enabled" && (
echo "Site $site succesfully disabled"
[[ "$NGX_RELOAD_CONF" ]] || (review_conf && echo "Run \"service nginx reload\" to apply the changes.")
)
done
}
edit_site() {
for site in $@; do
[[ -f "$sites_available/$site" ]] || die "Available site $site does not exist."
edits=$edits" "$sites_available/$site
done
if [[ -x "$(which sensible-editor 2>/dev/null)" ]]; then
sensible-editor $edits
elif [[ -x "$(which vi 2>/dev/null)" ]]; then
vi $edits
else
warn 'Unable to find appropriate editor'
fi
}
rm_site() {
if [$# -lt 1]; then
echo "Debe seleccionar uno de los sitios existentes para borrar:"
ls_site
fi
for site in "$@"; do
read -p "Are you sure you want to permanently remove $site? (y/N)" confirm
[[ ! "$confirm" == 'y' && ! "$confirm" == 'Y' ]] && exit
rm -f "$sites_available/$site" "$sites_enabled/$site"
done
}
helpls(){
cat <<HELP
Shows the available/enabled/disabled sites in nginx:
-a available
-d disabled
-e enabled
HELP
}
ls_site(){
if [[ ( "$#" -lt 1 ) || (( "$#" -eq 1 ) && ("$1" == "-l"))]]; then
echo "avaliable!"
ls "$sites_available"
fi
while getopts 'aedh' opt; do
case "$opt" in
e)ls "$sites_enabled";;
d)comm -13 <(ls "$sites_enabled") <(ls "$sites_available");;
h)helpls;;
a|*)ls "$sites_available";;
esac
done
}
review_conf() {
nginx -t >/dev/null 2>&1 || (
errmsg=$(nginx -t 2>&1 | sed -e "s/$/\\\n/" | tr -d "\r\n")
echo 'Configuration Error: Please review the enabled sites configuration before reloading NGINX.'
echo -e 'Error message is : \n-----\n'$errmsg'-----'
exit 1
) && echo "Configuration OK"
}
reload_nginx() {
review_conf && service nginx reload
}
show_help() {
cat <<HELP
This script is part of a package and provides:
ngx-ensite <sites> - Enable site
- Create a symlink from sites-available to sites-enabled
ngx-dissite <sites> - Disable site
- Remove a symlink in sites-enabled
ngx-edsite <site> - Edit site configuration
- Opens the configuration file in sites-available
ngx-rmsite <sites> - Remove site configuration
- Deletes the configuration in both site-enabled and sites-available
ngx-site <opt> <site> - Using <opt> lets you do the same thing as above
Options:
<site> - The site to be acted on
- Tab completion can be used help pick the correct site
<sites> - Multiple sites can me given here
-e - Same as ngx-ensite
-d - Same as ngx-dissite
-c - Same as ngx-edsite
-r - Same as ngx-rmsite
Files:
~/.config/nginx-utils - Per-user settings for the nginx-utils package.
NGX_CONF_DIR='/etc/nginx'
Base Nginx configuration directory
NGX_SITES_AVAILABLE='sites-available'
Available sites directory (should only contain config files)
NGX_SITES_ENABLED='sites-enabled'
Enabled sites directory (should only contain symbolic links)
NGX_RELOAD_CONF=1
If set, the reload command will be sent to nginx after changes are made.
Examples:
ngx-ensite domain.tld foo.tld bar.tld
ngx-site -e domain.tld
ngx-edsite
HELP
}
if [[ ! -d "$sites_enabled" || ! -d "$sites_available" ]]; then
warn 'Both a sites-enabled and sites-available directory are required.'
exit 1
fi
case "$(basename "$0")" in
*ensite) enable_site "$@";;
*dissite) disable_site "$@";;
*edsite) edit_site "$@";;
*rmsite) rm_site "$@";;
*lssites) ls_site "$@";;
*site)
while getopts 'e:d:c:r:lh' opt; do
case "$opt" in
e) enable_site "$OPTARG";;
d) disable_site "$OPTARG";;
c) edit_site "$OPTARG";;
r) rm_site "$OPTARG";;
l) ls_site "$@" && exit 0;;
h) show_help && exit 0;;
*) show_help && exit 0;;
esac
done;;
*) show_help;;
esac
[[ "$NGX_RELOAD_CONF" && -x "$(which nginx)" ]] && reload_nginx