-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.sh
executable file
·156 lines (129 loc) · 4.29 KB
/
generate.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
#!/bin/bash
# credit to Alexander Klimetschek answer at https://unix.stackexchange.com/a/415155
function select_option {
ESC=$( printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_option() { printf " $1 "; }
print_selected() { printf " $ESC[7m $1 $ESC[27m"; }
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]]; then echo up; fi
if [[ $key = $ESC[B ]]; then echo down; fi
if [[ $key = "" ]]; then echo enter; fi; }
for opt; do printf "\n"; done
local lastrow=`get_cursor_row`
local startrow=$(($lastrow - $#))
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
local selected=0
while true; do
local idx=0
for opt; do
cursor_to $(($startrow + $idx))
if [ $idx -eq $selected ]; then
print_selected "$opt"
else
print_option "$opt"
fi
((idx++))
done
case `key_input` in
enter) break;;
up) ((selected--));
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;;
down) ((selected++));
if [ $selected -ge $# ]; then selected=0; fi;;
esac
done
cursor_to $lastrow
printf "\n"
cursor_blink_on
return $selected
}
function select_opt {
select_option "$@" 1>&2
local result=$?
echo $result
return $result
}
echo "Welcome to the NGINX config generator"
echo "Created By Zainokta"
echo "[email protected]"
echo ""
echo "This generator only generate simple NGINX config, you need to configure manually for another config if necessary"
echo "There are two options for this generator"
echo "1. For service config generator"
echo "ex. web service that using ip:port to run"
echo "2. For index file config generator"
echo "ex. HTML CSS or PHP Website"
echo ""
echo "Choose your option:"
option_generator_type=("Service" "Index")
generator_type=""
case `select_opt ${option_generator_type[@]}` in
0) generator_type="service";;
1) generator_type="index";;
esac
echo -e "\nEnter your domain name (example.com): "
read server_name
if [[ $server_name == "" ]]; then
echo "Domain name cannot be empty."
exit;
fi
echo -e "\nEnter your project name (my-awesome-project): "
read project_name
if [[ $project_name == "" ]]; then
echo "Project name cannot be empty."
exit;
fi
directory_paths=$(locate $project_name | grep /$project_name$)
echo -e "\nSelect your project directory path: "
directory_path=${directory_paths[$(select_opt ${directory_paths[@]})]}
echo $directory_path
echo -e "\nEnter your directory root (/public): "
read directory_root
if [[ $directory_root == "" ]]; then
directory_root='/'
fi
echo $directory_path$directory_root
filename=""
case $generator_type in
service)
filename=${server_name}.conf
cp ./stub/nginx.api.stub $filename
echo -e "\nEnter your service endpoint (localhost:3000): "
read proxy_pass
sed -i "s|proxy_pass_placeholder|$proxy_pass|gi" "$filename"
;;
index)
filename=${server_name}.conf
cp ./stub/nginx.html.stub $filename
;;
esac
sed -i "s|server_name_placeholder|$server_name|gi" "$filename"
sed -i "s|directory_path_placeholder|$directory_path|gi" "$filename"
sed -i "s|directory_root_placeholder|$directory_root|gi" "$filename"
# move generated file to nginx sites available
mv $filename /etc/nginx/sites-available/
# create symlink to sites enabled
ln -s /etc/nginx/sites-available/$filename /etc/nginx/sites-enabled/
# reload nginx service
service nginx reload
echo -e "\nChecking if certbot is installed..."
certbot_loc=$(which certbot)
echo $certbot_loc
if [[ $certbot_loc == "" ]]; then
echo -e "\nDo you want to install certbot?[Y/n]"
read install_certbot
if [[ $install_certbot == 'y' ]]; then
add-apt-repository ppa:certbot/certbot
apt install python-certbot-nginx
else
exit
fi
fi
certbot --nginx -d $server_name
# reload nginx service again
service nginx reload