-
Notifications
You must be signed in to change notification settings - Fork 4
/
va-van16-service
150 lines (134 loc) · 4.12 KB
/
va-van16-service
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
#!/bin/sh
### BEGIN ###
# Author: idevz
# Since: 2016/03/12
# Description: Manage a Vanilla App Service
### END ###
OPENRESTY_NGINX_ROOT=/usr/local/openresty/nginx
NGINX=$OPENRESTY_NGINX_ROOT/sbin/nginx
NGINX_CONF_PATH=$OPENRESTY_NGINX_ROOT/conf
VA_APP_PATH=/vagrant/van16
VA_APP_NAME=`basename $VA_APP_PATH`
NGINX_CONF_SRC_PATH=$VA_APP_PATH/nginx_conf
TIME_MARK=`date "+%Y_%m_%d_%H_%M_%S"`
NGINX_CONF_SRC_PATH=$VA_APP_PATH/nginx_conf
DESC=va-van16-service
DESC=va-ok-service
IS_FORCE=''
PLATFORM=`uname`
ECHO_E=" -e "
[ $PLATFORM = "Darwin" ] && ECHO_E=""
ok()
{
MSG=$1
echo $ECHO_E"\033[35m$MSG \033[0m\n"
}
die()
{
MSG=$1
echo $ECHO_E"\033[31m$MSG \033[0m\n"; exit $?;
}
if [ -n "$2" -a "$2" = 'dev' ];then
VA_ENV="development"
IS_FORCE=$3
NGINX_CONF=$OPENRESTY_NGINX_ROOT/conf/va-nginx-$VA_ENV.conf
NGINX_APP_CONF=$OPENRESTY_NGINX_ROOT/conf/dev_vhost/$VA_APP_NAME.conf
NGINX_CONF_SRC=$NGINX_CONF_SRC_PATH/va-nginx-$VA_ENV.conf
VA_APP_CONF_SRC=$NGINX_CONF_SRC_PATH/dev_vhost/$VA_APP_NAME.conf
else
NGINX_CONF=$OPENRESTY_NGINX_ROOT/conf/va-nginx.conf
NGINX_APP_CONF=$OPENRESTY_NGINX_ROOT/conf/vhost/$VA_APP_NAME.conf
NGINX_CONF_SRC=$NGINX_CONF_SRC_PATH/va-nginx.conf
VA_APP_CONF_SRC=$NGINX_CONF_SRC_PATH/vhost/$VA_APP_NAME.conf
IS_FORCE=$2
VA_ENV=''
fi
if [ ! -f $NGINX ]; then
echo $ECHO_E"Didn't Find Nginx sbin."; exit 0
fi
conf_move()
{
NGINX_CONF_SRC=$1
NGINX_CONF=$2
VA_APP_CONF_SRC=$3
NGINX_APP_CONF=$4
IS_FORCE=$5
NGINX_APP_CONF_DIR=`dirname $NGINX_APP_CONF`
if [ -e "$NGINX_CONF" -a "$IS_FORCE" = "-f" ]; then
mv -f $NGINX_CONF $NGINX_CONF".old."$TIME_MARK && cp -f $NGINX_CONF_SRC $NGINX_CONF
echo $ECHO_E"Move And Copy \033[32m" $NGINX_CONF_SRC "\033[0m" to "\033[31m" $NGINX_CONF "\033[m";
elif [ ! -e "$NGINX_CONF" ]; then
cp -f $NGINX_CONF_SRC $NGINX_CONF
echo $ECHO_E"Copy \033[32m" $NGINX_CONF_SRC "\033[0m" to "\033[31m" $NGINX_CONF "\033[m";
else
ok $NGINX_CONF" is already exist, Add param '-f' to Force move."
fi
if [ -e $NGINX_APP_CONF ]; then
mv -f $NGINX_APP_CONF $NGINX_APP_CONF".old."$TIME_MARK && cp -f $VA_APP_CONF_SRC $NGINX_APP_CONF
elif [ ! -d "$NGINX_APP_CONF_DIR" ]; then
mkdir -p $NGINX_APP_CONF_DIR && cp -f $VA_APP_CONF_SRC $NGINX_APP_CONF
else
cp -f $VA_APP_CONF_SRC $NGINX_APP_CONF
fi
echo $ECHO_E"copy \033[32m" $VA_APP_CONF_SRC "\033[0m" to "\033[31m" $NGINX_APP_CONF "\033[m";
exit 0
}
nginx_conf_test() {
if $NGINX -t -c $1 >/dev/null 2>&1; then
return 0
else
$NGINX -t -c $1
return $?
fi
}
case "$1" in
start)
echo $ECHO_E"Starting $DESC: "
nginx_conf_test $NGINX_CONF
$NGINX -c $NGINX_CONF || true
ok "Succ."
;;
stop)
echo $ECHO_E"Stopping $DESC: "
$NGINX -c $NGINX_CONF -s stop || true
ok "Succ."
;;
restart|force-reload)
echo $ECHO_E"Restarting $DESC: "
$NGINX -c $NGINX_CONF -s stop || true
sleep 1
nginx_conf_test $NGINX_CONF
$NGINX -c $NGINX_CONF || true
ok "Succ."
;;
reload)
echo $ECHO_E"Reloading $DESC configuration: "
nginx_conf_test $NGINX_CONF
$NGINX -c $NGINX_CONF -s reload || true
ok "Succ."
;;
configtest)
echo $ECHO_E"Testing $DESC configuration: "
if nginx_conf_test $NGINX_CONF; then
echo $ECHO_E"Config Test Succ."
else
die "Config Test Fail."
fi
;;
confinit|initconf)
echo $ECHO_E"Initing $DESC configuration: "
if conf_move $NGINX_CONF_SRC $NGINX_CONF $VA_APP_CONF_SRC $NGINX_APP_CONF $IS_FORCE; then
if nginx_conf_test $NGINX_CONF; then
tree $NGINX_CONF_PATH/vhost
tree $NGINX_CONF_PATH/dev_vhost
echo $ECHO_E"Config init Succ."
fi
die "Config Test Fail."
fi
;;
*)
echo $ECHO_E"Usage: ./va-ok-service {start|stop|restart|reload|force-reload|confinit[-f]|configtest} [dev]" >&2
exit 1
;;
esac
exit 0