forked from RackHD/image-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-service
executable file
·163 lines (140 loc) · 3.15 KB
/
image-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
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
### BEGIN INIT INFO
# Provides: image-service
# Required-Start: $syslog $network mongodb rabbitmq-server
# Required-Stop: $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: image-service
# Description: RackHD image-service service
### END INIT INFO
NAME="image-service"
USER="root"
APP_DIR="/var/$NAME"
NODE_APP="index.js"
PID_DIR="/var/run"
PID_FILE="$PID_DIR/$NAME.pid"
LOG_DIR="/var/log/$NAME"
LOG_FILE="$LOG_DIR/$NAME.log"
NODE_EXEC=$(which node)
USAGE="Usage: $0 {start|stop|restart|status}"
RESTART=false
# Time to wait for the server to die, in seconds
DIETIME=1
STARTTIME=1
# if [ ! -f "/etc/default/$NAME" ]
# then
# echo "Service disabled due to absence of /etc/default/$NAME" 1>&2
# exit 1
# else
# . "/etc/default/$NAME"
# fi
pid_file_exists() {
[ -f "$PID_FILE" ]
}
get_pid() {
echo "$(cat "$PID_FILE")"
}
is_running() {
PID=$(get_pid)
! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
}
start_it() {
if [ ! -d $PID_DIR ]; then mkdir -p "$PID_DIR"; fi
if [ ! -d $LOG_DIR ]; then mkdir -p "$LOG_DIR"; fi
echo "Starting $NAME ..."
/sbin/start-stop-daemon --background --start --quiet\
--make-pidfile --pidfile $PID_FILE \
-d $APP_DIR \
--startas /bin/bash -- -c "exec $NODE_EXEC $APP_DIR/$NODE_APP >> $LOG_FILE 2>&1"
}
stop_process() {
echo "Killing process $PID"
/sbin/start-stop-daemon -K --quiet --pidfile $PID_FILE --retry 300
return $?
}
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}
start_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "$NAME already running with pid $PID"
exit 0
else
echo "$NAME stopped, but pid file exists"
remove_pid_file
fi
fi
start_it && sleep $STARTTIME
if is_running
then
echo "$NAME started with pid $(get_pid)"
else
echo "Failed to start $NAME"
exit 1
fi
}
stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping $NAME ..."
if stop_process
then
remove_pid_file
echo "$NAME stopped"
else
echo "Failed to stop $NAME"
exit 1
fi
else
echo "$NAME already stopped, but pid file exists"
remove_pid_file
echo "$NAME stopped"
fi
else
echo "$NAME already stopped, pid file does not exist"
fi
}
status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "$NAME running with pid $PID"
exit 0
else
echo "$NAME stopped, but pid file exists"
exit 1
fi
else
echo "$NAME stopped"
exit 2
fi
}
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app && sleep $DIETIME
start_app
;;
status)
status_app
;;
*)
echo $USAGE
exit 1
;;
esac