From 77158323254c40f475d368bbbed31157b8fb6311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Sun, 25 Nov 2018 13:54:51 -0500 Subject: [PATCH] Add main function --- psu | 209 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 108 insertions(+), 101 deletions(-) diff --git a/psu b/psu index e89bae0..9bdc592 100755 --- a/psu +++ b/psu @@ -2,6 +2,113 @@ # # Deploy/update/undeploy Docker stacks in a Portainer instance. +################### +# Main entrypoint # +################### +main () { + # Set arguments through envvars + ACTION=${ACTION} + PORTAINER_USER=${PORTAINER_USER} + PORTAINER_PASSWORD=${PORTAINER_PASSWORD} + PORTAINER_URL=${PORTAINER_URL} + PORTAINER_STACK_NAME=${PORTAINER_STACK_NAME} + 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"} + DEBUG_MODE=${DEBUG_MODE:-"false"} + STRICT_MODE=${STRICT_MODE:-"false"} + + # Set arguments through flags (overwrite envvars) + while getopts a:u:p:l:n:c:e:rsvdt option; do + case "${option}" in + a) ACTION=${OPTARG} ;; + u) PORTAINER_USER=${OPTARG} ;; + p) PORTAINER_PASSWORD=${OPTARG} ;; + l) PORTAINER_URL=${OPTARG} ;; + n) PORTAINER_STACK_NAME=${OPTARG} ;; + c) DOCKER_COMPOSE_FILE=${OPTARG} ;; + e) PORTAINER_ENDPOINT=${OPTARG} ;; + r) PORTAINER_PRUNE="true" ;; + s) HTTPIE_VERIFY_SSL="no" ;; + v) VERBOSE_MODE="true" ;; + d) DEBUG_MODE="true" ;; + t) STRICT_MODE="true" ;; + *) + err "Unexpected option ${option}" + exit 1 + ;; + esac + done + + # Print config (only if debug mode is active) + echo_debug "ACTION -> $ACTION" + echo_debug "PORTAINER_USER -> $PORTAINER_USER" + echo_debug "PORTAINER_PASSWORD -> $PORTAINER_PASSWORD" + echo_debug "PORTAINER_URL -> $PORTAINER_URL" + echo_debug "PORTAINER_STACK_NAME -> $PORTAINER_STACK_NAME" + echo_debug "DOCKER_COMPOSE_FILE -> $DOCKER_COMPOSE_FILE" + echo_debug "PORTAINER_ENDPOINT -> $PORTAINER_ENDPOINT" + echo_debug "PORTAINER_PRUNE -> $PORTAINER_PRUNE" + echo_debug "HTTPIE_VERIFY_SSL -> $HTTPIE_VERIFY_SSL" + echo_debug "VERBOSE_MODE -> $VERBOSE_MODE" + echo_debug "DEBUG_MODE -> $DEBUG_MODE" + + # Check required arguments have been provided + check_argument "$ACTION" "action" "ACTION" "a" + check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u" + check_argument "$PORTAINER_PASSWORD" "portainer password" "PORTAINER_PASSWORD" "p" + check_argument "$PORTAINER_URL" "portainer url" "PORTAINER_URL" "l" + check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n" + if [ $ACTION == "deploy" ]; then + check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" + fi + + # Get Portainer auth token. Will be used on every API request. + echo_verbose "Getting auth token..." + AUTH_TOKEN=$(http \ + --check-status \ + --ignore-stdin \ + --verify=$HTTPIE_VERIFY_SSL \ + $PORTAINER_URL/api/auth \ + username=$PORTAINER_USER \ + password=$PORTAINER_PASSWORD) + echo_debug "Get auth token response -> $AUTH_TOKEN" + check_for_errors $? "$AUTH_TOKEN" + AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt) + echo_debug "Auth token -> $AUTH_TOKEN" + + # Get list of all stacks + echo_verbose "Getting stack $PORTAINER_STACK_NAME..." + local stacks=$(http \ + --check-status \ + --ignore-stdin \ + --verify=$HTTPIE_VERIFY_SSL \ + "$PORTAINER_URL/api/stacks" \ + "Authorization: Bearer $AUTH_TOKEN") + echo_debug "Get stacks response -> $stacks" + check_for_errors $? "$stacks" + + # Get desired stack from stacks list by it's name + STACK=$(echo "$stacks" \ + | jq --arg PORTAINER_STACK_NAME "$PORTAINER_STACK_NAME" -jc '.[] | select(.Name == $PORTAINER_STACK_NAME)') + echo_debug "Stack -> $STACK" + + if [ $ACTION == "deploy" ]; then + deploy + exit 0 + fi + + if [ $ACTION == "undeploy" ]; then + undeploy + exit 0 + fi + + err "Error: Unknown action \"$ACTION\"." + exit 1 +} + ############################ # Print an error to stderr # # Globals: # @@ -266,104 +373,4 @@ undeploy () { check_for_errors $? "$DELETE" } -# Set arguments through envvars -ACTION=${ACTION} -PORTAINER_USER=${PORTAINER_USER} -PORTAINER_PASSWORD=${PORTAINER_PASSWORD} -PORTAINER_URL=${PORTAINER_URL} -PORTAINER_STACK_NAME=${PORTAINER_STACK_NAME} -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"} -DEBUG_MODE=${DEBUG_MODE:-"false"} -STRICT_MODE=${STRICT_MODE:-"false"} - -# Set arguments through flags (overwrite envvars) -while getopts a:u:p:l:n:c:e:rsvdt option; do - case "${option}" in - a) ACTION=${OPTARG} ;; - u) PORTAINER_USER=${OPTARG} ;; - p) PORTAINER_PASSWORD=${OPTARG} ;; - l) PORTAINER_URL=${OPTARG} ;; - n) PORTAINER_STACK_NAME=${OPTARG} ;; - c) DOCKER_COMPOSE_FILE=${OPTARG} ;; - e) PORTAINER_ENDPOINT=${OPTARG} ;; - r) PORTAINER_PRUNE="true" ;; - s) HTTPIE_VERIFY_SSL="no" ;; - v) VERBOSE_MODE="true" ;; - d) DEBUG_MODE="true" ;; - t) STRICT_MODE="true" ;; - *) - err "Unexpected option ${option}" - exit 1 - ;; - esac -done - -# Print config (only if debug mode is active) -echo_debug "ACTION -> $ACTION" -echo_debug "PORTAINER_USER -> $PORTAINER_USER" -echo_debug "PORTAINER_PASSWORD -> $PORTAINER_PASSWORD" -echo_debug "PORTAINER_URL -> $PORTAINER_URL" -echo_debug "PORTAINER_STACK_NAME -> $PORTAINER_STACK_NAME" -echo_debug "DOCKER_COMPOSE_FILE -> $DOCKER_COMPOSE_FILE" -echo_debug "PORTAINER_ENDPOINT -> $PORTAINER_ENDPOINT" -echo_debug "PORTAINER_PRUNE -> $PORTAINER_PRUNE" -echo_debug "HTTPIE_VERIFY_SSL -> $HTTPIE_VERIFY_SSL" -echo_debug "VERBOSE_MODE -> $VERBOSE_MODE" -echo_debug "DEBUG_MODE -> $DEBUG_MODE" - -# Check required arguments have been provided -check_argument "$ACTION" "action" "ACTION" "a" -check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u" -check_argument "$PORTAINER_PASSWORD" "portainer password" "PORTAINER_PASSWORD" "p" -check_argument "$PORTAINER_URL" "portainer url" "PORTAINER_URL" "l" -check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n" -if [ $ACTION == "deploy" ]; then - check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" -fi - -# Get Portainer auth token. Will be used on every API request. -echo_verbose "Getting auth token..." -AUTH_TOKEN=$(http \ - --check-status \ - --ignore-stdin \ - --verify=$HTTPIE_VERIFY_SSL \ - $PORTAINER_URL/api/auth \ - username=$PORTAINER_USER \ - password=$PORTAINER_PASSWORD) -echo_debug "Get auth token response -> $AUTH_TOKEN" -check_for_errors $? "$AUTH_TOKEN" -AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt) -echo_debug "Auth token -> $AUTH_TOKEN" - -# Get list of all stacks -echo_verbose "Getting stack $PORTAINER_STACK_NAME..." -STACKS=$(http \ - --check-status \ - --ignore-stdin \ - --verify=$HTTPIE_VERIFY_SSL \ - "$PORTAINER_URL/api/stacks" \ - "Authorization: Bearer $AUTH_TOKEN") -echo_debug "Get stacks response -> $STACKS" -check_for_errors $? "$STACKS" - -# Get desired stack from stacks list by it's name -STACK=$(echo "$STACKS" \ - | jq --arg PORTAINER_STACK_NAME "$PORTAINER_STACK_NAME" -jc '.[] | select(.Name == $PORTAINER_STACK_NAME)') -echo_debug "Stack -> $STACK" - -if [ $ACTION == "deploy" ]; then - deploy - exit 0 -fi - -if [ $ACTION == "undeploy" ]; then - undeploy - exit 0 -fi - -err "Error: Unknown action \"$ACTION\"." -exit 1 +main "$@"