From de4150afa647452214a6b0ff47326f5bb05ef0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Sat, 24 Nov 2018 14:29:10 -0500 Subject: [PATCH] Add verbose mode --- README.md | 2 ++ psu | 40 +++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) mode change 100644 => 100755 psu diff --git a/README.md b/README.md index 97252a1..1abfbe3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ This is particularly useful for CI/CD pipelines. - `PORTAINER_PRUNE` ("true" or "false", optional): Whether to prune unused containers or not. Defaults to `"false"`. - `PORTAINER_ENDPOINT` (int, optional): Which endpoint to use. Defaults to `1`. - `HTTPIE_VERIFY_SSL` ("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to `"yes"`. +- `VERBOSE_MODE` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`. #### Examples @@ -70,6 +71,7 @@ This is more suitable for standalone script usage. - `-r` ("true" or "false", optional): Whether to prune unused containers or not. Defaults to `"false"`. - `-e` (int, optional): Which endpoint to use. Defaults to `1`. - `-s` ("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to `"yes"`. +- `-v` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`. #### Examples diff --git a/psu b/psu old mode 100644 new mode 100755 index 2bddea6..8dc2fdc --- a/psu +++ b/psu @@ -42,6 +42,18 @@ check_for_errors () { fi } +########################################### +# Print message if verbose mode is active # +# Arguments: # +# $1 Message # +########################################### +echo_verbose () { + local message=$1 + if [ $VERBOSE_MODE == "true" ]; then + echo $message + fi +} + deploy () { STACK_YAML_PATH=$DOCKER_COMPOSE_FILE @@ -57,9 +69,9 @@ deploy () { STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\n'/'\n'}" if [ -z "$STACK" ]; then - echo "Result: Stack $STACK_NAME not found." + echo_verbose "Result: Stack $STACK_NAME not found." - echo "Getting swarm cluster (if any)..." + echo_verbose "Getting swarm cluster (if any)..." SWARM_ID=$(http \ --check-status \ --ignore-stdin \ @@ -69,7 +81,7 @@ deploy () { check_for_errors $? "$SWARM_ID" SWARM_ID=$(echo $SWARM_ID | jq -r ".Swarm.Cluster.ID // empty") - echo "Creating stack $STACK_NAME..." + echo_verbose "Creating stack $STACK_NAME..." if [ -z "$SWARM_ID" ];then DATA_PREFIX="{\"Name\":\"$STACK_NAME\",\"StackFileContent\":\"" DATA_SUFFIX="\"}" @@ -107,7 +119,7 @@ deploy () { rm json.tmp else - echo "Result: Stack $STACK_NAME found." + echo_verbose "Result: Stack $STACK_NAME found." STACK_ID="$(echo "$STACK" | jq -j ".Id")" STACK_ENV_VARS="$(echo -n "$STACK"| jq ".Env" -jc)" @@ -115,7 +127,7 @@ deploy () { DATA_SUFFIX="\",\"Env\":"$STACK_ENV_VARS",\"Prune\":$PORTAINER_PRUNE}" echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp - echo "Updating stack $STACK_NAME..." + echo_verbose "Updating stack $STACK_NAME..." UPDATE=$(http \ --check-status \ --ignore-stdin \ @@ -129,7 +141,7 @@ deploy () { rm json.tmp fi - echo "Done" + echo_verbose "Done" } undeploy () { @@ -137,18 +149,18 @@ undeploy () { echo "Result: Stack $STACK_NAME not found." exit 1 fi - echo "Result: Stack $STACK_NAME found." + echo_verbose "Result: Stack $STACK_NAME found." STACK_ID="$(echo "$STACK" | jq -j ".Id")" - echo "Deleting stack $STACK_NAME..." + echo_verbose "Deleting stack $STACK_NAME..." DELETE=$(http \ --ignore-stdin \ --verify=$HTTPIE_VERIFY_SSL \ DELETE "$PORTAINER_URL/api/stacks/$STACK_ID" \ "Authorization: Bearer $AUTH_TOKEN") check_for_errors $? "$DELETE" - echo "Done" + echo_verbose "Done" } # Set arguments through envvars @@ -161,9 +173,10 @@ DOCKER_COMPOSE_FILE=${DOCKER_COMPOSE_FILE} PORTAINER_ENDPOINT=${PORTAINER_ENDPOINT:-"1"} PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"} HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"} +VERBOSE_MODE=${VERBOSE_MODE:-"false"} # Set arguments through flags -while getopts a:u:p:l:n:c:e:rs option; do +while getopts a:u:p:l:n:c:e:rsv option; do case "${option}" in a) ACTION=${OPTARG} ;; u) PORTAINER_USER=${OPTARG} ;; @@ -174,6 +187,7 @@ while getopts a:u:p:l:n:c:e:rs option; do e) PORTAINER_ENDPOINT=${OPTARG} ;; r) PORTAINER_PRUNE="true" ;; s) HTTPIE_VERIFY_SSL="no" ;; + v) VERBOSE_MODE="true" ;; esac done @@ -186,7 +200,7 @@ check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_N STACK_NAME=$PORTAINER_STACK_NAME -echo "Getting auth token..." +echo_verbose "Getting auth token..." AUTH_TOKEN=$(http \ --check-status \ --ignore-stdin \ @@ -196,9 +210,9 @@ AUTH_TOKEN=$(http \ password=$PORTAINER_PASSWORD) check_for_errors $? "$AUTH_TOKEN" AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt) -echo "Done" +echo_verbose "Done" -echo "Getting stack $STACK_NAME..." +echo_verbose "Getting stack $STACK_NAME..." STACKS=$(http \ --check-status \ --ignore-stdin \