-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathcontrol
executable file
·118 lines (107 loc) · 2.95 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
118
#!/bin/bash
source $OPENSHIFT_CARTRIDGE_SDK_BASH
source $HOME/nodejs/lib/util
START_COMMAND=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json'); console.log(p.scripts.start);")
STOP_COMMAND=$(node -e "var p = require('$OPENSHIFT_REPO_DIR/package.json'); console.log(p.scripts.stop || '');")
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 ${OPENSHIFT_REPO_DIR}node_modules ]; then
rm -rf ${OPENSHIFT_DATA_DIR}node_modules
mv -f ${OPENSHIFT_REPO_DIR}node_modules ${OPENSHIFT_DATA_DIR}
fi
}
function build() {
update_nodejs
local INIT_DIR=`pwd`
cd ${OPENSHIFT_REPO_DIR}
if [ -d ${OPENSHIFT_DATA_DIR}node_modules ]; then
rm -rf ./node_modules
mv -f ${OPENSHIFT_DATA_DIR}node_modules ./
rm -rf ${OPENSHIFT_DATA_DIR}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 ${OPENSHIFT_REPO_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...'
if [[ ! -z "$STOP_COMMAND" ]]; then
local INIT_DIR=`pwd`
cd ${OPENSHIFT_REPO_DIR}
$STOP_COMMAND > /dev/null 2>&1
cd ${INIT_DIR}
else
kill $(ps -ef | grep "$START_COMMAND" | grep -v grep | awk '{ print $2 }') > /dev/null 2>&1
fi
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 tidying up Node.js cartridge.'
}
case ${1} in
pre-repo-archive) pre-repo-archive ;;
build) build ;;
start) start ;;
stop) stop ;;
restart) restart ;;
status) status ;;
tidy) tidy ;;
*) exit 0
esac