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
2022-12-02 15:54:48 -05:00
parent a2cc6b84d6
commit c2ebf48524
10 changed files with 108 additions and 50 deletions

View File

@@ -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

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -7,4 +7,4 @@
/install
/post-*
/report
/builder-detect
/builder-*

View File

@@ -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

View File

@@ -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)

View File

@@ -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), "/")

View File

@@ -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() {

View File

@@ -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 {

View File

@@ -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"
}