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. --- STYLE-GUIDE.md | 10 +++- app | 7 ++- bin/app-conf | 29 +++++++--- lib/common | 11 +++- 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 +- test/app-cat-conf.bats | 7 +++ test/app-conf.bats | 19 +++++- test/app-init.bats | 5 +- test/data/app-a/app.config | 1 + test/data/app-a/root/etc/app.conf | 1 - 15 files changed, 230 insertions(+), 167 deletions(-) delete mode 100755 libexec/app-method-pid create mode 100755 libexec/app-operator-pid create mode 100644 test/data/app-a/app.config delete mode 100644 test/data/app-a/root/etc/app.conf diff --git a/STYLE-GUIDE.md b/STYLE-GUIDE.md index 21f1523..1137425 100644 --- a/STYLE-GUIDE.md +++ b/STYLE-GUIDE.md @@ -1,9 +1,14 @@ Style Guide ----------- -Usage +Basic ===== +* Indent: two spaces. Spaces >> tabs. + +Creating `usage()` +================== + * Always echo to `stderr`. * Exit with code 1. * Enclose required arguments in angle brackets: `-v ` @@ -14,4 +19,7 @@ Usage exit 1 } +Resources +--------- +* Parameter expansion: diff --git a/app b/app index a70b026..8fcc585 100755 --- a/app +++ b/app @@ -14,6 +14,7 @@ usage() { echo " operate" >&2 echo "" >&2 echo "Run $0 -h for more help" >&2 + exit 1 } PRG="$0" @@ -32,16 +33,18 @@ APPSH_HOME=`cd "$APPSH_HOME" && pwd` . $APPSH_HOME/lib/common +echo_debug=no while getopts "h" opt do case $opt in h) usage - exit 1 + ;; + D) + echo_debug=yes ;; \?) usage - exit 1 ;; esac done diff --git a/bin/app-conf b/bin/app-conf index 3602a39..d281740 100755 --- a/bin/app-conf +++ b/bin/app-conf @@ -61,8 +61,16 @@ conf_delete() { mv $file.tmp $file } +conf_import() { + local dst=$1; shift + local src=$1; shift + + app-cat-conf -f "$src" -f "$dst" > "$dst.tmp" + mv "$dst.tmp" "$dst" +} + usage() { - if [ -n "$1" ] + if [ $# -gt 0 ] then echo "Error: $@" >&2 fi @@ -70,12 +78,16 @@ usage() { echo "usage: $0 conf " >&2 echo "" echo "Available commands:" >&2 + echo " get [name] - returns a single value" >&2 echo " list - list all config values" >&2 echo " set [name] [value] - set a config parameter" >&2 echo " delete [name] - deletes a config parameter" >&2 + echo " import [file] - import a file" >&2 exit 1 } +file=".app/config" + if [ $# -gt 0 ] then command=$1 @@ -84,8 +96,6 @@ else command=list fi -file=".app/config" - assert_is_app -C case "$command" in @@ -93,7 +103,6 @@ case "$command" in if [ $# != 1 ] then usage - exit 1 fi app-cat-conf -f "$file" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," @@ -102,7 +111,6 @@ case "$command" in if [ $# -gt 0 ] then usage "Extra options." - exit 1 fi app-cat-conf -f "$file" | format_conf @@ -111,7 +119,6 @@ case "$command" in if [ $# -ne 2 ] then usage - exit 1 fi conf_set "$1" "$2" @@ -120,11 +127,19 @@ case "$command" in if [ $# -ne 1 ] then usage "Missing [name] argument." - exit 1 fi conf_delete "$1" ;; + import) + if [ $# -ne 1 ] + then + usage "Missing [file] argument." + exit 1 + fi + + conf_import "$file" "$1" + ;; *) if [ -z "$command" ] then diff --git a/lib/common b/lib/common index 8c33209..d53f0cf 100644 --- a/lib/common +++ b/lib/common @@ -39,6 +39,14 @@ assert_is_app() { fi } +debug() { + [[ $echo_debug == no ]] || echo "D: $usage_app: $@" 2>&1 +} + +info() { + echo "I: $usage_app: $@" 2>&1 +} + fatal() { echo "$usage_app: fatal: $@" 2>&1 exit 1 @@ -192,4 +200,5 @@ run_app() { PATH=$PATH:$APPSH_HOME/bin:$APPSH_HOME/libexec # Save for later -usage_app=$0 +usage_app=${0##*/} +echo_debug=${echo_debug-no} 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 diff --git a/test/app-cat-conf.bats b/test/app-cat-conf.bats index a95049a..8060cea 100755 --- a/test/app-cat-conf.bats +++ b/test/app-cat-conf.bats @@ -41,6 +41,13 @@ setup_inner() { eq '${#lines[*]}' 1 } +@test "app-cat-conf can use stdin and multiple files" { + x=$(cat $APPSH_HOME/test/data/app-cat-conf/config-3 | \ + $APPSH_HOME/libexec/app-cat-conf -D -f $APPSH_HOME/test/data/app-cat-conf/config-2 -f -) + [[ $x == "foo.bar=wat +foo.wat=bar" ]] +} + @test "uses \$APPSH_DEFAULT_CONFIG" { APPSH_DEFAULT_CONFIG=$APPSH_HOME/test/data/app-cat-conf/config-2 app_libexec app-cat-conf -f /dev/null diff --git a/test/app-conf.bats b/test/app-conf.bats index 08676d2..f85dbab 100755 --- a/test/app-conf.bats +++ b/test/app-conf.bats @@ -4,7 +4,8 @@ load utils setup_inner() { - mkdir .app; touch .app/config + mkdir .app; + echo > .app/config export APPSH_DEFAULT_CONFIG=/dev/null } @@ -116,4 +117,20 @@ setup_inner() { app conf list; echo_lines eq '$status' 0 eq '${lines[0]}' "foo.bar awesome " + eq '${#lines[*]}' 1 +} + +@test "./app conf import" { + echo "foo.bar=1" > .app/config + echo "foo.baz=1" > config-b + echo "foo.bar=2" >> config-b + + app conf import config-b; echo_lines + eq '$status' 0 + eq '${#lines[*]}' 0 + + app_libexec app-cat-conf + eq '${lines[0]}' "foo.bar=2" + eq '${lines[1]}' "foo.baz=1" + eq '${#lines[*]}' 2 } diff --git a/test/app-init.bats b/test/app-init.bats index 4e7c281..10edc63 100755 --- a/test/app-init.bats +++ b/test/app-init.bats @@ -37,9 +37,8 @@ load utils match '${lines[2]}' "Downloading org.example:app-a:1.0-.*" eq '${lines[3]}' "Unpacking..." match '${lines[4]}' "Creating current symlink for version 1.0-.*" - eq '${lines[5]}' "Running hook: post-install" - eq '${lines[6]}' "Post install" - eq '${#lines[*]}' 7 + eq '${lines[5]}' "Post install" + eq '${#lines[*]}' 6 is_directory "my-app/.app" } diff --git a/test/data/app-a/app.config b/test/data/app-a/app.config new file mode 100644 index 0000000..d2c3f48 --- /dev/null +++ b/test/data/app-a/app.config @@ -0,0 +1 @@ +app.bin=bin/app-a diff --git a/test/data/app-a/root/etc/app.conf b/test/data/app-a/root/etc/app.conf deleted file mode 100644 index d2c3f48..0000000 --- a/test/data/app-a/root/etc/app.conf +++ /dev/null @@ -1 +0,0 @@ -app.bin=bin/app-a -- cgit v1.2.3