diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-10-12 23:23:10 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-10-12 23:23:10 +0200 |
commit | cdfcae52a49118d43e2064dd228b789b8452664f (patch) | |
tree | 62ee1d5f74adf0d085e9421b7a17ab3162dfde96 /.app/lib/app-operate | |
parent | 33ac32f2351b688300e656dfca3f7f6d69e56949 (diff) | |
download | app.sh-cdfcae52a49118d43e2064dd228b789b8452664f.tar.gz app.sh-cdfcae52a49118d43e2064dd228b789b8452664f.tar.bz2 app.sh-cdfcae52a49118d43e2064dd228b789b8452664f.tar.xz app.sh-cdfcae52a49118d43e2064dd228b789b8452664f.zip |
Major refactoring. Splitting out the method groups into separate files.
Diffstat (limited to '.app/lib/app-operate')
-rw-r--r-- | .app/lib/app-operate | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/.app/lib/app-operate b/.app/lib/app-operate new file mode 100644 index 0000000..fc0d63f --- /dev/null +++ b/.app/lib/app-operate @@ -0,0 +1,130 @@ +#!/bin/bash + +start_usage() { + if [ -n "$1" ] + then + echo "Error:" "$@" >&2 + fi + + echo "usage: $0 start -n name -i instance" >&2 + exit 1 +} + +stop_usage() { + if [ -n "$1" ] + then + echo "Error:" "$@" >&2 + fi + + echo "usage: $0 stop -n name -i instance" >&2 + exit 1 +} + +# TODO: set ulimit +# TODO: set umask +# TODO: change group newgrp/sg +method_start() { + run_control start_usage "start" "$@" +} + +method_stop() { + run_control stop_usage "stop" "$@" +} + +run_control() { + local usage=$0; shift + local method=$1; shift + local name + local instance + + while getopts "n:i:" opt + do + case $opt in + n) + name=$OPTARG + ;; + i) + instance=$OPTARG + ;; + \?) + $usage "Invalid option: -$OPTARG" + ;; + esac + done + + assert_is_instance $usage "$name" "$instance" + + ( + cd $name/$instance/current + + bin=`get_conf $BASEDIR $name $instance app.method` + + if [ -z "$bin" ] + then + bin=$BASEDIR/.app/lib/pid-method + fi + + if [ ! -x "$bin" ] + then + echo "Invalid executable: $bin" >&2 + exit 1 + fi + + e="`get_conf_in_group $BASEDIR $name $instance env`" + + # Set a default PATH which can be overridden by the application's settings + set +e + env -i \ + PATH=/bin:/usr/bin \ + $e \ + APPSH_METHOD=$method \ + APPSH_BASEDIR=$BASEDIR \ + APPSH_NAME=$name \ + APPSH_INSTANCE=$instance \ + $bin + local ret=$? + set +x + set -e + + case $ret in + 0) + echo "Application ${method}ed" + ;; + *) + echo "Error starting $name/$instance" + ;; + esac + ) +} + +method_operate_usage() { + if [ -n "$1" ] + then + echo "Error:" $@ >&2 + fi + + echo "usage: $0 operate <method>" >&2 + echo "" >&2 + echo "Available methods:" >&2 + echo " start" >&2 + echo " stop" >&2 + echo " restart" >&2 + echo " status" >&2 +} + +method_operate() { + if [ $# -gt 0 ] + then + method=$1 + shift + fi + + case "$method" in + start) method_start $first "$@" ;; + stop) method_stop $first "$@" ;; + status) method_status $first "$@" ;; + restart) method_restart $first "$@" ;; + *) method_operate_usage ;; + esac + exit $? +} |