mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Merge deploy and undeploy scripts into psu script
This commit is contained in:
172
deploy → psu
Normal file → Executable file
172
deploy → psu
Normal file → Executable file
@ -42,76 +42,21 @@ check_for_errors () {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set arguments through envvars
|
deploy () {
|
||||||
PORTAINER_USER=${PORTAINER_USER}
|
STACK_YAML_PATH=$DOCKER_COMPOSE_FILE
|
||||||
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"}
|
|
||||||
|
|
||||||
# Set arguments through flags
|
STACK_YAML_CONTENT=$(cat "$STACK_YAML_PATH")
|
||||||
while getopts u:p:l:n:c:e:rs option; do
|
|
||||||
case "${option}" in
|
|
||||||
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:-$PORTAINER_ENDPOINT} ;;
|
|
||||||
r) PORTAINER_PRUNE=${"true":-$PORTAINER_PRUNE} ;;
|
|
||||||
s) HTTPIE_VERIFY_SSL=${"no":-$HTTPIE_VERIFY_SSL} ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# Check required arguments have been provided
|
# Escape carriage returns
|
||||||
check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u"
|
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\r'/''}"
|
||||||
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"
|
|
||||||
check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c"
|
|
||||||
|
|
||||||
STACK_NAME=$PORTAINER_STACK_NAME
|
# Escape double quotes
|
||||||
STACK_YAML_PATH=$DOCKER_COMPOSE_FILE
|
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'"'/'\"'}"
|
||||||
|
|
||||||
STACK_YAML_CONTENT=$(cat "$STACK_YAML_PATH")
|
# Escape newlines
|
||||||
|
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\n'/'\n'}"
|
||||||
|
|
||||||
# Escape carriage returns
|
if [ -z "$STACK" ]; then
|
||||||
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\r'/''}"
|
|
||||||
|
|
||||||
# Escape double quotes
|
|
||||||
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'"'/'\"'}"
|
|
||||||
|
|
||||||
# Escape newlines
|
|
||||||
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\n'/'\n'}"
|
|
||||||
|
|
||||||
echo "Getting auth token..."
|
|
||||||
AUTH_TOKEN=$(http \
|
|
||||||
--check-status \
|
|
||||||
--ignore-stdin \
|
|
||||||
--verify=$HTTPIE_VERIFY_SSL \
|
|
||||||
$PORTAINER_URL/api/auth \
|
|
||||||
username=$PORTAINER_USER \
|
|
||||||
password=$PORTAINER_PASSWORD)
|
|
||||||
check_for_errors $? "$AUTH_TOKEN"
|
|
||||||
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt)
|
|
||||||
echo "Done"
|
|
||||||
|
|
||||||
echo "Getting stack $STACK_NAME..."
|
|
||||||
STACKS=$(http \
|
|
||||||
--check-status \
|
|
||||||
--ignore-stdin \
|
|
||||||
--verify=$HTTPIE_VERIFY_SSL \
|
|
||||||
"$PORTAINER_URL/api/stacks" \
|
|
||||||
"Authorization: Bearer $AUTH_TOKEN")
|
|
||||||
check_for_errors $? "$STACKS"
|
|
||||||
|
|
||||||
STACK=$(echo "$STACKS" \
|
|
||||||
| jq --arg STACK_NAME "$STACK_NAME" -jc '.[] | select(.Name == $STACK_NAME)')
|
|
||||||
|
|
||||||
if [ -z "$STACK" ]; then
|
|
||||||
echo "Result: Stack $STACK_NAME not found."
|
echo "Result: Stack $STACK_NAME not found."
|
||||||
|
|
||||||
echo "Getting swarm cluster (if any)..."
|
echo "Getting swarm cluster (if any)..."
|
||||||
@ -161,7 +106,7 @@ if [ -z "$STACK" ]; then
|
|||||||
check_for_errors $? "$CREATE"
|
check_for_errors $? "$CREATE"
|
||||||
|
|
||||||
rm json.tmp
|
rm json.tmp
|
||||||
else
|
else
|
||||||
echo "Result: Stack $STACK_NAME found."
|
echo "Result: Stack $STACK_NAME found."
|
||||||
|
|
||||||
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
|
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
|
||||||
@ -183,5 +128,98 @@ else
|
|||||||
check_for_errors $? "$UPDATE"
|
check_for_errors $? "$UPDATE"
|
||||||
|
|
||||||
rm json.tmp
|
rm json.tmp
|
||||||
fi
|
fi
|
||||||
|
echo "Done"
|
||||||
|
}
|
||||||
|
|
||||||
|
undeploy () {
|
||||||
|
if [ -z "$STACK" ]; then
|
||||||
|
echo "Result: Stack $STACK_NAME not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Result: Stack $STACK_NAME found."
|
||||||
|
|
||||||
|
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
|
||||||
|
|
||||||
|
echo "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"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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"}
|
||||||
|
|
||||||
|
# Set arguments through flags
|
||||||
|
while getopts a:u:p:l:n:c:e:rs 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:-$PORTAINER_ENDPOINT} ;;
|
||||||
|
r) PORTAINER_PRUNE=${"true":-$PORTAINER_PRUNE} ;;
|
||||||
|
s) HTTPIE_VERIFY_SSL=${"no":-$HTTPIE_VERIFY_SSL} ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
STACK_NAME=$PORTAINER_STACK_NAME
|
||||||
|
|
||||||
|
echo "Getting auth token..."
|
||||||
|
AUTH_TOKEN=$(http \
|
||||||
|
--check-status \
|
||||||
|
--ignore-stdin \
|
||||||
|
--verify=$HTTPIE_VERIFY_SSL \
|
||||||
|
$PORTAINER_URL/api/auth \
|
||||||
|
username=$PORTAINER_USER \
|
||||||
|
password=$PORTAINER_PASSWORD)
|
||||||
|
check_for_errors $? "$AUTH_TOKEN"
|
||||||
|
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt)
|
||||||
echo "Done"
|
echo "Done"
|
||||||
|
|
||||||
|
echo "Getting stack $STACK_NAME..."
|
||||||
|
STACKS=$(http \
|
||||||
|
--check-status \
|
||||||
|
--ignore-stdin \
|
||||||
|
--verify=$HTTPIE_VERIFY_SSL \
|
||||||
|
"$PORTAINER_URL/api/stacks" \
|
||||||
|
"Authorization: Bearer $AUTH_TOKEN")
|
||||||
|
check_for_errors $? "$STACKS"
|
||||||
|
|
||||||
|
STACK=$(echo "$STACKS" \
|
||||||
|
| jq --arg STACK_NAME "$STACK_NAME" -jc '.[] | select(.Name == $STACK_NAME)')
|
||||||
|
|
||||||
|
if [ $ACTION == "deploy" ]; then
|
||||||
|
check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c"
|
||||||
|
deploy
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $ACTION == "undeploy" ]; then
|
||||||
|
undeploy
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Error: Unknown action \"$ACTION\"."
|
||||||
|
exit 1
|
85
undeploy
85
undeploy
@ -1,85 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# Checks for error exit codes from httpie #
|
|
||||||
# Arguments: #
|
|
||||||
# $1 Httpie exit code #
|
|
||||||
# $2 Response returned by Portainer API #
|
|
||||||
###########################################
|
|
||||||
check_for_errors () {
|
|
||||||
local exit_code=$1
|
|
||||||
local response=$2
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
case $exit_code in
|
|
||||||
2) echo 'Request timed out!' ;;
|
|
||||||
3) echo 'Unexpected HTTP 3xx Redirection!' ;;
|
|
||||||
4) echo 'HTTP 4xx Client Error!' && echo $response ;;
|
|
||||||
5) echo 'HTTP 5xx Server Error!' && echo $response ;;
|
|
||||||
6) echo 'Exceeded --max-redirects=<n> redirects!' ;;
|
|
||||||
*) echo 'Unholy Error!' ;;
|
|
||||||
esac
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
PORTAINER_USER=${PORTAINER_USER:-"user"}
|
|
||||||
PORTAINER_PASSWORD=${PORTAINER_PASSWORD:-"password"}
|
|
||||||
PORTAINER_URL=${PORTAINER_URL:-"https://portainer.example.com"}
|
|
||||||
PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"}
|
|
||||||
PORTAINER_ENDPOINT=${PORTAINER_ENDPOINT:-"1"}
|
|
||||||
HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"}
|
|
||||||
|
|
||||||
if [ -z ${1+x} ]; then
|
|
||||||
echo "Error: Parameter #1 missing (stack name)"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
STACK_NAME="$1"
|
|
||||||
|
|
||||||
echo "Getting auth token..."
|
|
||||||
AUTH_TOKEN=$(http \
|
|
||||||
--ignore-stdin \
|
|
||||||
--verify=$HTTPIE_VERIFY_SSL \
|
|
||||||
$PORTAINER_URL/api/auth \
|
|
||||||
username=$PORTAINER_USER \
|
|
||||||
password=$PORTAINER_PASSWORD \
|
|
||||||
| jq -r .jwt)
|
|
||||||
|
|
||||||
AUTH_TOKEN=$(http \
|
|
||||||
--check-status \
|
|
||||||
--ignore-stdin \
|
|
||||||
--verify=$HTTPIE_VERIFY_SSL \
|
|
||||||
$PORTAINER_URL/api/auth \
|
|
||||||
username=$PORTAINER_USER \
|
|
||||||
password=$PORTAINER_PASSWORD)
|
|
||||||
check_for_errors $? "$AUTH_TOKEN"
|
|
||||||
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt)
|
|
||||||
echo "Done"
|
|
||||||
|
|
||||||
echo "Getting stack $STACK_NAME..."
|
|
||||||
STACKS=$(http \
|
|
||||||
--check-status \
|
|
||||||
--ignore-stdin \
|
|
||||||
--verify=$HTTPIE_VERIFY_SSL \
|
|
||||||
"$PORTAINER_URL/api/stacks" \
|
|
||||||
"Authorization: Bearer $AUTH_TOKEN")
|
|
||||||
check_for_errors $? "$STACKS"
|
|
||||||
|
|
||||||
STACK=$(echo "$STACKS" \
|
|
||||||
| jq --arg STACK_NAME "$STACK_NAME" -jc '.[] | select(.Name == $STACK_NAME)')
|
|
||||||
|
|
||||||
if [ -z "$STACK" ]; then
|
|
||||||
echo "Result: Stack $STACK_NAME not found."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "Result: Stack $STACK_NAME found."
|
|
||||||
|
|
||||||
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
|
|
||||||
|
|
||||||
echo "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"
|
|
Reference in New Issue
Block a user