forked from icflorescu/openshift-cartridge-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control
executable file
·117 lines (107 loc) · 2.78 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
113
114
115
116
117
#!/bin/bash
source $OPENSHIFT_CARTRIDGE_SDK_BASH
source $HOME/nodejs/lib/util
if [ -f "$OPENSHIFT_REPO_DIR/.openshift/SOURCE_DIR" ]; then
SOURCE_DIR=$OPENSHIFT_REPO_DIR$(cat $OPENSHIFT_REPO_DIR/.openshift/SOURCE_DIR)
else
SOURCE_DIR=${OPENSHIFT_REPO_DIR}
fi
if [ -f "$SOURCE_DIR/package.json" ]; then
START_COMMAND=$(node -e "var p = require('$SOURCE_DIR/package.json'); console.log(p.scripts.start);")
else
START_COMMAND="supervisor server.js"
fi
function is_running() {
if [ ! -z "$(ps -ef | grep "$START_COMMAND" | grep -v grep)" ]; then
return 0
else
return 1
fi
}
function pre-repo-archive() {
if [ -d ${SOURCE_DIR}node_modules ]; then
mv -f ${SOURCE_DIR}node_modules ${TMP}
fi
}
function build() {
client_message 'Check for updates...'
update_nodejs
client_message 'Installing modules...'
local INIT_DIR=`pwd`
cd ${SOURCE_DIR}
if [ -d ${TMP}node_modules ]; then
mv -f ${TMP}node_modules ./
fi
npm prune --production
npm i --production
cd ${INIT_DIR}
client_result 'Node.js modules installed.'
}
function start() {
if is_running; then
client_result 'Application is already running.'
else
client_message 'Starting Node.js application...'
local INIT_DIR=`pwd`
cd ${SOURCE_DIR}
nohup ${START_COMMAND} |& /usr/bin/logshifter -tag nodejs &
cd ${INIT_DIR}
i=0
while ! is_running && [ $i -lt 60 ]; do
sleep 1
i=$(($i + 1))
done
if is_running; then
client_result 'Node.js application started.'
else
client_result 'Warning! Could not start Node.js application!'
exit 1
fi
fi
}
function stop() {
if ! is_running; then
client_result 'Application is already stopped.'
else
client_message 'Stopping Node.js application...'
kill $(ps -ef | grep "$START_COMMAND" | grep -v grep | awk '{ print $2 }') > /dev/null 2>&1
i=0
while is_running && [ $i -lt 60 ]; do
sleep 1
i=$(($i + 1))
done
if is_running; then
client_result 'Warning! Could not stop Node.js application!'
exit 1
else
client_result 'Node.js application stopped.'
fi
fi
}
function restart() {
stop
start
}
function status() {
if is_running; then
client_result 'Node.js application appears to be running.'
else
client_result 'Node.js application appears to be stopped.'
fi
}
function tidy() {
shopt -s dotglob
client_message "Emptying logs in ${OPENSHIFT_LOG_DIR}..."
rm -rf ${OPENSHIFT_LOG_DIR}/*.log*
client_message 'Done.'
}
case ${1} in
pre-repo-archive) pre-repo-archive ;;
build) build ;;
start) build && start ;;
stop) stop ;;
restart) restart ;;
status) status ;;
tidy) tidy ;;
*) exit 0
esac