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,7 +1,7 @@
name: "Newt Add-on" name: "Newt Add-on"
version: "1.0.4" version: "1.0.5"
slug: "newt" slug: "newt"
description: "Runs the Newt container inside Home Assistant" description: "Runs the Newt container inside Home Assistant using Supervisor API"
arch: arch:
- amd64 - amd64
- armv7 - armv7
@ -10,10 +10,7 @@ arch:
startup: system startup: system
boot: auto boot: auto
host_network: true host_network: true
privileged: privileged: true
- SYS_ADMIN
- NET_ADMIN
- DAC_READ_SEARCH
options: options:
PANGOLIN_ENDPOINT: "https://example.com" PANGOLIN_ENDPOINT: "https://example.com"
NEWT_ID: "your_newt_id" NEWT_ID: "your_newt_id"
@ -22,7 +19,6 @@ schema:
PANGOLIN_ENDPOINT: "str?" PANGOLIN_ENDPOINT: "str?"
NEWT_ID: "str?" NEWT_ID: "str?"
NEWT_SECRET: "str?" NEWT_SECRET: "str?"
environment: homeassistant: true
PANGOLIN_ENDPOINT: "$PANGOLIN_ENDPOINT" hassio_api: true
NEWT_ID: "$NEWT_ID" hassio_role: "admin"
NEWT_SECRET: "$NEWT_SECRET"

View file

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