Skip to content

Commit c1dc5ce

Browse files
authored
Merge pull request #49 from dokku/error-when-link-exists
Show an error when an ambassador container already exists but we don't have expose configuration
2 parents 1eb24f4 + 0f6d9e3 commit c1dc5ce

File tree

3 files changed

+94
-55
lines changed

3 files changed

+94
-55
lines changed

common-functions

+51-50
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn-services-list() {
6262
[[ -d $f ]] || continue
6363
services+=("$f")
6464
done
65-
popd &>/dev/null || pushd "/tmp" >/dev/null
65+
popd >/dev/null 2>&1 || pushd "/tmp" >/dev/null
6666

6767
if [[ "${#services[@]}" -eq 0 ]]; then
6868
return
@@ -282,7 +282,7 @@ service_backup() {
282282
BACKUP_TMPDIR=$(mktemp -d --tmpdir)
283283
trap 'rm -rf "$BACKUP_TMPDIR" > /dev/null' RETURN INT TERM EXIT
284284

285-
"$DOCKER_BIN" container inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
285+
"$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1 || dokku_log_fail "Service container does not exist"
286286
is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running"
287287

288288
(service_export "$SERVICE" >"${BACKUP_TMPDIR}/export")
@@ -483,7 +483,7 @@ service_enter() {
483483
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
484484
local ID="$(cat "$SERVICE_ROOT/ID")"
485485

486-
"$DOCKER_BIN" container inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
486+
"$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1 || dokku_log_fail "Service container does not exist"
487487
is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running"
488488

489489
local EXEC_CMD=""
@@ -691,7 +691,7 @@ service_logs() {
691691
DOKKU_LOGS_ARGS+=" --follow"
692692
fi
693693

694-
"$DOCKER_BIN" container inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
694+
"$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1 || dokku_log_fail "Service container does not exist"
695695
is_container_status "$ID" "Running" || dokku_log_warn "Service logs may not be output as service is not running"
696696

697697
# shellcheck disable=SC2086
@@ -810,79 +810,78 @@ service_root_password() {
810810

811811
service_port_expose() {
812812
declare desc="wrapper for exposing service ports"
813-
declare SERVICE="$1"
814-
service_start "$SERVICE" "true"
815-
service_port_unpause "$SERVICE" "true" "${@:2}"
816-
}
817-
818-
service_port_pause() {
819-
declare desc="pause service exposure"
820-
declare SERVICE="$1" LOG_FAIL="$2"
813+
declare SERVICE="$1" PORTS=(${@:2})
821814
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
822-
local EXPOSED_NAME="$(get_service_name "$SERVICE").ambassador"
823815
local PORT_FILE="$SERVICE_ROOT/PORT"
816+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
817+
local EXPOSED_NAME="$SERVICE_NAME.ambassador"
824818

825-
if [[ "$LOG_FAIL" == "true" ]]; then
826-
[[ ! -f "$PORT_FILE" ]] && dokku_log_fail "Service not exposed"
827-
else
828-
[[ ! -f "$PORT_FILE" ]] && return 0
819+
if [[ ${#PORTS[@]} -eq 0 ]]; then
820+
# shellcheck disable=SC2206
821+
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
829822
fi
830823

831-
local GREP_NAME="^/${EXPOSED_NAME}$"
832-
local CONTAINER_NAME="$("$DOCKER_BIN" container ps -f name="$GREP_NAME" --format "{{.Names}}")"
833-
if [[ -z "$CONTAINER_NAME" ]]; then
834-
if [[ "$LOG_FAIL" == "true" ]]; then
835-
dokku_log_info1 "Service $SERVICE unexposed"
836-
fi
824+
[[ "${#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[*]}"
837825

838-
return
826+
if [[ -s "$PORT_FILE" ]]; then
827+
# shellcheck disable=SC2207
828+
PORTS=($(cat "$PORT_FILE"))
829+
dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
839830
fi
840831

841-
"$DOCKER_BIN" container stop "$EXPOSED_NAME" >/dev/null 2>&1 || true
842-
"$DOCKER_BIN" container rm "$EXPOSED_NAME" >/dev/null 2>&1 || true
843-
if [[ "$LOG_FAIL" == "true" ]]; then
844-
dokku_log_info1 "Service $SERVICE unexposed"
832+
if "$DOCKER_BIN" container inspect "$EXPOSED_NAME" >/dev/null 2>&1; then
833+
dokku_log_warn "Service $SERVICE has an untracked expose container, removing"
834+
"$DOCKER_BIN" container stop "$EXPOSED_NAME" >/dev/null 2>&1 || true
835+
suppress_output "$DOCKER_BIN" container rm "$EXPOSED_NAME"
845836
fi
837+
838+
echo "${PORTS[@]}" >"$PORT_FILE"
839+
840+
service_start "$SERVICE" "true"
841+
service_port_reconcile_status "$SERVICE"
842+
dokku_log_info1 "Service $SERVICE exposed on port(s) [container->host]: $(service_exposed_ports "$SERVICE")"
846843
}
847844

848845
service_port_unexpose() {
849846
declare desc="wrapper for pausing exposed service ports"
850847
declare SERVICE="$1"
851848
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
852849
local PORT_FILE="$SERVICE_ROOT/PORT"
853-
service_port_pause "$SERVICE" "true"
850+
854851
rm -rf "$PORT_FILE"
852+
service_port_reconcile_status "$SERVICE"
853+
dokku_log_info1 "Service $SERVICE unexposed"
855854
}
856855

857-
service_port_unpause() {
858-
declare desc="start service exposure"
859-
declare SERVICE="$1" LOG_FAIL="$2"
856+
service_port_reconcile_status() {
857+
declare SERVICE="$1"
860858
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
861-
local SERVICE_NAME="$(get_service_name "$SERVICE")"
862-
local EXPOSED_NAME="${SERVICE_NAME}.ambassador"
863859
local PORT_FILE="$SERVICE_ROOT/PORT"
864-
# shellcheck disable=SC2068
865-
local PORTS=(${@:3})
866-
# shellcheck disable=SC2068
867-
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
868-
local ID=$(cat "$SERVICE_ROOT/ID")
860+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
861+
local EXPOSED_NAME="$SERVICE_NAME.ambassador"
869862

870-
[[ "${#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[*]}"
863+
if [[ ! -s "$PORT_FILE" ]]; then
864+
if "$DOCKER_BIN" container inspect "$EXPOSED_NAME" >/dev/null 2>&1; then
865+
"$DOCKER_BIN" container stop "$EXPOSED_NAME" >/dev/null 2>&1 || true
866+
suppress_output "$DOCKER_BIN" container rm "$EXPOSED_NAME"
867+
return $?
868+
fi
869+
return
870+
fi
871871

872-
if [[ "$LOG_FAIL" == "true" ]]; then
873-
[[ -f "$PORT_FILE" ]] && PORTS=($(cat "$PORT_FILE")) && dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
874-
else
875-
[[ ! -f "$PORT_FILE" ]] && return 0
876-
PORTS=($(cat "$PORT_FILE"))
872+
if is_container_status "$EXPOSED_NAME" "Running"; then
873+
return
877874
fi
878875

879-
echo "${PORTS[@]}" >"$PORT_FILE"
876+
if "$DOCKER_BIN" container inspect "$EXPOSED_NAME" >/dev/null 2>&1; then
877+
suppress_output "$DOCKER_BIN" container start "$EXPOSED_NAME"
878+
return $?
879+
fi
880880

881+
# shellcheck disable=SC2207
882+
PORTS=($(cat "$PORT_FILE"))
881883
# shellcheck disable=SC2046
882884
"$DOCKER_BIN" container 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" "$PLUGIN_AMBASSADOR_IMAGE" >/dev/null
883-
if [[ "$LOG_FAIL" == "true" ]]; then
884-
dokku_log_info1 "Service $SERVICE exposed on port(s) [container->host]: $(service_exposed_ports "$SERVICE")"
885-
fi
886885
}
887886

888887
service_promote() {
@@ -946,7 +945,9 @@ service_pause() {
946945
if [[ -n $ID ]]; then
947946
dokku_log_info2_quiet "Pausing container"
948947
"$DOCKER_BIN" container stop "$SERVICE_NAME" >/dev/null
949-
service_port_pause "$SERVICE"
948+
if "$DOCKER_BIN" container inspect "$ID" >/dev/null 2>&1; then
949+
"$DOCKER_BIN" container stop "$SERVICE_NAME.ambassador" >/dev/null 2>&1 || true
950+
fi
950951
dokku_log_verbose_quiet "Container paused"
951952
else
952953
dokku_log_verbose_quiet "No container exists for $SERVICE"

functions

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ service_create_container() {
122122
done < <(fn-plugin-property-get "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "post-create-network" | tr "," "\n")
123123
fi
124124
suppress_output "$DOCKER_BIN" container start "$(cat "$SERVICE_ROOT/ID")"
125+
service_port_reconcile_status "$SERVICE"
126+
125127
if [[ -n "$(fn-plugin-property-get "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "post-start-network")" ]]; then
126128
dokku_log_verbose_quiet "Connecting to networks after container start"
127129
while read -r line || [[ -n "$line" ]]; do
@@ -184,7 +186,7 @@ service_start() {
184186

185187
if [[ -n $PREVIOUS_ID ]]; then
186188
"$DOCKER_BIN" container start "$PREVIOUS_ID" >/dev/null
187-
service_port_unpause "$SERVICE"
189+
service_port_reconcile_status "$SERVICE"
188190
dokku_log_info2 "Container started"
189191
elif service_image_exists "$SERVICE" && [[ -n "$PASSWORD" ]]; then
190192
service_create_container "$SERVICE"

tests/service_expose.bats

+40-4
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,65 @@
22
load test_helper
33

44
setup() {
5-
dokku "$PLUGIN_COMMAND_PREFIX:create" l
5+
dokku "$PLUGIN_COMMAND_PREFIX:create" ls
66
}
77

88
teardown() {
9-
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
9+
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ls
1010
}
1111

1212
@test "($PLUGIN_COMMAND_PREFIX:expose) error when there are no arguments" {
1313
run dokku "$PLUGIN_COMMAND_PREFIX:expose"
14+
echo "output: $output"
15+
echo "status: $status"
16+
assert_failure
1417
assert_contains "${lines[*]}" "Please specify a valid name for the service"
1518
}
1619

1720
@test "($PLUGIN_COMMAND_PREFIX:expose) error when service does not exist" {
1821
run dokku "$PLUGIN_COMMAND_PREFIX:expose" not_existing_service
22+
echo "output: $output"
23+
echo "status: $status"
24+
assert_failure
1925
assert_contains "${lines[*]}" "service not_existing_service does not exist"
2026
}
2127

28+
@test "($PLUGIN_COMMAND_PREFIX:expose) error when already exposed" {
29+
run dokku "$PLUGIN_COMMAND_PREFIX:expose" ls
30+
echo "output: $output"
31+
echo "status: $status"
32+
assert_success
33+
34+
run dokku "$PLUGIN_COMMAND_PREFIX:expose" ls
35+
echo "output: $output"
36+
echo "status: $status"
37+
assert_failure
38+
assert_contains "${lines[*]}" "Service ls already exposed on port(s)"
39+
40+
run sudo rm $PLUGIN_DATA_ROOT/ls/PORT
41+
echo "output: $output"
42+
echo "status: $status"
43+
assert_success
44+
45+
run dokku "$PLUGIN_COMMAND_PREFIX:expose" ls
46+
echo "output: $output"
47+
echo "status: $status"
48+
assert_success
49+
assert_contains "${lines[*]}" "Service ls has an untracked expose container, removing"
50+
}
51+
2252
@test "($PLUGIN_COMMAND_PREFIX:expose) success when not providing custom ports" {
23-
run dokku "$PLUGIN_COMMAND_PREFIX:expose" l
53+
run dokku "$PLUGIN_COMMAND_PREFIX:expose" ls
54+
echo "output: $output"
55+
echo "status: $status"
56+
assert_success
2457
[[ "${lines[*]}" =~ exposed\ on\ port\(s\)\ \[container\-\>host\]\:\ [[:digit:]]+ ]]
2558
}
2659

2760
@test "($PLUGIN_COMMAND_PREFIX:expose) success when providing custom ports" {
28-
run dokku "$PLUGIN_COMMAND_PREFIX:expose" l 4242 4243 4244 4245 4246
61+
run dokku "$PLUGIN_COMMAND_PREFIX:expose" ls 4242 4243 4244 4245 4246
62+
echo "output: $output"
63+
echo "status: $status"
64+
assert_success
2965
assert_contains "${lines[*]}" "exposed on port(s) [container->host]: 8125->4242 8126->4243 80->4244 81->4245 2003->4246"
3066
}

0 commit comments

Comments
 (0)