Skip to content

Commit

Permalink
Merge pull request #216 from GeoDerp/removed-requirments
Browse files Browse the repository at this point in the history
Docker, requirements and amhf support
  • Loading branch information
davidusb-geek authored Mar 10, 2024
2 parents 4c7cbe3 + 1a332c3 commit 46d6849
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/python:0-3.11
# EXPOSE 5000:5000
COPY .devcontainer/setup.sh requirements.txt requirements_webserver.txt ./
COPY .devcontainer/setup.sh requirements.txt ./
RUN ./setup.sh
2 changes: 1 addition & 1 deletion .devcontainer/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ main() {
export HDF5_DIR=/usr/include/hdf5
pip install netCDF4

pip install -r requirements_webserver.txt
pip install -r requirements.txt
pip install requests-mock

rm -rf "$0"
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/docker-build-test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#This action test builds EMHASS standalone Docker images, in each architecture.
name: "Test Docker Stanadlone"
name: "Test Docker Standalone"

on:
push:
Expand All @@ -15,10 +15,10 @@ jobs:
fail-fast: false
matrix:
platform: [
{buildx: linux/amd64, target_arch: amd64},
{buildx: linux/arm/v7, target_arch: armv7},
{buildx: linux/arm/v7, target_arch: armhf},
{buildx: linux/arm64, target_arch: aarch64}
{buildx: linux/amd64, target_arch: amd64, os_version: debian},
{buildx: linux/arm/v7, target_arch: armv7, os_version: debian},
{buildx: linux/arm/v7, target_arch: armhf, os_version: raspbian},
{buildx: linux/arm64, target_arch: aarch64, os_version: debian}
]
steps:
- name: Checkout the repository
Expand All @@ -36,6 +36,7 @@ jobs:
build-args: |
build_version=standalone
TARGETARCH=${{ matrix.platform.target_arch }}
os_version=${{ matrix.platform.os_version }}
tags: emhass/standalone-test
load: true
- name: Test #assume docker fail with FileNotFound secrets_emhass.yaml error
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/publish_docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
fail-fast: false
matrix:
platform: [
{buildx: linux/amd64, target_arch: amd64},
{buildx: linux/arm/v7, target_arch: armv7},
{buildx: linux/arm/v7, target_arch: armhf},
{buildx: linux/arm64, target_arch: aarch64}
{buildx: linux/amd64, target_arch: amd64, os_version: debian},
{buildx: linux/arm/v7, target_arch: armv7, os_version: debian},
{buildx: linux/arm/v7, target_arch: armhf, os_version: raspbian},
{buildx: linux/arm64, target_arch: aarch64, os_version: debian}
]
steps:
- name: Checkout the repository
Expand Down Expand Up @@ -45,6 +45,7 @@ jobs:
build-args: |
build_version=standalone
TARGETARCH=${{ matrix.platform.target_arch }}
os_version=${{ matrix.platform.os_version }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ secrets.DOCKERHUB_USERNAME }}/emhass-docker-standalone,push-by-digest=true,name-canonical=true,push=true
- name: Export digest
Expand Down
6 changes: 2 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"name": "EMHASS run",
"type": "debugpy",
"request": "launch",
"program": "web_server.py",
"module": "emhass.web_server",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/src/emhass/",
"purpose":["debug-in-terminal"],
"justMyCode": true,
"env": {
Expand All @@ -29,9 +28,8 @@
"name": "EMHASS run ADDON",
"type": "debugpy",
"request": "launch",
"program": "web_server.py",
"module": "emhass.web_server",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/src/emhass/",
"args": ["--addon", "true", "--no_response", "true"],
"purpose":["debug-in-terminal"],
"justMyCode": true,
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"tasks": [
{
"label": "EMHASS install",
"command": "sudo",
"command": "pip3",
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"python3", "-m", "pip", "install", "."
"install", "--no-deps", "--force-reinstall", "."
],
"presentation": {
"echo": true,
Expand Down
111 changes: 66 additions & 45 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,63 +1,89 @@
## EMHASS Docker
## Docker run ADD-ON testing example:
## docker build -t emhass/docker --build-arg build_version=addon-local .
## docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE
##
## Docker run Standalone example:
## docker build -t emhass/docker --build-arg build_version=standalone .
## docker run -it -p 5000:5000 --name emhass-container -v $(pwd)/config_emhass.yaml:/app/config_emhass.yaml -v $(pwd)/secrets_emhass.yaml:/app/secrets_emhass.yaml emhass/docker
## EMHASS Docker
## Docker run addon testing example:
## docker build -t emhass/docker --build-arg build_version=addon-local .
## docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE
## Docker run standalone example:
## docker build -t emhass/docker --build-arg build_version=standalone .
## docker run -it -p 5000:5000 --name emhass-container -v $(pwd)/config_emhass.yaml:/app/config_emhass.yaml -v $(pwd)/secrets_emhass.yaml:/app/secrets_emhass.yaml emhass/docker

#build_version options are: addon, addon-pip, addon-git, addon-local, standalone (default)
ARG build_version=standalone

FROM ghcr.io/home-assistant/$TARGETARCH-base-debian:bookworm AS base

#armhf=raspbian, amd64,armv7,aarch64=debian
ARG os_version=debian

FROM ghcr.io/home-assistant/$TARGETARCH-base-$os_version:bookworm AS base

#check if TARGETARCH was passed by build-arg
ARG TARGETARCH
ENV TARGETARCH=${TARGETARCH:?}

WORKDIR /app
COPY requirements.txt /app/

# Setup
#apt package install
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libffi-dev \
python3 \
python3-pip \
python3-dev \
git \
build-essential \
gcc \
coinor-cbc \
coinor-libcbc-dev \
libglpk-dev \
glpk-utils \
patchelf \
cmake \
meson \
ninja-build \
build-essential \
libhdf5-dev \
libhdf5-serial-dev \
netcdf-bin \
libnetcdf-dev \
pkg-config \
gfortran \
netcdf-bin \
libnetcdf-dev \
coinor-cbc \
coinor-libcbc-dev \
libglpk-dev \
glpk-utils \
libatlas-base-dev \
&& ln -s /usr/include/hdf5/serial /usr/include/hdf5/include \
&& export HDF5_DIR=/usr/include/hdf5 \
&& pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U setuptools wheel \
&& pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -r requirements.txt \
&& apt-get purge -y --auto-remove \
libopenblas-dev
#specify hdf5
RUN ln -s /usr/include/hdf5/serial /usr/include/hdf5/include && export HDF5_DIR=/usr/include/hdf5

#install packages from pip, use piwheels if arm 32bit
RUN [[ "${TARGETARCH}" == "armhf" || "${TARGETARCH}" == "armv7" ]] && pip3 install --index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -r requirements.txt || pip3 install --no-cache-dir --break-system-packages -r requirements.txt

#try, symlink apt cbc, to pulp cbc, in python directory (for 32bit)
RUN [[ "${TARGETARCH}" == "armhf" || "${TARGETARCH}" == "armv7" ]] && ln -sf /usr/bin/cbc /usr/local/lib/python3.11/dist-packages/pulp/solverdir/cbc/linux/32/cbc || echo "cbc symlink didnt work/not required"

#if armv7, try install libatomic1 to fix scipy issue
RUN [[ "${TARGETARCH}" == "armv7" ]] && apt-get update && apt-get install libatomic1 || echo "libatomic1 cant be installed"


#remove build only packages
RUN apt-get purge -y --auto-remove \
git \
gcc \
patchelf \
cmake \
meson \
ninja-build \
build-essential \
libhdf5-dev \
libhdf5-serial-dev \
pkg-config \
gfortran \
netcdf-bin \
libnetcdf-dev \
&& rm -rf /var/lib/apt/lists/*


#copy config file (on all builds)
#copy config file
COPY config_emhass.yaml /app/

# Make sure data directory exists
#make sure data directory exists
RUN mkdir -p /app/data/

#-------------------------
#EMHASS-ADDON Default (this has no emhass packadge)
##EMHASS-Add-on default (this has no emhass package)
FROM base as addon

LABEL \
Expand All @@ -67,23 +93,21 @@ LABEL \
io.hass.type="addon" \
io.hass.arch="aarch64|amd64|armhf|armv7"

ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True", "--url", "http://supervisor/core/api"]

#-----------
#EMHASS-ADD-ON Testing with pip emhass (closest testing reference)
#EMHASS-ADD-ON testing with pip emhass (EMHASS-Add-on testing reference)
FROM addon as addon-pip
#set build arg for pip version
ARG build_pip_version=""
RUN pip3 install --no-cache-dir --break-system-packages --upgrade --upgrade-strategy=only-if-needed -U emhass${build_pip_version}
RUN pip3 install --no-cache-dir --break-system-packages --upgrade --force-reinstall --no-deps --upgrade-strategy=only-if-needed -U emhass${build_pip_version}

COPY options.json /app/

ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True", "--no_response", "True"]

#-----------
#EMHASS-ADD-ON Testing from local files
#EMHASS-Add-on testing from local files
FROM addon as addon-local
COPY src/emhass/ /app/src/emhass/
COPY src/emhass/ /app/src/emhass/
COPY src/emhass/templates/ /app/src/emhass/templates/
COPY src/emhass/static/ /app/src/emhass/static/
COPY src/emhass/static/img/ /app/src/emhass/static/img/
Expand All @@ -93,12 +117,12 @@ COPY options.json /app/
COPY README.md /app/
COPY setup.py /app/
#compile EMHASS locally
RUN python3 -m pip install --no-cache-dir --break-system-packages -U .
RUN pip3 install --no-cache-dir --break-system-packages --no-deps --force-reinstall .
ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True" , "--no_response", "True"]


#-----------
#EMHASS-ADD-ON testing with git
#EMHASS-Add-on testing with git
FROM addon as addon-git
ARG build_repo=https://github.com/davidusb-geek/emhass.git
ARG build_branch=master
Expand All @@ -116,31 +140,28 @@ RUN cp /tmp/emhass/README.md /app/
#add options.json, this otherwise would be generated via HA
RUN cp /tmp/emhass/options.json /app/
WORKDIR /app
RUN python3 -m pip install --no-cache-dir --break-system-packages -U .
RUN pip3 install --no-cache-dir --break-system-packages --no-deps --force-reinstall .
ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True" , "--no_response", "True"]

#-------------------------
#EMHASS STANDALONE
#EMHASS stanalone
FROM base as standalone

RUN pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U flask waitress plotly

COPY src/emhass/ /app/src/emhass/
COPY src/emhass/ /app/src/emhass/
COPY src/emhass/templates/ /app/src/emhass/templates/
COPY src/emhass/static/ /app/src/emhass/static/
COPY src/emhass/static/img/ /app/src/emhass/static/img/
COPY data/opt_res_latest.csv /app/data/
COPY README.md /app/
COPY setup.py /app/
#secrets file will need to be copied manually at docker run
#secrets file can be copied manually at docker run

# # set env variables
#set python env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

#build EMHASS
# RUN python3 setup.py install
RUN python3 -m pip install --no-cache-dir --break-system-packages -U .
RUN pip3 install --no-cache-dir --break-system-packages --no-deps --force-reinstall .
ENTRYPOINT [ "python3", "-m", "emhass.web_server"]
#-------------------------

Expand Down
Loading

0 comments on commit 46d6849

Please sign in to comment.