Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to skip restarts on link and unlink #39

Merged
merged 12 commits into from
Feb 25, 2023
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dokku meilisearch [![Build Status](https://img.shields.io/github/actions/workflow/status/dokku/dokku-meilisearch/ci.yml?branch=master&style=flat-square "Build Status")](https://github.com/dokku/dokku-meilisearch/actions/workflows/ci.yml?query=branch%3Amaster) [![IRC Network](https://img.shields.io/badge/irc-libera-blue.svg?style=flat-square "IRC Libera")](https://webchat.libera.chat/?channels=dokku)

Official meilisearch plugin for dokku. Currently defaults to installing [getmeili/meilisearch v1.0.0](https://hub.docker.com/r/getmeili/meilisearch/).
Official meilisearch plugin for dokku. Currently defaults to installing [getmeili/meilisearch v1.0.2](https://hub.docker.com/r/getmeili/meilisearch/).

## Requirements

Expand Down Expand Up @@ -188,6 +188,7 @@ flags:

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

A meilisearch 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.

Expand All @@ -211,7 +212,7 @@ DOKKU_MEILISEARCH_LOLLIPOP_PORT_7700_TCP_ADDR=172.17.0.1
The following will be set on the linked application by default:

```
MEILISEARCH_URL=http://lollipop:SOME_PASSWORD@dokku-meilisearch-lollipop:7700/lollipop
MEILISEARCH_URL=http://:SOME_PASSWORD@dokku-meilisearch-lollipop:7700
```

The host exposed here only works internally in docker containers. If you want your container to be reachable from outside, you should use the `expose` subcommand. Another service can be linked to your app:
Expand All @@ -230,7 +231,7 @@ dokku meilisearch:link lollipop playground
This will cause `MEILISEARCH_URL` to be set as:

```
http2://lollipop:SOME_PASSWORD@dokku-meilisearch-lollipop:7700/lollipop
http2://:SOME_PASSWORD@dokku-meilisearch-lollipop:7700
```

### unlink the meilisearch service from the app
Expand All @@ -240,6 +241,10 @@ http2://lollipop:SOME_PASSWORD@dokku-meilisearch-lollipop:7700/lollipop
dokku meilisearch:unlink <service> <app>
```

flags:

- `-n|--no-restart "false"`: whether or not to restart the app on unlink (default: true)

You can unlink a meilisearch service:

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

Expand Down
12 changes: 9 additions & 3 deletions common-functions
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,9 @@ service_link() {
fi
[[ -n "$SERVICE_QUERYSTRING" ]] && SERVICE_URL="${SERVICE_URL}?${SERVICE_QUERYSTRING}"
plugn trigger service-action post-link "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "$APP"
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]]; then
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]] || [[ "$SERVICE_RESTART_APPS" == "false" ]]; then
config_set --no-restart "$APP" "${ALIAS}_URL=$SERVICE_URL"
dokku_log_verbose "Skipping restart of linked app"
else
config_set "$APP" "${ALIAS}_URL=$SERVICE_URL"
fi
Expand Down Expand Up @@ -714,6 +715,7 @@ service_parse_args() {
"--initial-network") set -- "$@" "-N" ;;
"--image") set -- "$@" "-i" ;;
"--memory") set -- "$@" "-m" ;;
"--no-restart") set -- "$@" "-n" ;;
"--password") set -- "$@" "-p" ;;
"--post-create-network") set -- "$@" "-P" ;;
"--post-start-network") set -- "$@" "-S" ;;
Expand All @@ -727,7 +729,7 @@ service_parse_args() {
done

OPTIND=1
while getopts "a:c:C:d:i:I:m:n:N:p:P:q:R:r:s:S:u:" opt; do
while getopts "na:c:C:d:i:I:m:n:N:p:P:q:R:r:s:S:u:" opt; do
case "$opt" in
a)
SERVICE_ALIAS="${OPTARG^^}"
Expand All @@ -751,6 +753,9 @@ service_parse_args() {
m)
export SERVICE_MEMORY=$OPTARG
;;
n)
export SERVICE_RESTART_APPS=false
;;
N)
export SERVICE_INITIAL_NETWORK=$OPTARG
;;
Expand Down Expand Up @@ -971,8 +976,9 @@ service_unlink() {

[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
plugn trigger service-action post-unlink "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "$APP"
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]]; then
if [[ "$DOKKU_GLOBAL_FLAGS" == *"--no-restart"* ]] || [[ "$SERVICE_RESTART_APPS" == "false" ]]; then
config_unset --no-restart "$APP" "${LINK[@]}"
dokku_log_verbose "Skipping restart of linked app"
else
config_unset "$APP" "${LINK[@]}"
fi
Expand Down
5 changes: 3 additions & 2 deletions subcommands/link
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ service-link-cmd() {
#E
#E the following will be set on the linked application by default:
#E
#E ${PLUGIN_DEFAULT_ALIAS}_URL=${PLUGIN_SCHEME}://lollipop:SOME_PASSWORD@dokku-${PLUGIN_COMMAND_PREFIX}-lollipop:${PLUGIN_DATASTORE_PORTS[0]}/lollipop
#E ${PLUGIN_DEFAULT_ALIAS}_URL=${PLUGIN_SCHEME}://:SOME_PASSWORD@dokku-${PLUGIN_COMMAND_PREFIX}-lollipop:${PLUGIN_DATASTORE_PORTS[0]}
#E
#E the host exposed here only works internally in docker containers.
#E if you want your container to be reachable from outside, you should
Expand All @@ -37,11 +37,12 @@ service-link-cmd() {
#E dokku $PLUGIN_COMMAND_PREFIX:link lollipop playground
#E this will cause ${PLUGIN_DEFAULT_ALIAS}_URL to be set as:
#E
#E ${PLUGIN_SCHEME}2://lollipop:SOME_PASSWORD@dokku-${PLUGIN_COMMAND_PREFIX}-lollipop:${PLUGIN_DATASTORE_PORTS[0]}/lollipop
#E ${PLUGIN_SCHEME}2://:SOME_PASSWORD@dokku-${PLUGIN_COMMAND_PREFIX}-lollipop:${PLUGIN_DATASTORE_PORTS[0]}
#A service, service to run command against
#A app, app to run command against
#F -a|--alias "BLUE_DATABASE", an alternative alias to use for linking to an app via environment variable
#F -q|--querystring "pool=5", ampersand delimited querystring arguments to append to the service link
#F -n|--no-restart "false", whether or not to restart the app on link (default: true)
declare desc="link the $PLUGIN_SERVICE service to the app"
local cmd="$PLUGIN_COMMAND_PREFIX:link" argv=("$@")
[[ ${argv[0]} == "$cmd" ]] && shift 1
Expand Down
1 change: 1 addition & 0 deletions subcommands/unlink
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ service-unlink-cmd() {
#E dokku $PLUGIN_COMMAND_PREFIX:unlink lollipop playground
#A service, service to run command against
#A app, app to run command against
#F -n|--no-restart "false", whether or not to restart the app on unlink (default: true)
declare desc="unlink the $PLUGIN_SERVICE service from the app"
local cmd="$PLUGIN_COMMAND_PREFIX:unlink" argv=("$@")
[[ ${argv[0]} == "$cmd" ]] && shift 1
Expand Down
2 changes: 1 addition & 1 deletion subcommands/upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ service-upgrade-cmd() {
#F -I|--image-version IMAGE_VERSION, the image version to start the service with
#F -N|--initial-network INITIAL_NETWORK, the initial network to attach the service to
#F -P|--post-create-network NETWORKS, a comman-separated list of networks to attach the service container to after service creation
#F -R|--restart-apps "true", whether to force an app restart
#F -R|--restart-apps "true", whether or not to force an app restart (default: false)
#F -S|--post-start-network NETWORKS, a comman-separated list of networks to attach the service container to after service start
#F -s|--shm-size SHM_SIZE, override shared memory size for $PLUGIN_COMMAND_PREFIX docker container
declare desc="upgrade service <service> to the specified versions"
Expand Down
84 changes: 54 additions & 30 deletions tests/service_link.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
load test_helper

setup() {
dokku "$PLUGIN_COMMAND_PREFIX:create" l
dokku "$PLUGIN_COMMAND_PREFIX:create" m
dokku "$PLUGIN_COMMAND_PREFIX:create" ls
dokku "$PLUGIN_COMMAND_PREFIX:create" ms
dokku apps:create my-app
}

teardown() {
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" m
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ms
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ls
dokku --force apps:destroy my-app
}

Expand All @@ -22,15 +22,15 @@ teardown() {
}

@test "($PLUGIN_COMMAND_PREFIX:link) error when the app argument is missing" {
run dokku "$PLUGIN_COMMAND_PREFIX:link" l
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls
echo "output: $output"
echo "status: $status"
assert_contains "${lines[*]}" "Please specify an app to run the command on"
assert_failure
}

@test "($PLUGIN_COMMAND_PREFIX:link) error when the app does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:link" l not_existing_app
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls not_existing_app
echo "output: $output"
echo "status: $status"
assert_contains "${lines[*]}" "App not_existing_app does not exist"
Expand All @@ -46,73 +46,97 @@ teardown() {
}

@test "($PLUGIN_COMMAND_PREFIX:link) error when the service is already linked to app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
run dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
echo "output: $output"
echo "status: $status"
assert_contains "${lines[*]}" "Already linked as MEILISEARCH_URL"
assert_failure

dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) exports MEILISEARCH_URL to app" {
run dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
echo "output: $output"
echo "status: $status"
url=$(dokku config:get my-app MEILISEARCH_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "$url" "http://:$password@dokku-meilisearch-l:7700"
password="$(sudo cat "$PLUGIN_DATA_ROOT/ls/PASSWORD")"
assert_contains "$url" "http://:$password@dokku-meilisearch-ls:7700"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) generates an alternate config url when MEILISEARCH_URL already in use" {
dokku config:set my-app MEILISEARCH_URL=http://user:pass@host:7700/db
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
run dokku config my-app
assert_contains "${lines[*]}" "DOKKU_MEILISEARCH_AQUA_URL"
assert_success

dokku "$PLUGIN_COMMAND_PREFIX:link" m my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ms my-app
run dokku config my-app
assert_contains "${lines[*]}" "DOKKU_MEILISEARCH_BLACK_URL"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" m my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ms my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) links to app with docker-options" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
run dokku docker-options:report my-app
assert_contains "${lines[*]}" "--link dokku.meilisearch.l:dokku-meilisearch-l"
assert_contains "${lines[*]}" "--link dokku.meilisearch.ls:dokku-meilisearch-ls"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) uses apps MEILISEARCH_DATABASE_SCHEME variable" {
dokku config:set my-app MEILISEARCH_DATABASE_SCHEME=http2
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
url=$(dokku config:get my-app MEILISEARCH_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "$url" "http2://:$password@dokku-meilisearch-l:7700"
password="$(sudo cat "$PLUGIN_DATA_ROOT/ls/PASSWORD")"
assert_contains "$url" "http2://:$password@dokku-meilisearch-ls:7700"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) adds a querystring" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app --querystring "pool=5"
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --querystring "pool=5"
url=$(dokku config:get my-app MEILISEARCH_URL)
assert_contains "$url" "?pool=5"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) uses a specified config url when alias is specified" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app --alias "ALIAS"
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --alias "ALIAS"
url=$(dokku config:get my-app ALIAS_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "$url" "http://:$password@dokku-meilisearch-l:7700"
password="$(sudo cat "$PLUGIN_DATA_ROOT/ls/PASSWORD")"
assert_contains "$url" "http://:$password@dokku-meilisearch-ls:7700"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
}

@test "($PLUGIN_COMMAND_PREFIX:link) respects --no-restart" {
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
echo "output: $output"
echo "status: $status"
assert_output_contains "Skipping restart of linked app" 0
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
echo "output: $output"
echo "status: $status"
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app --no-restart
echo "output: $output"
echo "status: $status"
assert_output_contains "Skipping restart of linked app"
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
echo "output: $output"
echo "status: $status"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
}
42 changes: 33 additions & 9 deletions tests/service_unlink.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ load test_helper

setup() {
dokku apps:create my-app
dokku "$PLUGIN_COMMAND_PREFIX:create" l
dokku "$PLUGIN_COMMAND_PREFIX:create" ls
}

teardown() {
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" ls
dokku --force apps:destroy my-app
}

Expand All @@ -17,12 +17,12 @@ teardown() {
}

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

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

Expand All @@ -32,22 +32,46 @@ teardown() {
}

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

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

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

@test "($PLUGIN_COMMAND_PREFIX:unlink) unsets config url from app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my-app >&2
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my-app
dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app >&2
dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
config=$(dokku config:get my-app MEILISEARCH_URL || true)
assert_equal "$config" ""
}

@test "($PLUGIN_COMMAND_PREFIX:unlink) respects --no-restart" {
run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
echo "output: $output"
echo "status: $status"
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app
echo "output: $output"
echo "status: $status"
assert_output_contains "Skipping restart of linked app" 0
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:link" ls my-app
echo "output: $output"
echo "status: $status"
assert_success

run dokku "$PLUGIN_COMMAND_PREFIX:unlink" ls my-app --no-restart
echo "output: $output"
echo "status: $status"
assert_output_contains "Skipping restart of linked app"
assert_success
}