Skip to content

Instantly share code, notes, and snippets.

@andrew-codechimp
Created March 30, 2020 09:25
Show Gist options
  • Save andrew-codechimp/60ac94d40be81a1181eb868fc9015269 to your computer and use it in GitHub Desktop.
Save andrew-codechimp/60ac94d40be81a1181eb868fc9015269 to your computer and use it in GitHub Desktop.
Portainer API Home Assistant Command Line Switch Example
...
switch:
- platform: command_line
switches:
portainer_transmission:
command_on: "/bin/bash /config/portainer_transmission_start.sh"
command_off: "/bin/bash /config/portainer_transmission_stop.sh"
command_state: "/bin/bash /config/portainer_transmission_state.sh"
friendly_name: "Portainer Transmission"
...
#!/bin/bash
P_USER=${P_USER:-"USERNAME"}
P_PASS=${P_PASS:-"PASSWORD"}
P_URL=${P_URL:-"http://YOURIP:PORT"}
echo "Logging in..."
P_TOKEN=$(curl -s -X POST -H "Content-Type: application/json;charset=UTF-8" -d "{\"username\":\"$P_USER\",\"password\":\"$P_PASS\"}" "$P_URL/api/auth")
if [[ $P_TOKEN = *"jwt"* ]]; then
echo " ... success"
else
echo "Result: failed to login"
exit 1
fi
T=$(echo $P_TOKEN | jq -r '.jwt')
echo "Getting containers..."
P_CONTAINERS=$(curl -s -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $T" "$P_URL/api/endpoints/1/docker/containers/json?all=true")
C=$(echo $P_CONTAINERS | jq -r '.[] | select(.Image | contains("transmission")) | .Id')
if [ -z ${C} ]; then
echo "Failed to get container ID"
exit 1
fi
echo " ... success"
echo "Starting container..."
P_STATUS=$(curl -X POST -s -H "Content-Type:application/json" -H "Authorization: Bearer ${T}" "$P_URL/api/endpoints/1/docker/containers/${C}/start")
exit 0
#!/bin/bash
P_USER=${P_USER:-"USERNAME"}
P_PASS=${P_PASS:-"PASSWORD"}
P_URL=${P_URL:-"http://YOURIP:PORT"}
echo "Logging in..."
P_TOKEN=$(curl -s -X POST -H "Content-Type: application/json;charset=UTF-8" -d "{\"username\":\"$P_USER\",\"password\":\"$P_PASS\"}" "$P_URL/api/auth")
if [[ $P_TOKEN = *"jwt"* ]]; then
echo " ... success"
else
echo "Result: failed to login"
exit 1
fi
T=$(echo $P_TOKEN | jq -r '.jwt')
echo "Getting containers..."
P_CONTAINERS=$(curl -s -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $T" "$P_URL/api/endpoints/1/docker/containers/json?all=true")
# echo "Containers: $P_CONTAINERS"
C=$(echo $P_CONTAINERS | jq -r '.[] | select(.Image | contains("transmission")) | .State')
if [ -z ${C} ]; then
echo "Failed to get container state"
exit 1
fi
echo "Container State: $C"
if [[ $C = "running" ]]; then
exit 0
else
exit 1
fi
#!/bin/bash
P_USER=${P_USER:-"USERNAME"}
P_PASS=${P_PASS:-"PASSWORD"}
P_URL=${P_URL:-"http://YOURIP:PORT"}
echo "Logging in..."
P_TOKEN=$(curl -s -X POST -H "Content-Type: application/json;charset=UTF-8" -d "{\"username\":\"$P_USER\",\"password\":\"$P_PASS\"}" "$P_URL/api/auth")
if [[ $P_TOKEN = *"jwt"* ]]; then
echo " ... success"
else
echo "Result: failed to login"
exit 1
fi
T=$(echo $P_TOKEN | jq -r '.jwt')
echo "Getting containers..."
P_CONTAINERS=$(curl -s -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $T" "$P_URL/api/endpoints/1/docker/containers/json?all=true")
C=$(echo $P_CONTAINERS | jq -r '.[] | select(.Image | contains("transmission")) | .Id')
if [ -z ${C} ]; then
echo "Failed to get container ID"
exit 1
fi
echo " ... success"
echo "Stopping container..."
P_STATUS=$(curl -X POST -s -H "Content-Type:application/json" -H "Authorization: Bearer ${T}" "$P_URL/api/endpoints/1/docker/containers/${C}/stop")
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment