From c0028f122bfb6d471237ab4032104ce2d2b86c3e Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Wed, 14 Jan 2015 15:19:58 -0800 Subject: [PATCH] initial pass at a ps plugin --- plugins/ps/commands | 82 +++++++++++++++++++++++++++++++++++++++++++++ tests/unit/ps.bats | 55 ++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100755 plugins/ps/commands create mode 100644 tests/unit/ps.bats diff --git a/plugins/ps/commands b/plugins/ps/commands new file mode 100755 index 000000000..26ac34d15 --- /dev/null +++ b/plugins/ps/commands @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +release_and_deploy() { + local APP="$1"; + + if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then + echo "-----> Releasing $APP ..." + dokku release $APP + echo "-----> Release complete!" + echo "-----> Deploying $APP ..." + dokku deploy $APP + echo "=====> Application deployed:" + dokku urls $APP | sed "s/^/ /" + echo + fi +} + +case "$1" in + ps) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + + APP="$2"; [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] && CONTAINER_ID=$(< "$DOKKU_ROOT/$APP/CONTAINER") + [[ -z "$CONTAINER_ID" ]] && echo "App $APP has not been deployed" && exit 1 + + docker exec -ti "$CONTAINER_ID" /bin/bash -c "ps auxwww" + ;; + + ps:start) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + + APP="$2"; [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] && CONTAINER_ID=$(< "$DOKKU_ROOT/$APP/CONTAINER") + [[ -z "$CONTAINER_ID" ]] && echo "App $APP has not been deployed" && exit 1 + + if [[ "$(docker ps -q --no-trunc| grep -q $CONTAINER_ID; echo $?)" != "0" ]]; then + release_and_deploy $APP + else + echo "App $APP already running" + fi + ;; + + ps:stop) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + + APP="$2"; [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] && CONTAINER_ID=$(< "$DOKKU_ROOT/$APP/CONTAINER") + [[ -z "$CONTAINER_ID" ]] && echo "App $APP has not been deployed" && exit 1 + + if [[ "$(docker ps -q --no-trunc| grep -q $CONTAINER_ID; echo $?)" = "0" ]]; then + echo "Stopping $APP ..." + docker stop $CONTAINER_ID > /dev/null + else + echo "App $APP already stopped" + fi + ;; + + ps:restart) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + + APP="$2"; [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] && CONTAINER_ID=$(< "$DOKKU_ROOT/$APP/CONTAINER") + [[ -z "$CONTAINER_ID" ]] && echo "App $APP has not been deployed" && exit 1 + + release_and_deploy $APP + ;; + + help | ps:help) + cat && cat< List containers for app + ps:start Start app container(s) + ps:stop Stop app container(s) + ps:restart Restart app container(s) +EOF + ;; + + *) + exit $DOKKU_NOT_IMPLEMENTED_EXIT + ;; + +esac diff --git a/tests/unit/ps.bats b/tests/unit/ps.bats new file mode 100644 index 000000000..58d6e787f --- /dev/null +++ b/tests/unit/ps.bats @@ -0,0 +1,55 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + deploy_app +} + +teardown() { + destroy_app +} + +@test "ps" { + run bash -c "dokku ps $TEST_APP | grep -q \"node web.js\"" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "ps:start" { + run bash -c "dokku ps:stop $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "dokku ps:start $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "ps:stop" { + run bash -c "dokku ps:stop $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_failure +} + +@test "ps:restart" { + run bash -c "dokku ps:restart $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_success +}