diff options
Diffstat (limited to '.app/lib')
-rw-r--r-- | .app/lib/app-common | 160 | ||||
-rw-r--r-- | .app/lib/app-conf | 205 | ||||
-rw-r--r-- | .app/lib/app-instance | 496 | ||||
-rw-r--r-- | .app/lib/app-operate | 67 | ||||
-rwxr-xr-x | .app/lib/pid-method | 113 |
5 files changed, 0 insertions, 1041 deletions
diff --git a/.app/lib/app-common b/.app/lib/app-common deleted file mode 100644 index fabdad4..0000000 --- a/.app/lib/app-common +++ /dev/null @@ -1,160 +0,0 @@ -#!/bin/bash - -assert_is_instance() { - local usage=$1 - local name=$2 - local instance=$3 - local check_link=$4 - - if [ -z "$name" ] - then - $usage "Missing required option -n." - fi - - if [ -z "$instance" ] - then - $usage "Missing required option -i." - fi - - # Use $APPSH_APPS as prefix if it's set - local x="" - if [ ! -z "$APPSH_APPS" ] - then - x="$APPSH_APPS/" - fi - - if [ ! -d $x$name/$instance ] - then - echo "No such instance: $name/$instance." >&2 - exit 1 - fi - - if [ "$check_link" != "no" ] - then - if [ ! -e $x$name/$instance/current ] - then - echo "Missing 'current' link." >&2 - exit 1 - fi - fi -} - -list_apps() { - filter_name=$1; shift - filter_instnace=$1; shift - vars="$@" - - sort $apps/.app/var/list | while read line - do - echo $line | (IFS=:; while read name instance version junk - do - if [ -n "$filter_name" -a "$filter_name" != "$name" ] - then - continue - fi - - if [ -n "$filter_instance" -a "$filter_instance" != "$instance" ] - then - continue - fi - - local line="" - IFS=" "; for var in $vars - do - case $var in - name) x=$name;; - instance) x=$instance;; - version) x=$version;; - current_version) x=`find_current_version $name $instance`;; - *) x="";; - esac - - if [ -z "$line" ] - then - line="$line$x" - else - line="$line:$x" - fi - done - echo $line - done) - done -} - -find_current_version() { - name=$1 - instance=$2 - - if [ ! -L $apps/$name/$instance/current ] - then - return 0 - fi - - ( - cd $apps/$name/$instance - ls -l current | sed -n "s,.* current -> versions/\(.*\)/root,\1,p" - ) -} - -find_versions() { - name=$1 - instance=$2 - - if [ ! -d $apps/$name/$instance/versions ] - then - return 0 - fi - - ( - cd $apps/$name/$instance/versions - ls -1d * - ) -} - -# TODO: set ulimit -# TODO: set umask -# TODO: change group newgrp/sg -run_app() { - local name=$1; shift - local instance=$1; shift - local bin=$1; shift - local method=$1; shift - - assert_is_instance operate_usage "$name" "$instance" - - local x="" - if [ ! -z "$APPSH_APPS" ] - then - x="$APPSH_APPS/" - fi - - ( - cd $x$name/$instance - APPSH_INSTANCE_HOME=`pwd` - cd current - - e="`get_conf_in_group $apps $name $instance env`" - # This magically get the expansion of $u correct. - IFS=" -" - - # Set a default PATH which can be overridden by the application's settings - set +e - env -i \ - PATH=/bin:/usr/bin \ - $e \ - PWD=$PWD \ - APPSH_METHOD=$method \ - APPSH_APPS=$apps \ - APPSH_HOME=$APPSH_HOME \ - APPSH_NAME=$name \ - APPSH_INSTANCE=$instance \ - APPSH_INSTANCE_HOME=$APPSH_INSTANCE_HOME \ - $bin "$@" - local ret=$? - set +x - set -e - - exit $ret - ) -} diff --git a/.app/lib/app-conf b/.app/lib/app-conf deleted file mode 100644 index 8149e41..0000000 --- a/.app/lib/app-conf +++ /dev/null @@ -1,205 +0,0 @@ -#!/bin/bash - -# TODO: Add a 'get' method that returns a single value -# Exit with 0 if found, 1 otherwise. - -key_expr="[a-zA-Z][_a-zA-Z0-9]*\.[a-zA-Z][_a-zA-Z0-9]*" - -format_conf() { - local IFS== - while read key value - do - printf "%-20s %-20s" "$key" "$value" - echo - done -} - -get_conf() { - local apps=$1 - local name=$2 - local instance=$3 - local key=$4 - local default= - local file=$apps/$name/$instance/current/etc/app.conf - - shift 4 - - if [ $# -gt 0 ] - then - default=$1 - shift - fi - - 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 apps=$1 - local name=$2 - local instance=$3 - local file=$apps/$name/$instance/current/etc/app.conf - - if [ ! -r "$file" ] - then - return 0 - fi - - sed -n "s,^[ ]*\($key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p" "$file" | sort -} - -get_conf_in_group() { - local apps=$1 - local name=$2 - local instance=$3 - local group=$4 - - get_conf_all "$apps" "$name" "$instance" | sed -n "s,^${group}\.\(.*\),\1,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() { - local apps=$1 - local name=$2 - local instance=$3 - local key=$4 - local value=$5 - - local file=$apps/$name/$instance/current/etc/app.conf - - assert_key "$key" - - if [ -r $file ] - then - sed "/^$key[ ]*=.*/d" $file > $file.tmp - fi - - echo "$key=$value" >> $file.tmp - mv $file.tmp $file -} - -conf_delete() { - local apps=$1 - local name=$2 - local instance=$3 - local key=$4 - - local file=$apps/$name/$instance/current/etc/app.conf - - assert_key "$key" - - sed "/^$key[ ]*=.*/d" $file > $file.tmp - mv $file.tmp $file -} - -method_conf_list() { - local name=$1; shift - local instance=$1; shift - - get_conf_all "$apps" "$name" "$instance" | format_conf -} - -method_conf_usage() { - if [ -n "$1" ] - then - echo "Error:" $@ >&2 - fi - - echo "usage: $0 conf <method>" >&2 - echo "" - echo "Available methods:" >&2 - echo " list - list all configuration values" >&2 - echo " list-group [group] - list all configuration values in the specified group" >&2 - echo " set [group.key] [value] - set a configuration parameter" >&2 - echo " delete [group.key] - deletes a configuration parameter" >&2 - exit 1 -} - -method_conf() { - local name="$1"; shift - local instance="$1"; shift - local method="$1" - - if [ $# -gt 0 ] - then - shift - fi - - assert_is_instance method_conf_usage "$name" "$instance" - - case "$method" in - list) - if [ $# -gt 0 ] - then - method_conf_usage "Extra options." - exit 1 - fi - - method_conf_list "$name" "$instance" - ;; - list-group) - if [ $# -ne 1 ] - then - method_conf_usage - exit 1 - fi - - get_conf_in_group "$apps" "$name" "$instance" "$1" | format_conf - ;; - set) - if [ $# -ne 2 ] - then - method_conf_usage - exit 1 - fi - - conf_set "$apps" "$name" "$instance" "$1" "$2" - ;; - delete) - if [ $# -ne 1 ] - then - method_conf_usage - exit 1 - fi - - conf_delete "$apps" "$name" "$instance" "$1" - ;; - *) - if [ $# -eq 0 ] - then - method_conf_list "$name" "$instance" - exit 0 - fi - - if [ -z "$method" ] - then - method_conf_usage - else - method_conf_usage "Unknown method $method" - fi - exit 1 - ;; - esac -} diff --git a/.app/lib/app-instance b/.app/lib/app-instance deleted file mode 100644 index 25983f0..0000000 --- a/.app/lib/app-instance +++ /dev/null @@ -1,496 +0,0 @@ -#!/bin/bash - -if [ -n "$APPSH_REPO" ] -then - repo="$APPSH_REPO" -else - repo="http://repo1.maven.org" -fi - -calculate_md5() { - local file="$1"; shift - - md5sum "$file" | cut -c 1-32 -} - -# TODO: support file:// repositories -# TODO: look in the local repository first -get() { - local url=$1 - local file=$2 - local exit - - curl -o $file $url -D curl.tmp - - exit=`grep "^HTTP/[0-9]\.[0-9] 200 .*" curl.tmp >/dev/null; echo $?` - head=`head -n 1 curl.tmp` - rm -f curl.tmp - if [ "$exit" != 0 ] - then - echo "Unable to download $url: $head" >&2 - exit 1 - fi -} - -resolve_snapshot() { - local groupId=$1; shift - local groupIdSlash=$1; shift - local artifactId=$1; shift - local version=$1; shift - - local metadata=$apps/.app/var/download/$groupId-$artifactId-$version-metadata.xml - local base_url=$repo/$groupIdSlash/$artifactId/$version - get $base_url/maven-metadata.xml $metadata - local resolved_version=`xmlstarlet sel -t -m '//snapshotVersion[extension[text()="zip"]]' -v value $metadata` - echo $resolved_version -} - -download_artifact() { - local file="$1"; shift - local url="$1"; shift - - echo "Downloading $url.md5" - get $url.md5 $file.md5 - local expected_md5="`cat $file.md5`" - - if [ -r $file ] - then - if [ "$expected_md5" == "`calculate_md5 $file`" ] - then - echo "Artifact already downloaded." - else - rm -f "$file" - fi - return 0 - fi - echo "Downloading artifact: $url" - get $url $file - - local actual_md5="`calculate_md5 $file`" - if [ "$expected_md5" == "$actual_md5" ] - then - echo "Artifact downloaded." - else - echo "Invalid checksum. Expected $expected_md5, got $actual_md5" >&2 - exit 1 - fi -} - -method_install_usage() { - if [ -n "$1" ] - then - echo "Error:" "$@" >&2 - fi - - echo "usage: install <-r resolver> -u <url>" >&2 - echo "" >&2 - echo "Install package from a Maven repository:" >&2 - echo " $0 [-n name] [-i instance] instance install -r maven -u groupId:artifactId:version" >&2 - echo "" >&2 - echo "Install package from a file:" >&2 - echo " $0 [-n name] [-i instance] instance install -r file -u file [-v version]" >&2 - echo "The version defaults to the current timestamp" >&2 - exit 1 -} - -method_install() { - local name="$1"; shift - local instance="$1"; shift - local version - local resolver - local url - local groupId - local artifactId - local zip_file - - - if [ $# -eq 0 ] - then - method_install_usage - fi - - while getopts "n:i:v:r:u:" opt - do - case $opt in - n) - name=$OPTARG - ;; - i) - instance=$OPTARG - ;; - v) - version=$OPTARG - ;; - r) - resolver=$OPTARG - ;; - u) - url=$OPTARG - ;; - \?) - method_install_usage "Invalid option: -$OPTARG" - ;; - esac - done - - if [ -z "$name" ] - then - method_install_usage "Missing required argument: -i name." - fi - - if [ -z "$instance" ] - then - method_install_usage "Missing required argument: -i instance." - fi - - if [ -z "$resolver" ] - then - method_install_usage "Missing required option: -r resolver" - fi - - if [ -z "$url" ] - then - method_install_usage "Missing required option: -u url" - fi - - case "$resolver" in - maven) - url=`echo $url | tr ":" " "`; set -- $url - groupId=$1 - artifactId=$2 - version=$3 - - if [ -z "$groupId" -o -z "$artifactId" -o -z "$version" ] - then - method_install_usage "Invalid Maven url." - fi - - local groupIdSlash=$(echo $groupId | sed "s,\.,/,g") - if [ "`echo $version | sed -n s,.*-SNAPSHOT$,SNAPSHOT,p`" == "SNAPSHOT" ] - then - echo "Resolving version $version..." - local resolved_version=`resolve_snapshot $groupId $groupIdSlash $artifactId $version` - if [ -z "$resolved_version" ] - then - echo "Unable to resolve version." - exit 1 - fi - echo "Resolved version $version to $resolved_version" - else - resolved_version=$version - fi - - zip_file=$apps/.app/var/download/$groupId-$artifactId-$resolved_version.zip - artifact_url=$repo/$groupIdSlash/$artifactId/$version/$artifactId-$resolved_version.zip - - download_artifact "$zip_file" "$artifact_url" - ;; - file) - if [ ! -r "$url" ] - then - echo "Could not read file: $url" >&2 - exit 1 - fi - - # TODO: should the zip file be copied into download/ so that - # there's always a local copy? - zip_file=$url - - if [ -z "$version" ] - then - version=`TZ=UTC date +"%Y%m%d-%H%M%S"` - fi - - resolved_version=$version - ;; - *) - method_install_usage "Invalid resolver type: $resolver" - ;; - esac - - if [ -d $name/$instance/versions/$resolved_version ] - then - echo "Version $resolved_version is already installed" - exit 1 - fi - - if [ ! -d $name/$instance ] - then - echo "Creating instance '$instance' for '$name'" - mkdir -p $name/$instance - fi - - mkdir -p $name/$instance/versions/$resolved_version - - echo "Unpacking..." - unzip -q -d $name/$instance/versions/$resolved_version $zip_file - - if [ ! -d $name/$instance/versions/$resolved_version/root ] - then - echo "Invalid zip file, did not contain a ./root directory." >&2 - exit 1 - fi - - echo "Changing current symlink" - rm -f $apps/$name/$instance/current - ln -s versions/$resolved_version/root $apps/$name/$instance/current - - if [ -d $name/$instance/current/bin ] - then - ( - cd $name/$instance/current - find bin -type f | xargs chmod +x - ) - fi - - ( - cd $name/$instance/versions/$resolved_version - if [ -d scripts ] - then - find scripts | xargs chmod +x - fi - - if [ -x scripts/postinstall ] - then - echo "Running postinstall..." - cd root - set +e - env -i \ - PATH=/bin:/usr/bin \ - APPSH_APPS=$apps \ - APPSH_HOME=$APPSH_HOME \ - APPSH_NAME=$name \ - APPSH_INSTANCE=$instance \ - APPSH_VERSION=$resolved_version \ - ../scripts/postinstall - set -e - ret=`echo $?` - if [ "$ret" != 0 ] - then - echo "Postinstall failed!" - exit 1 - fi - echo "Postinstall completed successfully" - fi - ) - - if [ -r $apps/.app/var/list ] - then - sed "/^$name:$instance/d" $apps/.app/var/list > $apps/.app/var/list.new - fi - echo "$name:$instance:$version:$url" >> $apps/.app/var/list.new - mv $apps/.app/var/list.new $apps/.app/var/list -} - -method_set_current_usage() { - if [ -n "$1" ] - then - echo "Error:" "$@" >&2 - fi - - echo "usage: set-current -v version" >&2 - exit 1 -} - -method_set_current() { - local name="$1"; shift - local instance="$1"; shift - local version - - if [ $# -eq 0 ] - then - method_set_current_usage - fi - - while getopts "n:i:v:" opt - do - case $opt in - n) - name=$OPTARG - ;; - i) - instance=$OPTARG - ;; - v) - version=$OPTARG - ;; - \?) - method_set_current_usage "Invalid option: -$OPTARG" - ;; - esac - done - - if [ -z "$version" ] - then - echo "Missing required option -v version." >&2 - exit 1 - fi - - assert_is_instance method_set_current_usage "$name" "$instance" "no" - - if [ ! -d $apps/$name/$instance/versions/$version ] - then - echo "Invalid version: $version." - exit 1 - fi - - rm -f $apps/$name/$instance/current - ln -s versions/$version/root $apps/$name/$instance/current - - return 0 -} - -method_list_usage() { - if [ -n "$1" ] - then - echo "Error:" "$@" >&2 - fi - - echo "usage: list [-n name] [-P field]" >&2 - echo "" - echo "List all installed applications:" >&2 - echo " list" >&2 - echo "" - echo "List all applications with the selected fields with parseable output:" >&2 - echo " list -P instance -P version -n foo" >&2 - exit 1 -} - -method_list() { - local filter_name="$1"; shift - local filter_instance="$1"; shift - local mode="pretty" - local vars - local filter_name - - while getopts "P:n:i:" opt - do - case $opt in - P) - mode="parseable" - vars="$vars $OPTARG" - ;; - n) - filter_name=$OPTARG - ;; - i) - filter_instance=$OPTARG - ;; - \?) - method_list_usage "Invalid option: -$OPTARG" - ;; - esac - done - - if [ ! -r $apps/.app/var/list ] - then - return - fi - - if [ $mode = "pretty" ] - then - printf "%-20s %-20s %-20s\n" "Name" "Instance" "Version" - list_apps "$filter_name" "$filter_instance" name instance version | (IFS=:; while read name instance version - do - printf "%-20s %-20s %-20s\n" "$name" "$instance" "$version" - done) - else - list_apps "$filter_name" "$filter_instance" $vars - fi -} - -method_list_versions_usage() { - if [ -n "$1" ] - then - echo "Error:" "$@" >&2 - fi - - echo "usage: list-versions -n name -i instance [-P]" >&2 - exit 1 -} - -method_list_versions() { - local filter_name="$1"; shift - local instance="$1"; shift - local version - local mode="pretty" - - if [ $# -eq 0 ] - then - method_list_versions_usage - fi - - while getopts "n:i:P" opt - do - case $opt in - n) - name=$OPTARG - ;; - i) - instance=$OPTARG - ;; - v) - version=$OPTARG - ;; - P) - mode="parseable" - ;; - \?) - method_list_versions_usage "Invalid option: -$OPTARG" - ;; - esac - done - - assert_is_instance method_list_versions_usage "$name" "$instance" "no" - - if [ $mode = "pretty" ] - then - echo "Available versions for $name/$instance:" - fi - - find_versions $name $instance - - return 0 -} - -method_instance_usage() { - if [ -n "$1" ] - then - echo "Error:" $@ >&2 - fi - - echo "usage: $0 instance <method>" >&2 - echo "" >&2 - echo "Available methods:" >&2 - echo " install - Installs an application" >&2 - echo " list - List all installed applications" >&2 - echo " list-versions - List all available versions for a single application" >&2 - echo " set-current - Set the current version" >&2 -} - -method_instance() { - local name="$1"; shift - local instance="$1"; shift - local method="$1" - - if [ $# -gt 0 ] - then - shift - fi - - case "$method" in - install) method_install "$name" "$instance" "$@" ;; - list) method_list "$name" "$instance" "$@" ;; - list-versions) method_list_versions "$name" "$instance" "$@" ;; - set-current) method_set_current "$name" "$instance" "$@" ;; - *) - if [ -z "$method" ] - then - method_instance_usage - else - method_instance_usage "Unknown method $method" - fi - ;; - esac - exit $? -} diff --git a/.app/lib/app-operate b/.app/lib/app-operate deleted file mode 100644 index 2b1408d..0000000 --- a/.app/lib/app-operate +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -operate_usage() { - if [ -n "$1" ] - then - echo "Error:" "$@" >&2 - fi - - echo "usage: $0 [operate method] -n name -i instance" >&2 - exit 1 -} - -method_operate_usage() { - if [ -n "$1" ] - then - echo "Error:" $@ >&2 - fi - - echo "usage: $0 operate <operate method>" >&2 - echo "" >&2 - echo "Available operate methods:" >&2 - echo " start" >&2 - echo " stop" >&2 - echo " restart" >&2 - echo " status" >&2 -} - -method_operate() { - local name="$1"; shift - local instance="$1"; shift - local method="$1" - - if [ $# -gt 0 ] - then - shift - fi - - bin=`get_conf $apps $name $instance app.method` - - if [ -z "$bin" ] - then - bin=$APPSH_HOME/.app/lib/pid-method - fi - - if [ ! -x "$name/$instance/current/$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 - exit $? -} diff --git a/.app/lib/pid-method b/.app/lib/pid-method deleted file mode 100755 index 29f6b4f..0000000 --- a/.app/lib/pid-method +++ /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 $? |