diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 4222a9fb7..ebf47d9ce 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -344,6 +344,66 @@ if [[ ! -f "$DOKKU_ROOT/HOSTNAME" ]]; then fi ``` +### `network-build-config` + +- Description: Rebuilds network configuration +- Invoked by: `internally triggered by proxy-build-config within proxy implementations` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `network-compute-ports` + +- Description: Computes the ports for a given app container +- Invoked by: `internally triggered by proxy-build-config within proxy implementations` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `network-get-ipaddr` + +- Description: Return the ipaddr for a given app container +- Invoked by: `internally triggered by a deploy` +- Arguments: `$APP $PROC_TYPE $CONTAINER_ID` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `network-get-port` + +- Description: Return the port for a given app container +- Invoked by: `internally triggered by a deploy` +- Arguments: `$APP $PROC_TYPE $CONTAINER_ID` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + ### `nginx-hostname` - Description: Allows you to customize the hostname for a given application. @@ -410,6 +470,40 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `post-certs-remove` + +- Description: Allows you to run commands after a cert is removed +- Invoked by: `dokku certs:remove` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +APP="$1"; verify_app_name "$APP" + +# TODO +``` + +### `post-certs-update` + +- Description: Allows you to run commands after a cert is added/updated +- Invoked by: `dokku certs:add`, `dokku certs:update` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +APP="$1"; verify_app_name "$APP" + +# TODO +``` + ### `post-create` - Description: Can be used to run commands after an application is created. @@ -763,10 +857,10 @@ APP="$1"; verify_app_name "$APP" # TODO ``` -### `post-certs-update` +### `proxy-build-config` -- Description: Allows you to run commands after a cert is added/updated -- Invoked by: `dokku certs:add`, `dokku certs:update` +- Description: Builds the proxy implementation configuration for a given app +- Invoked by: `internally triggered by ps:restore` - Arguments: `$APP` - Example: @@ -774,16 +868,29 @@ APP="$1"; verify_app_name "$APP" #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" -APP="$1"; verify_app_name "$APP" # TODO ``` -### `post-certs-remove` +### `proxy-enable` -- Description: Allows you to run commands after a cert is removed -- Invoked by: `dokku certs:remove` +- Description: Enables the configured proxy implementation for an app +- Invoked by: `internally triggered by ps:restore` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `proxy-disable` + +- Description: Disables the configured proxy implementation for an app +- Invoked by: `internally triggered by ps:restore` - Arguments: `$APP` - Example: @@ -791,8 +898,6 @@ APP="$1"; verify_app_name "$APP" #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" -APP="$1"; verify_app_name "$APP" # TODO ``` diff --git a/plugins/common/functions b/plugins/common/functions index c9f28250a..816e59391 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -574,8 +574,6 @@ dokku_deploy_cmd() { while [[ $CONTAINER_INDEX -le $PROC_COUNT ]]; do local id=""; local port=""; local ipaddr="" local DOKKU_CONTAINER_ID_FILE="$DOKKU_ROOT/$APP/CONTAINER.$PROC_TYPE.$CONTAINER_INDEX" - local DOKKU_IP_FILE="$DOKKU_ROOT/$APP/IP.$PROC_TYPE.$CONTAINER_INDEX" - local DOKKU_PORT_FILE="$DOKKU_ROOT/$APP/PORT.$PROC_TYPE.$CONTAINER_INDEX" # start the app local DOCKER_ARGS @@ -593,39 +591,29 @@ dokku_deploy_cmd() { fi if [[ "$PROC_TYPE" == "web" ]]; then - if [[ -z "${DOKKU_DOCKERFILE_PORTS[*]}" ]]; then - local port=5000 - local DOKKU_DOCKER_PORT_ARGS+="-p $port" - else - local p - for p in ${DOKKU_DOCKERFILE_PORTS[*]};do - if [[ ! "$p" =~ .*udp.* ]]; then - # set port to first non-udp port - local p=${p//\/tcp} - local port=${port:="$p"} - fi - local DOKKU_DOCKER_PORT_ARGS+=" -p $p " - done - fi + ports="$(plugn trigger network-compute-ports "$APP" "$PROC_TYPE")" + local DOKKU_DOCKER_PORT_ARGS="" + local DOKKU_PORT="" + for port in "${ports[@]}"; do + DOKKU_DOCKER_PORT_ARGS+=" -p $port " + PORTS="$port" + done + if [[ "$DOKKU_IS_APP_PROXY_ENABLED" == "true" ]]; then # shellcheck disable=SC2086 local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD) - local ipaddr=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$id") - # Docker < 1.9 compatibility - if [[ -z $ipaddr ]]; then - local ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$id") - fi else # shellcheck disable=SC2086 local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $DOKKU_DOCKER_PORT_ARGS -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD) - local port=$(docker port "$id" "$port" | sed 's/[0-9.]*://') - local ipaddr=127.0.0.1 fi else # shellcheck disable=SC2086 local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $DOCKER_ARGS $IMAGE $START_CMD) fi + ipaddr=$(plugn trigger network-get-ipaddr "$APP" "PROC_TYPE" "$id") + port=$(plugn trigger network-get-port "$APP" "PROC_TYPE" "$id") + kill_new() { declare desc="wrapper function to kill newly started app container" local id="$1" @@ -644,8 +632,8 @@ dokku_deploy_cmd() { # now using the new container [[ -n "$id" ]] && echo "$id" > "$DOKKU_CONTAINER_ID_FILE" - [[ -n "$ipaddr" ]] && echo "$ipaddr" > "$DOKKU_IP_FILE" - [[ -n "$port" ]] && echo "$port" > "$DOKKU_PORT_FILE" + [[ -n "$ipaddr" ]] && network-write-container-ipaddr "$APP" "$PROC_TYPE" "$CONTAINER_INDEX" "$ipaddr" + [[ -n "$port" ]] && network-write-container-port "$APP" "$PROC_TYPE" "$CONTAINER_INDEX" "$port" # cleanup pre-migration files rm -f "$DOKKU_ROOT/$APP/CONTAINER" "$DOKKU_ROOT/$APP/IP" "$DOKKU_ROOT/$APP/PORT" diff --git a/plugins/network/functions b/plugins/network/functions index e10e07323..106dfdd28 100755 --- a/plugins/network/functions +++ b/plugins/network/functions @@ -4,8 +4,8 @@ source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_AVAILABLE_PATH/config/functions" source "$PLUGIN_AVAILABLE_PATH/proxy/functions" -network-write-config() { - declare desc="writes the network config files out" +network-build-config() { + declare desc="builds network config files" declare APP="$1"; verify_app_name "$APP" local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE" @@ -41,7 +41,7 @@ network-write-config() { } network-compute-container-ports() { - declare desc="Return the ipaddr for a given app container" + declare desc="Computes the ports for a given app container" declare APP="$1" PROC_TYPE="$2" local PORTS @@ -72,7 +72,6 @@ network-compute-container-ports() { echo "$PORTS" } - network-get-container-ipaddr() { declare desc="Return the ipaddr for a given app container" declare APP="$1" PROC_TYPE="$2" CONTAINER_ID="$3" @@ -96,7 +95,7 @@ network-get-container-ipaddr() { } network-get-container-port() { - declare desc="Return the ipaddr for a given app container" + declare desc="Return the port for a given app container" declare APP="$1" PROC_TYPE="$2" CID="$3" local DOKKU_IS_APP_PROXY_ENABLED local PORT=5000 diff --git a/plugins/network/network-build-config b/plugins/network/network-build-config new file mode 100755 index 000000000..235aa1806 --- /dev/null +++ b/plugins/network/network-build-config @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/network/functions" + +network-build-config "$@" diff --git a/plugins/network/network-compute-ports b/plugins/network/network-compute-ports new file mode 100755 index 000000000..04d91debe --- /dev/null +++ b/plugins/network/network-compute-ports @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/network/functions" + +network-compute-container-ports "$@" diff --git a/plugins/network/network-get-ipaddr b/plugins/network/network-get-ipaddr new file mode 100755 index 000000000..254840d02 --- /dev/null +++ b/plugins/network/network-get-ipaddr @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/network/functions" + +network-get-container-ipaddr "$@" diff --git a/plugins/network/network-get-port b/plugins/network/network-get-port new file mode 100755 index 000000000..cc2ac7ddf --- /dev/null +++ b/plugins/network/network-get-port @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/network/functions" + +network-get-container-port "$@" diff --git a/plugins/nginx-vhosts/proxy-build-config b/plugins/nginx-vhosts/proxy-build-config new file mode 100755 index 000000000..7aa867b20 --- /dev/null +++ b/plugins/nginx-vhosts/proxy-build-config @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" +source "$PLUGIN_AVAILABLE_PATH/proxy/functions" + +nginx_build_config() { + declare desc="build nginx config to proxy app containers from command line" + declare APP="$1" + [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" + + if [[ "$(get_app_proxy_type "$1")" = "nginx" ]]; then + plugn trigger network-build-config "$APP" + nginx_build_config "$APP" + fi +} + +nginx_build_config_cmd "$@" diff --git a/plugins/nginx-vhosts/subcommands/build-config b/plugins/nginx-vhosts/subcommands/build-config index 74b118da2..581b2b36b 100755 --- a/plugins/nginx-vhosts/subcommands/build-config +++ b/plugins/nginx-vhosts/subcommands/build-config @@ -1,14 +1,20 @@ #!/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/nginx-vhosts/functions" nginx_build_config_cmd() { declare desc="build nginx config to proxy app containers from command line" + declare APP="$2" local cmd="nginx:build-config" - [[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on" - nginx_build_config "$2" + [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" + local PROXY_APP_TYPE + PROXY_APP_TYPE="$(get_app_proxy_type "$APP")" + + if [[ "$PROXY_APP_TYPE" == "nginx" ]]; then + plugn trigger proxy-build-config "$APP" + else + dokku_log_fail "Configured proxy for ${APP} is ${PROXY_APP_TYPE}" + fi } nginx_build_config_cmd "$@" diff --git a/plugins/ps/subcommands/restore b/plugins/ps/subcommands/restore index 818c3d980..db501f751 100755 --- a/plugins/ps/subcommands/restore +++ b/plugins/ps/subcommands/restore @@ -10,6 +10,7 @@ ps_restore_cmd() { if which parallel > /dev/null 2>&1; then dokku_apps | parallel dokku ps:start + dokku_apps | parallel plugn trigger proxy-build-config return fi @@ -18,6 +19,7 @@ ps_restore_cmd() { if [[ $DOKKU_APP_RESTORE != 0 ]]; then echo "Restoring app $app ..." if ps_start "$app"; then + plugn trigger proxy-build-config "$app" continue fi dokku_log_warn "dokku ps:restore ${app} failed"