Fix Docker access by using Supervisor API - version 1.0.5

This commit is contained in:
Jørgen Ferdinand 2025-03-07 22:37:30 +01:00
parent de355f1ced
commit cdf4287818
2 changed files with 37 additions and 27 deletions

View file

@ -1,34 +1,48 @@
#!/usr/bin/env bash
set -e
echo "Starting Newt container..."
echo "Starting Newt container using Home Assistant Supervisor API..."
# Load config from environment variables
PANGOLIN_ENDPOINT=${PANGOLIN_ENDPOINT:-"https://example.com"}
NEWT_ID=${NEWT_ID:-"default_id"}
NEWT_SECRET=${NEWT_SECRET:-"default_secret"}
# Check if Docker is available
if ! docker info >/dev/null 2>&1; then
echo "Docker is not available inside Home Assistant OS!"
# Define the Supervisor API endpoint
SUPERVISOR_API="http://supervisor/docker"
# Check if we have access to the Home Assistant Supervisor API
if ! curl --silent --fail --header "Authorization: Bearer ${SUPERVISOR_TOKEN}" "${SUPERVISOR_API}/info"; then
echo "Supervisor API is not accessible. Ensure the add-on has access to the Supervisor API."
exit 1
fi
# Stop and remove any existing Newt container
if docker ps -a --format '{{.Names}}' | grep -q "newt"; then
docker stop newt
docker rm newt
fi
echo "Stopping and removing any existing Newt container..."
curl --silent --fail --header "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-X POST "${SUPERVISOR_API}/containers/newt/stop" || true
curl --silent --fail --header "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-X POST "${SUPERVISOR_API}/containers/newt/remove" || true
# Run Newt container
docker run -d --restart unless-stopped \
--name newt \
-e PANGOLIN_ENDPOINT="$PANGOLIN_ENDPOINT" \
-e NEWT_ID="$NEWT_ID" \
-e NEWT_SECRET="$NEWT_SECRET" \
fosrl/newt
# Start a new Newt container using the Supervisor API
echo "Starting new Newt container..."
curl --silent --fail --header "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-X POST "${SUPERVISOR_API}/containers/create" \
-H "Content-Type: application/json" \
-d '{
"Image": "fosrl/newt",
"HostConfig": {
"RestartPolicy": {"Name": "unless-stopped"},
"Env": [
"PANGOLIN_ENDPOINT='"$PANGOLIN_ENDPOINT"'",
"NEWT_ID='"$NEWT_ID"'",
"NEWT_SECRET='"$NEWT_SECRET"'"
]
},
"Name": "newt"
}'
echo "Newt container is running!"
echo "Newt container started successfully!"
# Keep the script running
# Keep the script running to prevent add-on from exiting
exec tail -f /dev/null