Merge pull request #3114 from dokku/dokku-git-rev

Enhanced core git plugin
This commit is contained in:
Jose Diaz-Gonzalez
2018-03-31 03:42:49 -04:00
committed by GitHub
14 changed files with 425 additions and 71 deletions

View File

@@ -0,0 +1,94 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
fn-plugin-property-delete() {
declare desc="delete a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3"
if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
rm -f "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
}
fn-plugin-property-destroy() {
declare desc="destroy the properties for an app"
declare PLUGIN="$1" APP="$2"
if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]]; then
return 1
fi
if [[ "$APP" == "_all_" ]]; then
rm -rf "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
else
rm -rf "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}"
fi
}
fn-plugin-property-get() {
declare desc="returns the property store value for an app"
declare PLUGIN="$1" APP="$2" KEY="$3" DEFAULT="$4"
local CONFIG_VALUE
if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
CONFIG_VALUE="$(fn-plugin-property-read "$PLUGIN" "$APP" "$KEY" || true)"
if [[ -z "$CONFIG_VALUE" ]]; then
CONFIG_VALUE="$DEFAULT"
fi
echo "$CONFIG_VALUE"
}
fn-plugin-property-setup() {
declare desc="creates the plugin config root"
declare PLUGIN="$1"
if [[ -z "$PLUGIN" ]]; then
return 1
fi
mkdir -p "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
chmod 0755 "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
if [[ -z "$DOKKU_SYSTEM_GROUP" ]]; then
export DOKKU_SYSTEM_GROUP=dokku
fi
if [[ -z "$DOKKU_SYSTEM_USER" ]]; then
export DOKKU_SYSTEM_USER=dokku
fi
chown "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
}
fn-plugin-property-read() {
declare desc="read a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3"
if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
if [[ ! -f "${DOKKU_LIB_ROOT:?}/config/${PLUGIN}/${APP}/${KEY}" ]]; then
return 1
fi
cat "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
}
fn-plugin-property-write() {
declare desc="read a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4"
if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
mkdir -p "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}"
echo "$VALUE" > "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
chmod 600 "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
}

View File

@@ -1,29 +1,30 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$PLUGIN_AVAILABLE_PATH/apps/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
use_git_worktree() {
declare desc="detects whether to use git worktree"
local GIT_VERSION MAJOR_VERSION MINOR_VERSION
declare desc="detects whether to use git worktree"
local GIT_VERSION MAJOR_VERSION MINOR_VERSION
GIT_VERSION=$(git --version | awk '{split($0,a," "); print a[3]}')
MAJOR_VERSION=$(echo "$GIT_VERSION" | awk '{split($0,a,"."); print a[1]}')
MINOR_VERSION=$(echo "$GIT_VERSION" | awk '{split($0,a,"."); print a[2]}')
GIT_VERSION=$(git --version | awk '{split($0,a," "); print a[3]}')
MAJOR_VERSION=$(echo "$GIT_VERSION" | awk '{split($0,a,"."); print a[1]}')
MINOR_VERSION=$(echo "$GIT_VERSION" | awk '{split($0,a,"."); print a[2]}')
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
return 0
elif [[ "$MAJOR_VERSION" -eq "2" ]] && [[ "$MINOR_VERSION" -ge "11" ]]; then
return 0
else
return 1
fi
if [[ "$MAJOR_VERSION" -ge "3" ]]; then
return 0
elif [[ "$MAJOR_VERSION" -eq "2" ]] && [[ "$MINOR_VERSION" -ge "11" ]]; then
return 0
else
return 1
fi
}
git_build_app_repo() {
declare desc="builds local git app repo for app"
declare APP="$1"; local REV="$2"
declare APP="$1" REV="$2"
local DOKKU_GLOBAL_DISABLE_AUTOCREATE
verify_app_name "$APP"
@@ -104,8 +105,8 @@ git_deploy_branch() {
local cmd="git-hook"
local APP="$1"
local DOKKU_DEPLOY_BRANCH="$(config_get "$APP" DOKKU_DEPLOY_BRANCH || true)"
local DOKKU_GLOBAL_DEPLOY_BRANCH="$(config_get --global DOKKU_DEPLOY_BRANCH || true)"
local DOKKU_DEPLOY_BRANCH="$(fn-plugin-property-get "git" "$APP" "deploy-branch" "")"
local DOKKU_GLOBAL_DEPLOY_BRANCH="$(fn-plugin-property-get "git" "--global" "deploy-branch" "")"
if [[ -n "$DOKKU_DEPLOY_BRANCH" ]]; then
echo "$DOKKU_DEPLOY_BRANCH"
elif [[ -n "$DOKKU_GLOBAL_DEPLOY_BRANCH" ]]; then
@@ -152,8 +153,12 @@ git_hook_cmd() {
git_build() {
declare desc="setup and call git_build_app_repo"
local APP="$1"
local DOKKU_DEPLOY_BRANCH REF
local DOKKU_DEPLOY_BRANCH ENV_VAR_NAME REF
if [[ $# -ge 2 ]]; then
ENV_VAR_NAME="$(fn-plugin-property-get "git" "$APP" "rev-env-var" "GIT_REV")"
if [[ -n "$ENV_VAR_NAME" ]]; then
config_set --no-restart "$APP" "${ENV_VAR_NAME}=${REV}" > /dev/null
fi
local REF="$2"
else
DOKKU_DEPLOY_BRANCH="$(git_deploy_branch "$APP")"

33
plugins/git/install Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
trigger-git-install() {
declare desc="installs the git plugin"
fn-plugin-property-setup "git"
}
migrate_git_vars_0_12_0() {
declare desc="migrates git config variables from 0.11.x"
local APPS="$(dokku_apps)"
local DOKKU_DEPLOY_BRANCH app
DOKKU_DEPLOY_BRANCH=$(config_get --global DOKKU_DEPLOY_BRANCH || true)
if [[ -n "$DOKKU_DEPLOY_BRANCH" ]]; then
fn-plugin-property-write "git" --global "deploy-branch" "$DOKKU_DEPLOY_BRANCH"
config_unset --global DOKKU_DEPLOY_BRANCH || true
fi
for app in $APPS; do
DOKKU_DEPLOY_BRANCH=$(config_get "$app" DOKKU_DEPLOY_BRANCH || true)
if [[ -n "$DOKKU_DEPLOY_BRANCH" ]]; then
fn-plugin-property-write "git" "$APP" "deploy-branch" "$DOKKU_DEPLOY_BRANCH"
config_unset --no-restart "$app" DOKKU_DEPLOY_BRANCH || true
fi
done
}
trigger-git-install "$@"
migrate_git_vars_0_12_0 "$@"

42
plugins/git/internal-functions Executable file
View File

@@ -0,0 +1,42 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
cmd-git-report() {
declare desc="Display the configured git status for an application"
declare cmd="git:report" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
declare APP="$1" INFO_FLAG="$2"
local flag flag_map key match valid_flags value_exists
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$APP"
flag_map=(
"--git-rev-env-var: $(fn-plugin-property-get "$APP" "rev-env-var" "GIT_REV")"
"--git-deploy-branch: $(fn-plugin-property-get "$APP" "deploy-branch" "master")"
"--git-global-deploy-branch: $(fn-plugin-property-get "--global" "deploy-branch" "master")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2_quiet "${APP} Git Information"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")"
done
else
match=false
value_exists=false
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
value=${flag#*: }
size="${#value}"
if [[ "$size" -ne 0 ]]; then
echo "$value" && match=true && value_exists=true
else
match=true
fi
fi
done
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
[[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
fi
}

11
plugins/git/post-delete Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
trigger-git-post-delete() {
declare desc="destroys the git properties for a given app"
declare APP="$1"
fn-plugin-property-destroy "git" "$APP"
}
trigger-git-post-delete "$@"

5
plugins/git/subcommands/report Executable file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
cmd-git-report "$@"

40
plugins/git/subcommands/set Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
fn-in-array() {
declare desc="return true if value ($1) is in list (all other arguments)"
local e
for e in "${@:2}"; do
[[ "$e" == "$1" ]] && return 0
done
return 1
}
git-set-cmd() {
declare desc="sets a git configuration value for an app"
local cmd="git:set" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
declare APP="$1" KEY="$2" VALUE="$3"
local VALID_KEYS=("deploy-branch" "rev-env-var")
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
[[ -z "$KEY" ]] && dokku_log_fail "No key specified"
if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then
dokku_log_fail "Invalid key specified, valid keys include: deploy-branch, rev-env-var"
fi
if [[ -n "$VALUE" ]]; then
dokku_log_info2_quiet "Setting ${KEY} to ${VALUE}"
fn-plugin-property-write "git" "$APP" "$KEY" "$VALUE"
else
dokku_log_info2_quiet "Unsetting ${KEY}"
fn-plugin-property-delete "git" "$APP" "$KEY"
if [[ "$KEY" == "enabled" ]]; then
fn-plugin-property-destroy "git" "$APP"
fi
fi
}
git-set-cmd "$@"