Skip to content

Commit 9b4c861

Browse files
authoredFeb 25, 2023
Merge pull request #48 from dokku/no-restart-link
Add ability to skip restarts on link and unlink
2 parents 65b3400 + d2fbc16 commit 9b4c861

File tree

7 files changed

+102
-41
lines changed

7 files changed

+102
-41
lines changed
 

‎README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ flags:
190190

191191
- `-a|--alias "BLUE_DATABASE"`: an alternative alias to use for linking to an app via environment variable
192192
- `-q|--querystring "pool=5"`: ampersand delimited querystring arguments to append to the service link
193+
- `-n|--no-restart "false"`: whether or not to restart the app on link (default: true)
193194

194195
A graphite service can be linked to a container. This will use native docker links via the docker-options plugin. Here we link it to our `playground` app.
195196

@@ -242,6 +243,10 @@ statsd2://dokku-graphite-lollipop:8125
242243
dokku graphite:unlink <service> <app>
243244
```
244245

246+
flags:
247+
248+
- `-n|--no-restart "false"`: whether or not to restart the app on unlink (default: true)
249+
245250
You can unlink a graphite service:
246251

247252
> NOTE: this will restart your app and unset related environment variables
@@ -428,7 +433,7 @@ flags:
428433
- `-I|--image-version IMAGE_VERSION`: the image version to start the service with
429434
- `-N|--initial-network INITIAL_NETWORK`: the initial network to attach the service to
430435
- `-P|--post-create-network NETWORKS`: a comman-separated list of networks to attach the service container to after service creation
431-
- `-R|--restart-apps "true"`: whether to force an app restart
436+
- `-R|--restart-apps "true"`: whether or not to force an app restart (default: false)
432437
- `-S|--post-start-network NETWORKS`: a comman-separated list of networks to attach the service container to after service start
433438
- `-s|--shm-size SHM_SIZE`: override shared memory size for graphite docker container
434439

‎common-functions

+9-3
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,9 @@ service_link() {
631631
fi
632632
[[ -n "$SERVICE_QUERYSTRING" ]] && SERVICE_URL="${SERVICE_URL}?${SERVICE_QUERYSTRING}"
633633
plugn trigger service-action post-link "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "$APP"
634-
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]]; then
634+
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]] || [[ "$SERVICE_RESTART_APPS" == "false" ]]; then
635635
config_set --no-restart "$APP" "${ALIAS}_URL=$SERVICE_URL"
636+
dokku_log_verbose "Skipping restart of linked app"
636637
else
637638
config_set "$APP" "${ALIAS}_URL=$SERVICE_URL"
638639
fi
@@ -714,6 +715,7 @@ service_parse_args() {
714715
"--initial-network") set -- "$@" "-N" ;;
715716
"--image") set -- "$@" "-i" ;;
716717
"--memory") set -- "$@" "-m" ;;
718+
"--no-restart") set -- "$@" "-n" ;;
717719
"--password") set -- "$@" "-p" ;;
718720
"--post-create-network") set -- "$@" "-P" ;;
719721
"--post-start-network") set -- "$@" "-S" ;;
@@ -727,7 +729,7 @@ service_parse_args() {
727729
done
728730

729731
OPTIND=1
730-
while getopts "a:c:C:d:i:I:m:n:N:p:P:q:R:r:s:S:u:" opt; do
732+
while getopts "na:c:C:d:i:I:m:n:N:p:P:q:R:r:s:S:u:" opt; do
731733
case "$opt" in
732734
a)
733735
SERVICE_ALIAS="${OPTARG^^}"
@@ -751,6 +753,9 @@ service_parse_args() {
751753
m)
752754
export SERVICE_MEMORY=$OPTARG
753755
;;
756+
n)
757+
export SERVICE_RESTART_APPS=false
758+
;;
754759
N)
755760
export SERVICE_INITIAL_NETWORK=$OPTARG
756761
;;
@@ -971,8 +976,9 @@ service_unlink() {
971976

972977
[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
973978
plugn trigger service-action post-unlink "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "$APP"
974-
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]]; then
979+
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]] || [[ "$SERVICE_RESTART_APPS" == "false" ]]; then
975980
config_unset --no-restart "$APP" "${LINK[@]}"
981+
dokku_log_verbose "Skipping restart of linked app"
976982
else
977983
config_unset "$APP" "${LINK[@]}"
978984
fi

‎subcommands/link

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ service-link-cmd() {
4242
#A app, app to run command against
4343
#F -a|--alias "BLUE_DATABASE", an alternative alias to use for linking to an app via environment variable
4444
#F -q|--querystring "pool=5", ampersand delimited querystring arguments to append to the service link
45+
#F -n|--no-restart "false", whether or not to restart the app on link (default: true)
4546
declare desc="link the $PLUGIN_SERVICE service to the app"
4647
local cmd="$PLUGIN_COMMAND_PREFIX:link" argv=("$@")
4748
[[ ${argv[0]} == "$cmd" ]] && shift 1

‎subcommands/unlink

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ service-unlink-cmd() {
1111
#E dokku $PLUGIN_COMMAND_PREFIX:unlink lollipop playground
1212
#A service, service to run command against
1313
#A app, app to run command against
14+
#F -n|--no-restart "false", whether or not to restart the app on unlink (default: true)
1415
declare desc="unlink the $PLUGIN_SERVICE service from the app"
1516
local cmd="$PLUGIN_COMMAND_PREFIX:unlink" argv=("$@")
1617
[[ ${argv[0]} == "$cmd" ]] && shift 1

‎subcommands/upgrade

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ service-upgrade-cmd() {
1616
#F -I|--image-version IMAGE_VERSION, the image version to start the service with
1717
#F -N|--initial-network INITIAL_NETWORK, the initial network to attach the service to
1818
#F -P|--post-create-network NETWORKS, a comman-separated list of networks to attach the service container to after service creation
19-
#F -R|--restart-apps "true", whether to force an app restart
19+
#F -R|--restart-apps "true", whether or not to force an app restart (default: false)
2020
#F -S|--post-start-network NETWORKS, a comman-separated list of networks to attach the service container to after service start
2121
#F -s|--shm-size SHM_SIZE, override shared memory size for $PLUGIN_COMMAND_PREFIX docker container
2222
declare desc="upgrade service <service> to the specified versions"

‎tests/service_link.bats

+51-27
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
load test_helper
33

44
setup() {
5-
dokku "$PLUGIN_COMMAND_PREFIX:create" l
6-
dokku "$PLUGIN_COMMAND_PREFIX:create" m
5+
dokku "$PLUGIN_COMMAND_PREFIX:create" ls
6+
dokku "$PLUGIN_COMMAND_PREFIX:create" ms
77
dokku apps:create my-app
88
}
99

1010
teardown() {
11-
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" m
12-
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
11+
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ms
12+
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ls
1313
dokku --force apps:destroy my-app
1414
}
1515

@@ -22,15 +22,15 @@ teardown() {
2222
}
2323

2424
@test "($PLUGIN_COMMAND_PREFIX:link) error when the app argument is missing" {
25-
run dokku "$PLUGIN_COMMAND_PREFIX:link" l
25+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls
2626
echo "output: $output"
2727
echo "status: $status"
2828
assert_contains "${lines[*]}" "Please specify an app to run the command on"
2929
assert_failure
3030
}
3131

3232
@test "($PLUGIN_COMMAND_PREFIX:link) error when the app does not exist" {
33-
run dokku "$PLUGIN_COMMAND_PREFIX:link" l not_existing_app
33+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls not_existing_app
3434
echo "output: $output"
3535
echo "status: $status"
3636
assert_contains "${lines[*]}" "App not_existing_app does not exist"
@@ -46,70 +46,94 @@ teardown() {
4646
}
4747

4848
@test "($PLUGIN_COMMAND_PREFIX:link) error when the service is already linked to app" {
49-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
50-
run dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
49+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
50+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
5151
echo "output: $output"
5252
echo "status: $status"
5353
assert_contains "${lines[*]}" "Already linked as STATSD_URL"
5454
assert_failure
5555

56-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
56+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
5757
}
5858

5959
@test "($PLUGIN_COMMAND_PREFIX:link) exports STATSD_URL to app" {
60-
run dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
60+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
6161
echo "output: $output"
6262
echo "status: $status"
6363
url=$(dokku config:get my-app STATSD_URL)
64-
assert_contains "$url" "statsd://dokku-graphite-l:8125"
64+
assert_contains "$url" "statsd://dokku-graphite-ls:8125"
6565
assert_success
66-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
66+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
6767
}
6868

6969
@test "($PLUGIN_COMMAND_PREFIX:link) generates an alternate config url when STATSD_URL already in use" {
7070
dokku config:set my-app STATSD_URL=http://host:8125
71-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
71+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
7272
run dokku config my-app
7373
assert_contains "${lines[*]}" "DOKKU_STATSD_AQUA_URL"
7474
assert_success
7575

76-
dokku "$PLUGIN_COMMAND_PREFIX:link" m my-app
76+
dokku "$PLUGIN_COMMAND_PREFIX:link" ms my-app
7777
run dokku config my-app
7878
assert_contains "${lines[*]}" "DOKKU_STATSD_BLACK_URL"
7979
assert_success
80-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" m my-app
81-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
80+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ms my-app
81+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
8282
}
8383

8484
@test "($PLUGIN_COMMAND_PREFIX:link) links to app with docker-options" {
85-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
85+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
8686
run dokku docker-options:report my-app
87-
assert_contains "${lines[*]}" "--link dokku.graphite.l:dokku-graphite-l"
87+
assert_contains "${lines[*]}" "--link dokku.graphite.ls:dokku-graphite-ls"
8888
assert_success
89-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
89+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
9090
}
9191

9292
@test "($PLUGIN_COMMAND_PREFIX:link) uses apps STATSD_DATABASE_SCHEME variable" {
9393
dokku config:set my-app STATSD_DATABASE_SCHEME=statsd2
94-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
94+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
9595
url=$(dokku config:get my-app STATSD_URL)
96-
assert_contains "$url" "statsd2://dokku-graphite-l:8125"
96+
assert_contains "$url" "statsd2://dokku-graphite-ls:8125"
9797
assert_success
98-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
98+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
9999
}
100100

101101
@test "($PLUGIN_COMMAND_PREFIX:link) adds a querystring" {
102-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app --querystring "pool=5"
102+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --querystring "pool=5"
103103
url=$(dokku config:get my-app STATSD_URL)
104104
assert_contains "$url" "?pool=5"
105105
assert_success
106-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
106+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
107107
}
108108

109109
@test "($PLUGIN_COMMAND_PREFIX:link) uses a specified config url when alias is specified" {
110-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app --alias "ALIAS"
110+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --alias "ALIAS"
111111
url=$(dokku config:get my-app ALIAS_URL)
112-
assert_contains "$url" "statsd://dokku-graphite-l:8125"
112+
assert_contains "$url" "statsd://dokku-graphite-ls:8125"
113+
assert_success
114+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
115+
}
116+
117+
@test "($PLUGIN_COMMAND_PREFIX:link) respects --no-restart" {
118+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
119+
echo "output: $output"
120+
echo "status: $status"
121+
assert_output_contains "Skipping restart of linked app" 0
122+
assert_success
123+
124+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
125+
echo "output: $output"
126+
echo "status: $status"
127+
assert_success
128+
129+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --no-restart
130+
echo "output: $output"
131+
echo "status: $status"
132+
assert_output_contains "Skipping restart of linked app"
133+
assert_success
134+
135+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
136+
echo "output: $output"
137+
echo "status: $status"
113138
assert_success
114-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
115139
}

‎tests/service_unlink.bats

+33-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ load test_helper
33

44
setup() {
55
dokku apps:create my-app
6-
dokku "$PLUGIN_COMMAND_PREFIX:create" l
6+
dokku "$PLUGIN_COMMAND_PREFIX:create" ls
77
}
88

99
teardown() {
10-
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
10+
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ls
1111
dokku --force apps:destroy my-app
1212
}
1313

@@ -17,12 +17,12 @@ teardown() {
1717
}
1818

1919
@test "($PLUGIN_COMMAND_PREFIX:unlink) error when the app argument is missing" {
20-
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l
20+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls
2121
assert_contains "${lines[*]}" "Please specify an app to run the command on"
2222
}
2323

2424
@test "($PLUGIN_COMMAND_PREFIX:unlink) error when the app does not exist" {
25-
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l not_existing_app
25+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls not_existing_app
2626
assert_contains "${lines[*]}" "App not_existing_app does not exist"
2727
}
2828

@@ -32,22 +32,46 @@ teardown() {
3232
}
3333

3434
@test "($PLUGIN_COMMAND_PREFIX:unlink) error when service not linked to app" {
35-
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
35+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
3636
assert_contains "${lines[*]}" "Not linked to app my-app"
3737
}
3838

3939
@test "($PLUGIN_COMMAND_PREFIX:unlink) removes link from docker-options" {
40-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app >&2
41-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
40+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app >&2
41+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
4242

4343
check_value="Docker options build: Docker options deploy: --restart=on-failure:10 Docker options run:"
4444
options=$(dokku --quiet docker-options:report my-app | xargs)
4545
assert_equal "$options" "$check_value"
4646
}
4747

4848
@test "($PLUGIN_COMMAND_PREFIX:unlink) unsets config url from app" {
49-
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app >&2
50-
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
49+
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app >&2
50+
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
5151
config=$(dokku config:get my-app STATSD_URL || true)
5252
assert_equal "$config" ""
5353
}
54+
55+
@test "($PLUGIN_COMMAND_PREFIX:unlink) respects --no-restart" {
56+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
57+
echo "output: $output"
58+
echo "status: $status"
59+
assert_success
60+
61+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
62+
echo "output: $output"
63+
echo "status: $status"
64+
assert_output_contains "Skipping restart of linked app" 0
65+
assert_success
66+
67+
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
68+
echo "output: $output"
69+
echo "status: $status"
70+
assert_success
71+
72+
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app --no-restart
73+
echo "output: $output"
74+
echo "status: $status"
75+
assert_output_contains "Skipping restart of linked app"
76+
assert_success
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.