aboutsummaryrefslogtreecommitdiff
path: root/libexec
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
commit110ffae47db27a49bbc43f86ba3737bccc1b3085 (patch)
treed74934b12e2904b8aff5fe87421a6501b8ea5c8e /libexec
parentca27d6f6d9ccc35bf55db3d360d1d464d5e206e7 (diff)
downloadapp.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.gz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.bz2
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.xz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.zip
o Rewriting most of this stuff to make it feel more like git.
Diffstat (limited to 'libexec')
-rwxr-xr-xlibexec/app-cat-conf47
-rwxr-xr-xlibexec/app-grep-path13
-rwxr-xr-xlibexec/app-install-file172
-rwxr-xr-xlibexec/app-resolver-maven231
4 files changed, 463 insertions, 0 deletions
diff --git a/libexec/app-cat-conf b/libexec/app-cat-conf
new file mode 100755
index 0000000..78d3c36
--- /dev/null
+++ b/libexec/app-cat-conf
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+if [[ $APPSH_HOME == "" ]]
+then
+ APPSH_HOME=`dirname "$0"`
+ APPSH_HOME=`cd "$APPSH_HOME/.." && pwd`
+fi
+
+set -e
+
+key_expr="[a-zA-Z][_a-zA-Z0-9]*"
+
+file=.app/config
+
+while getopts "f:n:" opt
+do
+ case $opt in
+ f)
+ file=$OPTARG
+ ;;
+ n)
+ name=$OPTARG
+ ;;
+ \?)
+ echo "Invalid option: $OPTARG" >&2
+ exit 1
+ ;;
+ esac
+done
+
+if [ -z "$name" ]
+then
+ filter="s,^[ ]*\($key_expr\.$key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p"
+else
+ filter="s,^\($name\)=\(.*\),\1=\2,p"
+fi
+
+if [[ $APPSH_DEFAULT_CONFIG == "" ]]
+then
+ APPSH_DEFAULT_CONFIG=$APPSH_HOME/lib/default-config
+fi
+
+# The awk script makes sure each key only appears once
+cat "$file" "$APPSH_DEFAULT_CONFIG" | \
+ sed -n -e "$filter" $extra | \
+ awk -F = ' (!($1 in a)){a[$1]; print }' | \
+ sort
diff --git a/libexec/app-grep-path b/libexec/app-grep-path
new file mode 100755
index 0000000..f5e8287
--- /dev/null
+++ b/libexec/app-grep-path
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+# A command line wrapper around get grep_path function
+
+if [[ $APPSH_HOME == "" ]]
+then
+ APPSH_HOME=`dirname "$0"`
+ APPSH_HOME=`cd "$APPSH_HOME/.." && pwd`
+fi
+
+. $APPSH_HOME/lib/common
+
+grep_path "$1" "$2"
diff --git a/libexec/app-install-file b/libexec/app-install-file
new file mode 100755
index 0000000..6c06aa7
--- /dev/null
+++ b/libexec/app-install-file
@@ -0,0 +1,172 @@
+#!/bin/bash
+
+if [[ $APPSH_HOME == "" ]]
+then
+ APPSH_HOME=`dirname "$0"`
+ APPSH_HOME=`cd "$APPSH_HOME/.." && pwd`
+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
+}
+
+if [ $# -lt 2 ]
+then
+ method_install_usage
+fi
+
+case "$resolver" in
+ maven)
+ ;;
+ 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 versions/$resolved_version ]
+then
+ echo "Version $resolved_version is already installed"
+ exit 1
+fi
+
+mkdir -p versions/$resolved_version
+
+echo "Unpacking..."
+unzip -q -d versions/$resolved_version $zip_file
+
+if [ ! -d 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 current
+ln -s versions/$resolved_version/root current
+
+if [ -d current/bin ]
+then
+ (
+ cd $name/$instance/current
+ find bin -type f | xargs chmod +x
+ )
+fi
+
+(
+ cd 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_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
diff --git a/libexec/app-resolver-maven b/libexec/app-resolver-maven
new file mode 100755
index 0000000..cb137fa
--- /dev/null
+++ b/libexec/app-resolver-maven
@@ -0,0 +1,231 @@
+#!/bin/bash -e
+
+set -u
+
+if [[ $APPSH_HOME == "" ]]
+then
+ APPSH_HOME=`dirname "$0"`
+ APPSH_HOME=`cd "$APPSH_HOME/.." && pwd`
+fi
+
+usage() {
+ message=${1-}
+
+ if [ ! -z "$message" ]
+ then
+ echo $message
+ fi
+
+ echo "usage: $subapp init -r <repo> <maven url>"
+ echo "usage: $subapp resolve-version"
+ echo "usage: $subapp download-version -v <version> -f <download target>"
+ exit 1
+}
+
+slash() {
+ echo $1 | sed "s,\.,/,g"
+}
+
+# TODO: support file:// repositories
+# TODO: look in the local repository first
+get() {
+ local url=$1; shift
+ local file=$1; shift
+
+ if [[ $url == file://* ]]
+ then
+ get_file "$url" "$file"
+ else
+ get_http "$url" "$file"
+ fi
+}
+
+get_file() {
+ url=${1:7}
+
+ cp "$url" "$2"
+}
+
+get_http() {
+ 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
+}
+
+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
+}
+
+resolve_version() {
+ local group_id=`app-conf get maven.group_id`
+ local artifact_id=`app-conf get maven.artifact_id`
+ local version=`app-conf get maven.version`
+
+ repo=`app-conf get maven.repo`
+
+ if [[ ! $version == *-SNAPSHOT ]]
+ then
+ app-conf set app.version "$version"
+ exit 0
+ fi
+
+ echo "Resolving version $version..."
+ resolve_snapshot $group_id $artifact_id $version
+}
+
+resolve_snapshot() {
+ local group_id=$1; shift
+ local artifact_id=$1; shift
+ local version=$1; shift
+
+ repo=`app-conf get maven.repo`
+
+ local group_id_slash=`slash $group_id`
+
+ local base_path=$group_id_slash/$artifact_id/$version
+
+ mkdir -p .app/cache/$base_path
+
+ local l=.app/cache/$base_path/maven-metadata.xml
+ local r=$repo/$base_path/maven-metadata.xml
+
+ get $r $l
+# x=`xmlstarlet sel -t -m '//snapshotVersion[extension[text()="zip"]]' -v value $l`
+ set -- `xmlstarlet sel -t -m '/metadata/versioning/snapshot' -v "timestamp|buildNumber" $l`
+ snapshot_version="$1-$2"
+
+ if [[ $snapshot_version == "" ]]
+ then
+ echo "Unable to resolve SNAPSHOT version for $group_id:$artifact_id:$version"
+ exit 1
+ fi
+
+ app-conf set maven.snapshotVersion "$snapshot_version"
+ app-conf set app.version "${version%-SNAPSHOT}-$snapshot_version"
+}
+
+download_version() {
+ resolved_version=""
+ target=""
+ while getopts "v:f:" opt
+ do
+ case $opt in
+ v)
+ resolved_version="$OPTARG"
+ ;;
+ f)
+ target="$OPTARG"
+ ;;
+ *)
+ usage "Invalid option: $OPTARG"
+ ;;
+ esac
+ done
+
+ if [[ $resolved_version == "" || $target == "" ]]
+ then
+ usage
+ fi
+
+ repo=`app-conf get maven.repo`
+ group_id=`app-conf get maven.group_id`
+ artifact_id=`app-conf get maven.artifact_id`
+ version=`app-conf get maven.version`
+
+ group_id_slash=`slash $group_id`
+ base_path=$group_id_slash/$artifact_id/$version
+
+ mkdir -p .app/cache/$base_path
+
+ l=.app/cache/$base_path/$artifact_id-$resolved_version.zip
+ r=$repo/$base_path/$artifact_id-$resolved_version.zip
+
+ echo "Downloading $group_id:$artifact_id:$resolved_version..."
+ get $r $l
+
+ ln -s "`pwd`/$l" "$target"
+}
+
+init() {
+ while getopts "r:" opt
+ do
+ case $opt in
+ r)
+ app-conf set maven.repo "$OPTARG"
+ shift 2
+ OPTIND=1
+ ;;
+ *)
+ usage "Invalid option: $OPTARG"
+ ;;
+ esac
+ done
+
+ x=`echo $1 | tr ":" " "`; set -- $x
+ group_id=$1
+ artifact_id=$2
+ version=$3
+
+ if [ -z "$group_id" -o -z "$artifact_id" -o -z "$version" ]
+ then
+ usage "Invalid Maven coordinates: $coordinates"
+ fi
+
+ app-conf set maven.group_id "$group_id"
+ app-conf set maven.artifact_id "$artifact_id"
+ app-conf set maven.version "$version"
+}
+
+subapp="$0";
+command="$1"; shift
+
+case "$command" in
+ init)
+ init "$@"
+ ;;
+ resolve-version)
+ resolve_version
+ ;;
+ download-version)
+ download_version "$@"
+ ;;
+ *)
+ usage
+ ;;
+esac
+
+exit 0