Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active September 15, 2019 09:26
Show Gist options
  • Select an option

  • Save x-yuri/3ab4b90bfd14c923ae74c0883a3547ce to your computer and use it in GitHub Desktop.

Select an option

Save x-yuri/3ab4b90bfd14c923ae74c0883a3547ce to your computer and use it in GitHub Desktop.
portainer: redeploy stack
#!/usr/bin/env bash
set -eu
portainer=https://example.com
user=name
password=...
endpoint=name
stack=name
auth_resp=$(http POST "$portainer/api/auth" Username="$user" Password="$password")
jwt=$(echo "$auth_resp" | jq -r '.jwt')
endpoint_id=$(http GET "$portainer/api/endpoints" "Authorization: Bearer $jwt" \
| jq -r --arg endpoint "$endpoint" 'map(select(.Name == $endpoint))[0].Id')
if [[ $endpoint_id == null ]]; then
echo "$0: can't access endpoint" >&2
exit 1
fi
swarm_id=$(http GET "$portainer/api/endpoints/$endpoint_id/docker/swarm" \
"Authorization: Bearer $jwt" \
| jq -r '.ID')
stack_dict=$(http GET "$portainer/api/stacks?filters="'{"SwarmID":"'"$swarm_id"'"}' \
"Authorization: Bearer $jwt" \
| jq --arg stack "$stack" 'map(select(.Name == $stack))[0]')
stack_id=$(echo "$stack_dict" | jq -r '.Id')
if [[ $stack_id == null ]]; then
echo "$0: can't access stack" >&2
exit 1
fi
environment=$(echo "$stack_dict" | jq '.Env')
file_resp=$(http GET "$portainer/api/stacks/$stack_id/file" "Authorization: Bearer $jwt")
compose_file=$(echo "$file_resp" | jq -r '.StackFileContent')
http --print=HBhb PUT "$portainer/api/stacks/$stack_id?endpointId=$endpoint_id" "Authorization: Bearer $jwt" "Env:=$environment" "Prune:=true" "StackFileContent=$compose_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment