#!/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 cmd-git-allow-host() { declare desc="adds a host to known_hosts" local cmd="git:allow-host" [[ "$1" == "$cmd" ]] && shift 1 declare HOST="$1" [[ -z "$HOST" ]] && dokku_log_fail "Please supply a git host" ssh-keyscan -t rsa "$HOST" >> "$DOKKU_ROOT/.ssh/known_hosts" dokku_log_info1 "$HOST added to known hosts" } cmd-git-clone() { declare desc="creates an app from remote git repo" local cmd="git:clone" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" ARGS=() for arg in "$@"; do if [[ "$arg" == "--build" ]]; then FLAG="--build" continue fi ARGS+=("$arg") done APP="$1" GIT_REMOTE="$2" GIT_REF="$3" verify_app_name "$APP" if [[ -z "$GIT_REMOTE" ]]; then dokku_log_fail "Missing GIT_REMOTE parameter" return fi fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" if [[ "$FLAG" == "--build" ]]; then if [[ -n "$GIT_REF" ]]; then plugn trigger receive-app "$APP" "$GIT_REF" else plugn trigger receive-app "$APP" fi fi } cmd-git-fetch() { declare desc="fetch the app at an optional repo object and build the codebase" local cmd="git:fetch" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4" ARGS=() for arg in "$@"; do if [[ "$arg" == "--build" ]]; then FLAG="--build" continue fi ARGS+=("$arg") done APP="$1" GIT_REMOTE="$2" GIT_REF="$3" verify_app_name "$APP" if [[ -z "$GIT_REMOTE" ]]; then dokku_log_fail "Missing GIT_REMOTE parameter" return fi fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" if [[ "$FLAG" == "--build" ]]; then if [[ -n "$GIT_REF" ]]; then plugn trigger receive-app "$APP" "$GIT_REF" else plugn trigger receive-app "$APP" fi fi } cmd-git-public-key() { declare desc="outputs the dokku public deploy key" local cmd="git:public-key" [[ "$1" == "$cmd" ]] && shift 1 if [[ ! -f "$DOKKU_ROOT/.ssh/id_rsa.pub" ]]; then dokku_log_fail "There is no deploy key" fi cat "$DOKKU_ROOT/.ssh/id_rsa.pub" } cmd-git-report() { declare desc="displays a git report for one or more apps" declare cmd="git:report" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" INFO_FLAG="$2" local INSTALLED_APPS=$(dokku_apps) if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then INFO_FLAG="$APP" APP="" fi if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then INFO_FLAG="true" fi if [[ -z "$APP" ]]; then for app in $INSTALLED_APPS; do cmd-git-report-single "$app" "$INFO_FLAG" | tee || true done else cmd-git-report-single "$APP" "$INFO_FLAG" fi } cmd-git-report-single() { declare APP="$1" INFO_FLAG="$2" local APP_DIR="$DOKKU_ROOT/$APP" if [[ "$INFO_FLAG" == "true" ]]; then INFO_FLAG="" fi verify_app_name "$APP" local flag_map=( "--git-deploy-branch: $(fn-plugin-property-get "git" "$APP" "deploy-branch" "master")" "--git-global-deploy-branch: $(fn-plugin-property-get "git" "--global" "deploy-branch" "master")" "--git-keep-git-dir: $(fn-plugin-property-get "git" "$APP" "keep-git-dir" "false")" "--git-rev-env-var: $(fn-plugin-property-get "git" "$APP" "rev-env-var" "GIT_REV")" "--git-sha: $(GIT_DIR="$APP_DIR" git rev-parse --short HEAD 2>/dev/null || false)" "--git-last-updated-at: $(fn-git-last-updated-at "$APP")" ) 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 "%-30s %-25s" "${key^}" "${flag#*: }")" done else local match=false local 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 } fn-git-clone() { declare desc="creates an app from remote git repo" declare APP="$1" GIT_REMOTE="$2" GIT_REF="$2" local APP_CLONE_ROOT="$PLUGIN_DATA_ROOT/$APP" local APP_ROOT="$DOKKU_ROOT/$APP" [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" if [[ -d "$DOKKU_ROOT/$APP/refs" ]]; then dokku_log_fail "The clone subcommand can only be executed for new applications" fi if [[ -d "$APP_CLONE_ROOT" ]]; then dokku_log_fail "Clone in progress" fi if [[ -z "$GIT_REF" ]]; then GIT_REF="$(fn-git-deploy-branch "$APP")" fi dokku_log_info1_quiet "Creating $APP from $GIT_REMOTE#$GIT_REF" trap 'rm -rf $APP_CLONE_ROOT > /dev/null' RETURN INT TERM EXIT git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT" if [[ "$GIT_REF" != "master" ]]; then pushd "$APP" > /dev/null git checkout -qq -b master popd > /dev/null fi rsync -a "$APP_CLONE_ROOT/.git/" "$APP_ROOT" fn-git-create-hook "$APP" } fn-git-fetch() { declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" local DOKKU_DEPLOY_BRANCH DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH." echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'" return fi pushd "$DOKKU_ROOT/$APP" > /dev/null git remote rm remote > /dev/null 2>&1 || true git remote add --mirror=fetch --no-tags remote "$GIT_REMOTE" if [[ -z "$GIT_REF" ]]; then git fetch remote "$DOKKU_DEPLOY_BRANCH" else git fetch remote git update-ref refs/heads/master "$GIT_REF" fi popd > /dev/null } fn-git-last-updated-at() { declare desc="retrieve the deploy branch for a given application" declare APP="$1" local DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" local HEAD_FILE="/home/dokku/test/refs/heads/$DOKKU_DEPLOY_BRANCH" if [[ -f "$HEAD_FILE" ]]; then stat -c %Y "$HEAD_FILE" fi } fn-git-deploy-branch() { declare desc="retrieve the deploy branch for a given application" local APP="$1" local DEFAULT_BRANCH="${2-master}" 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 echo "$DOKKU_GLOBAL_DEPLOY_BRANCH" else echo "$DEFAULT_BRANCH" fi }