Skip to content

Commit

Permalink
Move info and list commands into shared functions
Browse files Browse the repository at this point in the history
Also add better output formatting for both commands
  • Loading branch information
josegonzalez committed Aug 29, 2015
1 parent fb51474 commit 08f90bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
16 changes: 2 additions & 14 deletions commands
Expand Up @@ -181,23 +181,11 @@ case "$1" in
;;

$PLUGIN_COMMAND_PREFIX:info)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"; SERVICE_URL=$(service_url "$SERVICE")

echo " DSN: $SERVICE_URL"
service_info $2
;;

$PLUGIN_COMMAND_PREFIX:list)
CONTAINERS=$(ls $PLUGIN_DATA_ROOT 2> /dev/null)
if [[ -z $CONTAINERS ]]; then
echo "There are no $PLUGIN_SERVICE services"
else
echo "$PLUGIN_SERVICE services:"
for CONTAINER in $CONTAINERS; do
echo " - $CONTAINER"
done
fi
service_list
;;

$PLUGIN_COMMAND_PREFIX:clone)
Expand Down
47 changes: 47 additions & 0 deletions functions
Expand Up @@ -40,6 +40,41 @@ service_alias() {
fi
}

service_info() {
[[ -z $1 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$1"

local SERVICE="$1";
local SERVICE_URL=$(service_url "$SERVICE")

echo " DSN: $SERVICE_URL"
}

service_list() {
local SERVICES=$(ls $PLUGIN_DATA_ROOT 2> /dev/null)
if [[ -z $SERVICES ]]; then
dokku_log_warn "There are no $PLUGIN_SERVICE services"
else
dokku_log_info1_quiet "$PLUGIN_SERVICE services:"
for SERVICE in $SERVICES; do
dokku_log_verbose "$SERVICE $(service_status $SERVICE)"
done
fi
}

service_status() {
local SERVICE="$1";
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"

is_container_status "$ID" "Dead" && echo "(dead)" && return 0
is_container_status "$ID" "OOMKilled" && echo "(oomkilled)" && return 0
is_container_status "$ID" "Paused" && echo "(paused)" && return 0
is_container_status "$ID" "Restarting" && echo "(restarting)" && return 0
is_container_status "$ID" "Running" && echo "(running)" && return 0
echo "(stopped)" && return 0
}

service_url() {
local SERVICE="$1";
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
Expand All @@ -48,3 +83,15 @@ service_url() {
local IP="$(get_container_ip "$ID")"
echo "$PLUGIN_SCHEME://$IP:$PLUGIN_DATASTORE_PORT/0"
}

is_container_status () {
local CID=$1
local TEMPLATE="{{.State.$2}}"
local CONTAINER_STATUS=$(docker inspect -f "$TEMPLATE" "$CID" || true)

if [[ "$CONTAINER_STATUS" == "true" ]]; then
return 0
else
return 1
fi
}

0 comments on commit 08f90bf

Please sign in to comment.