Add debug mode

This commit is contained in:
Juan Carlos Mejías Rodríguez 2018-11-24 15:09:14 -05:00
parent de4150afa6
commit e873d4a9d8
2 changed files with 51 additions and 4 deletions

View File

@ -34,6 +34,7 @@ This is particularly useful for CI/CD pipelines.
- `PORTAINER_ENDPOINT` (int, optional): Which endpoint to use. Defaults to `1`. - `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"`. - `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"`. - `VERBOSE_MODE` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`.
- `DEBUG_MODE` ("true" or "false", optional): Whether to activate debug output mode or not. Defaults to `"false"`. See [debug mode warning](#debug-mode) below.
#### Examples #### Examples
@ -72,6 +73,7 @@ This is more suitable for standalone script usage.
- `-e` (int, optional): Which endpoint to use. Defaults to `1`. - `-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"`. - `-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"`. - `-v` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`.
- `-d` ("true" or "false", optional): Whether to activate debug output mode or not. Defaults to `"false"`. See [debug mode warning](#debug-mode) below.
#### Examples #### Examples
@ -83,6 +85,10 @@ This is more suitable for standalone script usage.
./psu -a undeploy -u admin -p password -l http://portainer.local -n mystack ./psu -a undeploy -u admin -p password -l http://portainer.local -n mystack
``` ```
### Debug mode
**WARNING**: In debug mode the script prints as much information as possible, including configuration values (with portainer credentials) and Portainer API responses (with sensitive information like authentication token and stacks environment variables). Avoid using debug mode in CI/CD pipelines, as pipeline logs are usually recorded.
## License ## License
Source code contained by this project is licensed under the [GNU General Public License version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). See [LICENSE](LICENSE) file for reference. Source code contained by this project is licensed under the [GNU General Public License version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). See [LICENSE](LICENSE) file for reference.

49
psu
View File

@ -54,6 +54,18 @@ echo_verbose () {
fi fi
} }
#########################################
# Print message if debug mode is active #
# Arguments: #
# $1 Message #
#########################################
echo_debug () {
local message=$1
if [ $DEBUG_MODE == "true" ]; then
echo $message
fi
}
deploy () { deploy () {
STACK_YAML_PATH=$DOCKER_COMPOSE_FILE STACK_YAML_PATH=$DOCKER_COMPOSE_FILE
@ -72,20 +84,23 @@ deploy () {
echo_verbose "Result: Stack $STACK_NAME not found." echo_verbose "Result: Stack $STACK_NAME not found."
echo_verbose "Getting swarm cluster (if any)..." echo_verbose "Getting swarm cluster (if any)..."
SWARM_ID=$(http \ DOCKER_INFO=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
"$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/info" \ "$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/info" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
check_for_errors $? "$SWARM_ID" check_for_errors $? "$DOCKER_INFO"
SWARM_ID=$(echo $SWARM_ID | jq -r ".Swarm.Cluster.ID // empty") echo_debug "Docker info -> $DOCKER_INFO"
SWARM_ID=$(echo $DOCKER_INFO | jq -r ".Swarm.Cluster.ID // empty")
echo_debug "Swarm ID -> $SWARM_ID"
echo_verbose "Creating stack $STACK_NAME..." echo_verbose "Creating stack $STACK_NAME..."
if [ -z "$SWARM_ID" ];then if [ -z "$SWARM_ID" ];then
DATA_PREFIX="{\"Name\":\"$STACK_NAME\",\"StackFileContent\":\"" DATA_PREFIX="{\"Name\":\"$STACK_NAME\",\"StackFileContent\":\""
DATA_SUFFIX="\"}" DATA_SUFFIX="\"}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX"
CREATE=$(http \ CREATE=$(http \
--check-status \ --check-status \
@ -98,10 +113,12 @@ deploy () {
method==string \ method==string \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Create action response -> $CREATE"
else else
DATA_PREFIX="{\"Name\":\"$STACK_NAME\",\"SwarmID\":\"$SWARM_ID\",\"StackFileContent\":\"" DATA_PREFIX="{\"Name\":\"$STACK_NAME\",\"SwarmID\":\"$SWARM_ID\",\"StackFileContent\":\""
DATA_SUFFIX="\"}" DATA_SUFFIX="\"}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX"
CREATE=$(http \ CREATE=$(http \
--check-status \ --check-status \
@ -114,6 +131,7 @@ deploy () {
method==string \ method==string \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Create action response -> $CREATE"
fi fi
check_for_errors $? "$CREATE" check_for_errors $? "$CREATE"
@ -126,6 +144,7 @@ deploy () {
DATA_PREFIX="{\"Id\":\"$STACK_ID\",\"StackFileContent\":\"" DATA_PREFIX="{\"Id\":\"$STACK_ID\",\"StackFileContent\":\""
DATA_SUFFIX="\",\"Env\":"$STACK_ENV_VARS",\"Prune\":$PORTAINER_PRUNE}" DATA_SUFFIX="\",\"Env\":"$STACK_ENV_VARS",\"Prune\":$PORTAINER_PRUNE}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX"
echo_verbose "Updating stack $STACK_NAME..." echo_verbose "Updating stack $STACK_NAME..."
UPDATE=$(http \ UPDATE=$(http \
@ -137,6 +156,7 @@ deploy () {
"Authorization: Bearer $AUTH_TOKEN" \ "Authorization: Bearer $AUTH_TOKEN" \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Update action response -> $UPDATE"
check_for_errors $? "$UPDATE" check_for_errors $? "$UPDATE"
rm json.tmp rm json.tmp
@ -152,6 +172,7 @@ undeploy () {
echo_verbose "Result: Stack $STACK_NAME found." echo_verbose "Result: Stack $STACK_NAME found."
STACK_ID="$(echo "$STACK" | jq -j ".Id")" STACK_ID="$(echo "$STACK" | jq -j ".Id")"
echo_debug "Stack ID -> $STACK_ID"
echo_verbose "Deleting stack $STACK_NAME..." echo_verbose "Deleting stack $STACK_NAME..."
DELETE=$(http \ DELETE=$(http \
@ -159,6 +180,7 @@ undeploy () {
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
DELETE "$PORTAINER_URL/api/stacks/$STACK_ID" \ DELETE "$PORTAINER_URL/api/stacks/$STACK_ID" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
echo_debug "Delete action response -> $UPDATE"
check_for_errors $? "$DELETE" check_for_errors $? "$DELETE"
echo_verbose "Done" echo_verbose "Done"
} }
@ -174,9 +196,10 @@ PORTAINER_ENDPOINT=${PORTAINER_ENDPOINT:-"1"}
PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"} PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"}
HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"} HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"}
VERBOSE_MODE=${VERBOSE_MODE:-"false"} VERBOSE_MODE=${VERBOSE_MODE:-"false"}
DEBUG_MODE=${DEBUG_MODE:-"false"}
# Set arguments through flags # Set arguments through flags
while getopts a:u:p:l:n:c:e:rsv option; do while getopts a:u:p:l:n:c:e:rsvd option; do
case "${option}" in case "${option}" in
a) ACTION=${OPTARG} ;; a) ACTION=${OPTARG} ;;
u) PORTAINER_USER=${OPTARG} ;; u) PORTAINER_USER=${OPTARG} ;;
@ -188,9 +211,23 @@ while getopts a:u:p:l:n:c:e:rsv option; do
r) PORTAINER_PRUNE="true" ;; r) PORTAINER_PRUNE="true" ;;
s) HTTPIE_VERIFY_SSL="no" ;; s) HTTPIE_VERIFY_SSL="no" ;;
v) VERBOSE_MODE="true" ;; v) VERBOSE_MODE="true" ;;
d) DEBUG_MODE="true" ;;
esac esac
done done
# Print config
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 required arguments have been provided
check_argument "$ACTION" "action" "ACTION" "a" check_argument "$ACTION" "action" "ACTION" "a"
check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u" check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u"
@ -208,8 +245,10 @@ AUTH_TOKEN=$(http \
$PORTAINER_URL/api/auth \ $PORTAINER_URL/api/auth \
username=$PORTAINER_USER \ username=$PORTAINER_USER \
password=$PORTAINER_PASSWORD) password=$PORTAINER_PASSWORD)
echo_debug "Get auth token response -> $AUTH_TOKEN"
check_for_errors $? "$AUTH_TOKEN" check_for_errors $? "$AUTH_TOKEN"
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt) AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt)
echo_debug "Auth token -> $AUTH_TOKEN"
echo_verbose "Done" echo_verbose "Done"
echo_verbose "Getting stack $STACK_NAME..." echo_verbose "Getting stack $STACK_NAME..."
@ -219,10 +258,12 @@ STACKS=$(http \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
"$PORTAINER_URL/api/stacks" \ "$PORTAINER_URL/api/stacks" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
echo_debug "Get stacks response -> $STACKS"
check_for_errors $? "$STACKS" check_for_errors $? "$STACKS"
STACK=$(echo "$STACKS" \ STACK=$(echo "$STACKS" \
| jq --arg STACK_NAME "$STACK_NAME" -jc '.[] | select(.Name == $STACK_NAME)') | jq --arg STACK_NAME "$STACK_NAME" -jc '.[] | select(.Name == $STACK_NAME)')
echo_debug "Stack -> $STACK"
if [ $ACTION == "deploy" ]; then if [ $ACTION == "deploy" ]; then
check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c"