-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginxd
More file actions
executable file
·142 lines (124 loc) · 3.1 KB
/
nginxd
File metadata and controls
executable file
·142 lines (124 loc) · 3.1 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
#!/bin/bash
# @Auther skiqqy
# This script is best run as root inside the root of this repo.
# This script allows me to enable/disable certain sections of my website.
# Available units
units=( git blog wiki irc proj files all )
RED='\033[0;31m'
NC='\033[0m'
# Show the help menu
help_menu () {
echo "Welcome!"
echo -e "e\t->\tEnable a unit."
echo -e "d\t->\tDisable a unit."
echo -e "u\t->\tSpecify the path to the units directory"
echo -e "h\t->\tShows this message."
}
# Display an error message and exit.
# error [message] [exit code]
error () {
printf "[${RED}ERROR${NC}] $1\n"
exit $2
}
# links available site to enabled.
# link [unit]
link () {
[ ! -f /etc/nginx/sites-available/$1 ] && error "The unit <$1> file cannot be linked." 1
ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/$1
}
# Adds a unit so that nginx can use it, also sets up sym links.
# add [unit]
add () {
echo Enabling $1
cp $unitsd/$1 /etc/nginx/sites-available
link $1
}
# Remove a unit so that nginx does not use it anymore, also sets up sym links.
# remove [unit]
remove () {
echo Removing $1
rm -f /etc/nginx/sites-enabled/$1
}
# Enable or disable all units.
# all [ e | d ]
all () {
# Test and check valid args
[ -z $1 ] && error "[ALL] Not given a flag." 1
! [[ $1 == 'e' || $1 == 'd' ]] && error "[ALL] Not given a valid flag." 1
REMOVE=()
ADD=()
for u in "${units[@]}"
do
if [ $u != "all" ]
then
if [ $1 == 'e' ]
then
ADD+=("$u")
REMOVE+=("d$u")
else
REMOVE+=("$u")
ADD+=("d$u")
fi
fi
done
echo "Adding units ( ${ADD[@]} )"
echo "Removing units ( ${REMOVE[@]} )"
for u in "${ADD[@]}"
do
add $u
done
for u in "${REMOVE[@]}"
do
remove $u
done
}
# Handle arguments and do basic error checking
while getopts "he:d:u:" opt
do
case $opt in
h)
help_menu
exit 0
;;
e | d)
[ ! -z $unit ] && error "Unit <$unit> already set." 1
[[ $opt == "e" ]] && oper=e || oper=d
unit="$OPTARG"
;;
u)
[ -d "$OPTARG" ] && unitsd="$OPTARG" || error "Not <$OPTARG> a valid directory." 1
;;
*)
exit 2
;;
esac
done
# if not specified, then by default we enable everything
[ -z $unit ] && unit="all" && oper="e"
# Unit directory
unitsd=${unitsd:="./units"}
unitsd=$(echo $unitsd | sed 's:/*$::') # Remove trailing slash
# Check that a valid unit was given.
[[ ! " ${units[@]} " =~ " ${unit} " ]] && error "The unit <$unit> does not exist." 1 \
|| echo "Unit <$unit> is supported!"
# Check if we are disabeling, that we have a replacement unit
[ $oper == "d" ] && [ $unit != "all" ] && [[ ! -f "$unitsd/d$unit" ]] && error "Cannot find replacement unit for <$unit>." 1
[ $oper == "e" ] && [ $unit != "all" ] && [[ ! -f "$unitsd/$unit" ]] && error "Cannot find unit file for <$unit>" 1
# Everything is valid, Perform tasks
if [ $unit == "all" ]
then
all $oper
elif [ $oper == 'e' ]
then
add $unit
remove "d$unit"
elif [ $oper == 'd' ]
then
add "d$unit"
remove $unit
echo Disabling $unit
else
error "No operation set." 1
fi
# Finally we test the new configs, and restart nginx
service nginx testconfig && systemctl restart nginx || error "nginx config test failed..." 1