-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngc.sh
More file actions
358 lines (328 loc) · 12 KB
/
ngc.sh
File metadata and controls
358 lines (328 loc) · 12 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
SITES_AVAILABLE="/etc/nginx/sites-available"
SITES_ENABLED="/etc/nginx/sites-enabled"
green="\033[0;32m"
red="\033[0;31m"
reset="\033[0m"
yellow="\033[0;33m"
cyan="\033[0;36m"
white="\033[1;37m"
magenta="\033[0;35m"
boldgray="\033[1;90m"
show_help() {
echo -e "${magenta}ngc - v.1.1${reset} | ${boldgray}https://github.com/KRWCLASSIC/ngc${reset}"
echo -e "${cyan}Usage:${reset}"
printf " ${cyan}ngc${reset} ${yellow}-n${reset} ${green}<domain>${reset} %b\n" "Edit or create Nginx config for domain using nano"
printf " ${cyan}ngc${reset} ${yellow}run${reset} %b\n" "Test and reload Nginx ${boldgray}(starts nginx if stopped)${reset}"
printf " ${cyan}ngc${reset} ${yellow}fullrun${reset} %b\n" "Link all, test once, and reload Nginx ${boldgray}(starts nginx if stopped)${reset}"
printf " ${cyan}ngc${reset} ${yellow}rm${reset} ${green}<domain>${reset} %b\n" "Remove domain config and symlink"
printf " ${cyan}ngc${reset} ${yellow}list${reset} %b\n" "List all domains and their status"
printf " ${cyan}ngc${reset} ${yellow}listbak${reset} %b\n" "List all backed up ${boldgray}(.bak)${reset} configs"
printf " ${cyan}ngc${reset} ${yellow}restore${reset} ${green}<domain>${reset} %b\n" "Restore a config from a ${boldgray}(.bak)${reset} file"
printf " ${cyan}ngc${reset} ${yellow}ren${reset} ${green}<old> <new>${reset} %b\n" "Rename a config file ${boldgray}(both available and enabled)${reset}"
printf " ${cyan}ngc${reset} ${yellow}enable${reset} ${green}<domain>${reset} %b\n" "Enable a config ${boldgray}(add symlink)${reset}"
printf " ${cyan}ngc${reset} ${yellow}disable${reset} ${green}<domain>${reset} %b\n" "Disable a config ${boldgray}(remove symlink)${reset}"
printf " ${cyan}ngc${reset} ${yellow}clean${reset} %b\n" "Remove all from enabled and relink all domains"
printf " ${cyan}ngc${reset} ${yellow}ls${reset} ${magenta}av|en${reset} %b\n" "Print path and list contents of available or enabled folder"
printf " ${cyan}ngc${reset} ${yellow}set${reset} ${magenta}av|en${reset} ${green}<path>${reset} %b\n" "Set the path for available or enabled folders in this script"
}
edit_config() {
DOMAIN="$1"
CONF_FILE="${SITES_AVAILABLE}/${DOMAIN}"
nano "$CONF_FILE"
}
enable_config() {
DOMAIN="$1"
CONF_FILE="${SITES_AVAILABLE}/${DOMAIN}"
LINK_FILE="${SITES_ENABLED}/${DOMAIN}"
if [ ! -f "$CONF_FILE" ]; then
echo -e "${red}Config file $CONF_FILE does not exist.${reset}"
exit 1
fi
if [ -L "$LINK_FILE" ]; then
echo -e "${green}Symlink for $DOMAIN already exists.${reset}"
exit 0
fi
if [ -e "$LINK_FILE" ]; then
echo -e "${red}$LINK_FILE exists and is not a symlink. Please resolve manually.${reset}"
exit 1
fi
ln -s "$CONF_FILE" "$LINK_FILE"
echo -e "${green}Enabled $DOMAIN (symlink created)${reset}"
}
disable_config() {
DOMAIN="$1"
LINK_FILE="${SITES_ENABLED}/${DOMAIN}"
if [ -L "$LINK_FILE" ]; then
rm "$LINK_FILE"
echo -e "${green}Disabled $DOMAIN (symlink removed)${reset}"
else
echo -e "${red}Symlink $LINK_FILE does not exist.${reset}"
exit 1
fi
}
run_all_configs() {
echo -e "${green}Ensuring symlinks exist for all configs...${reset}"
if [ ! -d "$SITES_AVAILABLE" ]; then
echo -e "${red}Available folder $SITES_AVAILABLE does not exist. Aborting.${reset}"
exit 1
fi
if [ ! -d "$SITES_ENABLED" ]; then
echo -e "${red}Enabled folder $SITES_ENABLED does not exist. Aborting.${reset}"
exit 1
fi
for CONF_FILE in "$SITES_AVAILABLE"/*; do
[ ! -e "$CONF_FILE" ] && continue
DOMAIN=$(basename "$CONF_FILE")
[[ "$DOMAIN" == *.bak ]] && continue
[[ "$DOMAIN" == "*" ]] && continue
LINK_FILE="${SITES_ENABLED}/${DOMAIN}"
if [ -L "$LINK_FILE" ]; then
TARGET=$(readlink "$LINK_FILE")
if [ "$TARGET" = "$CONF_FILE" ]; then
continue
else
rm "$LINK_FILE"
ln -s "$CONF_FILE" "$LINK_FILE"
echo -e "${cyan}Fixed symlink for $DOMAIN${reset}"
fi
elif [ -e "$LINK_FILE" ]; then
echo -e "${red}$LINK_FILE exists and is not a symlink. Please resolve manually.${reset}"
else
ln -s "$CONF_FILE" "$LINK_FILE"
echo -e "${green}Linked $DOMAIN${reset}"
fi
done
echo -e "${yellow}Testing all configs with nginx -t...${reset}"
if nginx -t 2>/dev/null; then
echo -e "${green}All configs valid. Reloading Nginx...${reset}"
systemctl reload nginx && echo -e "${green}Nginx reloaded.${reset}"
else
echo -e "${red}Config test failed. Check output above.${reset}"
exit 1
fi
# Start nginx if it is not running
nginx_status=$(systemctl is-active nginx 2>/dev/null)
if [ "$nginx_status" != "active" ]; then
echo -e "${red}Nginx is not running. Attempting to start...${reset}"
systemctl start nginx && echo -e "${green}Nginx started.${reset}"
fi
}
remove_config() {
DOMAIN="$1"
CONF_FILE="${SITES_AVAILABLE}/${DOMAIN}"
LINK_FILE="${SITES_ENABLED}/${DOMAIN}"
if [ -f "$CONF_FILE" ]; then
mv "$CONF_FILE" "${CONF_FILE}.bak"
echo -e "${green}Backed up $CONF_FILE to ${CONF_FILE}.bak${reset}"
else
echo -e "${red}Config file $CONF_FILE does not exist.${reset}"
exit 1
fi
if [ -L "$LINK_FILE" ]; then
rm "$LINK_FILE"
echo -e "${green}Removed symlink $LINK_FILE${reset}"
else
echo -e "${red}Symlink $LINK_FILE does not exist.${reset}"
fi
}
list_configs() {
echo -e "${green}Nginx config overview:${reset}"
if [ ! -d "$SITES_AVAILABLE" ]; then
echo -e "${red}No configs found (available folder missing)${reset}"
return
fi
found=0
for CONF_FILE in "$SITES_AVAILABLE"/*; do
[ ! -e "$CONF_FILE" ] && continue
DOMAIN=$(basename "$CONF_FILE")
[[ "$DOMAIN" == *.bak ]] && continue
[[ "$DOMAIN" == "*" ]] && continue
found=1
LINK_FILE="${SITES_ENABLED}/${DOMAIN}"
STATUS="${red}Not linked${reset}"
[ -L "$LINK_FILE" ] && STATUS="${green}Linked${reset}"
echo -e "${yellow}$DOMAIN${reset}: $STATUS"
done
if [ $found -eq 0 ]; then
echo -e "${red}No configs found.${reset}"
fi
nginx_status=$(systemctl is-active nginx 2>/dev/null)
if [ "$nginx_status" = "active" ]; then
echo -e "\nNginx service status: ${green}$nginx_status${reset}"
else
echo -e "\nNginx service status: ${red}$nginx_status${reset}"
fi
}
list_bak_configs() {
echo -e "${green}Backed up Nginx configs:${reset}"
if [ ! -d "$SITES_AVAILABLE" ]; then
echo -e "${red}No backups found (available folder missing)${reset}"
return
fi
found=0
for CONF_FILE in "$SITES_AVAILABLE"/*.bak; do
[ ! -e "$CONF_FILE" ] && continue
DOMAIN=$(basename "$CONF_FILE" .bak)
[[ "$DOMAIN" == "*" ]] && continue
found=1
echo -e "${yellow}$DOMAIN${reset}: ${green}Backed up${reset}"
done
if [ $found -eq 0 ]; then
echo -e "${red}No backups found.${reset}"
fi
}
restore_config() {
DOMAIN="$1"
BAK_FILE="${SITES_AVAILABLE}/${DOMAIN}.bak"
CONF_FILE="${SITES_AVAILABLE}/${DOMAIN}"
if [ -f "$BAK_FILE" ]; then
mv "$BAK_FILE" "$CONF_FILE"
echo -e "${green}Restored $CONF_FILE from $BAK_FILE${reset}"
else
echo -e "${red}Backup file $BAK_FILE does not exist.${reset}"
exit 1
fi
}
rename_config() {
OLD_DOMAIN="$1"
NEW_DOMAIN="$2"
OLD_CONF_FILE="${SITES_AVAILABLE}/${OLD_DOMAIN}"
NEW_CONF_FILE="${SITES_AVAILABLE}/${NEW_DOMAIN}"
OLD_LINK_FILE="${SITES_ENABLED}/${OLD_DOMAIN}"
NEW_LINK_FILE="${SITES_ENABLED}/${NEW_DOMAIN}"
if [ ! -f "$OLD_CONF_FILE" ]; then
echo -e "${red}Config file $OLD_CONF_FILE does not exist.${reset}"
exit 1
fi
if [ -f "$NEW_CONF_FILE" ]; then
echo -e "${red}Config file $NEW_CONF_FILE already exists. Aborting to avoid overwriting.${reset}"
exit 1
fi
if [ -L "$NEW_LINK_FILE" ]; then
echo -e "${red}Symlink $NEW_LINK_FILE already exists. Aborting to avoid overwriting.${reset}"
exit 1
fi
mv "$OLD_CONF_FILE" "$NEW_CONF_FILE"
echo -e "${green}Renamed $OLD_CONF_FILE to $NEW_CONF_FILE${reset}"
if [ -L "$OLD_LINK_FILE" ]; then
rm "$OLD_LINK_FILE"
echo -e "${green}Removed old symlink $OLD_LINK_FILE${reset}"
fi
echo -e "${yellow}To apply changes, run: 'ngc enable ${NEW_DOMAIN}' or 'ngc fullrun'${reset}"
}
clean_enabled() {
if [ ! -d "$SITES_ENABLED" ]; then
echo -e "${red}Enabled folder $SITES_ENABLED does not exist. Aborting.${reset}"
exit 1
fi
echo -e "${red}Cleaning all files from $SITES_ENABLED ...${reset}"
rm -f ${SITES_ENABLED}/*
echo -e "${green}All files removed from $SITES_ENABLED.${reset}"
run_all_configs
}
test_and_reload_nginx() {
echo -e "${yellow}Testing all configs with nginx -t...${reset}"
if nginx -t 2>/dev/null; then
echo -e "${green}All configs valid. Reloading Nginx...${reset}"
systemctl reload nginx && echo -e "${green}Nginx reloaded.${reset}"
else
echo -e "${red}Config test failed. Check output above.${reset}"
exit 1
fi
# Start nginx if it is not running
nginx_status=$(systemctl is-active nginx 2>/dev/null)
if [ "$nginx_status" != "active" ]; then
echo -e "${red}Nginx is not running. Attempting to start...${reset}"
systemctl start nginx && echo -e "${green}Nginx started.${reset}"
fi
}
set_path() {
TARGET="$2"
NEW_PATH="$3"
SCRIPT_PATH="$0"
if [ "$TARGET" = "av" ]; then
sed -i "s|^SITES_AVAILABLE=.*$|SITES_AVAILABLE=\"$NEW_PATH\"|" "$SCRIPT_PATH"
echo -e "${magenta}SITES_AVAILABLE${reset} set to ${green}$NEW_PATH${reset} in $SCRIPT_PATH"
elif [ "$TARGET" = "en" ]; then
sed -i "s|^SITES_ENABLED=.*$|SITES_ENABLED=\"$NEW_PATH\"|" "$SCRIPT_PATH"
echo -e "${magenta}SITES_ENABLED${reset} set to ${green}$NEW_PATH${reset} in $SCRIPT_PATH"
else
echo -e "${red}Usage: ngc set av|en <path>${reset}"
exit 1
fi
}
ls_ngc() {
case "$2" in
av)
echo -e "${boldgray}Available folder:${reset} $SITES_AVAILABLE"
if [ ! -d "$SITES_AVAILABLE" ]; then
echo -e "${red}Folder does not exist: $SITES_AVAILABLE${reset}"
exit 1
fi
ls -l "$SITES_AVAILABLE"
;;
en)
echo -e "${boldgray}Enabled folder:${reset} $SITES_ENABLED"
if [ ! -d "$SITES_ENABLED" ]; then
echo -e "${red}Folder does not exist: $SITES_ENABLED${reset}"
exit 1
fi
ls -l "$SITES_ENABLED"
;;
*)
echo -e "${red}Usage: ngc ls av|en${reset}"
exit 1
;;
esac
}
# Entry point
case "$1" in
-n)
[ -n "$2" ] && edit_config "$2" || show_help
;;
run)
test_and_reload_nginx
;;
fullrun)
run_all_configs
;;
rm)
[ -n "$2" ] && remove_config "$2" || show_help
;;
list)
list_configs
;;
listbak)
list_bak_configs
;;
restore)
[ -n "$2" ] && restore_config "$2" || show_help
;;
ren)
[ -n "$3" ] && rename_config "$2" "$3" || show_help
;;
enable)
[ -n "$2" ] && enable_config "$2" || show_help
;;
disable)
[ -n "$2" ] && disable_config "$2" || show_help
;;
clean)
clean_enabled
;;
ls)
ls_ngc "$@"
;;
set)
[ -n "$3" ] && set_path "$@" || { echo -e "${red}Usage: ngc set av|en <path>${reset}"; exit 1; }
;;
""|help|--help|-h)
show_help
;;
*)
echo -e "${red}Unknown command: $*${reset}"
show_help
exit 1
;;
esac