Skip to content

Commit 22582f5

Browse files
committed
Fixed issues #39
Signed-off-by: Teddysun <[email protected]>
1 parent 8809542 commit 22582f5

File tree

1 file changed

+60
-21
lines changed

1 file changed

+60
-21
lines changed

haproxy.sh

+60-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Intro: https://shadowsocks.be/10.html
1010
#
1111

12+
cur_dir=`pwd`
13+
1214
[[ $EUID -ne 0 ]] && echo "Error: This script must be run as root!" && exit 1
1315

1416
clear
@@ -20,24 +22,56 @@ echo "# Author: Teddysun <[email protected]> #"
2022
echo "#############################################################"
2123
echo
2224

23-
checkos(){
24-
if [[ -f /etc/redhat-release ]]; then
25-
OS="centos"
25+
check_sys() {
26+
local checkType=$1
27+
local value=$2
28+
29+
local release=''
30+
local systemPackage=''
31+
32+
if [ -f /etc/redhat-release ]; then
33+
release="centos"
34+
systemPackage="yum"
2635
elif cat /etc/issue | grep -Eqi "debian"; then
27-
OS="debian"
36+
release="debian"
37+
systemPackage="apt"
2838
elif cat /etc/issue | grep -Eqi "ubuntu"; then
29-
OS="ubuntu"
39+
release="ubuntu"
40+
systemPackage="apt"
3041
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
31-
OS="centos"
42+
release="centos"
43+
systemPackage="yum"
3244
elif cat /proc/version | grep -Eqi "debian"; then
33-
OS="debian"
45+
release="debian"
46+
systemPackage="apt"
3447
elif cat /proc/version | grep -Eqi "ubuntu"; then
35-
OS="ubuntu"
48+
release="ubuntu"
49+
systemPackage="apt"
3650
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
37-
OS="centos"
51+
release="centos"
52+
systemPackage="yum"
53+
fi
54+
55+
if [ ${checkType} == "sysRelease" ]; then
56+
if [ "$value" == "$release" ]; then
57+
return 0
58+
else
59+
return 1
60+
fi
61+
elif [ ${checkType} == "packageManager" ]; then
62+
if [ "$value" == "$systemPackage" ]; then
63+
return 0
64+
else
65+
return 1
66+
fi
67+
fi
68+
}
69+
70+
install_check() {
71+
if check_sys packageManager yum || check_sys packageManager apt; then
72+
return 0
3873
else
39-
echo "Not supported OS, Please reinstall OS and try again."
40-
exit 1
74+
return 1
4175
fi
4276
}
4377

@@ -81,8 +115,14 @@ get_char(){
81115

82116
# Pre-installation settings
83117
pre_install(){
118+
if ! install_check; then
119+
echo "Your OS is not supported to run it."
120+
echo "Please change to CentOS 6+/Debian 7+/Ubuntu 12+ and try again."
121+
exit 1
122+
fi
123+
84124
# Set haproxy config port
85-
while :
125+
while true
86126
do
87127
echo -e "Please enter a port for haproxy and Shadowsocks server [1-65535]"
88128
read -p "(Default port: 8989):" haproxyport
@@ -169,29 +209,29 @@ EOF
169209

170210
install(){
171211
# Install haproxy
172-
if [ "${OS}" == "centos" ]; then
212+
if check_sys packageManager yum; then
173213
yum install -y haproxy
174-
else
214+
elif check_sys packageManager apt; then
175215
apt-get -y update
176216
apt-get install -y haproxy
177217
fi
178218

179219
if [ -d /etc/haproxy ]; then
180-
echo "haproxy install successed."
220+
echo "haproxy install success."
181221

182222
echo "Config haproxy start..."
183223
config_haproxy
184224
echo "Config haproxy completed..."
185225

186-
if [ "${OS}" == "centos" ]; then
226+
if check_sys packageManager yum; then
187227
chkconfig --add haproxy
188228
chkconfig haproxy on
189-
else
229+
elif check_sys packageManager apt; then
190230
update-rc.d haproxy defaults
191231
fi
192232

193233
# Start haproxy
194-
/etc/init.d/haproxy start
234+
service haproxy start
195235
if [ $? -eq 0 ]; then
196236
echo "haproxy start success..."
197237
else
@@ -205,7 +245,7 @@ install(){
205245

206246
sleep 3
207247
# restart haproxy
208-
/etc/init.d/haproxy restart
248+
service haproxy restart
209249
# Active Internet connections confirm
210250
netstat -nxtlp
211251
echo
@@ -222,11 +262,10 @@ install(){
222262

223263
# Install haproxy
224264
install_haproxy(){
225-
checkos
226265
disable_selinux
227266
pre_install
228267
install
229268
}
230269

231270
# Initialization step
232-
install_haproxy 2>&1 | tee ~/haproxy_for_shadowsocks.log
271+
install_haproxy 2>&1 | tee ${cur_dir}/haproxy_for_shadowsocks.log

0 commit comments

Comments
 (0)