aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-10-17 10:55:23 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-10-17 10:55:23 +0200
commitc5401a6d595a595ee16ee6aede55fb016b9cfdfd (patch)
tree93d9bb33837bc1cea44ba4ca3c8ce9f8882ab523
parenta3ce3f7eabbb746aa9ced430bcd55391742c738b (diff)
downloadapp.sh-c5401a6d595a595ee16ee6aede55fb016b9cfdfd.tar.gz
app.sh-c5401a6d595a595ee16ee6aede55fb016b9cfdfd.tar.bz2
app.sh-c5401a6d595a595ee16ee6aede55fb016b9cfdfd.tar.xz
app.sh-c5401a6d595a595ee16ee6aede55fb016b9cfdfd.zip
o Proper implementation of start, stop and status.
-rw-r--r--.app/lib/app-conf10
-rw-r--r--.app/lib/app-operate17
-rwxr-xr-x.app/lib/pid-method102
-rw-r--r--test/data/app-a/root/bin/app-a4
-rwxr-xr-xtest/it-install-remove.bats24
-rw-r--r--test/utils.bash28
6 files changed, 153 insertions, 32 deletions
diff --git a/.app/lib/app-conf b/.app/lib/app-conf
index 29b74dd..6216d6f 100644
--- a/.app/lib/app-conf
+++ b/.app/lib/app-conf
@@ -7,9 +7,17 @@ get_conf() {
local name=$2
local instance=$3
local key=$4
- local default=$5
+ local default=
local file=$BASEDIR/$name/$instance/current/etc/app.conf
+ shift 4
+
+ if [ $# -gt 0 ]
+ then
+ default=$1
+ shift
+ fi
+
if [ ! -r $file ]
then
echo "$default"
diff --git a/.app/lib/app-operate b/.app/lib/app-operate
index 7789939..8c9692b 100644
--- a/.app/lib/app-operate
+++ b/.app/lib/app-operate
@@ -53,14 +53,15 @@ run_control() {
set +x
set -e
- case $ret in
- 0)
- echo "Method ${method} completed successfully for $name/$instance"
- ;;
- *)
- echo "Error running method ${method} for $name/$instance"
- ;;
- esac
+# case $ret in
+# 0)
+# echo "Method ${method} completed successfully for $name/$instance"
+# ;;
+# *)
+# echo "Error running method ${method} for $name/$instance"
+# ;;
+# esac
+ exit $ret
)
}
diff --git a/.app/lib/pid-method b/.app/lib/pid-method
index b4a672e..15e85ff 100755
--- a/.app/lib/pid-method
+++ b/.app/lib/pid-method
@@ -1,8 +1,8 @@
-#!/bin/bash
+#!/bin/bash -e
-set -e
+set -u
-PID_FILE=$APPSH_BASEDIR/.app/var/pid/$name-$instance.pid
+PID_FILE=$APPSH_BASEDIR/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid
. $APPSH_HOME/.app/lib/app-conf
@@ -22,16 +22,90 @@ fi
chmod +x "$bin"
+PID=
+if [ -r $PID_FILE ]
+then
+ PID="`cat $PID_FILE`"
+fi
+
+do_status() {
+ if [ -z "$PID" ]
+ then
+ echo stopped
+ else
+ if [ `ps -p "$PID" 2>/dev/null | wc -l` -gt 1 ]
+ then
+ echo running
+ else
+ echo crashed
+ fi
+ fi
+}
+
+method_start() {
+ case `do_status` in
+ running)
+ echo "The application is already running as $PID."
+ exit 1
+ ;;
+ esac
+
+ $bin <&- 1<&- 2<&- &
+ PID=$!
+ echo "Application launched as $PID"
+ echo $PID > $PID_FILE
+
+ return 0
+}
+
+method_stop() {
+ case `do_status` in
+ stopped)
+ echo "The application not running."
+ exit 1
+ ;;
+ crashed)
+ echo "The application crashed. Was running as $PID"
+ # TODO: should this remove the PID file?
+ # That makes it possible to run "stop" to stop "status" from showing "crashed"
+ exit 1
+ ;;
+ esac
+
+ signal="-9"
+ echo -n "Sending kill $signal to $PID, waiting for shutdown: "
+ kill $signal $PID
+
+ while [ "`do_status`" == "running" ]
+ do
+ sleep 1
+ echo -n "."
+ done
+
+ echo "OK"
+ rm -f $PID_FILE
+ return 0
+}
+
+method_status() {
+ case `do_status` in
+ running)
+ echo "$APPSH_NAME/$APPSH_INSTANCE is running as $PID"
+ ;;
+ stopped)
+ echo "$APPSH_NAME/$APPSH_INSTANCE is not running"
+ ;;
+ crashed)
+ echo "$APPSH_NAME/$APPSH_INSTANCE crashed. Was running as $PID"
+ ;;
+ esac
+}
+
case "$APPSH_METHOD" in
- start)
- set -x
- $bin &
- ret=$?
- pid=$!
- echo "Application launched with PID=$pid"
- echo $pid > $PID_FILE
- ;;
- *)
- exit 1
- ;;
+ start) method_start ;;
+ stop) method_stop ;;
+ status) method_status ;;
+ *) exit 1 ;;
esac
+
+exit $?
diff --git a/test/data/app-a/root/bin/app-a b/test/data/app-a/root/bin/app-a
index 7d251a0..1386f31 100644
--- a/test/data/app-a/root/bin/app-a
+++ b/test/data/app-a/root/bin/app-a
@@ -2,8 +2,12 @@
me=`basename $0`
+echo "Starting" >> $me.log
+
for i in {1..10}
do
echo "#$i: `date`" >> $me.log
sleep 1
done
+
+echo "Exiting" >> $me.log
diff --git a/test/it-install-remove.bats b/test/it-install-remove.bats
index 98a79c9..3358118 100755
--- a/test/it-install-remove.bats
+++ b/test/it-install-remove.bats
@@ -5,18 +5,28 @@ load utils
@test "install remove roundtrip" {
mkzip "app-a"
- a="-n app-a -i prod"
+ name="app-a"
+ instance="prod"
+ a="-n $name -i $instance"
+
+ describe "Installing $name/$instance"
app instance install \
-r file \
-u $BATS_TEST_DIRNAME/data/app-a.zip \
- $a
+ -n $name -i $instance
+
+# set -x
+ can_not_read ".app/var/pid/$name-$instance.pid"
- [ ! -r .app/var/pid/$name-$instance.pid ]
- app $a operate start; echo_lines
- [ -r .app/var/pid/$name-$instance.pid ]
+ describe "Starting $name/$instance"
+ app -n $name -i $instance operate start
+ echo_lines
+ can_read .app/var/pid/$name-$instance.pid
- app $a operate stop; echo_lines
- [ ! -r .app/var/pid/$name-$instance.pid ]
+ describe "Stopping $name/$instance"
+ app -n $name -i $instance operate stop
+ echo_lines
+ can_not_read .app/var/pid/$name-$instance.pid
# app instance install \
# -r file \
diff --git a/test/utils.bash b/test/utils.bash
index 03871f8..653aae4 100644
--- a/test/utils.bash
+++ b/test/utils.bash
@@ -30,6 +30,30 @@ mkzip() {
}
app() {
- (set -x
- run ./app "$@")
+ echo ./app $@
+ run ./app $@
+}
+
+describe() {
+ echo "# " $@ >&3
+}
+
+can_read() {
+ if [ -r "$1" ]
+ then
+ return 0
+ else
+ echo "Can't read $1"
+ return 1
+ fi
+}
+
+can_not_read() {
+ if [ ! -r "$1" ]
+ then
+ return 0
+ else
+ echo "Can read $1"
+ return 1
+ fi
}