diff --git a/Makefile b/Makefile index 493875d..afda9e9 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,11 @@ # $(MAKE) -C app $@ clean build: - $(MAKE) -C ha_addons/ha_addon $@ + $(MAKE) -C ha_addons $@ addon-dev addon-debug addon-rc: - $(MAKE) -C ha_addons/ha_addon $(patsubst addon-%,%,$@) \ No newline at end of file + $(MAKE) -C ha_addons $(patsubst addon-%,%,$@) + +check-docker-compose: + docker-compose config -q + diff --git a/app/requirements-test.txt b/app/requirements-test.txt index bbf4e68..a26074c 100644 --- a/app/requirements-test.txt +++ b/app/requirements-test.txt @@ -4,4 +4,5 @@ pytest-cov python-dotenv mock - coverage \ No newline at end of file + coverage + jinja2-cli \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100755 index ac1c23c..0000000 --- a/build.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash -# Usage: ./build.sh [dev|rc|rel] -# dev: development build -# rc: release candidate build -# rel: release build and push to ghcr.io -# Note: for release build, you need to set GHCR_TOKEN -# export GHCR_TOKEN= in your .zprofile -# see also: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry - - -set -e - -BUILD_DATE=$(date -Iminutes) -BRANCH=$(git rev-parse --abbrev-ref HEAD) -VERSION=$(git describe --tags --abbrev=0) -VERSION="${VERSION:1}" -arr=(${VERSION//./ }) -MAJOR=${arr[0]} -IMAGE=tsun-gen3-proxy - -GREEN='\033[0;32m' -BLUE='\033[0;34m' -NC='\033[0m' - -if [[ $1 == debug ]] || [[ $1 == dev ]] ;then -IMAGE=docker.io/sallius/${IMAGE} -VERSION=${VERSION}+$1 -elif [[ $1 == rc ]] || [[ $1 == rel ]] || [[ $1 == preview ]] ;then -IMAGE=ghcr.io/s-allius/${IMAGE} -echo 'login to ghcr.io' -echo $GHCR_TOKEN | docker login ghcr.io -u s-allius --password-stdin -else -echo argument missing! -echo try: $0 '[debug|dev|preview|rc|rel]' -exit 1 -fi - -export IMAGE -export VERSION -export BUILD_DATE -export BRANCH -export MAJOR - -echo version: $VERSION build-date: $BUILD_DATE image: $IMAGE -docker buildx bake -f app/docker-bake.hcl $1 - -echo -e "${BLUE} => checking docker-compose.yaml file${NC}" -docker-compose config -q -echo -echo -e "${GREEN}${BUILD_DATE} => Version: ${VERSION}${NC} finished" -echo diff --git a/ha_addons/.gitignore b/ha_addons/.gitignore new file mode 100644 index 0000000..1e162f6 --- /dev/null +++ b/ha_addons/.gitignore @@ -0,0 +1,2 @@ +.data.json +config.yaml \ No newline at end of file diff --git a/ha_addons/Makefile b/ha_addons/Makefile new file mode 100644 index 0000000..3831e44 --- /dev/null +++ b/ha_addons/Makefile @@ -0,0 +1,132 @@ +#!make +include ../.env + +.PHONY: debug dev build clean rootfs repro rc rel + +SHELL = /bin/sh +JINJA = jinja2 +IMAGE = tsun-gen3-addon + + +# Folders +SRC=../app +SRC_PROXY=$(SRC)/src +CNF_PROXY=$(SRC)/config + +ADDON_PATH = ha_addon +DST=$(ADDON_PATH)/rootfs +DST_PROXY=$(DST)/home/proxy + +INST_BASE=../../ha-addons/ha-addons + +TEMPL=templates + +# collect source files +SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\ + $(wildcard $(SRC_PROXY)/*.ini)\ + $(wildcard $(SRC_PROXY)/cnf/*.py)\ + $(wildcard $(SRC_PROXY)/gen3/*.py)\ + $(wildcard $(SRC_PROXY)/gen3plus/*.py) +CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml) + +# determine destination files +TARGET_FILES = $(SRC_FILES:$(SRC_PROXY)/%=$(DST_PROXY)/%) +CONFIG_FILES = $(CNF_FILES:$(CNF_PROXY)/%=$(DST_PROXY)/%) + +export BUILD_DATE := ${shell date -Iminutes} +VERSION := $(shell cat $(SRC)/.version) +export MAJOR := $(shell echo $(VERSION) | cut -f1 -d.) + +PUBLIC_URL := $(shell echo $(PUBLIC_CONTAINER_REGISTRY) | cut -f1 -d/) +PUBLIC_USER :=$(shell echo $(PUBLIC_CONTAINER_REGISTRY) | cut -f2 -d/) + + +dev debug: build + @echo version: $(VERSION) build-date: $(BUILD_DATE) image: $(PRIVAT_CONTAINER_REGISTRY)$(IMAGE) + export VERSION=$(VERSION)-$@ && \ + export IMAGE=$(PRIVAT_CONTAINER_REGISTRY)$(IMAGE) && \ + docker buildx bake -f docker-bake.hcl $@ + +rc rel: build + @echo version: $(VERSION) build-date: $(BUILD_DATE) image: $(PUBLIC_CONTAINER_REGISTRY)$(IMAGE) + @echo login at $(PUBLIC_URL) as $(PUBLIC_USER) + @DO_LOGIN="$(shell echo $(PUBLIC_CR_KEY) | docker login $(PUBLIC_URL) -u $(PUBLIC_USER) --password-stdin)" + export VERSION=$(VERSION)-$@ && \ + export IMAGE=$(PUBLIC_CONTAINER_REGISTRY)$(IMAGE) && \ + docker buildx bake -f docker-bake.hcl $@ + + +build: rootfs $(ADDON_PATH)/config.yaml repro + +clean: + rm -r -f $(DST_PROXY) + rm -f $(DST)/requirements.txt + rm -f $(ADDON_PATH)/config.yaml + rm -f $(TEMPL)/.data.json + +# +# Build rootfs and config.yaml as local add-on +# The rootfs is needed to build the add-on Dockercontainers +# + +rootfs: $(TARGET_FILES) $(CONFIG_FILES) $(DST)/requirements.txt + +STAGE=dev +debug : STAGE=debug +rc : STAGE=rc +rel : STAGE=rel + +$(CONFIG_FILES): $(DST_PROXY)/% : $(CNF_PROXY)/% + @echo Copy $< to $@ + @mkdir -p $(@D) + @cp $< $@ + +$(TARGET_FILES): $(DST_PROXY)/% : $(SRC_PROXY)/% + @echo Copy $< to $@ + @mkdir -p $(@D) + @cp $< $@ + +$(DST)/requirements.txt : $(SRC)/requirements.txt + @echo Copy $< to $@ + @cp $< $@ + +$(ADDON_PATH)/%.yaml: $(TEMPL)/%.jinja $(TEMPL)/.data.json + $(JINJA) --strict --format=json $^ -o $@ + +$(TEMPL)/.data.json: FORCE + rsync --checksum $(TEMPL)/$(STAGE)_data.json $@ + +FORCE : ; + + +# +# Build repository for Home Assistant Add-On +# + +INST=$(INST_BASE)/ha_addon_dev +repro_files = DOCS.md icon.png logo.png translations/en.yaml +repro_templates = config.yaml +repro_subdirs = translations +repro_vers = debug dev rel + +repro_all_files := $(foreach dir,$(repro_vers), $(foreach file,$(repro_files),$(INST_BASE)/ha_addon_$(dir)/$(file))) +repro_all_templates := $(foreach dir,$(repro_vers), $(foreach file,$(repro_templates),$(INST_BASE)/ha_addon_$(dir)/$(file))) +repro_all_subdirs := $(foreach dir,$(repro_vers), $(foreach file,$(repro_subdirs),$(INST_BASE)/ha_addon_$(dir)/$(file))) + +repro: $(repro_all_subdirs) $(repro_all_templates) $(repro_all_files) + +$(repro_all_subdirs) : + mkdir -p $@ + +$(repro_all_templates) : $(INST_BASE)/ha_addon_%/config.yaml: $(TEMPL)/config.jinja $(TEMPL)/%_data.json $(SRC)/.version + $(JINJA) --strict -D AppVersion=$(VERSION) $< $(filter %.json,$^) -o $@ + + +$(filter $(INST_BASE)/ha_addon_debug/%,$(repro_all_files)) : $(INST_BASE)/ha_addon_debug/% : ha_addon/% + cp $< $@ +$(filter $(INST_BASE)/ha_addon_dev/%,$(repro_all_files)) : $(INST_BASE)/ha_addon_dev/% : ha_addon/% + cp $< $@ +$(filter $(INST_BASE)/ha_addon_rel/%,$(repro_all_files)) : $(INST_BASE)/ha_addon_rel/% : ha_addon/% + cp $< $@ + + diff --git a/ha_addons/ha_addon/docker-bake.hcl b/ha_addons/docker-bake.hcl similarity index 99% rename from ha_addons/ha_addon/docker-bake.hcl rename to ha_addons/docker-bake.hcl index 408a326..5c978a2 100644 --- a/ha_addons/ha_addon/docker-bake.hcl +++ b/ha_addons/docker-bake.hcl @@ -18,7 +18,7 @@ variable "DESCRIPTION" { } target "_common" { - context = "." + context = "ha_addon" dockerfile = "Dockerfile" args = { VERSION = "${VERSION}" diff --git a/ha_addons/ha_addon/Makefile b/ha_addons/ha_addon/Makefile deleted file mode 100644 index 43de018..0000000 --- a/ha_addons/ha_addon/Makefile +++ /dev/null @@ -1,74 +0,0 @@ -#!make -include ../../.env - -SHELL = /bin/sh -IMAGE = tsun-gen3-addon - - -# Folders -SRC=../../app -SRC_PROXY=$(SRC)/src -CNF_PROXY=$(SRC)/config - -DST=rootfs -DST_PROXY=$(DST)/home/proxy - -# collect source files -SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\ - $(wildcard $(SRC_PROXY)/*.ini)\ - $(wildcard $(SRC_PROXY)/cnf/*.py)\ - $(wildcard $(SRC_PROXY)/gen3/*.py)\ - $(wildcard $(SRC_PROXY)/gen3plus/*.py) -CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml) - -# determine destination files -TARGET_FILES = $(SRC_FILES:$(SRC_PROXY)/%=$(DST_PROXY)/%) -CONFIG_FILES = $(CNF_FILES:$(CNF_PROXY)/%=$(DST_PROXY)/%) - -export BUILD_DATE := ${shell date -Iminutes} -VERSION := $(shell cat $(SRC)/.version) -export MAJOR := $(shell echo $(VERSION) | cut -f1 -d.) - -PUBLIC_URL := $(shell echo $(PUBLIC_CONTAINER_REGISTRY) | cut -f1 -d/) -PUBLIC_USER :=$(shell echo $(PUBLIC_CONTAINER_REGISTRY) | cut -f2 -d/) - - -dev debug: build - @echo version: $(VERSION) build-date: $(BUILD_DATE) image: $(PRIVAT_CONTAINER_REGISTRY)$(IMAGE) - export VERSION=$(VERSION)-$@ && \ - export IMAGE=$(PRIVAT_CONTAINER_REGISTRY)$(IMAGE) && \ - docker buildx bake -f docker-bake.hcl $@ - -rc: build - @echo version: $(VERSION) build-date: $(BUILD_DATE) image: $(PUBLIC_CONTAINER_REGISTRY)$(IMAGE) - @echo login at $(PUBLIC_URL) as $(PUBLIC_USER) - @DO_LOGIN="$(shell echo $(PUBLIC_CR_KEY) | docker login $(PUBLIC_URL) -u $(PUBLIC_USER) --password-stdin)" - export VERSION=$(VERSION)-$@ && \ - export IMAGE=$(PUBLIC_CONTAINER_REGISTRY)$(IMAGE) && \ - docker buildx bake -f docker-bake.hcl $@ - - -build: rootfs - -clean: - rm -r -f $(DST_PROXY) - rm -f $(DST)/requirements.txt - -rootfs: $(TARGET_FILES) $(CONFIG_FILES) $(DST)/requirements.txt - -.PHONY: debug dev build clean rootfs - - -$(CONFIG_FILES): $(DST_PROXY)/% : $(CNF_PROXY)/% - @echo Copy $< to $@ - @mkdir -p $(@D) - @cp $< $@ - -$(TARGET_FILES): $(DST_PROXY)/% : $(SRC_PROXY)/% - @echo Copy $< to $@ - @mkdir -p $(@D) - @cp $< $@ - -$(DST)/requirements.txt : $(SRC)/requirements.txt - @echo Copy $< to $@ - @cp $< $@ diff --git a/ha_addons/ha_addon/config.yaml b/ha_addons/templates/config.jinja similarity index 94% rename from ha_addons/ha_addon/config.yaml rename to ha_addons/templates/config.jinja index 3da3876..b0c3d2a 100755 --- a/ha_addons/ha_addon/config.yaml +++ b/ha_addons/templates/config.jinja @@ -1,16 +1,15 @@ -name: "TSUN-Proxy" -description: "MQTT Proxy for TSUN Photovoltaic Inverters" -version: "dev" +name: {{name}} +description: {{description}} +version: {% if version is defined and version|length %} {{version}} {% else %} {{AppVersion}} {% endif %} image: docker.io/sallius/tsun-gen3-addon url: https://github.com/s-allius/tsun-gen3-proxy -slug: "tsun-proxy" +slug: {{slug}} init: false arch: - aarch64 - amd64 - armhf - armv7 - - i386 startup: services homeassistant_api: true services: diff --git a/ha_addons/templates/debug_data.json b/ha_addons/templates/debug_data.json new file mode 100644 index 0000000..ce61957 --- /dev/null +++ b/ha_addons/templates/debug_data.json @@ -0,0 +1,7 @@ + +{ + "name": "TSUN-Proxy (Debug)", + "description": "MQTT Proxy for TSUN Photovoltaic Inverters with Debug Logging", + "version": "debug", + "slug": "tsun-proxy-debug" +} \ No newline at end of file diff --git a/ha_addons/templates/dev_data.json b/ha_addons/templates/dev_data.json new file mode 100644 index 0000000..8dd6614 --- /dev/null +++ b/ha_addons/templates/dev_data.json @@ -0,0 +1,7 @@ + +{ + "name": "TSUN-Proxy (Dev)", + "description": "MQTT Proxy for TSUN Photovoltaic Inverters", + "version": "dev", + "slug": "tsun-proxy-dev" +} \ No newline at end of file diff --git a/ha_addons/templates/rel_data.json b/ha_addons/templates/rel_data.json new file mode 100644 index 0000000..a106323 --- /dev/null +++ b/ha_addons/templates/rel_data.json @@ -0,0 +1,6 @@ + +{ + "name": "TSUN-Proxy", + "description": "MQTT Proxy for TSUN Photovoltaic Inverters", + "slug": "tsun-proxy" +} \ No newline at end of file