From fcefd4a29b1dd0fad9e320eb14a8c0174abec659 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 17 Jun 2016 16:10:51 -0700 Subject: [PATCH] use central deploy lock in git plugin --- plugins/common/functions | 22 ++++++++++++++-------- plugins/git/commands | 8 -------- plugins/git/functions | 38 +++++++++++++------------------------- plugins/git/receive-app | 5 +++-- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index e59ac6d4a..97279f404 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -824,19 +824,25 @@ merge_dedupe_list() { acquire_app_deploy_lock() { declare desc="acquire advisory lock for use in git/tar deploys" local APP="$1"; verify_app_name "$APP" + local LOCK_TYPE="${2:-waiting}" local APP_DEPLOY_LOCK_FD="200" local APP_DEPLOY_LOCK_FILE="$DOKKU_ROOT/$APP/.deploy.lock" - local APP_DEPLOY_LOCK_MSG="$APP is currently being deployed. Waiting..." + local LOCK_WAITING_MSG="$APP is currently being deployed. Waiting..." + local LOCK_FAILED_MSG="$APP is currently being deployed. Exiting..." local SHOW_MSG=true eval "exec $APP_DEPLOY_LOCK_FD>$APP_DEPLOY_LOCK_FILE" - while [[ $(flock -n "$APP_DEPLOY_LOCK_FD" &>/dev/null ; echo $?) -ne 0 ]]; do - if [[ "$SHOW_MSG" == "true" ]]; then - echo "$APP_DEPLOY_LOCK_MSG" - SHOW_MSG=false - fi - sleep 1 - done + if [[ "$LOCK_TYPE" == "waiting" ]]; then + while [[ $(flock -n "$APP_DEPLOY_LOCK_FD" &>/dev/null ; echo $?) -ne 0 ]]; do + if [[ "$SHOW_MSG" == "true" ]]; then + echo "$LOCK_WAITING_MSG" + SHOW_MSG=false + fi + sleep 1 + done + else + flock -n "$APP_DEPLOY_LOCK_FD" &>/dev/null || dokku_log_fail "$LOCK_FAILED_MSG" + fi } release_app_deploy_lock() { diff --git a/plugins/git/commands b/plugins/git/commands index 25502b961..7df19a867 100755 --- a/plugins/git/commands +++ b/plugins/git/commands @@ -11,14 +11,6 @@ case "$1" in git_upload_pack_cmd "$@" ;; - git-build) - git_build_cmd "$@" - ;; - - git-build-locked) - git_build_locked_cmd "$@" - ;; - git-*) git_glob_cmd "$@" ;; diff --git a/plugins/git/functions b/plugins/git/functions index bd779661e..a20f8414e 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -13,7 +13,6 @@ git_build_app_repo() { local GIT_BUILD_APP_REPO_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku_git.XXXX") trap 'rm -rf "$GIT_BUILD_APP_REPO_TMP_WORK_DIR" > /dev/null' RETURN INT TERM EXIT - # git clone - this method creates a new git repository and adds the primary # repo as a remote, then does a fetch depth=1 to avoid cloning # the entire repo @@ -67,6 +66,19 @@ git_hook_cmd() { done } +git_build() { + declare desc="setup and call git_build_app_repo" + local APP="$1" + if [[ $# -ge 2 ]]; then + local REF="$2" + else + local REF=$(< "$DOKKU_ROOT/$APP/refs/heads/master") + fi + + # shellcheck disable=SC2086 + git_build_app_repo $APP $REF +} + git_upload_pack_cmd() { declare desc="executes git-upload-pack" local cmd="git-upload-pack" @@ -76,30 +88,6 @@ git_upload_pack_cmd() { plugn trigger git-post-pull "$APP" } -git_build_cmd() { - declare desc="lock git-build" - local cmd="git-build" - 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 git-build-locked "$@" -} - -git_build_locked_cmd() { - declare desc="setup and call git_build_app_repo" - local cmd="git-build-locked" - local APP="$2" - if [[ $# -ge 3 ]]; then - local REF="$3" - else - local REF=$(< "$DOKKU_ROOT/$APP/refs/heads/master") - fi - # shellcheck disable=SC2086 - git_build_app_repo $APP $REF -} - git_glob_cmd() { declare desc="catch-all for any other git-* commands" local cmd="git-*" diff --git a/plugins/git/receive-app b/plugins/git/receive-app index 1af0457d2..652bc15b2 100755 --- a/plugins/git/receive-app +++ b/plugins/git/receive-app @@ -7,14 +7,15 @@ git_receive_app() { declare desc="git receive-app plugin trigger" local trigger="git_receive_app" local APP="$1"; local REV="$2" - local cmd="git-build" # Don't trigger git build if there is no git repository. if [[ ! -d "$DOKKU_ROOT/$APP/refs" ]]; then true else + acquire_app_deploy_lock "$APP" "exclusive" # shellcheck disable=SC2086 - git_build_cmd $cmd $APP $REV + git_build $APP $REV + release_app_deploy_lock "$APP" fi }