diff --git a/README.md b/README.md index c04bd4a..f4bfbee 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,13 @@ dokku plugins-install ``` mongo:alias Set an alias for the docker link -mongo:clone NOT IMPLEMENTED +mongo:clone Create container then copy data from into mongo:connect Connect via telnet to a mongo service mongo:create Create a mongo service mongo:destroy Delete the service and stop its container if there are no links left -mongo:export NOT IMPLEMENTED +mongo:export Export a dump of the $PLUGIN_SERVICE service database mongo:expose [port] Expose a mongo service on custom port if provided (random port otherwise) -mongo:import NOT IMPLEMENTED +mongo:import Import a dump into the $PLUGIN_SERVICE service database mongo:info Print the connection information mongo:link Link the mongo service to the app mongo:list List all mongo services @@ -85,6 +85,15 @@ dokku mongo:unlink lolipop playground dokku mongo:logs lolipop dokku mongo:logs lolipop -t # to tail +# you can dump the database +dokku mongo:export lolipop > lolipop.dump.tar + +# you can import a dump +dokku mongo:import lolipop < database.dump.tar + +# you can clone an existing database to a new one +dokku mongo:clone lolipop new_database + # finally, you can destroy the container dokku mongo:destroy lolipop ``` diff --git a/commands b/commands index dbc6d8d..0fefc6c 100755 --- a/commands +++ b/commands @@ -102,16 +102,21 @@ case "$1" in [[ -z $2 ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$2" SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + SERVICE_NAME="$(get_service_name "$SERVICE")" - dokku_log_fail "Not yet implemented" + docker exec "$SERVICE_NAME" bash -c "DIR=\$(mktemp -d) && mongodump -d $SERVICE -o=\"\$DIR\" && tar cf - -C \"\$DIR\" . && rm -rf \"\$DIR\"" ;; $PLUGIN_COMMAND_PREFIX:import) [[ -z $2 ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$2" - SERVICE="$2" + SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + SERVICE_NAME="$(get_service_name "$SERVICE")" - dokku_log_fail "Not yet implemented" + if [[ -t 0 ]]; then + dokku_log_fail "No data provided on stdin." + fi + docker exec -i "$SERVICE_NAME" bash -c "DIR=\$(mktemp -d) && tar xf - -C \"\$DIR\" && mongorestore -d $SERVICE \$(find \"\$DIR\" -mindepth 1 -maxdepth 1 -type d | head -n1) && rm -rf \"\$DIR\"" ;; $PLUGIN_COMMAND_PREFIX:logs) @@ -161,10 +166,14 @@ case "$1" in $PLUGIN_COMMAND_PREFIX:clone) [[ -z $2 ]] && dokku_log_fail "Please specify a name for the service" + [[ -z $3 ]] && dokku_log_fail "Please specify a name for the new service" verify_service_name "$2" SERVICE="$2" - - dokku_log_fail "Not yet implemented" + NEW_SERVICE="$3" + dokku "$PLUGIN_COMMAND_PREFIX:create" "$NEW_SERVICE" + dokku_log_info1 "Copying data from $SERVICE to $NEW_SERVICE" + dokku "$PLUGIN_COMMAND_PREFIX:export" "$SERVICE" | dokku "$PLUGIN_COMMAND_PREFIX:import" "$NEW_SERVICE" > /dev/null 2>&1 || true + dokku_log_info1 "Done" ;; $PLUGIN_COMMAND_PREFIX:expose) @@ -186,14 +195,14 @@ case "$1" in $PLUGIN_COMMAND_PREFIX:destroy , Delete the $PLUGIN_SERVICE service and stop its container if there are no links left $PLUGIN_COMMAND_PREFIX:link , Link the $PLUGIN_SERVICE service to the app $PLUGIN_COMMAND_PREFIX:unlink , Unlink the $PLUGIN_SERVICE service from the app - $PLUGIN_COMMAND_PREFIX:export , NOT IMPLEMENTED - $PLUGIN_COMMAND_PREFIX:import , NOT IMPLEMENTED + $PLUGIN_COMMAND_PREFIX:export , Export a dump of the $PLUGIN_SERVICE service database + $PLUGIN_COMMAND_PREFIX:import < , Import a dump into the $PLUGIN_SERVICE service database $PLUGIN_COMMAND_PREFIX:connect , Connect via telnet to a $PLUGIN_SERVICE service $PLUGIN_COMMAND_PREFIX:logs [-t], Print the most recent log(s) for this service $PLUGIN_COMMAND_PREFIX:restart , Graceful shutdown and restart of the $PLUGIN_SERVICE service container $PLUGIN_COMMAND_PREFIX:info , Print the connection information $PLUGIN_COMMAND_PREFIX:list, List all $PLUGIN_SERVICE services - $PLUGIN_COMMAND_PREFIX:clone , NOT IMPLEMENTED + $PLUGIN_COMMAND_PREFIX:clone , Create container then copy data from into $PLUGIN_COMMAND_PREFIX:expose [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise) $PLUGIN_COMMAND_PREFIX:unexpose , Unexpose a previously exposed $PLUGIN_SERVICE service $PLUGIN_COMMAND_PREFIX:start , Start a previously stopped $PLUGIN_SERVICE service diff --git a/tests/service_clone.bats b/tests/service_clone.bats new file mode 100755 index 0000000..658eb24 --- /dev/null +++ b/tests/service_clone.bats @@ -0,0 +1,42 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + export ECHO_DOCKER_COMMAND="false" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + export ECHO_DOCKER_COMMAND="false" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:clone) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:clone" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:clone) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:clone" not_existing_service new_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:clone) error when new service isn't provided" { + run dokku "$PLUGIN_COMMAND_PREFIX:clone" l + assert_contains "${lines[*]}" "Please specify a name for the new service" +} + +@test "($PLUGIN_COMMAND_PREFIX:clone) error when new service already exists" { + dokku "$PLUGIN_COMMAND_PREFIX:create" new_service + run dokku "$PLUGIN_COMMAND_PREFIX:clone" l new_service + assert_contains "${lines[*]}" "service new_service already exists" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" new_service +} + +@test "($PLUGIN_COMMAND_PREFIX:clone) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:clone" l new_service + [[ -f $PLUGIN_DATA_ROOT/new_service/ID ]] + assert_contains "${lines[*]}" "Copying data from l to new_service" + assert_contains "${lines[*]}" "Done" +} + diff --git a/tests/service_export.bats b/tests/service_export.bats new file mode 100755 index 0000000..1ddd52e --- /dev/null +++ b/tests/service_export.bats @@ -0,0 +1,29 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + export ECHO_DOCKER_COMMAND="false" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + export ECHO_DOCKER_COMMAND="false" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:export) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:export" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:export) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:export" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:export) success" { + export ECHO_DOCKER_COMMAND="true" + run dokku "$PLUGIN_COMMAND_PREFIX:export" l + assert_output "docker exec dokku.mongo.l bash -c DIR=\$(mktemp -d) && mongodump -d l -o=\"\$DIR\" && tar cf - -C \"\$DIR\" . && rm -rf \"\$DIR\"" +} + diff --git a/tests/service_import.bats b/tests/service_import.bats new file mode 100755 index 0000000..8627853 --- /dev/null +++ b/tests/service_import.bats @@ -0,0 +1,36 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + export ECHO_DOCKER_COMMAND="false" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 + echo "data" > "$PLUGIN_DATA_ROOT/fake.dump.tar" +} + +teardown() { + export ECHO_DOCKER_COMMAND="false" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 + rm -f "$PLUGIN_DATA_ROOT/fake.dump.tar" +} + +@test "($PLUGIN_COMMAND_PREFIX:import) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:import" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:import) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:import" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:import) error when data is not provided" { + run dokku "$PLUGIN_COMMAND_PREFIX:import" l + assert_contains "${lines[*]}" "No data provided on stdin" +} + +@test "($PLUGIN_COMMAND_PREFIX:import) success" { + export ECHO_DOCKER_COMMAND="true" + run dokku "$PLUGIN_COMMAND_PREFIX:import" l < "$PLUGIN_DATA_ROOT/fake.dump.tar" + assert_output "docker exec -i dokku.mongo.l bash -c DIR=\$(mktemp -d) && tar xf - -C \"\$DIR\" && mongorestore -d l \$(find \"\$DIR\" -mindepth 1 -maxdepth 1 -type d | head -n1) && rm -rf \"\$DIR\"" +} +