use central deploy lock in git plugin

This commit is contained in:
Michael Hobbs
2016-06-17 16:10:51 -07:00
parent 4f8c1b64c9
commit fcefd4a29b
4 changed files with 30 additions and 43 deletions

View File

@@ -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() {

View File

@@ -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 "$@"
;;

View File

@@ -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-*"

View File

@@ -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
}