-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsite.sh
More file actions
executable file
·267 lines (220 loc) · 6.02 KB
/
site.sh
File metadata and controls
executable file
·267 lines (220 loc) · 6.02 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
#!/usr/bin/env bash
set -o errexit # Exit if error occurs.
version=0.1
web_root=/srv
templates_dir=/etc/site.conf.d
##########################################################################
set_domain() {
while true
do
echo -n "Domain name: "
read -r
if [ "$REPLY" ]; then
if yn_dialog "Proceed with this domain? $REPLY"
then
domain_name="$REPLY"
break
fi
fi
done
}
check_vhost() {
if [ -f /etc/nginx/sites-available/$domain_name ]
then
echo "Virtual host for $domain_name already exists!"
if yn_dialog "Do you want to replace it?"
then
: # Do nothing. Vhost will be replaced.
else
echo Abort.
exit 1
fi
fi
}
select_template() {
# Get available vhost template list.
for vh in $(find "$templates_dir" -type f -name "*.vhost")
do
vhosts+=("${vh##*\/}")
done
if [[ "${#vhosts[@]}" == '0' ]]; then
echo "$0: No vhost templates found in $templates_dir" >&2
exit 1
fi
PS3='Enter a number: ' # Select prompt.
echo 'Select virtual host template:'
select val in ${vhosts[@]}
do
if [ "$val" ]
then
template="$templates_dir/$val"
break
else
echo 'Bad value. Try again or press ^C to cancel.'
fi
done
echo "Selected template: $template"
}
create_site_dir() {
local site_dir="$web_root/$domain_name/public"
mkdir -p "$site_dir"
# Add stub
if [ ! -f "$site_dir" ]
then
echo "$domain_name is created!" > "$site_dir"/index.html
fi
echo -e " -> Create root directory: $site_dir"
}
create_vhost() {
vhost=/etc/nginx/sites-available/$domain_name
if [ -f "$template" ]
then
cp "$template" "$vhost"
else
echo "Template not found: $template" >&2
exit 1
fi
# Replace template data with actual data.
#+Deafult placeholder in configs is 'example.org'.
sed -i "s%example\.org%$domain_name%g" "$vhost"
echo " -> Create virtual host: $vhost"
}
enable_vhost() {
symlink="$(sed 's%available%enabled%' <<< "$vhost")"
[ -f "$symlink" ] && rm "$symlink"
ln -s "$vhost" /etc/nginx/sites-enabled/
echo " -> Create symlink: $symlink -> $vhost"
# Check vhost config syntax
nginx -t
# Reload nginx configuration
nginx -s reload
echo "Site enabled. Check it out: http://$domain_name"
}
##########################################################################
print_help() {
cat << EOF
Create site dir and nginx virtual host.
Usage: site [-v | --version] [-h | --help] [--disabled] [--no-dir]
[-e | --edit] [-y] [-t | --template=<template>] <domain>
Options:
--disabled don't enable virtual host.
--no-dir don't create site directory.
-e, --edit open vhost in default editor.
-t, --template=<template> use <template> for site.
-y, --yes assume 'yes' in all dialogs.
-h, --help print this message and exit.
-v, --version print version and exit.
EOF
exit 0
}
open_in_editor() {
# $1 is file to open.
get_selected_editor() {
source $HOME/.selected_editor
echo $SELECTED_EDITOR
}
# Detect default editor.
if [ "$EDITOR" ]; then
local e=$EDITOR
elif [ -f $HOME/.selected_editor ]; then
local e="$(get_selected_editor)"
elif [ -f /usr/bin/select-editor ]; then
select-editor
local e="$(get_selected_editor)"
else
local e=/usr/bin/vi
fi
# Open file in editor.
echo "Open $1 in editor ..."
"$e" "$1"
}
yn_dialog() {
local question="$1" # Message prompt.
local yes=0
local no=1
local pending=2
[ "$assume_yes" = "1" ] && return "$yes"
local answer=$pending
while [ $answer -eq $pending ]
do
echo -en "$question [y/n] "
read -r reply
case "$reply" in
y|Y|Yes|YES) answer=$yes;;
n|N|No|NO) answer=$no;;
*) echo 'Please, answer y or n';;
esac
done
return "$answer"
}
continue_dialog() {
[ "$assume_yes" = "1" ] && return 0
[ "$1" ] && echo -e "$1" # Optional message prompt.
echo 'Press RETURN to continue, or ^C to cancel.'
read -e ignored
}
get_optarg() {
if [[ "$1" =~ .+=.+ ]]; then
opt="${1%%=*}"; arg="${1#*=}"; sft=1
elif [[ ! "$1" =~ .+=$ ]] && \
[ "$2" ] && [ "${2:0:1}" != '-' ]
then
opt="$1"; arg="$2"; sft=2
else
opt="$1"
if [[ "$1" =~ .+=$ ]]; then opt="${1:0: -1}"; fi
echo "$0: Missing argument for: $opt" >&2
exit 1
fi
}
# Check root.
if [[ ! "$UID" == 0 ]]; then
echo "$0: You must run this script as root." >&2
exit 1
fi
# Check nginx.
if ! hash nginx 2>/dev/null
then
echo "$0: nginx executable not found." >&2
exit 1
fi
# Check args.
[[ "$@" ]] || print_help
while (( "$#" ))
do
case "$1" in
--disabled) disabled=1; shift;;
--no-dir) no_dir=1; shift;;
-e|--edit) edit=1; shift;;
-y|--yes) assume_yes=1; shift;;
-t|--template|--template=*)
get_optarg "$1" "$2"
template="$templates_dir/$arg";
shift "$sft";;
-h|--help) print_help;;
-v|--version) echo "$0 $version"; exit 0;;
-*) echo "$0: Bad option: $1" >&2; exit 1;;
*)
domain_name+=("$1")
if [[ ${#domain_name[@]} > 1 ]]; then
echo "$0: Too many arguments." >&2; exit 1
fi
shift;;
esac
done
[ "$domain_name" ] || set_domain
check_vhost
if [ "$template" ]; then
if [ ! -f "$template" ];then
echo "$0: Template $template not found." >&2; exit 1
fi
else
select_template
fi
[ "$no_dir" ] || create_site_dir
create_vhost
[ "$edit" ] && open_in_editor "$vhost"
if [ ! "$disabled" ]; then
continue_dialog "Nginx will be reloaded!"
enable_vhost
fi