-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzstack-dashboard
executable file
·182 lines (168 loc) · 5.25 KB
/
zstack-dashboard
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
#!/bin/sh
# the following is chkconfig init header
#
# zstack-dashboard: ZStack dashboard daemon
#
# chkconfig: 345 97 03
# description: This is a daemon instructed by zstack-dashboard \
# to start a web server for display ZStack UI\
# See http://zstack.org
#
# processname: nohup python zstack-dashboard/web.py >$DASHBOARD_LOG 2>&1 </dev/null &
# pidfile: /var/run/zstack/zstack-dashboard.pid
#
pidfile='/var/run/zstack/zstack-dashboard.pid'
mkdir -p `dirname $pidfile`
VIRTUAL_ENV=/var/lib/zstack/virtualenv/zstack-dashboard
[ ! -d $VIRTUAL_ENV ] && echo "Not find virtualenv in $VIRTUAL_ENV" && exit 1
. $VIRTUAL_ENV/bin/activate
DASHBOARD_LOG="/var/log/zstack/zstack-dashboard.log"
mkdir -p `dirname $DASHBOARD_LOG`
dashboard_pid=''
no_exit=1
get_pid_from_ps() {
dashboard_pid=`ps -aef|grep python|grep 'zstack_dashboard'|awk '{print $2}'`
}
use_ctl_if_possible() {
zstack-ctl $1
ret_result=$?
if [ $no_exit -eq 0 ];then
return $ret_result
fi
exit $ret_result
}
check_status() {
which zstack-ctl >/dev/null 2>&1
if [ $? -eq 0 ]; then
use_ctl_if_possible ui_status
else
if [ ! -f $pidfile ]; then
pid="none"
else
pid=`cat $pidfile`
if [ x"$pid" == "x" ]; then
pid="none"
fi
fi
if [ $pid == "none" ]; then
ps -aef|grep python|grep 'zstack_dashboard import web' > /dev/null
if [ $? -eq 0 ]; then
get_pid_from_ps
if [ ! -z $dashboard_pid ]; then
echo "zstack-dashboard is running, pid is $dashboard_pid. ."
echo $dashboard_pid > $pidfile
exit 0
fi
fi
echo "zstack dashboard is stopped"
exit 0
else
pid=`cat $pidfile`
ps -p $pid > /dev/null
if [ $? -eq 0 ]; then
echo "zstack dashboard is running, pid is $pid"
exit 0
else
echo "zstack dashboard is stopped, but pidfile at $pidfile is not cleaned. It may be caused by the agent crashed at last time, manually cleaning it would be ok"
exit 1
fi
fi
fi
}
stop_dashboard(){
which zstack-ctl >/dev/null 2>&1
if [ $? -eq 0 ]; then
use_ctl_if_possible stop_ui
else
echo "Stop zstack dashboard ..."
if [ ! -f $pidfile ]; then
ps -aef|grep python|grep 'zstack_dashboard import web' > /dev/null
if [ $? -eq 0 ]; then
get_pid_from_ps
if [ ! -z $dashboard_pid ]; then
kill -1 $dashboard_pid
sleep 0.5
echo "zstack dashboard is killed"
kill -9 $dashboard_pid >/dev/null 2>&1
return 0
fi
fi
echo "Not find $pidfile , zstack dashboard might be stopped."
return 0
else
pid=`cat $pidfile`
if [ ! -z $pid ]; then
ps -p $pid > /dev/null
if [ $? -eq 0 ]; then
kill -3 $pid
echo "zstack dashboard is killed"
kill -9 $pid >/dev/null 2>&1
else
echo "Not find process: $pid, zstack dashboard might be stopped."
fi
fi
/bin/rm -f $pidfile
fi
fi
}
start_dashboard(){
which zstack-ctl >/dev/null 2>&1
if [ $? -eq 0 ]; then
use_ctl_if_possible start_ui
else
echo "Start zstack dashboard ..."
iptables-save | grep -- "-A INPUT -p tcp -m tcp --dport 5000 -j ACCEPT" > /dev/null || iptables -I INPUT -p tcp -m tcp --dport 5000 -j ACCEPT
if [ -f $pidfile ]; then
pid=`cat $pidfile`
if [ ! -z $pid ]; then
ps -p $pid > /dev/null
if [ $? -eq 0 ]; then
echo "zstack dashboard is running with PID: $pid, wont' startup again."
return 0
else
/bin/rm -f $pidfile
fi
fi
fi
ps -aef|grep python|grep 'zstack_dashboard import web' > /dev/null
if [ $? -eq 0 ]; then
get_pid_from_ps
if [ ! -z $dashboard_pid ]; then
echo "zstack dashboard is already running with PID: $pid. Can't start zstack dashboard again."
echo $pid >$pidfile
return 0
fi
else
shift
nohup python -c "from zstack_dashboard import web; web.main()" $* >$DASHBOARD_LOG 2>&1 </dev/null &
echo $! >$pidfile
fi
fi
return 0
}
if [ $# -eq 0 ]; then
echo "usage: $0
[start|stop|restart|status]"
exit 1
fi
if [ "$1" = "status" ]; then
check_status
elif [ "$1" = "start" ]; then
start_dashboard $*
elif [ "$1" = "stop" ]; then
stop_dashboard
elif [ "$1" = "restart" ]; then
no_exit=0
stop_dashboard
start_dashboard $*
else
echo "Not support service: $@. Please use [start|stop|restart|status]"
exit 1
fi
if [ $? -eq 0 ]; then
echo "$@ zstack dashboard .... SUCCESS"
exit 0
else
echo "$@ zstack dashboard .... FAILED"
exit 1
fi