From f11d5824f3aba7809cd673e28312634cb75206df Mon Sep 17 00:00:00 2001 From: rsarwar Date: Mon, 3 Jun 2024 23:38:55 +0100 Subject: [PATCH 1/3] tidy up docker deployment --- predbat/Dockerfile.standalone | 12 +++-- predbat/compose-standalone.yaml | 80 +++++++++++++++++++++++++++++++++ predbat/rootfs/hass.py | 12 ++--- predbat/rootfs/startup.py | 4 +- 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 predbat/compose-standalone.yaml diff --git a/predbat/Dockerfile.standalone b/predbat/Dockerfile.standalone index bf7108b..2e3007f 100644 --- a/predbat/Dockerfile.standalone +++ b/predbat/Dockerfile.standalone @@ -13,12 +13,16 @@ RUN apt-get install -y python3-venv RUN apt-get install -y python3-requests RUN apt-get install -y python3-yaml #RUN apt-get install -y python3-asyncio -RUN apt-get install -y python3-aiohttp RUN apt-get install csh -RUN apt-get install -y python3-tz +RUN apt-get install -y python3-aiohttp +RUN apt-get install -y python3-tz +RUN apt-get install -y vim wget zip -WORKDIR /config -COPY rootfs /config +WORKDIR /wkspace +COPY rootfs /wkspace +RUN wget https://codeload.github.com/springfall2008/batpred/zip/refs/heads/main -O main.zip +RUN unzip main.zip +RUN cp batpred-main/apps/predbat/predbat.py predbat.py #COPY requirements.txt /tmp/ #RUN pip3 install -r /tmp/requirements.txt diff --git a/predbat/compose-standalone.yaml b/predbat/compose-standalone.yaml new file mode 100644 index 0000000..fa040e7 --- /dev/null +++ b/predbat/compose-standalone.yaml @@ -0,0 +1,80 @@ +services: + eager_sammet: + + cap_drop: + - "AUDIT_CONTROL" + - "BLOCK_SUSPEND" + - "DAC_READ_SEARCH" + - "IPC_LOCK" + - "IPC_OWNER" + - "LEASE" + - "LINUX_IMMUTABLE" + - "MAC_ADMIN" + - "MAC_OVERRIDE" + - "NET_ADMIN" + - "NET_BROADCAST" + - "SYSLOG" + - "SYS_ADMIN" + - "SYS_BOOT" + - "SYS_MODULE" + - "SYS_NICE" + - "SYS_PACCT" + - "SYS_PTRACE" + - "SYS_RAWIO" + - "SYS_RESOURCE" + - "SYS_TIME" + - "SYS_TTY_CONFIG" + - "WAKE_ALARM" + + command: + - "csh" + - "-f" + - "run.csh" + + container_name: "eager_sammet" + + environment: + - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + + hostname: "948b22e2f2d0" + + image: "docker.io/library/predbat:latest" + + ipc: "private" + + labels: + io.hass.arch: "" + io.hass.description: "" + io.hass.name: "" + io.hass.type: "addon" + io.hass.version: "" + maintainer: "springfall2008 (https://github.com/springfall2008)" + org.opencontainers.image.authors: "springfall2008 (https://github.com/springfall2008)" + org.opencontainers.image.created: "" + org.opencontainers.image.description: "" + org.opencontainers.image.documentation: "https://github.com//blob/main/README.md" + org.opencontainers.image.licenses: "MIT" + org.opencontainers.image.ref.name: "ubuntu" + org.opencontainers.image.revision: "" + org.opencontainers.image.source: "https://github.com/" + org.opencontainers.image.title: "" + org.opencontainers.image.url: "https://github.com/springfall2008" + org.opencontainers.image.vendor: "Springfall2008" + org.opencontainers.image.version: "" + + logging: + driver: "json-file" + options: {} + + network_mode: "host" + + ports: + - "5052:5052/tcp" + + volumes: + - "/etc/localtime:/etc/localtime:ro" + - "/wkspace/homeassistant/predbat:/config" + + working_dir: "/wkspace" + +version: "3.6" diff --git a/predbat/rootfs/hass.py b/predbat/rootfs/hass.py index 80b571b..33cf84f 100644 --- a/predbat/rootfs/hass.py +++ b/predbat/rootfs/hass.py @@ -43,10 +43,10 @@ async def main(): return # Find all .py files in the directory hierarchy - py_files = [] + py_files = ['/config/apps.yaml'] for root, dirs, files in os.walk("."): for file in files: - if (file.endswith(".py") or file == "apps.yaml") and not file.startswith("."): + if (file.endswith(".py")) and not file.startswith("."): py_files.append(os.path.join(root, file)) print("Watching {} for changes".format(py_files)) @@ -80,8 +80,8 @@ def log(self, msg, quiet=True): log_size = self.logfile.tell() if log_size > 10000000: self.logfile.close() - os.rename("predbat.log", "predbat.log.1") - self.logfile = open("predbat.log", "w") + os.rename("/config/predbat.log", "/config/predbat.log.1") + self.logfile = open("/config/predbat.log", "w") async def run_in_executor(self, callback, *args): """ @@ -132,11 +132,11 @@ def __init__(self): self.run_list = [] self.threads = [] - self.logfile = open("predbat.log", "a") + self.logfile = open("/config/predbat.log", "a") # Open YAML file apps.yaml and read it self.log("Loading apps.yaml", quiet=False) - with io.open("apps.yaml", "r") as stream: + with io.open("/config/apps.yaml", "r") as stream: try: config = yaml.safe_load(stream) self.args = config["pred_bat"] diff --git a/predbat/rootfs/startup.py b/predbat/rootfs/startup.py index e583a81..6a4c5ea 100644 --- a/predbat/rootfs/startup.py +++ b/predbat/rootfs/startup.py @@ -6,14 +6,14 @@ import time print("Bootstrap Predbat") -root = "/config" +root = "/wkspace" # Check if config exists, if not run locally if not os.path.exists(root): root = "./" # Download the latest Predbat release from Github -if not os.path.exists(root + "/apps.yaml"): +if not os.path.exists("/config/apps.yaml"): url = "https://api.github.com/repos/springfall2008/batpred/releases" print("Download Predbat release list from {}".format(url)) try: From b636eb736e3a6c177fc869f504c0ef46a569869e Mon Sep 17 00:00:00 2001 From: rsarwar Date: Mon, 3 Jun 2024 23:47:21 +0100 Subject: [PATCH 2/3] update docs --- predbat/DOCS.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/predbat/DOCS.md b/predbat/DOCS.md index 8accbfb..4065646 100644 --- a/predbat/DOCS.md +++ b/predbat/DOCS.md @@ -46,17 +46,22 @@ Predbat can be run on a seperate machine also, you will need a MacOS with Python * Download the contents of 'https://github.com/springfall2008/predbat_addon/tree/main/predbat' onto your machine * You should see Dockerfile.standalone and rootfs directories -* Download predbat.py from Predbat repo (https://github.com/springfall2008/batpred/blob/main/apps/predbat/predbat.py) and place it into rootfs -* Download apps.yaml from Predbat repo (https://github.com/springfall2008/batpred/blob/main/apps/predbat/config/apps.yaml), place it into rootfs and edit it as per the Predbat documentation. +* Download apps.yaml from Predbat repo (https://github.com/springfall2008/batpred/blob/main/apps/predbat/config/apps.yaml), place it into config directory somewhere in the host machine and edit it as per the Predbat documentation. this config directory will be mounted on the docker container. * Add ha_url / ha_key settings into apps.yaml. * The ha_url must be your Home Assistant machine e.g. http://homeassistant.local:8123 * The ha_key must be the persistant key you can generate in Home Assistant in your user/security section -* Build and run your docker as follows: +* Build your docker as follows: ``` docker build . -t predbat -f ./Dockerfile.standalone -docker run docker.io/library/predbat +``` +* Modify the docker compose file compose-standalone.yaml to add the config directory by replacing the string "/wkspace/homeassistant/predbat" + +* Run container: + +``` +docker-compose -f compose-standalone.yaml up ``` Look into the docker under /config/predbat.log for the logfiles From 59669c869fbd4fa2e081211e46b73ca58b317a01 Mon Sep 17 00:00:00 2001 From: rsarwar Date: Sun, 8 Sep 2024 18:32:51 +0100 Subject: [PATCH 3/3] update for split design --- predbat/Dockerfile.standalone | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/predbat/Dockerfile.standalone b/predbat/Dockerfile.standalone index 2e3007f..cb57a50 100644 --- a/predbat/Dockerfile.standalone +++ b/predbat/Dockerfile.standalone @@ -22,7 +22,7 @@ WORKDIR /wkspace COPY rootfs /wkspace RUN wget https://codeload.github.com/springfall2008/batpred/zip/refs/heads/main -O main.zip RUN unzip main.zip -RUN cp batpred-main/apps/predbat/predbat.py predbat.py +RUN cp batpred-main/apps/predbat/*py . #COPY requirements.txt /tmp/ #RUN pip3 install -r /tmp/requirements.txt