From 4dbe06e99d5008530fbc07b852c19ad33ca45462 Mon Sep 17 00:00:00 2001 From: Tortue Torche Date: Sat, 10 Aug 2019 21:36:32 -0400 Subject: [PATCH] Add containers action It prints the containers of a stack (or specific service, if the `--service=service_name` option is set) --- psu | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/psu b/psu index d0d8fb0..99dd94c 100644 --- a/psu +++ b/psu @@ -1,7 +1,7 @@ #!/usr/bin/env bash # # Deploy/update/undeploy/info/status Docker stacks in a Portainer instance. -# And also list stacks, services and tasks +# And also list stacks, services, tasks and containers set -e @@ -27,8 +27,7 @@ set -e main() { # Set arguments through envvars VERSION="0.2.0-alpha1" - # TODO: Add a "containers" action, see: https://docs.docker.com/engine/api/v1.30/#operation/ContainerList - ACTIONS="deploy undeploy list info services tasks tasks_healthy status help version" + ACTIONS="deploy undeploy list info services tasks tasks_healthy containers status help version" set_globals "$@" @@ -153,6 +152,24 @@ main() { exit 1 fi + if [ $ACTION == "containers" ]; then + echo_verbose "List container(s) of stack '$PORTAINER_STACK_NAME'..." + local containers + containers=$(containers) + echo_debug "Containers action response -> $(echo $containers | jq -C .)" + if [ -n "$containers" ] && [ ! "$containers" == "[]" ]; then + if [ $QUIET_MODE == "false" ]; then + # Returns response in JSON format + echo "$containers" + else + # Only display container(s) id in quiet mode + echo "$containers" | jq -r '.[] | [.Id] | add' + fi + exit 0 + fi + exit 1 + fi + # Returns stack info # If it already exists if [ $ACTION == "info" ]; then @@ -973,6 +990,30 @@ services() { echo "$services" } -# TODO: add a "containers" function +containers() { + local containers + local filter_stack + local filter_task + local service_name + local filter_service + local filters + + filter_stack="\"label\":{\"com.docker.stack.namespace=$PORTAINER_STACK_NAME\":true}" + filter_task="\"is-task\":{\"true\":true}" + if [ -n "$PORTAINER_SERVICE_NAME" ]; then + service_name=${PORTAINER_STACK_NAME}_${PORTAINER_SERVICE_NAME} + filter_service="\"label\":{\"com.docker.swarm.service.name=$service_name\":true}" + fi + filters="{$filter_stack$(if [ -n "$filter_service" ]; then echo ",$filter_service"; fi),$filter_task}" + containers=$(http \ + --check-status \ + --ignore-stdin \ + --verify=$HTTPIE_VERIFY_SSL \ + "$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/containers/json" \ + filters=="$filters" \ + "Authorization: Bearer $AUTH_TOKEN") + check_for_errors $? "$containers" + echo "$containers" +} main "$@"