From 1e4a96730da70fcfa3b8c153874cbdebad0f9829 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 27 Jan 2013 19:41:28 +0100 Subject: o Starting on a style guide. app-conf: Adding 'import' command. app-cat-conf: Adding support for multiple -f flags. The default files can be switched off. A file named "-" is the same as /dev/stdin. app: Adding a way to enable debugging. app-install-file: Import any configuration delivered with the package. --- libexec/app-cat-conf | 39 +++++++++++----- libexec/app-install-file | 9 ++-- libexec/app-method-pid | 113 -------------------------------------------- libexec/app-operate | 25 +++------- libexec/app-operator-pid | 119 +++++++++++++++++++++++++++++++++++++++++++++++ libexec/app-run-hook | 2 +- 6 files changed, 156 insertions(+), 151 deletions(-) delete mode 100755 libexec/app-method-pid create mode 100755 libexec/app-operator-pid (limited to 'libexec') diff --git a/libexec/app-cat-conf b/libexec/app-cat-conf index 5b1c614..857ecaf 100755 --- a/libexec/app-cat-conf +++ b/libexec/app-cat-conf @@ -10,14 +10,23 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) key_expr="[a-zA-Z][_a-zA-Z0-9]*" -file=.app/config +files=() name="" +use_default_files=yes -while getopts "f:n:" opt +while getopts "f:Dn:" opt do case $opt in f) file=$OPTARG + if [[ $file == "-" ]] + then + file=/dev/stdin + fi + files+=($file) + ;; + D) + use_default_files=no ;; n) name=$OPTARG @@ -29,6 +38,19 @@ do esac done +if [[ $use_default_files == yes ]] +then + if [ -r ".app/config" ] + then + files+=(".app/config") + fi + + files+=(${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}) +fi + +# TODO: find config files in the paths above $file's paths and perhaps +# /etc/appsh/config. + if [ -z "$name" ] then filter="s,^[ ]*\($key_expr\.$key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p" @@ -36,17 +58,10 @@ else filter="s,^\($name\)=\(.*\),\1=\2,p" fi -APPSH_DEFAULT_CONFIG=${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config} - -# TODO: find config files in the paths above $file's paths and perhaps /etc/appsh/config - -if [[ ! -r $file ]] -then - fatal "No such file: $file" -fi +debug "Using files:" "${files[@]}" -# The awk script makes sure each key only appears once -cat "$file" "$APPSH_DEFAULT_CONFIG" | \ +# The awk script makes sure each key only appears once. The first one wins +cat "${files[@]}" | \ sed -n -e "$filter" | \ awk -F = ' (!($1 in a)){a[$1]; print }' | \ sort diff --git a/libexec/app-install-file b/libexec/app-install-file index 1e3edb8..c08a632 100755 --- a/libexec/app-install-file +++ b/libexec/app-install-file @@ -54,13 +54,10 @@ then exit 1 fi -# TODO: This should go away -if [ -d current/bin ] +if [ -r versions/$version/app.config ] then - ( - cd $name/$instance/current - find bin -type f | xargs chmod +x - ) + debug "Importing config from package" + app-conf import versions/$version/app.config fi app-run-hook -v "$version" -h pre-install diff --git a/libexec/app-method-pid b/libexec/app-method-pid deleted file mode 100755 index 29f6b4f..0000000 --- a/libexec/app-method-pid +++ /dev/null @@ -1,113 +0,0 @@ -#!/bin/bash -e - -set -u - -. $APPSH_HOME/.app/lib/app-conf - -pid_file=$APPSH_APPS/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid -bin=`get_conf $APPSH_APPS $APPSH_NAME $APPSH_INSTANCE app.bin` - -cd $APPSH_APPS/$APPSH_NAME/$APPSH_INSTANCE/current - -if [ -z "$bin" ] -then - echo "Missing required configuration: app.bin." >&2 - exit 1 -fi - -if [ ! -r "$bin" ] -then - echo "No such file: $bin" >&2 - exit 1 -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) method_start ;; - stop) method_stop ;; - status) method_status ;; - *) exit 1 ;; -esac - -exit $? diff --git a/libexec/app-operate b/libexec/app-operate index 007948c..452f658 100755 --- a/libexec/app-operate +++ b/libexec/app-operate @@ -10,29 +10,16 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) assert_is_app -method="$1" +method="$1"; shift +set -x -bin=`app-conf get app.method` -bin=${bin-$APPSH_HOME/.app/libexec/app-method-pid} +bin=`app-conf get app.operator` +bin=${bin:-$APPSH_HOME/libexec/app-operator-pid} -if [ ! -x "current/$bin" ] +if [ ! -x "$bin" ] then echo "Invalid executable: $bin" >&2 exit 1 fi -case "$method" in - start) run_app "$name" "$instance" "$bin" "start" "$@" ;; - stop) run_app "$name" "$instance" "$bin" "stop" "$@" ;; - status) run_app "$name" "$instance" "$bin" "status" "$@" ;; - restart) run_app "$name" "$instance" "$bin" "restart" "$@" ;; - run) run_app "$name" "$instance" "$bin" "run" "$@" ;; - *) - if [ -z "$method" ] - then - method_operate_usage - else - method_operate_usage "Unknown method $method" - fi - ;; -esac +run_app "$bin" "$@" diff --git a/libexec/app-operator-pid b/libexec/app-operator-pid new file mode 100755 index 0000000..a0ff097 --- /dev/null +++ b/libexec/app-operator-pid @@ -0,0 +1,119 @@ +#!/bin/bash -e + +#!/bin/bash + +set -e +set -u + +APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) + +. $APPSH_HOME/lib/common +# HEADER END + +pid_file=$APPSH_APPS/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid +bin=`get_conf $APPSH_APPS $APPSH_NAME $APPSH_INSTANCE app.bin` + +cd $APPSH_APPS/$APPSH_NAME/$APPSH_INSTANCE/current + +if [ -z "$bin" ] +then + echo "Missing required configuration: app.bin." >&2 + exit 1 +fi + +if [ ! -r "$bin" ] +then + echo "No such file: $bin" >&2 + exit 1 +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) method_start ;; + stop) method_stop ;; + status) method_status ;; + *) exit 1 ;; +esac + +exit $? diff --git a/libexec/app-run-hook b/libexec/app-run-hook index ec96c58..6f1eab5 100755 --- a/libexec/app-run-hook +++ b/libexec/app-run-hook @@ -45,5 +45,5 @@ fi # TODO: remove after #chmod +x $bin -echo "Running hook: $hook" +debug "Running hook: $hook" run_app -v $version hooks/$hook -- cgit v1.2.3