From 8b1d11508e985cc2cd97a1d70d01574e78109569 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 9 Oct 2012 18:49:27 +0200 Subject: o Reworked app starting. --- .app/lib/app-conf | 39 +++++++++++++----- .app/lib/default-method | 19 +++++++++ .gitignore | 3 +- README.md | 10 +++++ app | 102 +++++++++++++++++++++++------------------------- 5 files changed, 107 insertions(+), 66 deletions(-) create mode 100644 .app/lib/default-method diff --git a/.app/lib/app-conf b/.app/lib/app-conf index 141524d..b1b06c0 100644 --- a/.app/lib/app-conf +++ b/.app/lib/app-conf @@ -3,8 +3,11 @@ key_expr="[a-zA-Z][_a-zA-Z0-9]*\.[a-zA-Z][_a-zA-Z0-9]*" get_conf() { - local key=$1 - local default=$2 + local BASEDIR=$1 + local name=$2 + local instance=$3 + local key=$4 + local default=$5 local file=$BASEDIR/$name/$instance/current/etc/app.conf if [ ! -r $file ] @@ -24,6 +27,9 @@ get_conf() { } get_conf_all() { + local BASEDIR=$1 + local name=$2 + local instance=$3 local file=$BASEDIR/$name/$instance/current/etc/app.conf if [ ! -r $file ] @@ -35,9 +41,13 @@ get_conf_all() { } get_conf_in_group() { - prefix=$1 + local BASEDIR=$1 + local name=$2 + local instance=$3 + local prefix=$1 - get_conf_all | sed -n "s,^[ ]*${prefix}\.\([._a-zA-Z]*\)=\(.*\)$,\1=\2,p" + get_conf_all "$BASEDIR" "$name" "$instance" | \ + sed -n "s,^[ ]*${prefix}\.\([._a-zA-Z]*\)=\(.*\)$,\1=\2,p" } assert_key() { @@ -52,8 +62,12 @@ assert_key() { } conf_set() { - local key=$1 - local value=$2 + local BASEDIR=$1 + local name=$2 + local instance=$3 + local key=$4 + local value=$5 + local file=$BASEDIR/$name/$instance/current/etc/app.conf assert_key "$key" @@ -68,7 +82,10 @@ conf_set() { } conf_delete() { - key=$1 + local BASEDIR=$1 + local name=$2 + local instance=$3 + local key=$4 local file=$BASEDIR/$name/$instance/current/etc/app.conf @@ -92,6 +109,8 @@ conf_usage() { } method_conf() { + local name + local instance local mode="list" while getopts "n:i:s:d:" opt @@ -124,17 +143,17 @@ method_conf() { case $mode in list) - get_conf_all | (IFS==; while read key value + get_conf_all "$BASEDIR" "$name" "$instance" | (IFS==; while read key value do printf "%-20s %-20s" "$key" "$value" echo done) ;; set) - conf_set "$key" "$value" + conf_set "$BASEDIR" "$name" "$instance" "$key" "$value" ;; delete) - conf_delete "$key" + conf_delete "$BASEDIR" "$name" "$instance" "$key" ;; esac } diff --git a/.app/lib/default-method b/.app/lib/default-method new file mode 100644 index 0000000..2313f61 --- /dev/null +++ b/.app/lib/default-method @@ -0,0 +1,19 @@ +#!/bin/bash + +PID_FILE=$APPSH_BASEDIR/.app/var/pid/$name-$instance.pid + +. ./app-conf + +bin=`get_conf $APPSH_BASEDIR $APPSH_NAME $APPSH_INSTANCE app.bin` + +case "$APPSH_METHOD" in + start) + set -x + $bin & + ret=$? + echo $! > $PID_FILE + ;; + *) + exit 1 + ;; +esac diff --git a/.gitignore b/.gitignore index 92c6ff0..bafa041 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ apps.list downloads target .app/var -/*/*/current -/*/*/versions +/*/*/* diff --git a/README.md b/README.md index 6747efb..7ca5d3b 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,13 @@ TODOs * Document app.sh * Concept: config. group, key and value. * Scriptable + +Method Contract +--------------- + +### Environment variables you can depend on + +* `APPSH_NAME` +* `APPSH_INSTANCE` +* `APPSH_METHOD` +* `APPSH_PID_FILE` diff --git a/app b/app index e52e61d..5fd0d11 100755 --- a/app +++ b/app @@ -261,10 +261,33 @@ start_usage() { 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_method start_usage "start" "$@" +} + +method_stop() { + run_method stop_usage "stop" "$@" +} + +run_method() { + local usage=$0; shift + local method=$1; shift + local name + local instance + while getopts "n:i:" opt do case $opt in @@ -275,82 +298,53 @@ method_start() { instance=$OPTARG ;; \?) - start_usage "Invalid option: -$OPTARG" + $usage "Invalid option: -$OPTARG" ;; esac done - assert_is_instance start_usage "$name" "$instance" + assert_is_instance $usage "$name" "$instance" ( cd $name/$instance/current - bin=`get_conf app.start` + bin=`get_conf $BASEDIR $name $instance app.method` if [ -z "$bin" ] then - bin=`find bin -type f` + bin=$BASEDIR/.app/lib/default-method + fi - if [ ! -x "$bin" ] - then - echo "No app.start configured, couldn't detect an executable file to execute." >&2 - exit 1 - fi - elif [ ! -x "$bin" ] + if [ ! -x "$bin" ] then echo "Invalid executable: $bin" >&2 exit 1 fi - e=`get_conf_in_group env` + e=`get_conf_in_group $BASEDIR $name $instance env` - env -i $e \ - $bin & + set +e set -x - PID=$! - echo $PID > $BASEDIR/.app/var/pid/$name-$instance.pid - ) -} - -method_stop() { - while getopts "n:i:" opt - do - case $opt in - n) - name=$OPTARG - ;; - i) - instance=$OPTARG + env -i \ + $e \ + PATH=/bin:/usr/bin \ + 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" ;; - \?) - start_usage "Invalid option: -$OPTARG" + *) + echo "Error starting $name/$instance" ;; esac - done - - assert_is_instance stop_usage "$name" "$instance" - - ( - cd $name/$instance/current - - bin=`get_conf app.stop` - - if [ -z "$bin" ] - then - PID=`cat $BASEDIR/.app/var/pid/$name-$instance.pid` - echo "Sending TERM to $PID" - bin="kill $PID" - elif [ ! -x "$bin" ] - then - echo "Invalid executable: $bin" >&2 - exit 1 - fi - - e=`get_conf_in_group env` - - env -i $e \ - PID=$PID \ - $bin & ) } -- cgit v1.2.3