summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-10-09 18:49:27 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-10-09 18:59:21 +0200
commit8b1d11508e985cc2cd97a1d70d01574e78109569 (patch)
tree0f16b0320c8417e27150f305a6b316c3438c23b9
parentcb38e87306cde54144867ef7141682fc7fad0980 (diff)
downloadapp.sh-8b1d11508e985cc2cd97a1d70d01574e78109569.tar.gz
app.sh-8b1d11508e985cc2cd97a1d70d01574e78109569.tar.bz2
app.sh-8b1d11508e985cc2cd97a1d70d01574e78109569.tar.xz
app.sh-8b1d11508e985cc2cd97a1d70d01574e78109569.zip
o Reworked app starting.
-rw-r--r--.app/lib/app-conf39
-rw-r--r--.app/lib/default-method19
-rw-r--r--.gitignore3
-rw-r--r--README.md10
-rwxr-xr-xapp102
5 files changed, 107 insertions, 66 deletions
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 &
)
}