refactor: use a single implementation for checking cnb/herokuish images

Also use a plugin trigger for testing whether an image is herokuish or not rather than trying to mess around with sourced files.
This commit is contained in:
Jose Diaz-Gonzalez committed 2022-12-02 15:54:48 -05:00
1 parent a2cc6b84d6
commit c2ebf48524
10 files changed
+108 -50

No files matched your search

+36 -6
View File
@@ -173,6 +173,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO # 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` ### `builder-detect`
- Description: Allows overriding the auto-detected `herokuish` builder in favor of a custom one. Dockerfile gets lowest builder precedence. - 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 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` - Invoked by: `dokku deploy`
- Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE` - Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE`
- Example: - Example:
@@ -206,11 +221,26 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO # TODO
``` ```
### `builder-dokku-image` ### `builder-image-is-cnb`
- Description: Allows modification of the used dokku-image. - Description: Checks if an image is cnb-based
- Invoked by: `dokku deploy` - Invoked by: unit tests
- Arguments: `$BUILDER_TYPE` `$APP` `$SOURCECODE_WORK_DIR` `$DOKKU_IMAGE` - 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: - Example:
```shell ```shell
+1
View File
@@ -0,0 +1 @@
hook
+1
View File
@@ -0,0 +1 @@
hook
+1 -1
View File
@@ -7,4 +7,4 @@
/install /install
/post-* /post-*
/report /report
/builder-detect /builder-*
+1 -1
View File
@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/report subcommands/set 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 BUILD = commands subcommands triggers
PLUGIN_NAME = builder PLUGIN_NAME = builder
+8
View File
@@ -21,6 +21,14 @@ func main() {
case "builder-detect": case "builder-detect":
appName := flag.Arg(0) appName := flag.Arg(0)
err = builder.TriggerBuilderDetect(appName) 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": case "core-post-extract":
appName := flag.Arg(0) appName := flag.Arg(0)
sourceWorkDir := flag.Arg(1) sourceWorkDir := flag.Arg(1)
+22
View File
@@ -26,6 +26,28 @@ func TriggerBuilderDetect(appName string) error {
return nil 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 // TriggerCorePostExtract moves a configured build-dir to be in the app root dir
func TriggerCorePostExtract(appName string, sourceWorkDir string) error { func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
buildDir := strings.Trim(reportComputedBuildDir(appName), "/") buildDir := strings.Trim(reportComputedBuildDir(appName), "/")
+3 -25
View File
@@ -357,41 +357,19 @@ is_image_cnb_based() {
declare desc="returns true if app image is based on cnb" declare desc="returns true if app image is based on cnb"
declare IMAGE="$1" declare IMAGE="$1"
if [[ -z "$IMAGE" ]]; then
if [[ "$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet image-is-cnb-based "$IMAGE")" != "true" ]]; then
return 1 return 1
fi 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() { is_image_herokuish_based() {
declare desc="returns true if app image is based on herokuish or a buildpack-like env" declare desc="returns true if app image is based on herokuish or a buildpack-like env"
declare IMAGE="$1" APP="$2" 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 return 1
fi 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() { get_docker_version() {
+15
View File
@@ -32,6 +32,21 @@ func main() {
if !common.IsDeployed(appName) { if !common.IsDeployed(appName) {
err = fmt.Errorf("App %v not deployed", 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": case "scheduler-detect":
appName := flag.Arg(1) appName := flag.Arg(1)
if *global { if *global {
+20 -17
View File
@@ -70,40 +70,43 @@ EOF
} }
@test "(core) image type detection (herokuish default user)" { @test "(core) image type detection (herokuish default user)" {
export DOKKU_ROOT run deploy_app
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"
echo "output: $output" echo "output: $output"
echo "status: $status" echo "status: $status"
assert_success 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)" { @test "(core) image type detection (herokuish custom user)" {
export DOKKU_ROOT run deploy_app
deploy_app echo "output: $output"
echo "status: $status"
assert_success
CID=$(<"$DOKKU_ROOT/$TEST_APP/CONTAINER.web.1") CID=$(<"$DOKKU_ROOT/$TEST_APP/CONTAINER.web.1")
docker commit --change "ENV USER postgres" "$CID" "dokku/${TEST_APP}:latest" docker commit --change "ENV USER postgres" "$CID" "dokku/${TEST_APP}:latest"
dokku config:set --no-restart "$TEST_APP" DOKKU_APP_USER=postgres dokku config:set --no-restart "$TEST_APP" DOKKU_APP_USER=postgres
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/config/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 "output: $output"
echo "status: $status" echo "status: $status"
assert_success assert_output "true"
} }
@test "(core) image type detection (dockerfile)" { @test "(core) image type detection (dockerfile)" {
export DOKKU_ROOT run deploy_app dockerfile
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"
echo "output: $output" echo "output: $output"
echo "status: $status" 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"
} }