From f3e0c63f683581cc8cfb51e70b4f8779552a83dd Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Wed, 26 Aug 2015 00:06:57 -0400 Subject: [PATCH 1/5] First pass at tar plugin. --- plugins/tar/commands | 64 +++++++++++++++++++++++++++++++++++++++++ plugins/tar/receive-app | 12 ++++++++ 2 files changed, 76 insertions(+) create mode 100755 plugins/tar/commands create mode 100755 plugins/tar/receive-app diff --git a/plugins/tar/commands b/plugins/tar/commands new file mode 100755 index 000000000..d84d693da --- /dev/null +++ b/plugins/tar/commands @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$(dirname $0)/../common/functions" + +case "$1" in + tar-from|tar:from) + verify_app_name "$2" + APP=$2 + URL=$3 + shift 3 + curl -# --insecure -L "$URL" | dokku tar-in $APP $@ + ;; + + tar-in|tar:in) + APP="$2" + verify_app_name "$2" + tee "$DOKKU_ROOT/$APP/src.tar" | wc -c + pluginhook -p receive-app $APP + ;; + + tar-build) + verify_app_name "$2" + APP="$2"; APP_BUILD_LOCK="$DOKKU_ROOT/$APP/.build.lock" + 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) + APP="$2" + verify_app_name "$2" + shift 2 + + # clean up after ourselves + TMP_WORK_DIR=$(mktemp -d) + trap 'rm -rf "$TMP_WORK_DIR" > /dev/null' RETURN + + # extract tar file + chmod 755 $TMP_WORK_DIR + pushd $TMP_WORK_DIR > /dev/null + tar -x -C "$TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" + #find -name .git -prune -exec rm -rf {} \; > /dev/null + + if [[ -f Dockerfile ]] && [[ "$([[ -f .env ]] && grep -q BUILDPACK_URL .env; echo $?)" != "0" ]] && [[ ! -f ".buildpacks" ]]; then + dokku receive "$APP" "dockerfile" "$TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" + else + dokku receive "$APP" "herokuish" "$TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/" + fi + ;; + + help) + cat && cat<, Reads an tarball containing the app from stdin + tar:from , Loads an app tarball from url. +EOF + ;; + + *) + exit $DOKKU_NOT_IMPLEMENTED_EXIT + ;; + +esac diff --git a/plugins/tar/receive-app b/plugins/tar/receive-app new file mode 100755 index 000000000..ca2418290 --- /dev/null +++ b/plugins/tar/receive-app @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$(dirname $0)/../common/functions" + +APP="$1"; REV="$2" + +# Don't trigger tar build if there is no tarball. +if [ ! -f "$DOKKU_ROOT/$APP/src.tar" ]; then + cat +else + dokku tar-build $APP +fi From 0f1faf06a2448cb9c9bf66602ba38c8bd3f3a913 Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Wed, 26 Aug 2015 02:36:48 -0400 Subject: [PATCH 2/5] Write a unit test for the tar command. --- tests/unit/tar.bats | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/unit/tar.bats diff --git a/tests/unit/tar.bats b/tests/unit/tar.bats new file mode 100644 index 000000000..286e15a36 --- /dev/null +++ b/tests/unit/tar.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + create_app +} + +teardown() { + destroy_app +} + +deploy_app_tar() { + APP_TYPE="$1"; APP_TYPE=${APP_TYPE:="nodejs-express"} + GIT_REMOTE="$2"; GIT_REMOTE=${GIT_REMOTE:="dokku@dokku.me:$TEST_APP"} + TMP=$(mktemp -d -t "dokku.me.XXXXX") + rmdir $TMP && cp -r ./tests/apps/$APP_TYPE $TMP + cd $TMP + tar c . | ssh dokku@dokku.me tar:in $TEST_APP || destroy_app $? +} + +@test "(tar) basic deploy using tar:in" { + deploy_app_tar + + run bash -c "response=\"$(curl -s -S my-cool-guy-test-app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\"" + echo "output: "$output + echo "status: "$status + assert_success +} + From 712e1cada7eb1a84e8620983896d3ea6f6ccca45 Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Wed, 26 Aug 2015 02:37:52 -0400 Subject: [PATCH 3/5] - Fixed shellcheck error - Made test pass --- plugins/git/receive-app | 2 +- plugins/tar/commands | 2 +- plugins/tar/receive-app | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/git/receive-app b/plugins/git/receive-app index 328fbeb1d..3a95f6169 100755 --- a/plugins/git/receive-app +++ b/plugins/git/receive-app @@ -6,7 +6,7 @@ APP="$1"; REV="$2" # Don't trigger git build if there is no git repository. if [ ! -d "$DOKKU_ROOT/$APP/refs" ]; then - cat + cat || true else dokku git-build $APP $REV fi diff --git a/plugins/tar/commands b/plugins/tar/commands index d84d693da..cdf13b516 100755 --- a/plugins/tar/commands +++ b/plugins/tar/commands @@ -8,7 +8,7 @@ case "$1" in APP=$2 URL=$3 shift 3 - curl -# --insecure -L "$URL" | dokku tar-in $APP $@ + curl -# --insecure -L "$URL" | dokku tar-in $APP "$@" ;; tar-in|tar:in) diff --git a/plugins/tar/receive-app b/plugins/tar/receive-app index ca2418290..1c9428ccb 100755 --- a/plugins/tar/receive-app +++ b/plugins/tar/receive-app @@ -6,7 +6,7 @@ APP="$1"; REV="$2" # Don't trigger tar build if there is no tarball. if [ ! -f "$DOKKU_ROOT/$APP/src.tar" ]; then - cat + cat || true else dokku tar-build $APP fi From 5a51246007dc8c65ccd9b923253366c8f86dc587 Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Wed, 26 Aug 2015 20:08:20 -0400 Subject: [PATCH 4/5] - Strip prefix directories from tarball --- plugins/tar/commands | 9 ++++++++- tests/unit/tar.bats | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/plugins/tar/commands b/plugins/tar/commands index cdf13b516..b010262ca 100755 --- a/plugins/tar/commands +++ b/plugins/tar/commands @@ -40,7 +40,14 @@ case "$1" in # extract tar file chmod 755 $TMP_WORK_DIR pushd $TMP_WORK_DIR > /dev/null - tar -x -C "$TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" + + # Detect a common prefix that all files in the tar have, and strip off each directory found in it + COMMON_PREFIX=$(tar -tf "$DOKKU_ROOT/$APP/src.tar" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D') + 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 "$TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" --strip-components=$BOGUS_PARTS #find -name .git -prune -exec rm -rf {} \; > /dev/null if [[ -f Dockerfile ]] && [[ "$([[ -f .env ]] && grep -q BUILDPACK_URL .env; echo $?)" != "0" ]] && [[ ! -f ".buildpacks" ]]; then diff --git a/tests/unit/tar.bats b/tests/unit/tar.bats index 286e15a36..fbbb3f20f 100644 --- a/tests/unit/tar.bats +++ b/tests/unit/tar.bats @@ -12,19 +12,27 @@ teardown() { deploy_app_tar() { APP_TYPE="$1"; APP_TYPE=${APP_TYPE:="nodejs-express"} - GIT_REMOTE="$2"; GIT_REMOTE=${GIT_REMOTE:="dokku@dokku.me:$TEST_APP"} TMP=$(mktemp -d -t "dokku.me.XXXXX") rmdir $TMP && cp -r ./tests/apps/$APP_TYPE $TMP cd $TMP - tar c . | ssh dokku@dokku.me tar:in $TEST_APP || destroy_app $? + shift 1 + tar c . $* | ssh dokku@dokku.me tar:in $TEST_APP || destroy_app $? } -@test "(tar) basic deploy using tar:in" { - deploy_app_tar +@test "(tar) non-tarbomb deploy using tar:in" { + deploy_app_tar nodejs-express --transform 's,^,prefix/,' run bash -c "response=\"$(curl -s -S my-cool-guy-test-app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\"" echo "output: "$output echo "status: "$status - assert_success + assert_success } +@test "(tar) tarbomb deploy using tar:in" { + deploy_app_tar nodejs-express + + run bash -c "response=\"$(curl -s -S my-cool-guy-test-app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\"" + echo "output: "$output + echo "status: "$status + assert_success +} From b687e3297d394bf9c91f1e0f477da338ddcd86cb Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Sat, 29 Aug 2015 00:33:27 -0400 Subject: [PATCH 5/5] Cygwin tar can sometimes make tarballs with permissions where the owner doesnt have read rights on the files. Thats obviously no good. --- plugins/tar/commands | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/tar/commands b/plugins/tar/commands index b010262ca..f18898e66 100755 --- a/plugins/tar/commands +++ b/plugins/tar/commands @@ -48,6 +48,7 @@ case "$1" in dokku_log_info1_quiet "Striping $BOGUS_PARTS worth of directories from tarball" tar -x -C "$TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" --strip-components=$BOGUS_PARTS + chmod -R u+r "$TMP_WORK_DIR" #find -name .git -prune -exec rm -rf {} \; > /dev/null if [[ -f Dockerfile ]] && [[ "$([[ -f .env ]] && grep -q BUILDPACK_URL .env; echo $?)" != "0" ]] && [[ ! -f ".buildpacks" ]]; then