Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 449 lines (392 sloc) 14.7 KB
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
docker_ports_options() {
declare desc="Exports a list of exposed ports"
declare PORTS=("$@")
for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do
echo -n "-p ${PORTS[i]}:${PLUGIN_DATASTORE_PORTS[i]} "
done
}
get_container_ip() {
declare desc="Retrieves the ip address of a container"
declare CONTAINER_ID="$1"
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$CONTAINER_ID"
}
get_random_ports() {
declare desc="Retrieves N random ports"
declare iterations="${1:-1}"
for (( i=0; i < iterations; i++ )); do
local port=$RANDOM
local quit=0
while [ "$quit" -ne 1 ]; do
netstat -an | grep $port > /dev/null
# shellcheck disable=SC2181
if [ $? -gt 0 ]; then
quit=1
else
port=$((port + 1))
fi
done
echo $port
done
}
get_service_name() {
declare desc="Retrieves a docker service label"
declare SERVICE="$1"
echo "dokku.${PLUGIN_COMMAND_PREFIX}.$SERVICE"
}
get_url_from_config() {
declare desc="Retrieves a given _URL from a list of configuration variables"
declare EXISTING_CONFIG="$1" CONFIG_VAR="$2"
echo "$EXISTING_CONFIG" | grep "$CONFIG_VAR" | sed "s/$CONFIG_VAR:\s*//" | xargs
}
is_container_status() {
declare desc="Returns 0 or 1 depending upon whether a given container has a certain status"
declare CID="$1" STATUS="$2"
local TEMPLATE="{{.State.$STATUS}}"
local CONTAINER_STATUS=$(docker inspect -f "$TEMPLATE" "$CID" || true)
if [[ "$CONTAINER_STATUS" == "true" ]]; then
return 0
fi
return 1
}
remove_from_links_file() {
declare desc="Removes an app from the service link file"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
sed -i.bak "/^$APP\$/d" "$LINKS_FILE" && rm "$LINKS_FILE.bak"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
}
service_alias() {
declare desc="Retrieves the alias of a service"
declare SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
echo "$SERVICE_NAME" | tr ._ -
}
service_alternative_alias() {
declare desc="Retrieves an alternative alias for a service"
declare EXISTING_CONFIG="$1"
local COLORS=(AQUA BLACK BLUE FUCHSIA GRAY GREEN LIME MAROON NAVY OLIVE PURPLE RED SILVER TEAL WHITE YELLOW)
local ALIAS;
while [[ -z $ALIAS ]]; do
local IDX=$((RANDOM % ${#COLORS[*]}))
local COLOR=${COLORS[IDX]}
ALIAS="${PLUGIN_ALT_ALIAS}_${COLOR}"
local IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL")
if [[ -n $IN_USE ]]; then
unset ALIAS
fi
done
echo "$ALIAS"
}
service_enter() {
declare desc="enters running app container of specified proc type"
declare SERVICE="$1" && shift 1
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
docker inspect "$ID" &> /dev/null || dokku_log_fail "Container does not exist"
is_container_status "$ID" "Running" || dokku_log_fail "Container is not running"
local EXEC_CMD=""
has_tty && local DOKKU_RUN_OPTS+=" -i -t"
# shellcheck disable=SC2086
docker exec $DOKKU_RUN_OPTS $ID $EXEC_CMD "${@:-/bin/bash}"
}
service_exposed_ports() {
declare desc="Lists exposed ports for a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
[[ ! -f $PORT_FILE ]] && echo '-' && return 0
local PORTS=($(cat "$PORT_FILE"))
for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do
echo -n "${PLUGIN_DATASTORE_PORTS[i]}->${PORTS[i]} "
done
}
service_info() {
declare desc="Retrieves information about a given service"
declare SERVICE="$1" INFO_FLAG="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
local SERVICE_CONTAINER_ID="$(cat "$SERVICE_ROOT/ID")"
local flag key valid_flags
local flag_map=(
"--config-dir: ${SERVICE_ROOT}/config"
"--data-dir: ${SERVICE_ROOT}/data"
"--exposed-ports: $(service_exposed_ports "$SERVICE")"
"--id: ${SERVICE_CONTAINER_ID}"
"--internal-ip: $(get_container_ip "${SERVICE_CONTAINER_ID}")"
"--links: $(service_linked_apps "$SERVICE")"
"--service-root: ${SERVICE_ROOT}"
"--status: $(service_status "$SERVICE")"
"--version: $(service_version "$SERVICE")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2 "Container Information"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")"
done
else
local match=false
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
echo "${flag#*: }" && match=true
fi
done
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
fi
}
service_link() {
declare desc="Links a service to an application"
declare SERVICE="$1" APP="$2"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_HOST=$(service_host "$SERVICE")
local SERVICE_PORT=$(service_port "$SERVICE")
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXISTING_CONFIG=$(config_all "$APP")
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_HOST" | cut -d: -f1) || true
local DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINKS_FILE="$SERVICE_ROOT/LINKS"
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
local USER="$(cat "$SERVICE_ROOT/USER")"
[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
echo "$APP" >> "$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
local ALIAS="$PLUGIN_DEFAULT_ALIAS"
if [[ -n $DEFAULT_ALIAS ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi
if declare -f -F add_passed_docker_option > /dev/null; then
# shellcheck disable=SC2034
local passed_phases=(build deploy run)
add_passed_docker_option passed_phases[@] "--link $SERVICE_NAME:$SERVICE_ALIAS"
else
dokku docker-options:add "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
fi
config_set "$APP" "${ALIAS}_HOST=$SERVICE_HOST" "${ALIAS}_PORT=$SERVICE_PORT" "${ALIAS}_USER=$USER" "${ALIAS}_PASSWORD=$PASSWORD" "${ALIAS}_URL=$SERVICE_URL"
}
service_linked_apps() {
declare desc="Lists all applications linked to a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
touch "$LINKS_FILE"
[[ -z $(< "$LINKS_FILE") ]] && echo '-' && return 0
tr '\n' ' ' < "$LINKS_FILE"
}
service_list() {
declare desc="Lists all services and their status"
local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2> /dev/null)
if [[ -z $SERVICES ]]; then
dokku_log_warn "There are no $PLUGIN_SERVICE services"
else
LIST="NAME,VERSION,STATUS,EXPOSED PORTS,LINKS\n"
for SERVICE in $SERVICES; do
LIST+="$SERVICE,$(service_version "$SERVICE"),$(service_status "$SERVICE"),$(service_exposed_ports "$SERVICE"),$(service_linked_apps "$SERVICE")\n"
done
printf "%b" "$LIST" | column -t -s,
fi
}
service_logs() {
declare desc="Displays logs for a service"
declare SERVICE="$1" TAIL_FLAG="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID=$(cat "$SERVICE_ROOT/ID")
local RE_INTEGER='^[0-9]+$'
DOKKU_LOGS_ARGS="--tail 100"
if [[ "$TAIL_FLAG" == "-t" ]] || [[ "$TAIL_FLAG" == "--tail" ]]; then
DOKKU_LOGS_ARGS="--follow"
fi
# shellcheck disable=SC2086
docker logs $DOKKU_LOGS_ARGS "$ID"
}
service_parse_args() {
declare desc="cli arg parser"
local next_index=1; local skip=false; local args=("$@")
for arg in "$@"; do
shift
case "$arg" in
"--config-options") set -- "$@" "-c" ;;
"--custom-env") set -- "$@" "-C" ;;
"--image") set -- "$@" "-i" ;;
"--image-version") set -- "$@" "-I" ;;
"--password") set -- "$@" "-p" ;;
"--root-password") set -- "$@" "-r" ;;
"--alias") set -- "$@" "-a" ;;
"--database") set -- "$@" "-d" ;;
"--memory") set -- "$@" "-m" ;;
"--querystring") set -- "$@" "-q" ;;
"--user") set -- "$@" "-u" ;;
*) set -- "$@" "$arg"
esac
done
OPTIND=1
while getopts "a:c:C:d:i:I:m:p:q:r:u:" opt; do
case "$opt" in
a) export SERVICE_ALIAS=$OPTARG
;;
c) export PLUGIN_CONFIG_OPTIONS=$OPTARG
;;
C) export SERVICE_CUSTOM_ENV=$OPTARG
;;
d) export SERVICE_DATABASE=$OPTARG
;;
i) export PLUGIN_IMAGE=$OPTARG
;;
I) export PLUGIN_IMAGE_VERSION=$OPTARG
;;
m) export SERVICE_MEMORY=$OPTARG
;;
p) export SERVICE_PASSWORD=$OPTARG
;;
q) export SERVICE_QUERYSTRING=$OPTARG
;;
r) export SERVICE_ROOT_PASSWORD=$OPTARG
;;
u) export SERVICE_USER=$OPTARG
;;
esac
done
shift "$(( OPTIND - 1 ))" # remove options from positional parameters
}
service_port_expose() {
declare desc="Wrapper for exposing service ports"
declare SERVICE="$1"
service_start "$SERVICE" "true"
service_port_unpause "$SERVICE" "true" "${@:2}"
}
service_port_pause() {
declare desc="Pauses service exposure"
declare SERVICE="$1" LOG_FAIL="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXPOSED_NAME="$(get_service_name "$SERVICE").ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ ! -f "$PORT_FILE" ]] && dokku_log_fail "Service not exposed"
else
[[ ! -f "$PORT_FILE" ]] && return 0
fi
local GREP_NAME="^/${EXPOSED_NAME}$"
local CONTAINER_NAME="$(docker ps -f name="$GREP_NAME" --format "{{.Names}}")"
if [[ -z "$CONTAINER_NAME" ]]; then
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE unexposed"
fi
return
fi
docker stop "$EXPOSED_NAME" > /dev/null 2>&1 || true
docker rm "$EXPOSED_NAME" > /dev/null 2>&1 || true
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE unexposed"
fi
}
service_port_unexpose() {
declare desc="Wrapper for pausing exposed service ports"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
service_port_pause "$SERVICE" "true"
rm -rf "$PORT_FILE"
}
service_port_unpause() {
declare desc="Starts service exposure"
declare SERVICE="$1" LOG_FAIL="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local EXPOSED_NAME="${SERVICE_NAME}.ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
# shellcheck disable=SC2068
local PORTS=(${@:3})
# shellcheck disable=SC2068
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
local ID=$(cat "$SERVICE_ROOT/ID")
[[ "${#PORTS[@]}" != "${#PLUGIN_DATASTORE_PORTS[@]}" ]] && dokku_log_fail "${#PLUGIN_DATASTORE_PORTS[@]} ports to be exposed need to be provided in the following order: ${PLUGIN_DATASTORE_PORTS[*]}"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ -f "$PORT_FILE" ]] && PORTS=($(cat "$PORT_FILE")) && dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
else
[[ ! -f "$PORT_FILE" ]] && return 0
PORTS=($(cat "$PORT_FILE"))
fi
echo "${PORTS[@]}" > "$PORT_FILE"
# shellcheck disable=SC2046
docker run -d --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" --name "$EXPOSED_NAME" $(docker_ports_options "${PORTS[@]}") --restart always --label dokku=ambassador --label "dokku.ambassador=$PLUGIN_COMMAND_PREFIX" svendowideit/ambassador > /dev/null
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE exposed on port(s) [container->host]: $(service_exposed_ports "$SERVICE")"
fi
}
service_set_alias() {
declare desc="Sets the alias in use for a service"
declare SERVICE="$1" ALIAS="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ALIAS_FILE="$SERVICE_ROOT/ALIAS"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$ALIAS_FILE"
echo "$ALIAS" > "$ALIAS_FILE"
}
service_status() {
declare desc="Displays the status of a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
is_container_status "$ID" "Dead" && echo "dead" && return 0
is_container_status "$ID" "OOMKilled" && echo "oomkilled" && return 0
is_container_status "$ID" "Paused" && echo "paused" && return 0
is_container_status "$ID" "Restarting" && echo "restarting" && return 0
is_container_status "$ID" "Running" && echo "running" && return 0
echo "stopped" && return 0
}
service_stop() {
declare desc="Stops a running service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE";
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ID=$(docker ps -f status=running | grep -e "$SERVICE_NAME$" | awk '{print $1}') || true
[[ -z $ID ]] && dokku_log_warn "Service is already stopped" && return 0
if [[ -n $ID ]]; then
dokku_log_info1_quiet "Stopping container"
docker stop "$SERVICE_NAME" > /dev/null
service_port_pause "$SERVICE"
dokku_log_info2 "Container stopped"
else
dokku_log_verbose_quiet "No container exists for $SERVICE"
fi
}
service_unlink() {
declare desc="Unlinks an application from a service"
declare SERVICE="$1" APP="$2"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local EXISTING_CONFIG=$(config_all "$APP")
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINK=($(echo "${EXISTING_CONFIG[*]}" | grep "VERNEMQ" | cut -d: -f1)) || true
[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
remove_from_links_file "$SERVICE" "$APP"
if declare -f -F add_passed_docker_option > /dev/null; then
# shellcheck disable=SC2034
local passed_phases=(build deploy run)
remove_passed_docker_option passed_phases[@] "--link $SERVICE_NAME:$SERVICE_ALIAS"
else
dokku docker-options:remove "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
fi
config_unset "$APP" "${LINK[*]}"
}
service_version() {
declare desc="Displays the running version for an image"
declare SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
docker inspect -f '{{.Config.Image}}' "$SERVICE_NAME"
}
verify_service_name() {
declare desc="Verifies that a service exists"
declare SERVICE="$1"
[[ ! -n "$SERVICE" ]] && dokku_log_fail "(verify_service_name) SERVICE must not be null"
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist"
return 0
}