From aca2540651a256dc5bcc887fb1a72f592e2010e0 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 12 Jun 2016 20:22:53 -0400 Subject: [PATCH] Move tar functions into sourceable functions file --- plugins/tar/functions | 70 ++++++++++++++++++++++++++++ plugins/tar/subcommands/build | 15 +----- plugins/tar/subcommands/build-locked | 36 +------------- plugins/tar/subcommands/from | 12 +---- plugins/tar/subcommands/in | 12 +---- 5 files changed, 74 insertions(+), 71 deletions(-) create mode 100755 plugins/tar/functions diff --git a/plugins/tar/functions b/plugins/tar/functions new file mode 100755 index 000000000..10a935ed4 --- /dev/null +++ b/plugins/tar/functions @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" + +tar_build_cmd() { + declare desc="calls tar:build-locked via command line" + local cmd="tar:build" + + verify_app_name "$2" + local APP="$2"; local APP_BUILD_LOCK="$DOKKU_ROOT/$APP/.build.lock" + local APP_BUILD_LOCK_MSG="$APP is currently being deployed or locked. Waiting..." + [[ $(flock -n "$APP_BUILD_LOCK" true &>/dev/null ; echo $?) -ne 0 ]] && echo "$APP_BUILD_LOCK_MSG" + + shift 1 + flock -o "$APP_BUILD_LOCK" dokku tar:build-locked "$@" +} + +tar_build_locked_cmd() { + declare desc="builds apps from tarball via command line" + local cmd="tar:build-locked" + local APP="$2" + + verify_app_name "$2" + shift 2 + + # clean up after ourselves + local TAR_BUILD_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku_tar.XXXX") + trap 'rm -rf "$TAR_BUILD_TMP_WORK_DIR" > /dev/null' RETURN INT TERM EXIT + + # extract tar file + chmod 755 "$TAR_BUILD_TMP_WORK_DIR" + pushd "$TAR_BUILD_TMP_WORK_DIR" > /dev/null + + # Detect a common prefix that all files in the tar have, and strip off each directory found in it + local COMMON_PREFIX=$(tar -tf "$DOKKU_ROOT/$APP/src.tar" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D') + local BOGUS_PARTS=$(echo "$COMMON_PREFIX " | awk 'BEGIN{FS="/"} {print NF-1}') + + dokku_log_info1_quiet "Striping $BOGUS_PARTS worth of directories from tarball" + + tar -x -C "$TAR_BUILD_TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" --strip-components="$BOGUS_PARTS" + chmod -R u+r "$TAR_BUILD_TMP_WORK_DIR" + + if [[ -f Dockerfile ]] && [[ "$([[ -f .env ]] && grep -q BUILDPACK_URL .env; echo $?)" != "0" ]] && [[ ! -f ".buildpacks" ]]; then + plugn trigger pre-receive-app "$APP" "dockerfile" "$TAR_BUILD_TMP_WORK_DIR" + dokku receive "$APP" "dockerfile" "$TAR_BUILD_TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" + else + plugn trigger pre-receive-app "$APP" "herokuish" "$TAR_BUILD_TMP_WORK_DIR" + dokku receive "$APP" "herokuish" "$TAR_BUILD_TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" + fi +} + +tar_from_cmd() { + declare desc="deploys app from tarball at URL via command line" + local cmd="tar:from" + verify_app_name "$2" + local APP=$2 + local URL=$3 + shift 3 + curl -# --insecure -L "$URL" | dokku tar:in "$APP" "$@" +} + +tar_in_cmd() { + declare desc="deploys app from tarball on STDIN via command line" + local cmd="tar:in" + local APP="$2" + + verify_app_name "$2" + tee "$DOKKU_ROOT/$APP/src.tar" | wc -c + plugn trigger receive-app "$APP" +} diff --git a/plugins/tar/subcommands/build b/plugins/tar/subcommands/build index ecb0b96e0..2fbab85a0 100755 --- a/plugins/tar/subcommands/build +++ b/plugins/tar/subcommands/build @@ -1,18 +1,5 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -tar_build_cmd() { - declare desc="calls tar:build-locked via command line" - local cmd="tar:build" - - verify_app_name "$2" - local APP="$2"; local APP_BUILD_LOCK="$DOKKU_ROOT/$APP/.build.lock" - local APP_BUILD_LOCK_MSG="$APP is currently being deployed or locked. Waiting..." - [[ $(flock -n "$APP_BUILD_LOCK" true &>/dev/null ; echo $?) -ne 0 ]] && echo "$APP_BUILD_LOCK_MSG" - - shift 1 - flock -o "$APP_BUILD_LOCK" dokku tar:build-locked "$@" -} +source "$PLUGIN_AVAILABLE_PATH/tar/functions" tar_build_cmd "$@" diff --git a/plugins/tar/subcommands/build-locked b/plugins/tar/subcommands/build-locked index ace52fe92..e660d95f1 100755 --- a/plugins/tar/subcommands/build-locked +++ b/plugins/tar/subcommands/build-locked @@ -1,39 +1,5 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -tar_build_locked_cmd() { - declare desc="builds apps from tarball via command line" - local cmd="tar:build-locked" - local APP="$2" - - verify_app_name "$2" - shift 2 - - # clean up after ourselves - local TAR_BUILD_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku_tar.XXXX") - trap 'rm -rf "$TAR_BUILD_TMP_WORK_DIR" > /dev/null' RETURN INT TERM EXIT - - # extract tar file - chmod 755 "$TAR_BUILD_TMP_WORK_DIR" - pushd "$TAR_BUILD_TMP_WORK_DIR" > /dev/null - - # Detect a common prefix that all files in the tar have, and strip off each directory found in it - local COMMON_PREFIX=$(tar -tf "$DOKKU_ROOT/$APP/src.tar" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D') - local BOGUS_PARTS=$(echo "$COMMON_PREFIX " | awk 'BEGIN{FS="/"} {print NF-1}') - - dokku_log_info1_quiet "Striping $BOGUS_PARTS worth of directories from tarball" - - tar -x -C "$TAR_BUILD_TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" --strip-components="$BOGUS_PARTS" - chmod -R u+r "$TAR_BUILD_TMP_WORK_DIR" - - if [[ -f Dockerfile ]] && [[ "$([[ -f .env ]] && grep -q BUILDPACK_URL .env; echo $?)" != "0" ]] && [[ ! -f ".buildpacks" ]]; then - plugn trigger pre-receive-app "$APP" "dockerfile" "$TAR_BUILD_TMP_WORK_DIR" - dokku receive "$APP" "dockerfile" "$TAR_BUILD_TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" - else - plugn trigger pre-receive-app "$APP" "herokuish" "$TAR_BUILD_TMP_WORK_DIR" - dokku receive "$APP" "herokuish" "$TAR_BUILD_TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" - fi -} +source "$PLUGIN_AVAILABLE_PATH/tar/functions" tar_build_locked_cmd "$@" diff --git a/plugins/tar/subcommands/from b/plugins/tar/subcommands/from index c51b74fbf..9e66df76a 100755 --- a/plugins/tar/subcommands/from +++ b/plugins/tar/subcommands/from @@ -1,15 +1,5 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -tar_from_cmd() { - declare desc="deploys app from tarball at URL via command line" - local cmd="tar:from" - verify_app_name "$2" - local APP=$2 - local URL=$3 - shift 3 - curl -# --insecure -L "$URL" | dokku tar:in "$APP" "$@" -} +source "$PLUGIN_AVAILABLE_PATH/tar/functions" tar_from_cmd "$@" diff --git a/plugins/tar/subcommands/in b/plugins/tar/subcommands/in index 7bbdb5f87..d39277d75 100755 --- a/plugins/tar/subcommands/in +++ b/plugins/tar/subcommands/in @@ -1,15 +1,5 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -tar_in_cmd() { - declare desc="deploys app from tarball on STDIN via command line" - local cmd="tar:in" - local APP="$2" - - verify_app_name "$2" - tee "$DOKKU_ROOT/$APP/src.tar" | wc -c - plugn trigger receive-app "$APP" -} +source "$PLUGIN_AVAILABLE_PATH/tar/functions" tar_in_cmd "$@"