From 9a692e4c019529afaf9104402ecc70ceaf0fe759 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sun, 18 Jun 2023 13:48:45 +0200 Subject: [PATCH] Initial setup with zigbee2mqtt --- .gitignore | 1 + Makefile | 26 ++++++++++++ template/Dockerfile | 3 ++ template/docker-compose.template | 14 ++++++ template/start.sh | 10 +++++ zigbee/Dockerfile | 13 ++++++ zigbee/README | 28 ++++++++++++ zigbee/docker-compose.template | 20 +++++++++ zigbee/start.sh | 73 ++++++++++++++++++++++++++++++++ 9 files changed, 188 insertions(+) create mode 100644 Makefile create mode 100644 template/Dockerfile create mode 100644 template/docker-compose.template create mode 100755 template/start.sh create mode 100644 zigbee/Dockerfile create mode 100644 zigbee/README create mode 100644 zigbee/docker-compose.template create mode 100755 zigbee/start.sh diff --git a/.gitignore b/.gitignore index 24c6aa9..5389489 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +docker-compose.yml # ---> Linux *~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4b2af8a --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +DOCKER_COMP = sudo docker-compose + +help: + echo Help is not yet implemented... + + +build: + $(DOCKER_COMP) build + +up: ## Starts the docker hub + $(DOCKER_COMP) up -d + +down: ## Stops the docker hub + $(DOCKER_COMP) down --remove-orphans + +restart: ## Restarts the docker hub + $(DOCKER_COMP) restart + +shell_%: ## Connects to the application container + $(DOCKER_COMP) exec $(subst shell_,,$@) bash + +logs_%: ## Displays the logs of the application container + $(DOCKER_COMP) logs -f $(subst logs_,,$@) + +cleanall: + sudo docker system prune -a diff --git a/template/Dockerfile b/template/Dockerfile new file mode 100644 index 0000000..c75eeea --- /dev/null +++ b/template/Dockerfile @@ -0,0 +1,3 @@ +FROM debian:bookworm +RUN echo $TESTVAR_1 > /root/test.txt +COPY start.sh /root diff --git a/template/docker-compose.template b/template/docker-compose.template new file mode 100644 index 0000000..27cf7ce --- /dev/null +++ b/template/docker-compose.template @@ -0,0 +1,14 @@ +#version: '3' +#services: + + template: + restart: always + build: template + container_name: template + #network_mode: host + #privileged: true + #devices: + # - /dev/ttyACM0:/dev/ttyACM0 + #volumes: + # - /opt/zigbee2mqtt:/opt/zigbee2mqtt + command: bash -c "/root/start.sh" diff --git a/template/start.sh b/template/start.sh new file mode 100755 index 0000000..253af6e --- /dev/null +++ b/template/start.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# +echo "************************************" +echo "* This is a template only! *" +echo "* No Application has been started. *" +echo "************************************" +for (( ; ; )) +do + sleep 60 +done diff --git a/zigbee/Dockerfile b/zigbee/Dockerfile new file mode 100644 index 0000000..71466f4 --- /dev/null +++ b/zigbee/Dockerfile @@ -0,0 +1,13 @@ +FROM debian:bookworm +RUN apt update +RUN apt upgrade -y +RUN apt install curl git -y +# INSTALL nodejs +RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - +RUN apt install nodejs -y +# INSTALL yarn +RUN curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null +RUN echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list +RUN apt update +RUN apt install yarn -y +COPY start.sh /root diff --git a/zigbee/README b/zigbee/README new file mode 100644 index 0000000..f79131e --- /dev/null +++ b/zigbee/README @@ -0,0 +1,28 @@ +Install intruction: +=================== + +Configuration: +-------------- +* Append the docker-compose.template to your docker-compose.yml + * Edit the volumes section, to have a persistant zigbee2mqtt instance + at your local filesystem (fist entry is your local filesystem; + leave the second entry ontouched) + * Edit the environment variable of the added service, if you need to + create a zigbee2mqtt instance or + delete the variables, if you want to run an existing instance. + * Check and edit (if needed) the device section (first entry is your host filesystem) +* You might want to set differing versions for the needed software: + * The base operating system is defined in the Dockerfile (very top entry) + * The nodejs is defined in the Dockerfile (curl after #INSTALL nodejs) + * The zigbee2mqtt is already defined via the docker-compose.yml + +Installation: +------------- +* Execute make build up logs_zigbee + +Post-Installation actions: +-------------------------- +* You should remove the environments section espacially the credentials + from docker-compose.yml +* You should "chmod go-rwx" for ...zigbee2mqtt/data/configuration.yaml + diff --git a/zigbee/docker-compose.template b/zigbee/docker-compose.template new file mode 100644 index 0000000..1c1fafc --- /dev/null +++ b/zigbee/docker-compose.template @@ -0,0 +1,20 @@ +#version: '3' +#services: + + zigbee: + restart: always + build: zigbee + container_name: zigbee + volumes: + - /opt/zigbee2mqtt:/opt/zigbee2mqtt + environment: + APP_GIT_VERSION: 1.31.2 + #APP_MQTT_SERVER: mqtt_server + #APP_MQTT_USER: mqtt_user + #APP_MQTT_PASSWORD: mqtt_password + network_mode: host + #privileged: true + devices: + - /dev/ttyACM0:/dev/ttyACM0 + command: bash -c "/root/start.sh" + diff --git a/zigbee/start.sh b/zigbee/start.sh new file mode 100755 index 0000000..ad71106 --- /dev/null +++ b/zigbee/start.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# + +APP_TARGET_PATH=/opt/zigbee2mqtt + +my_print() +{ + echo `date` $HOST [$$]: $1 +} + +fail_loop() +{ + my_print "Start script failed..." + for (( ; ; )) + do + sleep 60 + done +} + +check_environment() +{ + fail=0 + # returns 0 if environment is not okay + if [[ -z $APP_GIT_VERSION ]]; then + my_print "Environment variable APP_GIT_VERSION does not exist!" + fail=1 + fi + if [[ -z $APP_MQTT_SERVER ]]; then + my_print "Environment variable APP_MQTT_SERVER does not exist!" + fail=1 + fi + # + if [[ $fail -ne 0 ]]; then + fail_loop + fi +} + + + +if [[ ! -d $APP_TARGET_PATH ]]; then + my_print "Directory \"$APP_TARGET_PATH\" does not exist." +fi +if [[ ! -d $APP_TARGET_PATH/.git ]]; then + check_environment + my_print "Cloning zigbee2mqtt repository..." + git clone https://github.com/Koenkk/zigbee2mqtt.git $APP_TARGET_PATH + my_print "Changing to directory $APP_TARGET_PAT" + cd $APP_TARGET_PATH + my_print "Changing to git version $APP_GIT_VERSION" + git checkout $APP_GIT_VERSION + my_print "Replacing MQTT servername in $APP_TARGET_PATH/data/configuration.yaml" + sed -i "s|mqtt://localhost|mqtt://$APP_MQTT_SERVER|g" $APP_TARGET_PATH/data/configuration.yaml + if [[ ! -z $APP_MQTT_USER ]]; then + my_print "Adding MQTT username in $APP_TARGET_PATH/data/configuration.yaml" + sed -i "s|# user: my_user|user: $APP_MQTT_USER|g" $APP_TARGET_PATH/data/configuration.yaml + fi + if [[ ! -z $APP_MQTT_PASSWORD ]]; then + my_print "Adding MQTT password in $APP_TARGET_PATH/data/configuration.yaml" + sed -i "s|# password: my_password|password: $APP_MQTT_PASSWORD|g" $APP_TARGET_PATH/data/configuration.yaml + fi + my_print "Preparing application (npm ci)" + npm ci +fi + +cd $APP_TARGET_PATH +my_print "Starting zigbee2mqtt..." +npm start + +my_print "**********************************************************" +my_print "* Start of zigbee2mqtt failed. *" +my_print "* Check running npm ci and npm start in /opt/zigbee2mqtt *" +my_print "**********************************************************" +fail_loop