From 8e9d04ae36b96bc71fdb1a5a22a8ac00fcaebd28 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 25 Nov 2015 19:11:32 -0500 Subject: [PATCH] Add post-create hook Note that this is invoked *before* the application is deployed, so it's usage is quite different than a potential post-successful-create hook. --- docs/development/plugin-triggers.md | 21 +++++++++++++++++++++ plugins/apps/commands | 8 ++------ plugins/apps/functions | 13 +++++++++++++ plugins/git/commands | 2 ++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100755 plugins/apps/functions diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 1cf4fb339..4e916f8dd 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -380,6 +380,27 @@ docker commit $id $IMAGE > /dev/null dokku_log_info1 "Building UI Complete" ``` +### `post-create` + +- Description: Can be used to run commands after an application is created. +- Invoked by: `dokku apps:create` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash +# Runs a command to ensure that an app +# has a postgres database when it is starting + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +APP="$1"; +POSTGRES="$1" + +dokku postgres:create $POSTGRES +dokku postgres:link $POSTGRES $APP +``` + ### `post-deploy` - Description: Allows running of commands after a deploy has completed. Dokku core currently uses this to switch traffic on nginx. diff --git a/plugins/apps/commands b/plugins/apps/commands index d83b14421..e844f9020 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -2,6 +2,7 @@ [[ " apps apps:create apps:destroy help apps:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/apps/functions" case "$1" in apps) @@ -10,12 +11,7 @@ case "$1" in ;; apps:create) - [[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on" - [[ -d "$DOKKU_ROOT/$2" ]] && dokku_log_fail "Name is already taken" - APP="$2" - - mkdir -p "$DOKKU_ROOT/$APP" - echo "Creating $APP... done" + apps_create $2 ;; apps:destroy) diff --git a/plugins/apps/functions b/plugins/apps/functions new file mode 100755 index 000000000..9cdd16b8c --- /dev/null +++ b/plugins/apps/functions @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" + +apps_create() { + [[ -z $1 ]] && dokku_log_fail "Please specify an app to run the command on" + [[ -d "$DOKKU_ROOT/$1" ]] && dokku_log_fail "Name is already taken" + local APP="$1" + + mkdir -p "$DOKKU_ROOT/$APP" + echo "Creating $APP... done" + plugn trigger post-create $APP +} diff --git a/plugins/git/commands b/plugins/git/commands index 0850f601b..cc04e227b 100755 --- a/plugins/git/commands +++ b/plugins/git/commands @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/apps/functions" git_build_app_repo() { verify_app_name "$1" @@ -14,6 +15,7 @@ git_build_app_repo() { chmod 755 $TMP_WORK_DIR unset GIT_DIR GIT_WORK_TREE pushd $TMP_WORK_DIR > /dev/null + [[ ! -d "$DOKKU_ROOT/$APP" ]] && apps_create $APP git clone -q "$DOKKU_ROOT/$APP" "$TMP_WORK_DIR" &> /dev/null git config advice.detachedHead false git checkout "$REV" &> /dev/null