From cdf4287818ff5dbea79afc7bf2d67634bcd7ce20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Ferdinand?= Date: Fri, 7 Mar 2025 22:37:30 +0100 Subject: [PATCH] Fix Docker access by using Supervisor API - version 1.0.5 --- newt/config.yaml | 16 ++++++---------- newt/run.sh | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/newt/config.yaml b/newt/config.yaml index ff16063..33c7446 100644 --- a/newt/config.yaml +++ b/newt/config.yaml @@ -1,7 +1,7 @@ name: "Newt Add-on" -version: "1.0.4" +version: "1.0.5" slug: "newt" -description: "Runs the Newt container inside Home Assistant" +description: "Runs the Newt container inside Home Assistant using Supervisor API" arch: - amd64 - armv7 @@ -10,10 +10,7 @@ arch: startup: system boot: auto host_network: true -privileged: - - SYS_ADMIN - - NET_ADMIN - - DAC_READ_SEARCH +privileged: true options: PANGOLIN_ENDPOINT: "https://example.com" NEWT_ID: "your_newt_id" @@ -22,7 +19,6 @@ schema: PANGOLIN_ENDPOINT: "str?" NEWT_ID: "str?" NEWT_SECRET: "str?" -environment: - PANGOLIN_ENDPOINT: "$PANGOLIN_ENDPOINT" - NEWT_ID: "$NEWT_ID" - NEWT_SECRET: "$NEWT_SECRET" \ No newline at end of file +homeassistant: true +hassio_api: true +hassio_role: "admin" \ No newline at end of file diff --git a/newt/run.sh b/newt/run.sh index 4423f98..766d26c 100644 --- a/newt/run.sh +++ b/newt/run.sh @@ -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 \ No newline at end of file