diff options
-rw-r--r-- | .app/lib/app-conf | 138 | ||||
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | README.md | 14 | ||||
-rwxr-xr-x | app | 95 |
4 files changed, 184 insertions, 65 deletions
diff --git a/.app/lib/app-conf b/.app/lib/app-conf new file mode 100644 index 0000000..8cc4949 --- /dev/null +++ b/.app/lib/app-conf @@ -0,0 +1,138 @@ +#!/bin/bash + +key_expr="[a-zA-Z][_a-zA-Z0-9]*\.[a-zA-Z][_a-zA-Z0-9]*" + +get_conf() { + key=$1 + default=$2 + + file=$BASEDIR/$name/$instance/latest/etc/app.conf + + if [ ! -r $file ] + then + echo "$default" + return 0 + fi + + value=`sed -n "s,^${key}[ ]*=[ ]*\(.*\)$,\1,p" $file` + + if [ -z "$value" ] + then + echo "$default" + fi + + echo "$value" +} + +get_conf_all() { + local file=$BASEDIR/$name/$instance/latest/etc/app.conf + + if [ ! -r $file ] + then + return 0 + fi + + sed -n "s,^[ ]*\($key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p" $file | sort +} + +get_conf_in_group() { + prefix=$1 + + get_conf_all | sed -n "s,^[ ]*${prefix}\.\([._a-zA-Z]*\)=\(.*\)$,\1=\2,p" +} + +assert_key() { + key=$1 + + local x=`echo $key | sed -n "/^$key_expr$/p"` + if [ -z "$x" ] + then + echo "Invalid key: $key" >&2 + exit 1 + fi +} + +conf_set() { + key=$1 + value=$2 + + local file=$BASEDIR/$name/$instance/latest/etc/app.conf + + assert_key "$key" + + sed "/^$key[ ]*=.*/d" $file > $file.tmp + echo "$key=$value" >> $file.tmp + mv $file.tmp $file +} + +conf_delete() { + key=$1 + + local file=$BASEDIR/$name/$instance/latest/etc/app.conf + + assert_key "$key" + + sed "/^$key[ ]*=.*/d" $file > $file.tmp + mv $file.tmp $file +} + +conf_usage() { + if [ -n "$1" ] + then + echo "Error:" $@ >&2 + fi + + echo "usage:" >&2 + echo "" + echo " $0 conf -n name -i instance" >&2 + echo " $0 conf -n name -i instance -s [group.key] [value]" >&2 + exit 1 +} + +method_conf() { + local mode="list" + + while getopts "n:i:s:d:" opt + do + case $opt in + n) + name=$OPTARG + ;; + i) + instance=$OPTARG + ;; + d) + mode="delete" + key=$OPTARG + ;; + s) + mode="set" + key=$OPTARG + # Remove all options so far + shift $((OPTIND-1)) + value=$1 + ;; + \?) + conf_usage "Invalid option: -$OPTARG" + ;; + esac + done + + assert_is_instance conf_usage "$name" "$instance" + + case $mode in + list) + get_conf_all | (IFS==; while read key value + do + printf "%-20s %-20s" "$key" "$value" + echo + done) + ;; + set) + conf_set "$key" "$value" + ;; + delete) + conf_delete "$key" + ;; + esac +} @@ -2,4 +2,4 @@ apps.list downloads /app-* target -.app +.app/var diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c5445f --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Environment +----------- + +The following environment variables are set by default: + +TODOs +----- + +* Support installation-wide settings. Useful for shared environment + settings etc (PATH). + +* Document app.sh + * Concept: config. group, key and value. + * Scriptable @@ -3,7 +3,9 @@ set -e mkdir -p .app -export BASEDIR=`pwd` +BASEDIR=`dirname $0` +BASEDIR=`cd $BASEDIR; pwd` +export BASEDIR if [ -n "$APPSH_REPO" ] then @@ -65,40 +67,33 @@ download_artifact() { # TODO: download checksum. bash is too shady to trust } -get_conf() { - key=$1 - default=$2 +assert_is_instance() { + usage=$1 + name=$2 + instance=$3 - file=$BASEDIR/$name/$instance/latest/etc/app.conf - - if [ ! -r $file ] + if [ -z "$name" ] then - echo "$default" - return 0 + $usage "Missing required option -n." fi - value=`sed -n "s,^${key}[ ]*=[ ]*\(.*\)$,\1,p" $file` - - if [ -z "$value" ] + if [ -z "$instance" ] then - echo "$default" + $usage "Missing required option -i." fi - echo "$value" -} - -get_conf_in_group() { - prefix=$1 - - file=$BASEDIR/$name/$instance/latest/etc/app.conf + if [ ! -d $name/$instance ] + then + echo "No such application/instance: $name/$instance." >&2 + exit 1 + fi - if [ ! -r $file ] + if [ ! -e $name/$instance/latest ] then - echo "$default" - return 0 + echo "Missing 'latest' link." >&2 + exit 1 fi - sed -n "s,^${prefix}\.\([._a-zA-Z]*\)[ ]*=[ ]*\(.*\)$,\1=\2,p" $file } install_usage() { @@ -241,12 +236,12 @@ method_install() { find bin -type f | xargs chmod +x ) - if [ -r apps.list ] + if [ -r $BASEDIR/.app/var/list ] then - sed "/^$name:$instance/d" apps.list > apps.list.new + sed "/^$name:$instance/d" $BASEDIR/.app/var/list > $BASEDIR/.app/var/list.new fi - echo "$name:$instance:$version" >> apps.list.new - mv apps.list.new apps.list + echo "$name:$instance:$version" >> $BASEDIR/.app/var/list.new + mv $BASEDIR/.app/var/list.new $BASEDIR/.app/var/list } start_usage() { @@ -278,22 +273,7 @@ method_start() { esac done - if [ -z "$name" -o -z "$instance" ] - then - start_usage "Missing required option." - fi - - if [ ! -d $name/$instance ] - then - echo "No such application/instance: $name/$instance." >&2 - exit 1 - fi - - if [ ! -e $name/$instance/latest ] - then - echo "Missing 'latest' link." >&2 - exit 1 - fi + assert_is_instance start_usage "$name" "$instance" ( cd $name/$instance/latest @@ -320,39 +300,24 @@ method_start() { env -i $e \ $bin & PID=`echo $!` - echo $PID > $BASEDIR/.app/$name-$instance.pid + echo $PID > $BASEDIR/.app/var/$name-$instance.pid ) } method_list() { printf "%20s %20s %20s\n" "instance" "name" "version" - if [ ! -r apps.list ] + if [ ! -r $BASEDIR/.app/var/list ] then return fi - cat apps.list | (export IFS=:; while read instance name version + sort $BASEDIR/.app/var/list | (export IFS=:; while read instance name version do printf "%20s %20s %20s\n" "$instance" "$name" "$version" done) } -method_list_config() { - name=$1 - instance=$2 - default=$3 - - conf=$name/$instance/etc/app.conf - - if [ ! -r $conf ] - then - echo $default - fi - - get_conf port -} - method_usage() { echo "usage: $0 <method>" >&2 echo "" >&2 @@ -363,6 +328,8 @@ method_usage() { echo "Run '$0 <method>' to get more help" >&2 } +. $BASEDIR/.app/lib/app-conf + if [ $# -gt 0 ] then method=$1 @@ -379,8 +346,8 @@ case "$method" in list) method_list $@ ;; - list-config) - method_list_config $@ + conf) + method_conf $@ ;; *) method_usage $@ |