-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontrol
executable file
·113 lines (97 loc) · 2.57 KB
/
control
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
#!/bin/bash
function is_node_running {
if [ -f ${OPENSHIFT_NODEDIY_DIR}server.pid ]; then
return 0
fi
ps axco command | grep -w '^node$' > /dev/null
}
function pidlist() {
# Recursive function to find pids children and grand children
local thispid=$1
local allpids=
local childpids=
childpids=$(ps --ppid $thispid -o pid h)
for pid in $childpids
do
allpids="$(pidlist $pid) $allpids"
done
echo "$thispid $allpids"
}
function status() {
client_message "Status:"
node_ver=$(node -v)
client_message "Node version: ${node_ver}"
npm_ver=$(npm -v)
client_message "NPM version: ${npm_ver}"
if [ is_node_running ]; then
client_message "Node is running."
else
client_message "Node is not running."
fi
}
function start() {
logf="${OPENSHIFT_IOJS_LOG_DIR}node.log"
touch $logf
if [ -f "${OPENSHIFT_REPO_DIR}package.json" ]; then
cd ${OPENSHIFT_REPO_DIR}
client_message "Starting node using npm start"
nohup npm start >> $logf 2>&1 &
npm_pid=$!;
sleep 3
echo `pidlist $npm_pid` > ${OPENSHIFT_IOJS_DIR}server.pid
else
client_message "npm start requires a package.json file, and we could not find one."
fi
}
function stop() {
if [ is_node_running ]; then
client_message "Stopping node"
if [ -f "${OPENSHIFT_IOJS_DIR}server.pid" ]; then
kill -9 `cat ${OPENSHIFT_IOJS_DIR}server.pid` > /dev/null 2>&1
rm -f ${OPENSHIFT_IOJS_DIR}server.pid
fi
else
client_message "Could not stop node... Is it running?"
fi
true
}
function restart() {
stop
# Give node two seconds to stop
sleep 2
start
}
function reload() {
source "${OPENSHIFT_NODEDIY_DIR}lib/util"
client_message "Reloading cartridge"
install
client_message "Restarting cartridge"
restart
}
function tidy() {
client_message "Emptying log dir: $OPENSHIFT_IOJS_LOG_DIR"
shopt -s dotglob
rm -rf ${OPENSHIFT_IOJS_LOG_DIR}*
rm -rf ${OPENSHIFT_IOJS_DIR}tmp/*
touch ${OPENSHIFT_IOJS_LOG_DIR}node.log
}
#
# main():
#
# Ensure arguments.
if ! [ $# -eq 1 ]; then
echo "Usage: $0 [start|restart|graceful|graceful-stop|stop|status]"
exit 1
fi
# Source utility functions.
source $OPENSHIFT_CARTRIDGE_SDK_BASH
# Handle commands.
case "$1" in
start) start ;;
restart|graceful) restart ;;
graceful-stop|stop) stop ;;
status) status ;;
reload) reload ;;
tidy) tidy ;;
*) exit 0;
esac