psu/psu

378 lines
12 KiB
Plaintext
Raw Normal View History

2018-11-19 19:16:44 +00:00
#!/usr/bin/env bash
2018-11-25 18:29:47 +00:00
#
# Deploy/update/undeploy Docker stacks in a Portainer instance.
2018-11-19 19:16:44 +00:00
2018-11-25 18:54:51 +00:00
###################
# Main entrypoint #
###################
main() {
2018-11-25 18:54:51 +00:00
# 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" ;;
*)
2018-11-26 03:06:03 +00:00
echo_error "Unexpected option ${option}"
2018-11-25 18:54:51 +00:00
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"
echo_debug "STRICT_MODE -> $STRICT_MODE"
2018-11-25 18:54:51 +00:00
# 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
2018-11-26 03:06:03 +00:00
echo_error "Error: Unknown action \"$ACTION\"."
2018-11-25 18:54:51 +00:00
exit 1
}
############################
# Print an error to stderr #
2018-11-25 18:29:47 +00:00
# Globals: #
# None #
# Arguments: #
# $1 Error message #
2018-11-25 18:29:47 +00:00
# Returns: #
# None #
############################
2018-11-26 03:06:03 +00:00
echo_error() {
local error_message=$@
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $error_message" >&2
}
#######################################
# Check a parameter has been provided #
2018-11-25 18:29:47 +00:00
# Globals: #
# None #
# Arguments: #
2018-11-25 18:29:47 +00:00
# $1 Argument value #
# $2 Argument name #
# $3 Argument envvar #
# $4 Argument flag #
# Returns: #
# None #
#######################################
check_argument() {
local argument_value=$1
local argument_name=$2
local argument_envvar=$3
local argument_flag=$4
if [ -z "$argument_value" ]; then
2018-11-26 03:06:03 +00:00
echo_error "Error: Missing argument \"$argument_name\"."
echo_error "Try setting \"$argument_envvar\" environment variable or using the \"-$argument_flag\" flag."
exit 1
fi
}
###########################################
# Checks for error exit codes from httpie #
2018-11-25 18:29:47 +00:00
# Globals: #
# None #
# Arguments: #
# $1 Httpie exit code #
# $2 Response returned by Portainer API #
2018-11-25 18:29:47 +00:00
# Returns: #
# None #
###########################################
check_for_errors() {
local exit_code=$1
local response=$2
if [ $exit_code -ne 0 ]; then
case $exit_code in
2018-11-26 03:06:03 +00:00
2) echo_error 'Request timed out!' ;;
3) echo_error 'Unexpected HTTP 3xx Redirection!' ;;
4) echo_error 'HTTP 4xx Client Error!' && echo_error $response ;;
5) echo_error 'HTTP 5xx Server Error!' && echo_error $response ;;
6) echo_error 'Exceeded --max-redirects=<n> redirects!' ;;
*) echo_error 'Unholy Error!' ;;
esac
exit 1
fi
}
2018-11-24 19:29:10 +00:00
###########################################
# Print message if verbose mode is active #
2018-11-25 18:29:47 +00:00
# Globals: #
# VERBOSE_MODE #
2018-11-24 19:29:10 +00:00
# Arguments: #
# $1 Message #
2018-11-25 18:29:47 +00:00
# Returns: #
# None #
2018-11-24 19:29:10 +00:00
###########################################
echo_verbose() {
2018-11-24 19:29:10 +00:00
local message=$1
if [ $VERBOSE_MODE == "true" ]; then
echo $message
fi
}
2018-11-24 20:09:14 +00:00
#########################################
# Print message if debug mode is active #
2018-11-25 18:29:47 +00:00
# Globals: #
# DEBUG_MODE #
2018-11-24 20:09:14 +00:00
# Arguments: #
# $1 Message #
2018-11-25 18:29:47 +00:00
# Returns: #
# None #
2018-11-24 20:09:14 +00:00
#########################################
echo_debug() {
2018-11-24 20:09:14 +00:00
local message=$1
if [ $DEBUG_MODE == "true" ]; then
echo $message
fi
}
2018-11-25 18:29:47 +00:00
##########################
# Create/update a stack #
# Globals: #
# DOCKER_COMPOSE_FILE #
# PORTAINER_STACK_NAME #
# PORTAINER_URL #
# HTTPIE_VERIFY_SSL #
# PORTAINER_ENDPOINT #
# AUTH_TOKEN #
# Arguments: #
# None #
# Returns: #
# None #
##########################
deploy() {
2018-11-25 16:21:46 +00:00
# Read docker-compose file content
2018-11-26 02:58:57 +00:00
local docker_compose_file_content=$(cat "$DOCKER_COMPOSE_FILE")
2018-11-25 16:21:46 +00:00
# Remove carriage returns
2018-11-26 02:58:57 +00:00
docker_compose_file_content="${docker_compose_file_content//$'\r'/''}"
# Escape double quotes
2018-11-26 02:58:57 +00:00
docker_compose_file_content="${docker_compose_file_content//$'"'/'\"'}"
# Escape newlines
2018-11-26 02:58:57 +00:00
docker_compose_file_content="${docker_compose_file_content//$'\n'/'\n'}"
2018-11-25 16:21:46 +00:00
# If the stack does not exist
if [ -z "$STACK" ]; then
2018-11-25 17:41:02 +00:00
echo_verbose "Stack $PORTAINER_STACK_NAME does not exist."
2018-11-25 16:21:46 +00:00
# Get Docker info
echo_verbose "Getting Docker info..."
2018-11-26 02:58:57 +00:00
local docker_info=$(http \
--check-status \
--ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \
"$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/info" \
"Authorization: Bearer $AUTH_TOKEN")
2018-11-26 02:58:57 +00:00
check_for_errors $? "$docker_info"
echo_debug "Docker info -> $docker_info"
2018-11-25 16:21:46 +00:00
# Get Docker swarm ID
echo_verbose "Getting swarm cluster (if any)..."
2018-11-26 02:58:57 +00:00
local swarm_id=$(echo $docker_info | jq -r ".Swarm.Cluster.ID // empty")
echo_debug "Swarm ID -> $swarm_id"
2018-11-25 16:21:46 +00:00
# If there is no swarm ID
2018-11-26 02:58:57 +00:00
if [ -z "$swarm_id" ];then
2018-11-25 16:21:46 +00:00
echo_verbose "Swarm cluster not found."
echo_verbose "Preparing stack JSON..."
2018-11-26 02:58:57 +00:00
local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"StackFileContent\":\""
local data_suffix="\"}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
2018-11-25 16:21:46 +00:00
# Create stack for single Docker instance
echo_verbose "Creating stack $PORTAINER_STACK_NAME..."
2018-11-26 02:58:57 +00:00
local create=$(http \
--check-status \
--ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \
--timeout=300 \
"$PORTAINER_URL/api/stacks" \
"Authorization: Bearer $AUTH_TOKEN" \
type==2 \
method==string \
endpointId==$PORTAINER_ENDPOINT \
@json.tmp)
2018-11-26 02:58:57 +00:00
echo_debug "Create action response -> $create"
else
2018-11-25 16:21:46 +00:00
echo_verbose "Swarm cluster found."
echo_verbose "Preparing stack JSON..."
2018-11-26 02:58:57 +00:00
local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"SwarmID\":\"$swarm_id\",\"StackFileContent\":\""
local data_suffix="\"}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
2018-11-25 16:21:46 +00:00
# Create stack for Docker swarm
echo_verbose "Creating stack $PORTAINER_STACK_NAME..."
2018-11-26 02:58:57 +00:00
local create=$(http \
--check-status \
--ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \
--timeout=300 \
"$PORTAINER_URL/api/stacks" \
"Authorization: Bearer $AUTH_TOKEN" \
type==1 \
method==string \
endpointId==$PORTAINER_ENDPOINT \
@json.tmp)
2018-11-26 02:58:57 +00:00
echo_debug "Create action response -> $create"
fi
2018-11-26 02:58:57 +00:00
check_for_errors $? "$create"
rm json.tmp
else
2018-11-25 17:41:02 +00:00
if [ $STRICT_MODE == "true" ]; then
2018-11-26 03:06:03 +00:00
echo_error "Error: Stack $PORTAINER_STACK_NAME already exists."
2018-11-25 17:41:02 +00:00
exit 1
fi
echo_verbose "Stack $PORTAINER_STACK_NAME exists."
2018-11-25 16:21:46 +00:00
echo_verbose "Preparing stack JSON..."
2018-11-26 02:58:57 +00:00
local stack_id="$(echo "$STACK" | jq -j ".Id")"
local stack_envvars="$(echo -n "$STACK"| jq ".Env" -jc)"
local data_prefix="{\"Id\":\"$stack_id\",\"StackFileContent\":\""
local data_suffix="\",\"Env\":"$stack_envvars",\"Prune\":$PORTAINER_PRUNE}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
2018-11-25 16:21:46 +00:00
# Update stack
echo_verbose "Updating stack $PORTAINER_STACK_NAME..."
2018-11-26 02:58:57 +00:00
local update=$(http \
--check-status \
--ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \
--timeout=300 \
2018-11-26 02:58:57 +00:00
PUT "$PORTAINER_URL/api/stacks/$stack_id" \
"Authorization: Bearer $AUTH_TOKEN" \
endpointId==$PORTAINER_ENDPOINT \
@json.tmp)
2018-11-26 02:58:57 +00:00
echo_debug "Update action response -> $update"
check_for_errors $? "$update"
rm json.tmp
fi
}
2018-11-25 18:29:47 +00:00
##########################
# Remove a stack #
# Globals: #
# PORTAINER_STACK_NAME #
# PORTAINER_URL #
# HTTPIE_VERIFY_SSL #
# AUTH_TOKEN #
# Arguments: #
# None #
# Returns: #
# None #
##########################
undeploy() {
if [ -z "$STACK" ]; then
2018-11-25 17:41:02 +00:00
if [ $STRICT_MODE == "true" ]; then
2018-11-26 03:06:03 +00:00
echo_error "Error: Stack $PORTAINER_STACK_NAME does not exist."
2018-11-25 17:41:02 +00:00
exit 1
else
echo_verbose "Stack $PORTAINER_STACK_NAME does not exist. No need to undeploy it."
exit 0
fi
fi
2018-11-25 17:41:02 +00:00
echo_verbose "Stack $PORTAINER_STACK_NAME exists."
2018-11-26 02:58:57 +00:00
local stack_id="$(echo "$STACK" | jq -j ".Id")"
echo_debug "Stack ID -> $stack_id"
echo_verbose "Deleting stack $PORTAINER_STACK_NAME..."
2018-11-26 02:58:57 +00:00
local delete=$(http \
--ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \
2018-11-26 02:58:57 +00:00
DELETE "$PORTAINER_URL/api/stacks/$stack_id" \
"Authorization: Bearer $AUTH_TOKEN")
2018-11-26 02:58:57 +00:00
echo_debug "Delete action response -> $delete"
check_for_errors $? "$delete"
}
2018-11-25 18:54:51 +00:00
main "$@"