Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit b6765ea

Browse files
committed
cartridge control scripts: Fix exit value handling
Fix some problems with how we handle exit values in control scripts. Most control scripts have `#!/bin/bash -e` or `set -e` so that they exit immediately if a command exits with a non-zero return value if that command is not part of a compound command or conditional statement. Thus if we anticipate that a command may fail, we must put it in a compound command or a conditional statement. This commit makes the following changes: • Replace `foo; [ $? -eq 0 ] && bar` with `local rc=0; foo || rc=$?; [[ "$rc" = 0 ]] && bar` where foo is an especially long command. • Replace `foo; [ $? -eq 0 ] || bar` with `if ! foo; then bar; fi`. • Replace `foo; ret=$?; if [ $ret -eq 0 ]; then` with `if foo; then`. • Replace `foo; ret=$?; if [ $ret -ne 0 ]; then` with `if ! foo; then`. • Replace `foo; bar $?` with `local rc=0; foo || rc=$?; bar "$rc"`. • Replace `foo; return $?` with `local ret=0; foo || ret=$?; return $ret`. • If a script runs with `/bin/bash -e` and ends with `exit $?`, delete the `exit $?` because it is redundant. Moreover, several control scripts had inverted logic around the /bin/kill command: These scripts would run /bin/kill, check its exit value, and retry /bin/kill if the first attempt returned 0. However, an exit value of 0 from /bin/kill means that it succeeded, so this code was retrying exactly when it did not need to do so. This commit fixes this logic.
1 parent dce7c14 commit b6765ea

File tree

10 files changed

+46
-46
lines changed
  • cartridges
    • openshift-origin-cartridge-haproxy/bin
    • openshift-origin-cartridge-mariadb/bin
    • openshift-origin-cartridge-mongodb/bin
    • openshift-origin-cartridge-mysql/bin
    • openshift-origin-cartridge-nodejs/bin
    • openshift-origin-cartridge-perl/bin
    • openshift-origin-cartridge-php/bin
    • openshift-origin-cartridge-postgresql/bin
    • openshift-origin-cartridge-python/usr/versions/shared/bin
    • openshift-origin-cartridge-ruby/bin

10 files changed

+46
-46
lines changed

cartridges/openshift-origin-cartridge-haproxy/bin/control

+1-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ function _stop_haproxy_service() {
134134
_stop_haproxy_ctld
135135
[ -f $HAPROXY_PID ] && pid=$( /bin/cat "${HAPROXY_PID}" )
136136
if `ps -p $pid > /dev/null 2>&1`; then
137-
/bin/kill $pid
138-
ret=$?
139-
if [ $ret -eq 0 ]; then
137+
if ! /bin/kill $pid; then
140138
TIMEOUT="$STOPTIMEOUT"
141139
while [ $TIMEOUT -gt 0 ] && [ -f "$HAPROXY_PID" ]; do
142140
/bin/kill -0 "$pid" >/dev/null 2>&1 || break

cartridges/openshift-origin-cartridge-mariadb/bin/control

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ function stop {
6565
if [ -f $pidfile ]; then
6666
echo "Stopping MariaDB cartridge"
6767
pid=$( /bin/cat $pidfile )
68-
/bin/kill $pid
69-
ret=$?
70-
if [ $ret -eq 0 ]; then
68+
if ! /bin/kill $pid; then
7169
TIMEOUT="$STOPTIMEOUT"
7270
while [ $TIMEOUT -gt 0 ] && [ -f "$pidfile" ]
7371
do

cartridges/openshift-origin-cartridge-mongodb/bin/control

+12-13
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ function _repair_mongod() {
3939
client_result "Attempting to repair MongoDB ..."
4040
tmp_config="/tmp/mongodb.repair.conf"
4141
grep -ve "fork\s*=\s*true" $OPENSHIFT_MONGODB_DIR/conf/mongodb.conf > $tmp_config
42-
mongodb_context "mongod ${authopts} -f ${tmp_config} --repair"
43-
client_result "MongoDB repair status = $?"
42+
local rc=0
43+
mongodb_context "mongod ${authopts} -f ${tmp_config} --repair" || rc=$?
44+
client_result "MongoDB repair status = $rc"
4445
rm -f $tmp_config
4546
else
4647
echo "MongoDB already running - not running repair"
@@ -84,16 +85,10 @@ function stop() {
8485

8586
if [ -n "$pid" ]; then
8687
echo "Stopping MongoDB cartridge"
87-
set +e
88-
/bin/kill $pid 2>/dev/null
89-
set -e
90-
ret=$?
91-
if [ $ret -eq 0 ]; then
88+
if ! /bin/kill $pid 2>/dev/null; then
9289
TIMEOUT="$STOPTIMEOUT"
9390
while [ $TIMEOUT -gt 0 ] && [ -f "$OPENSHIFT_MONGODB_DIR/pid/mongodb.pid" ]; do
94-
set +e
9591
/bin/kill -0 "$pid" >/dev/null 2>&1 || break
96-
set -e
9792
sleep 1
9893
let TIMEOUT=${TIMEOUT}-1
9994
done
@@ -125,8 +120,10 @@ function pre_snapshot {
125120
_wait_for_mongod_to_startup
126121
# Work in a temporary directory (create and cd to it).
127122
umask 077
128-
dumpdir=$(mktemp -d /tmp/mongodumpXXXXXXXX)
129-
[ $? -eq 0 ] || die 0 "ERROR" "Failed to create working directory."
123+
if ! dumpdir=$(mktemp -d /tmp/mongodumpXXXXXXXX)
124+
then
125+
die 0 "ERROR" "Failed to create working directory."
126+
fi
130127
pushd $dumpdir > /dev/null
131128

132129
# Take a "dump".
@@ -187,8 +184,10 @@ WARNING: You may have possibly encountered the mongorestore bugs related to
187184
function restore_from_mongodb_snapshot {
188185
# Work in a temporary directory (create and cd to it).
189186
umask 077
190-
dumpdir=$(mktemp -d /tmp/mongodumpXXXXXXXX)
191-
[ $? -eq 0 ] || die 0 "ERROR" "Failed to create working directory."
187+
if ! dumpdir=$(mktemp -d /tmp/mongodumpXXXXXXXX)
188+
then
189+
die 0 "ERROR" "Failed to create working directory."
190+
fi
192191
pushd $dumpdir > /dev/null
193192

194193
# Extract dump from the snapshot.

cartridges/openshift-origin-cartridge-mysql/bin/control

+1-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ function stop {
8080
if [ -f $pidfile ]; then
8181
echo "Stopping MySQL ${OPENSHIFT_MYSQL_VERSION} cartridge"
8282
pid=$( /bin/cat $pidfile )
83-
/bin/kill $pid
84-
ret=$?
85-
if [ $ret -eq 0 ]; then
83+
if ! /bin/kill $pid; then
8684
TIMEOUT="${stop_timeout}"
8785
while [[ "${TIMEOUT}" > 0 && -f "${pidfile}" ]]
8886
do

cartridges/openshift-origin-cartridge-nodejs/bin/control

+1-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ function stop() {
126126

127127
echo "`date +"$FMT"`: Stopping application '$OPENSHIFT_APP_NAME' ..."
128128

129-
/bin/kill $cart_pid
130-
ret=$?
131-
132-
if [ $ret -eq 0 ]; then
129+
if ! /bin/kill $cart_pid; then
133130
TIMEOUT="$STOPTIMEOUT"
134131
while [ $TIMEOUT -gt 0 ] && is_cartridge_running ; do
135132
/bin/kill -0 "$cart_pid" >/dev/null 2>&1 || break

cartridges/openshift-origin-cartridge-perl/bin/control

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ function start() {
2828
ensure_valid_httpd_process "$HTTPD_PID_FILE" "$HTTPD_CFG_FILE"
2929
# Force httpd into its own pgroup, as httpd is hard-coded to TERM everything in
3030
# its pgroup during shutdown (even while foregrounded)
31+
local rc=0
3132
set -m
32-
eval "nohup /usr/sbin/httpd $HTTPD_CMD_CONF -D FOREGROUND |& /usr/bin/logshifter -tag perl &"
33+
eval "nohup /usr/sbin/httpd $HTTPD_CMD_CONF -D FOREGROUND |& /usr/bin/logshifter -tag perl &" || rc=$?
3334
set +m
34-
[ "$?" == "0" ] && wait_for_pid_file $HTTPD_PID_FILE
35+
[[ "$rc" = 0 ]] && wait_for_pid_file $HTTPD_PID_FILE
3536
}
3637

3738
function stop() {

cartridges/openshift-origin-cartridge-php/bin/control

+20-11
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,45 @@ function pre_start_httpd_config {
2525
}
2626

2727
function start() {
28+
local ret=0
29+
2830
echo "Starting PHP ${OPENSHIFT_PHP_VERSION} cartridge (Apache+mod_php)"
2931
pre_start_httpd_config
3032
# Force httpd into its own pgroup, as httpd is hard-coded to TERM everything in
3133
# its pgroup during shutdown (even while foregrounded)
3234
set -m
3335
php_context "nohup /usr/sbin/httpd $HTTPD_CMD_CONF -D FOREGROUND |& /usr/bin/logshifter -tag php &" \
34-
&& wait_for_pid_file $HTTPD_PID_FILE
36+
&& wait_for_pid_file $HTTPD_PID_FILE || ret=$?
3537
set +m
36-
return $?
38+
39+
return $ret
3740
}
3841

3942
function reload() {
43+
local ret=0
44+
4045
echo "Reloading PHP ${OPENSHIFT_PHP_VERSION} cartridge (Apache+mod_php)"
4146
pre_start_httpd_config
4247
httpd_pid=`cat "$HTTPD_PID_FILE" 2> /dev/null`
43-
kill -USR1 $httpd_pid && wait_for_pid_file $HTTPD_PID_FILE
44-
return $?
48+
kill -USR1 $httpd_pid && wait_for_pid_file $HTTPD_PID_FILE || ret=$?
49+
50+
return $ret
4551
}
4652

4753
function restart() {
54+
local ret=0
55+
4856
echo "Restarting PHP ${OPENSHIFT_PHP_VERSION} cartridge (Apache+mod_php)"
4957
ensure_httpd_restart_succeed "$HTTPD_PID_FILE" "$HTTPD_CFG_FILE"
5058
if [ -f "$HTTPD_PID_FILE" ]; then
5159
pre_start_httpd_config
5260
httpd_pid=`cat "$HTTPD_PID_FILE" 2> /dev/null`
53-
kill -HUP $httpd_pid
61+
kill -HUP $httpd_pid || ret=$?
5462
else
55-
start
63+
start || ret=$?
5664
fi
5765

58-
return $?
66+
return $ret
5967
}
6068

6169
function stop() {
@@ -69,9 +77,12 @@ function stop() {
6977
}
7078

7179
function configtest() {
80+
local ret=0
81+
7282
echo "Testing Apache *.conf files"
73-
php_context "/usr/sbin/httpd $HTTPD_CMD_CONF -t"
74-
return $?
83+
php_context "/usr/sbin/httpd $HTTPD_CMD_CONF -t" || ret=$?
84+
85+
return $ret
7586
}
7687

7788
function status() {
@@ -185,5 +196,3 @@ case "$1" in
185196
build) build ;;
186197
*) exit 0
187198
esac
188-
189-
exit $?

cartridges/openshift-origin-cartridge-postgresql/bin/control

+1-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ function post_restore {
163163
local rexp="s#\(DROP DATABASE\) \(.*\)#\\1 IF EXISTS \\2#g;"
164164

165165
# Restore old postgres database
166-
zcat $dump_file | sed "${rexp}" | postgresql_context "psql -U postgres -d postgres" &> /dev/null
167-
168-
if [ $? -ne 0 ]
166+
if ! zcat $dump_file | sed "${rexp}" | postgresql_context "psql -U postgres -d postgres" &> /dev/null
169167
then
170168
warning "Error: Could not import Postgres Database! Continuing..."
171169
fi

cartridges/openshift-origin-cartridge-python/usr/versions/shared/bin/control

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ function start_apache() {
6868
ensure_valid_httpd_process "$HTTPD_PID_FILE" "$HTTPD_CFG_FILE"
6969
# Force httpd into its own pgroup, as httpd is hard-coded to TERM everything in
7070
# its pgroup during shutdown (even while foregrounded)
71+
local rc=0
7172
set -m
72-
eval "nohup /usr/sbin/httpd $HTTPD_CMD_CONF -DFOREGROUND |& /usr/bin/logshifter -tag python &"
73+
eval "nohup /usr/sbin/httpd $HTTPD_CMD_CONF -DFOREGROUND |& /usr/bin/logshifter -tag python &" || rc=$?
7374
set +m
74-
[ "$?" == "0" ] && wait_for_pid_file $HTTPD_PID_FILE
75+
[[ "$rc" = 0 ]] && wait_for_pid_file $HTTPD_PID_FILE
7576
}
7677

7778
function start() {

cartridges/openshift-origin-cartridge-ruby/bin/control

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ function start() {
4141
update_passenger_performance
4242
# Force httpd into its own pgroup, as httpd is hard-coded to TERM everything in
4343
# its pgroup during shutdown (even while foregrounded)
44+
local rc=0
4445
set -m
45-
ruby_context "umask 002 &>/dev/null ; RAILS_ENV=${RAILS_ENV:-production} nohup /usr/sbin/httpd $HTTPD_CMD_CONF -D FOREGROUND |& /usr/bin/logshifter -tag ruby &"
46+
ruby_context "umask 002 &>/dev/null ; RAILS_ENV=${RAILS_ENV:-production} nohup /usr/sbin/httpd $HTTPD_CMD_CONF -D FOREGROUND |& /usr/bin/logshifter -tag ruby &" || rc=$?
4647
set +m
47-
[ "$?" == "0" ] && wait_for_pid_file $HTTPD_PID_FILE
48+
[[ "$rc" = 0 ]] && wait_for_pid_file $HTTPD_PID_FILE
4849
}
4950

5051
function stop() {

0 commit comments

Comments
 (0)