diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 81a14ae57..a001ab3fa 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -173,6 +173,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `builder-create-dokku-image` + +- Description: Allows modification of the configured dokku-image +- Invoked by: `dokku deploy` +- Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + ### `builder-detect` - Description: Allows overriding the auto-detected `herokuish` builder in favor of a custom one. Dockerfile gets lowest builder precedence. @@ -191,9 +206,9 @@ if [[ -f "$SOURCECODE_WORK_DIR/project.toml" ]]; then fi ``` -### `builder-create-dokku-image` +### `builder-dokku-image` -- Description: Allows modification of the configured dokku-image +- Description: Allows modification of the used dokku-image. - Invoked by: `dokku deploy` - Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE` - Example: @@ -206,11 +221,26 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` -### `builder-dokku-image` +### `builder-image-is-cnb` -- Description: Allows modification of the used dokku-image. -- Invoked by: `dokku deploy` -- Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE` +- Description: Checks if an image is cnb-based +- Invoked by: unit tests +- Arguments: `$APP` `$IMAGE_TAG` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `builder-image-is-herokuish` + +- Description: Checks if an image is herokuish-based +- Invoked by: unit tests +- Arguments: `$APP` `$IMAGE_TAG` - Example: ```shell diff --git a/plugins/20_events/builder-image-is-cnb b/plugins/20_events/builder-image-is-cnb new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/builder-image-is-cnb @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/20_events/builder-image-is-herokuish b/plugins/20_events/builder-image-is-herokuish new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/builder-image-is-herokuish @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/builder/.gitignore b/plugins/builder/.gitignore index 6f788ece8..1dd54d973 100644 --- a/plugins/builder/.gitignore +++ b/plugins/builder/.gitignore @@ -7,4 +7,4 @@ /install /post-* /report -/builder-detect +/builder-* diff --git a/plugins/builder/Makefile b/plugins/builder/Makefile index b8a598628..2b9139e04 100644 --- a/plugins/builder/Makefile +++ b/plugins/builder/Makefile @@ -1,5 +1,5 @@ SUBCOMMANDS = subcommands/report subcommands/set -TRIGGERS = triggers/builder-detect triggers/core-post-extract triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report +TRIGGERS = triggers/builder-detect triggers/builder-image-is-cnb triggers/builder-image-is-herokuish triggers/core-post-extract triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report BUILD = commands subcommands triggers PLUGIN_NAME = builder diff --git a/plugins/builder/src/triggers/triggers.go b/plugins/builder/src/triggers/triggers.go index 56c623a05..25fba8921 100644 --- a/plugins/builder/src/triggers/triggers.go +++ b/plugins/builder/src/triggers/triggers.go @@ -21,6 +21,14 @@ func main() { case "builder-detect": appName := flag.Arg(0) err = builder.TriggerBuilderDetect(appName) + case "builder-image-is-cnb": + appName := flag.Arg(0) + image := flag.Arg(1) + err = builder.TriggerBuilderImageIsCNB(appName, image) + case "builder-image-is-herokuish": + appName := flag.Arg(0) + image := flag.Arg(1) + err = builder.TriggerBuilderImageIsHerokuish(appName, image) case "core-post-extract": appName := flag.Arg(0) sourceWorkDir := flag.Arg(1) diff --git a/plugins/builder/triggers.go b/plugins/builder/triggers.go index 8a0b7f5af..0ce1e6454 100644 --- a/plugins/builder/triggers.go +++ b/plugins/builder/triggers.go @@ -26,6 +26,28 @@ func TriggerBuilderDetect(appName string) error { return nil } +// TriggerBuilderImageIsCNB prints true if an image is cnb based, false otherwise +func TriggerBuilderImageIsCNB(appName string, image string) error { + if common.IsImageCnbBased(image) { + fmt.Println("true") + } else { + fmt.Println("false") + } + + return nil +} + +// TriggerBuilderImageIsHerokuish prints true if an image is herokuish based, false otherwise +func TriggerBuilderImageIsHerokuish(appName string, image string) error { + if common.IsImageHerokuishBased(image, appName) { + fmt.Println("true") + } else { + fmt.Println("false") + } + + return nil +} + // TriggerCorePostExtract moves a configured build-dir to be in the app root dir func TriggerCorePostExtract(appName string, sourceWorkDir string) error { buildDir := strings.Trim(reportComputedBuildDir(appName), "/") diff --git a/plugins/common/functions b/plugins/common/functions index a7ef0324d..3e0b6daf0 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -357,41 +357,19 @@ is_image_cnb_based() { declare desc="returns true if app image is based on cnb" declare IMAGE="$1" - if [[ -z "$IMAGE" ]]; then + + if [[ "$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet image-is-cnb-based "$IMAGE")" != "true" ]]; then return 1 fi - - CNB_STACK_ID="$("$DOCKER_BIN" image inspect --format '{{ index .Config.Labels "io.buildpacks.stack.id" }}' "$IMAGE")" - if [[ -n "$CNB_STACK_ID" ]]; then - return 0 - fi - - return 1 } is_image_herokuish_based() { declare desc="returns true if app image is based on herokuish or a buildpack-like env" declare IMAGE="$1" APP="$2" - local DOKKU_APP_USER - local USER_VALUE - if [[ -z "$IMAGE" ]]; then + if [[ "$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet image-is-herokuish-based "$IMAGE" "$APP")" != "true" ]]; then return 1 fi - - if is_image_cnb_based "$IMAGE"; then - return 0 - fi - - if [[ -n "$APP" ]]; then - DOKKU_APP_USER=$(plugn trigger config-get "$APP" DOKKU_APP_USER || true) - fi - DOKKU_APP_USER=${DOKKU_APP_USER:="herokuishuser"} - - # due to how the build process works, all herokuish images have the Environment variable USER=$DOKKU_APP_USER - USER_VALUE="$("$DOCKER_BIN" image inspect --format '{{range .Config.Env}}{{if eq . "USER='"$DOKKU_APP_USER"'"}}{{println .}}{{end}}{{end}}' "$IMAGE")" - [[ "$USER_VALUE" == "" ]] && return 1 - return 0 } get_docker_version() { diff --git a/plugins/common/src/common/common.go b/plugins/common/src/common/common.go index 4b4c078dc..25095fde1 100644 --- a/plugins/common/src/common/common.go +++ b/plugins/common/src/common/common.go @@ -32,6 +32,21 @@ func main() { if !common.IsDeployed(appName) { err = fmt.Errorf("App %v not deployed", appName) } + case "image-is-cnb-based": + image := flag.Arg(1) + if common.IsImageCnbBased(image) { + fmt.Print("true") + } else { + fmt.Print("false") + } + case "image-is-herokuish-based": + image := flag.Arg(1) + appName := flag.Arg(2) + if common.IsImageHerokuishBased(image, appName) { + fmt.Print("true") + } else { + fmt.Print("false") + } case "scheduler-detect": appName := flag.Arg(1) if *global { diff --git a/tests/unit/core_1.bats b/tests/unit/core_1.bats index 9939e3fc6..9d2413484 100644 --- a/tests/unit/core_1.bats +++ b/tests/unit/core_1.bats @@ -70,40 +70,43 @@ EOF } @test "(core) image type detection (herokuish default user)" { - export DOKKU_ROOT - deploy_app - source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - source "$PLUGIN_CORE_AVAILABLE_PATH/config/functions" - - run is_image_herokuish_based "dokku/$TEST_APP" "$TEST_APP" + run deploy_app echo "output: $output" echo "status: $status" assert_success + + run /bin/bash -c "dokku plugin:trigger builder-image-is-herokuish $TEST_APP dokku/$TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output "true" } @test "(core) image type detection (herokuish custom user)" { - export DOKKU_ROOT - deploy_app + run deploy_app + echo "output: $output" + echo "status: $status" + assert_success + CID=$(<"$DOKKU_ROOT/$TEST_APP/CONTAINER.web.1") docker commit --change "ENV USER postgres" "$CID" "dokku/${TEST_APP}:latest" dokku config:set --no-restart "$TEST_APP" DOKKU_APP_USER=postgres source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_CORE_AVAILABLE_PATH/config/functions" - run is_image_herokuish_based "dokku/$TEST_APP" "$TEST_APP" + run /bin/bash -c "dokku plugin:trigger builder-image-is-herokuish $TEST_APP dokku/$TEST_APP" echo "output: $output" echo "status: $status" - assert_success + assert_output "true" } @test "(core) image type detection (dockerfile)" { - export DOKKU_ROOT - deploy_app dockerfile - source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - source "$PLUGIN_CORE_AVAILABLE_PATH/config/functions" - - run is_image_herokuish_based "dokku/$TEST_APP" "$TEST_APP" + run deploy_app dockerfile echo "output: $output" echo "status: $status" - assert_failure + assert_success + + run /bin/bash -c "dokku plugin:trigger builder-image-is-herokuish $TEST_APP dokku/$TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output "false" }