Skip to content

Commit 8a680ae

Browse files
authored
Take changes from springfall2008#1
1 parent 820b65d commit 8a680ae

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

Dockerfile

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
#install python en pip
2-
FROM ubuntu:noble
1+
FROM python:3-slim-bookworm
32

4-
# Set shell
5-
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
3+
WORKDIR /
4+
COPY predbat/rootfs /
5+
COPY predbat/requirements.txt /
66

7-
RUN apt-get update
8-
RUN apt-get upgrade -y
9-
RUN apt-get dist-upgrade -y
10-
RUN apt-get install -y python3
11-
RUN apt-get install -y python3-pip
12-
RUN apt-get install -y python3-venv
13-
RUN apt-get install -y python3-requests
14-
RUN apt-get install -y python3-yaml
15-
#RUN apt-get install -y python3-asyncio
16-
RUN apt-get install -y python3-aiohttp
17-
RUN apt-get install csh
18-
RUN apt-get install -y python3-tz
19-
20-
WORKDIR /config
21-
COPY predbat/rootfs /config
22-
23-
#COPY requirements.txt /tmp/
24-
#RUN pip3 install -r /tmp/requirements.txt
7+
# Install dependencies
8+
RUN pip3 --no-cache-dir install -r /requirements.txt
259

2610
# Start app
27-
RUN chmod a+x run.csh
11+
RUN chmod a+x run.sh
2812

2913
ARG BUILD_ARCH
3014
ARG BUILD_DATE
@@ -52,4 +36,4 @@ LABEL \
5236
org.opencontainers.image.revision=${BUILD_REF} \
5337
org.opencontainers.image.version=${BUILD_VERSION}
5438

55-
CMD [ "csh", "-f", "run.csh"]
39+
ENTRYPOINT /run.sh

predbat/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pytz
22
requests
33
pyyaml
44
asyncio
5-
aiohttp==3.8.6
5+
aiohttp>=3.8.6

predbat/rootfs/run.sh

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
echo "Running Predbat inside Add-on"
2-
echo "Your API key is: $SUPERVISOR_TOKEN"
1+
#!/bin/bash
2+
3+
ROOT_DIR="/config"
4+
[[ -d ${ROOT_DIR} ]] || ROOT_DIR="./"
5+
6+
if [[ -n ${SUPERVISOR_TOKEN} ]]; then
7+
echo "Running Predbat inside Add-on"
8+
echo "Your API key is: ${SUPERVISOR_TOKEN}"
9+
fi
10+
311
if [ -f /config/dev ]; then
412
echo "Dev mode enabled, not copying hass.py"
513
else
6-
cp /hass.py /config/hass.py
14+
[[ ! -f ${ROOT_DIR}/hass.py ]] && cp /hass.py ${ROOT_DIR}/hass.py
715
fi
8-
python3 /startup.py $SUPERVISOR_TOKEN
16+
17+
## Bootstrap predbat
18+
python3 /bootstrap.py
19+
20+
## Run
21+
if [[ $? -eq 0 ]]; then
22+
echo "Starting Predbat..."
23+
## Loop if running in HA otherwise let container properties deal with restart
24+
if [[ -n ${SUPERVISOR_TOKEN} ]]; then
25+
while true; do
26+
cd ${ROOT_DIR} && python3 hass.py
27+
echo "Predbat crashed. Restarting in 5 seconds..."
28+
sleep 5
29+
done
30+
else
31+
cd ${ROOT_DIR} && python3 hass.py
32+
fi
33+
else
34+
echo "Bootstrap failed"
35+
fi

predbat/rootfs/startup.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
root = "./"
1414

1515
# Download the latest Predbat release from Github
16-
if not os.path.exists(root + "/apps.yaml"):
16+
if not os.path.exists(root + "/predbat.py"):
1717
url = "https://api.github.com/repos/springfall2008/batpred/releases"
1818
print("Download Predbat release list from {}".format(url))
1919
try:
@@ -60,17 +60,14 @@
6060
shutil.unpack_archive(save_path, unzip_path)
6161
unzip_path = unzip_path + "/batpred-" + tag_name.replace("v", "")
6262
os.system("cp {}/apps/predbat/* {}".format(unzip_path, root))
63-
os.system("cp {}/apps/predbat/config/* {}".format(unzip_path, root))
64-
os.system("rm -rf {}".format(unzip_path))
63+
if not os.path.exists(root + "/apps.yaml"):
64+
print("No apps.yaml found - extracting default.")
65+
os.system("cp {}/apps/predbat/config/* {}".format(unzip_path, root))
6566
else:
6667
print("Error: Unable to find a valid Predbat release")
6768
print("Sleep 5 minutes before restarting")
6869
time.sleep(5*60)
6970
sys.exit(1)
7071

7172

72-
print("Startup")
73-
os.system("cd " + root + "; python3 hass.py")
74-
75-
print("Shutdown, sleeping 30 seconds before restarting")
76-
time.sleep(30)
73+
print("Bootstrapped!")

0 commit comments

Comments
 (0)