From 9082c51074031ff66c60a43f20ea18594637e363 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 21 Mar 2021 21:45:01 -0400 Subject: [PATCH] feat: add ability to specify a custom project.toml path --- .../builders/cloud-native-buildpacks.md | 112 +++++++++++++++--- plugins/builder-pack/builder-build | 14 ++- plugins/builder-pack/commands | 15 +++ plugins/builder-pack/help-functions | 33 ++++++ plugins/builder-pack/internal-commands | 91 ++++++++++++++ plugins/builder-pack/report | 6 + plugins/builder-pack/subcommands/default | 6 + plugins/builder-pack/subcommands/report | 6 + plugins/builder-pack/subcommands/set | 30 +++++ 9 files changed, 292 insertions(+), 21 deletions(-) create mode 100755 plugins/builder-pack/commands create mode 100755 plugins/builder-pack/help-functions create mode 100755 plugins/builder-pack/internal-commands create mode 100755 plugins/builder-pack/report create mode 100755 plugins/builder-pack/subcommands/default create mode 100755 plugins/builder-pack/subcommands/report create mode 100755 plugins/builder-pack/subcommands/set diff --git a/docs/deployment/builders/cloud-native-buildpacks.md b/docs/deployment/builders/cloud-native-buildpacks.md index a62133d91..f7e7daa06 100644 --- a/docs/deployment/builders/cloud-native-buildpacks.md +++ b/docs/deployment/builders/cloud-native-buildpacks.md @@ -12,25 +12,6 @@ Cloud Native Buildpacks are an evolution over the Buildpacks technology provided ## Usage -### Detection - -This builder will be auto-detected in either the following cases: - -- The `DOKKU_CNB_EXPERIMENTAL` app environment variable is set to `1`. - ```shell - dokku config:set --no-restart node-js-app DOKKU_CNB_EXPERIMENTAL=1 - ``` -- A `project.toml` file exists in the root of the app repository. - - This file is consumed by `pack-cli` and used to describe how the app is built. - -The builder can also be specified via the `builder:set` command: - -```shell -dokku builder:set node-js-app selected pack -``` - -> Dokku will only select the `dockerfile` builder if both the `herokuish` and `pack` builders are not detected and a Dockerfile exists. See the [dockerfile builder documentation](/docs/deployment/builders/dockerfiles.md) for more information on how that builder functions. - ### Requirements The `pack` cli tool is not included by default with Dokku or as a dependency. It must also be installed as shown on [this page](https://buildpacks.io/docs/tools/pack/). @@ -51,6 +32,99 @@ As this functionality is highly experimental, there are a number of caveats. Ple - `pack` is not currently included with Dokku, nor is it added as a package dependency. - A future version will include it as a package dependency. +### Detection + +This builder will be auto-detected in either the following cases: + +- The `DOKKU_CNB_EXPERIMENTAL` app environment variable is set to `1`. + ```shell + dokku config:set --no-restart node-js-app DOKKU_CNB_EXPERIMENTAL=1 + ``` +- A `project.toml` file exists in the root of the app repository. + - This file is consumed by `pack-cli` and used to describe how the app is built. + +The builder can also be specified via the `builder:set` command: + +```shell +dokku builder:set node-js-app selected pack +``` + +> Dokku will only select the `dockerfile` builder if both the `herokuish` and `pack` builders are not detected and a Dockerfile exists. See the [dockerfile builder documentation](/docs/deployment/builders/dockerfiles.md) for more information on how that builder functions. + +### Changing the `project.toml` location + +When deploying a monorepo, it may be desirable to specify the specific path of the `project.toml` file to use for a given app. This can be done via the `builder-pack:set` command. + +```shell +dokku builder-pack:set node-js-app projecttoml-path project2.toml +``` + +The default value may be set by passing an empty value for the option: + +```shell +dokku builder-pack:set node-js-app projecttoml-path +``` + +The `projecttoml-path` property can also be set globally. The global default is `project.toml`, and the global value is used when no app-specific value is set. + +```shell +dokku builder-pack:set --global projecttoml-path project2.toml +``` + +The default value may be set by passing an empty value for the option. + +```shell +dokku builder-pack:set --global projecttoml-path +``` + +### Displaying builder-pack reports for an app + +> New as of 0.25.0 + +You can get a report about the app's storage status using the `builder-pack:report` command: + +```shell +dokku app-json:report +``` + +``` +=====> node-js-app builder-pack information + Builder-pack computed projecttoml path: project2.toml + Builder-pack global projecttoml path: project.toml + Builder-pack projecttoml path: project2.toml +=====> python-sample builder-pack information + Builder-pack computed projecttoml path: project.toml + Builder-pack global projecttoml path: project.toml + Builder-pack projecttoml path: +=====> ruby-sample builder-pack information + Builder-pack computed projecttoml path: project.toml + Builder-pack global projecttoml path: project.json + Builder-pack projecttoml path: +``` + +You can run the command for a specific app also. + +```shell +dokku builder-pack:report node-js-app +``` + +``` +=====> node-js-app builder-pack information + Builder-pack computed projecttoml path: project2.toml + Builder-pack global projecttoml path: project.toml + Builder-pack projecttoml path: project2.toml +``` + +You can pass flags which will output only the value of the specific information you want. For example: + +```shell +dokku builder-pack:report node-js-app --builder-pack-projecttoml-path +``` + +``` +project2.toml +``` + ### Customizing the Buildpack stack builder > New as of 0.23.0 diff --git a/plugins/builder-pack/builder-build b/plugins/builder-pack/builder-build index 93dcd6b70..6a54e296f 100755 --- a/plugins/builder-pack/builder-build +++ b/plugins/builder-pack/builder-build @@ -1,8 +1,9 @@ #!/usr/bin/env bash -set -eo pipefail -[[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_AVAILABLE_PATH/config/functions" +source "$PLUGIN_AVAILABLE_PATH/builder-pack/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x trigger-builder-pack-builder-build() { declare desc="builder-pack builder-build plugin trigger" @@ -28,6 +29,15 @@ trigger-builder-pack-builder-build() { pushd "$SOURCECODE_WORK_DIR" &>/dev/null + local NEW_PROJECT_TOML="$(fn-builder-pack-computed-projectoml-path "$APP")" + if [[ -f "$NEW_PROJECT_TOML" ]]; then + if [[ "$NEW_DOCKERFILE" != "project.toml" ]]; then + mv "$NEW_PROJECT_TOML" project.toml + fi + else + rm project.toml + fi + local TMP_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") trap "rm -rf '$TMP_FILE' >/dev/null" RETURN INT TERM diff --git a/plugins/builder-pack/commands b/plugins/builder-pack/commands new file mode 100755 index 000000000..2b7211e4e --- /dev/null +++ b/plugins/builder-pack/commands @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/builder-pack/help-functions" + +case "$1" in + help | builder-pack:help) + cmd-builder-pack-help "$@" + ;; + + *) + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" + ;; + +esac diff --git a/plugins/builder-pack/help-functions b/plugins/builder-pack/help-functions new file mode 100755 index 000000000..1191260df --- /dev/null +++ b/plugins/builder-pack/help-functions @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-pack-help() { + declare desc="help command" + declare CMD="$1" + local plugin_name="builder-pack" + local plugin_description="Manage the pack builder integration for an app" + + if [[ "$CMD" == "${plugin_name}:help" ]]; then + echo -e "Usage: dokku ${plugin_name}[:COMMAND]" + echo '' + echo "$plugin_description" + echo '' + echo 'Additional commands:' + fn-help-content | sort | column -c2 -t -s, + elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then + fn-help-content + else + cat <] [], Displays a builder-pack report for one or more apps + builder-pack:set (), Set or clear a builder-pack property for an app +help_content +} diff --git a/plugins/builder-pack/internal-commands b/plugins/builder-pack/internal-commands new file mode 100755 index 000000000..1cabe473d --- /dev/null +++ b/plugins/builder-pack/internal-commands @@ -0,0 +1,91 @@ +#!/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-builder-pack-report() { + declare desc="displays a builder-pack report for one or more apps" + declare cmd="builder-pack: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-builder-pack-report-single "$app" "$INFO_FLAG" | tee || true + done + else + cmd-builder-pack-report-single "$APP" "$INFO_FLAG" + fi +} + +cmd-builder-pack-report-single() { + declare APP="$1" INFO_FLAG="$2" + if [[ "$INFO_FLAG" == "true" ]]; then + INFO_FLAG="" + fi + verify_app_name "$APP" + local flag_map=( + "--builder-pack-computed-projecttoml-path: $(fn-builder-pack-computed-projectoml-path "$APP")" + "--builder-pack-global-projecttoml-path: $(fn-builder-pack-global-projectoml-path "$APP")" + "--builder-pack-projecttoml-path: $(fn-builder-pack-projectoml-path "$APP")" + ) + + if [[ -z "$INFO_FLAG" ]]; then + dokku_log_info2_quiet "${APP} builder-pack 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-builder-pack-computed-projectoml-path() { + declare APP="$1" + + file="$(fn-builder-pack-projectoml-path "$APP")" + if [[ "$file" == "" ]]; then + file="$(fn-builder-pack-global-projectoml-path "$APP")" + fi + + echo "$file" +} + +fn-builder-pack-global-projectoml-path() { + declare APP="$1" + + fn-plugin-property-get "builder-pack" "--global" "projecttoml-path" "project.toml" +} + +fn-builder-pack-projectoml-path() { + declare APP="$1" + + fn-plugin-property-get "builder-pack" "$APP" "projecttoml-path" "" +} diff --git a/plugins/builder-pack/report b/plugins/builder-pack/report new file mode 100755 index 000000000..600703509 --- /dev/null +++ b/plugins/builder-pack/report @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-pack/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-pack-report-single "$@" diff --git a/plugins/builder-pack/subcommands/default b/plugins/builder-pack/subcommands/default new file mode 100755 index 000000000..f29e6dfa4 --- /dev/null +++ b/plugins/builder-pack/subcommands/default @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-pack/help-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-pack-help "builder-pack:help" diff --git a/plugins/builder-pack/subcommands/report b/plugins/builder-pack/subcommands/report new file mode 100755 index 000000000..03f6be771 --- /dev/null +++ b/plugins/builder-pack/subcommands/report @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/builder-pack/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-builder-pack-report "$@" diff --git a/plugins/builder-pack/subcommands/set b/plugins/builder-pack/subcommands/set new file mode 100755 index 000000000..8642d829c --- /dev/null +++ b/plugins/builder-pack/subcommands/set @@ -0,0 +1,30 @@ +#!/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-builder-pack-set() { + declare desc="set or clear a builder-pack property for an app" + declare cmd="builder-pack:set" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" KEY="$2" VALUE="$3" + local VALID_KEYS=("projecttoml-path") + [[ "$APP" == "--global" ]] || verify_app_name "$APP" + + [[ -z "$KEY" ]] && dokku_log_fail "No key specified" + + if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then + dokku_log_fail "Invalid key specified, valid keys include: projecttoml-path" + fi + + if [[ -n "$VALUE" ]]; then + dokku_log_info2_quiet "Setting ${KEY} to ${VALUE}" + fn-plugin-property-write "builder-pack" "$APP" "$KEY" "$VALUE" + else + dokku_log_info2_quiet "Unsetting ${KEY}" + fn-plugin-property-delete "builder-pack" "$APP" "$KEY" + fi +} + +cmd-builder-pack-set "$@"