mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add main function
This commit is contained in:
parent
2be6d03efd
commit
7715832325
209
psu
209
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 "$@"
|
||||
|
Loading…
Reference in New Issue
Block a user