From eab716ac77be2d142fb1517bf7e2591027bf1d99 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Sep 2023 11:49:00 -0500 Subject: [PATCH 01/11] First commit for wrapper script approach --- app/conf_wrapper.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yaml | 43 ++++++++++++++++++++++++++++----- 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 app/conf_wrapper.py diff --git a/app/conf_wrapper.py b/app/conf_wrapper.py new file mode 100644 index 0000000..97a4fe3 --- /dev/null +++ b/app/conf_wrapper.py @@ -0,0 +1,58 @@ +import os +import yaml + +# Read environment variables and construct the configuration dictionary +relay_config = { + "matrix": { + "homeserver": os.environ.get('MATRIX_HOMESERVER'), + "access_token": os.environ.get('MATRIX_ACCESS_TOKEN'), + "bot_user_id": os.environ.get('MATRIX_BOT_USER_ID') + }, + "meshtastic": { + "connection_type": os.environ.get('MESHTASTIC_CONNECTION_TYPE'), + "serial_port": os.environ.get('MESHTASTIC_SERIAL_PORT'), + "host": os.environ.get('MESHTASTIC_HOST'), + "meshnet_name": os.environ.get('MESHTASTIC_MESHNET_NAME'), + "broadcast_enabled": os.environ.get('MESHTASTIC_BROADCAST_ENABLED') == 'true' + }, + "logging": { + "level": os.environ.get('LOGGING_LEVEL') + } +} + +# Construct the matrix_rooms list based on environment variables +matrix_rooms = [] +for i in range(1, 9): # Loop for 8 rooms + room_id = os.environ.get(f'MATRIX_ROOMS_ID_{i}') + meshtastic_channel = os.environ.get(f'MATRIX_ROOMS_MESHTASTIC_CHANNEL_{i}') + if room_id and meshtastic_channel is not None: + matrix_rooms.append({ + "id": room_id, + "meshtastic_channel": int(meshtastic_channel) + }) + +# Add the matrix_rooms list to the relay_config dictionary +relay_config["matrix_rooms"] = matrix_rooms + +# Construct the plugins dictionary based on environment variables +plugins_config = {} + +health_plugin_active = os.environ.get('HEALTH_PLUGIN_ACTIVE') +if health_plugin_active: + plugins_config["health"] = {"active": health_plugin_active.lower() == "true"} + +map_plugin_active = os.environ.get('MAP_PLUGIN_ACTIVE') +if map_plugin_active: + plugins_config["map"] = {"active": map_plugin_active.lower() == "true"} + +nodes_plugin_active = os.environ.get('NODES_PLUGIN_ACTIVE') +if nodes_plugin_active: + plugins_config["nodes"] = {"active": nodes_plugin_active.lower() == "true"} + +# Add the plugins dictionary to the relay_config if it's not empty +if plugins_config: + relay_config["plugins"] = plugins_config + +# Write the configuration to config.yaml +with open("config.yaml", "w") as f: + yaml.dump(relay_config, f) diff --git a/docker-compose.yaml b/docker-compose.yaml index 303ec86..2363bcc 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,5 @@ -version: "3" +version: '3.8' + services: mmrelaynode: build: node @@ -24,11 +25,41 @@ services: - mesh:/home/mesh networks: - mesh - command: ["sleep 30"] - entrypoint: ["python3", "main.py"] - -volumes: - mesh: + command: ["sh", "-c", "python3 config_wrapper.py && sleep 30 && python3 main.py"] + environment: + MATRIX_HOMESERVER: "https://example.matrix.org" + MATRIX_ACCESS_TOKEN: "your_access_token" + MATRIX_BOT_USER_ID: "@botuser:example.matrix.org" + MATRIX_ROOMS_ID_1: "#someroomalias1:example.matrix.org" + MATRIX_ROOMS_MESHTASTIC_CHANNEL_1: "0" + # MATRIX_ROOMS_ID_2: "#someroomalias2:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_2: "1" + # MATRIX_ROOMS_ID_3: "#someroomalias3:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_3: "2" + # MATRIX_ROOMS_ID_4: "#someroomalias4:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_4: "3" + # MATRIX_ROOMS_ID_5: "#someroomalias5:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_5: "4" + # MATRIX_ROOMS_ID_6: "#someroomalias6:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_6: "5" + # MATRIX_ROOMS_ID_7: "#someroomalias7:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_7: "6" + # MATRIX_ROOMS_ID_8: "#someroomalias8:example.matrix.org" + # MATRIX_ROOMS_MESHTASTIC_CHANNEL_8: "7" + MESHTASTIC_CONNECTION_TYPE: "serial" # "serial" or "network" + MESHTASTIC_SERIAL_PORT: "/dev/ttyUSB0" + MESHTASTIC_HOST: "meshtastic.local" + MESHTASTIC_MESHNET_NAME: "Your Meshnet Name" + MESHTASTIC_BROADCAST_ENABLED: "true" + LOGGING_LEVEL: "info" + # Plugin environment variables (commented out for reference) + # HEALTH_PLUGIN_ACTIVE: "true" + # MAP_PLUGIN_ACTIVE: "true" + # NODES_PLUGIN_ACTIVE: "true" networks: mesh: + driver: bridge + +volumes: + mesh: From 1cd227e091efb6aa3a1c2027ccc042646503cf68 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Sep 2023 11:51:16 -0500 Subject: [PATCH 02/11] README.md formatting change --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3c339bb..e9715c3 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ Relay operation is implemented using the **Meshtastic <=> Matrix Relay** [https: Feel free to explore the **Meshtastic** project on their website: [https://meshtastic.org](https://meshtastic.org). +``` git clone https://github.com/mate-dev/mmrelaynode.git cd mmrelaynode && git submodule update --init docker compose -f "docker-compose.yaml" up -d --build +``` \ No newline at end of file From c925391427688166a357dd793247ead13064cda5 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Sep 2023 11:59:33 -0500 Subject: [PATCH 03/11] Update docker-compose.yaml, del old config.yaml --- app/config.yaml | 30 ------------------------------ docker-compose.yaml | 2 +- 2 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 app/config.yaml diff --git a/app/config.yaml b/app/config.yaml deleted file mode 100644 index 4d0b05c..0000000 --- a/app/config.yaml +++ /dev/null @@ -1,30 +0,0 @@ -matrix: - homeserver: "https://example.matrix.org" - access_token: "reaalllllyloooooongsecretttttcodeeeeeeforrrrbot" # See: https://t2bot.io/docs/access_tokens/ - bot_user_id: "@botuser:example.matrix.org" - -matrix_rooms: # Needs at least 1 room & channel, but supports all Meshtastic channels - - id: "#someroomalias:example.matrix.org" # Matrix room aliases & IDs supported - meshtastic_channel: 0 - - id: "!someroomid:example.matrix.org" - meshtastic_channel: 2 - -meshtastic: - connection_type: network # Do not change! - # serial_port: /dev/ttyUSB0 # Only used when connection is "serial" - host: "mmrelaynode" # Do not change! - meshnet_name: "Your Meshnet Name" # This is displayed in full on Matrix, but is truncated when sent to a Meshnet - broadcast_enabled: true - hwid: "12345" # Change to your Hardware ID - -logging: - level: "info" - -plugins: # Optional plugins - health: - active: true - map: - active: true - nodes: - active: true - \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 2363bcc..f66d363 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -25,7 +25,7 @@ services: - mesh:/home/mesh networks: - mesh - command: ["sh", "-c", "python3 config_wrapper.py && sleep 30 && python3 main.py"] + entrypoint: ["sh", "-c", "python3 config_wrapper.py && python3 main.py"] environment: MATRIX_HOMESERVER: "https://example.matrix.org" MATRIX_ACCESS_TOKEN: "your_access_token" From ddd8d0bf7b9f121db759d525148de1726e7df145 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:00:31 -0500 Subject: [PATCH 04/11] Comment cleanup --- docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index f66d363..fc8bb82 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -52,7 +52,7 @@ services: MESHTASTIC_MESHNET_NAME: "Your Meshnet Name" MESHTASTIC_BROADCAST_ENABLED: "true" LOGGING_LEVEL: "info" - # Plugin environment variables (commented out for reference) + # Plugin environment variables # HEALTH_PLUGIN_ACTIVE: "true" # MAP_PLUGIN_ACTIVE: "true" # NODES_PLUGIN_ACTIVE: "true" From adf29369a6e95ee10c1637fd77442caba8a4f00f Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:12:45 -0500 Subject: [PATCH 05/11] Dockerfile cleanup --- app/Dockerfile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/Dockerfile b/app/Dockerfile index c053552..2f1c91b 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -7,8 +7,3 @@ COPY --chown=mesh:mesh meshtastic-matrix-relay /home/mesh/app WORKDIR /home/mesh/app RUN pip install -qq -r /home/mesh/app/requirements.txt --no-cache-dir RUN echo "export PATH=/home/mesh/.local/bin:\$PATH" >> /home/mesh/.bashrc -COPY --chown=mesh:mesh config.yaml . -#ENTRYPOINT ["sh", "-c", "sleep 30 && python3 main.py"] - - - From 64b816045f71c9fac887be61bbd5ebd75e1b9fa7 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 12:52:25 -0500 Subject: [PATCH 06/11] Meshtastic command wrapper script --- app/command_wrapper.py | 22 ++++++++++++++++++++++ docker-compose.yaml | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 app/command_wrapper.py diff --git a/app/command_wrapper.py b/app/command_wrapper.py new file mode 100644 index 0000000..0e58d21 --- /dev/null +++ b/app/command_wrapper.py @@ -0,0 +1,22 @@ +import os +import subprocess +import time + +def execute_meshtastic_command(options): + """Execute a meshtastic command with the given options.""" + command = ["meshtastic", "--set"] + options.split() + subprocess.run(command) + time.sleep(30) # Pause for 30 seconds + +# Loop through environment variables in sequence +index = 1 +while True: + command = os.environ.get(f'MESHTASTIC_COMMAND_{index}') + if command: + execute_meshtastic_command(command) + index += 1 + else: + break + +# Finally, run the main Meshtastic process (or whatever process you want to keep the container running) +subprocess.run(["meshtastic"]) diff --git a/docker-compose.yaml b/docker-compose.yaml index fc8bb82..bff7386 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -13,6 +13,9 @@ services: networks: - mesh entrypoint: ["sh", "-c", "meshtasticd"] + environment: + MESHTASTIC_COMMAND_1: "network.wifi_enabled true --set network.wifi_ssid 'my network' --set network.wifi_psk mypassword" + MESHTASTIC_COMMAND_2: "another_option value --set yet_another_option another_value" mmrelayapp: build: app @@ -30,7 +33,7 @@ services: MATRIX_HOMESERVER: "https://example.matrix.org" MATRIX_ACCESS_TOKEN: "your_access_token" MATRIX_BOT_USER_ID: "@botuser:example.matrix.org" - MATRIX_ROOMS_ID_1: "#someroomalias1:example.matrix.org" + MATRIX_ROOMS_ID_1: "#someroomalias1:example.matrix.org" # Need at least 1 room & channel mapped, maximum 8 MATRIX_ROOMS_MESHTASTIC_CHANNEL_1: "0" # MATRIX_ROOMS_ID_2: "#someroomalias2:example.matrix.org" # MATRIX_ROOMS_MESHTASTIC_CHANNEL_2: "1" From 965e9a9c72ef8b017283b9ce626277899fd76786 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 13:51:28 -0500 Subject: [PATCH 07/11] Add wait-for-it.sh to check for port 4403 --- app/Dockerfile | 2 ++ docker-compose.yaml | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/Dockerfile b/app/Dockerfile index 2f1c91b..6c643d1 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -7,3 +7,5 @@ COPY --chown=mesh:mesh meshtastic-matrix-relay /home/mesh/app WORKDIR /home/mesh/app RUN pip install -qq -r /home/mesh/app/requirements.txt --no-cache-dir RUN echo "export PATH=/home/mesh/.local/bin:\$PATH" >> /home/mesh/.bashrc +ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh +RUN chmod +x /usr/local/bin/wait-for-it.sh diff --git a/docker-compose.yaml b/docker-compose.yaml index bff7386..13d3f16 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,8 +14,8 @@ services: - mesh entrypoint: ["sh", "-c", "meshtasticd"] environment: - MESHTASTIC_COMMAND_1: "network.wifi_enabled true --set network.wifi_ssid 'my network' --set network.wifi_psk mypassword" - MESHTASTIC_COMMAND_2: "another_option value --set yet_another_option another_value" + MESHTASTIC_COMMAND_1: "network.wifi_enabled true --set network.wifi_ssid 'my network' --set network.wifi_psk mypassword" + # You can add as many MESHTASTIC_COMMAND_X as you need, they will be executed in order mmrelayapp: build: app @@ -27,8 +27,8 @@ services: volumes: - mesh:/home/mesh networks: - - mesh - entrypoint: ["sh", "-c", "python3 config_wrapper.py && python3 main.py"] + - mesh + entrypoint: [ "sh", "-c", "wait-for-it.sh mmrelaynode:4403 -t 60 && python3 command_wrapper.py && python3 conf_wrapper.py && python3 main.py" ] environment: MATRIX_HOMESERVER: "https://example.matrix.org" MATRIX_ACCESS_TOKEN: "your_access_token" From a129b307435a3e95f2bb95671a38487b97273b95 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 14:04:37 -0500 Subject: [PATCH 08/11] Make container names more uniform --- docker-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 13d3f16..d12dceb 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,7 +4,7 @@ services: mmrelaynode: build: node image: mmrelaynode:latest - container_name: mmrelay-node + container_name: mmrelaynode-device restart: unless-stopped volumes: - mesh:/home/mesh @@ -20,7 +20,7 @@ services: mmrelayapp: build: app image: mmrelayapp:latest - container_name: mmrelay-app + container_name: mmrelaynode-app restart: unless-stopped depends_on: - mmrelaynode From fba6dcd40dce0f1938141d7fa10f2b9e77538b21 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 14:07:17 -0500 Subject: [PATCH 09/11] device Dockerfile change --- app/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Dockerfile b/app/Dockerfile index 6c643d1..534800e 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -2,10 +2,10 @@ FROM python:3.11-slim-bookworm AS app LABEL "website"="https://github.com/mate-dev/mmrelaynode" RUN groupadd -g 1000 mesh && useradd -ml -u 1000 -g 1000 -s /bin/bash mesh RUN pip install --upgrade pip -qq +ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh +RUN chmod +x /usr/local/bin/wait-for-it.sh USER mesh COPY --chown=mesh:mesh meshtastic-matrix-relay /home/mesh/app WORKDIR /home/mesh/app RUN pip install -qq -r /home/mesh/app/requirements.txt --no-cache-dir -RUN echo "export PATH=/home/mesh/.local/bin:\$PATH" >> /home/mesh/.bashrc -ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh -RUN chmod +x /usr/local/bin/wait-for-it.sh +RUN echo "export PATH=/home/mesh/.local/bin:\$PATH" >> /home/mesh/.bashrc \ No newline at end of file From ae282c67b8792f3b95cb840b1aae8be999bb3ab1 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 14:12:18 -0500 Subject: [PATCH 10/11] Rename node dir to device for clarity --- {node => device}/Dockerfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {node => device}/Dockerfile (100%) diff --git a/node/Dockerfile b/device/Dockerfile similarity index 100% rename from node/Dockerfile rename to device/Dockerfile From 4791e6f24f055ee790c83480d5f65f14e6c087d4 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Sat, 9 Sep 2023 17:24:15 -0500 Subject: [PATCH 11/11] Dockerfile & docker-compose changes --- app/Dockerfile | 1 + docker-compose.yaml | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Dockerfile b/app/Dockerfile index 534800e..10459ae 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -4,6 +4,7 @@ RUN groupadd -g 1000 mesh && useradd -ml -u 1000 -g 1000 -s /bin/bash mesh RUN pip install --upgrade pip -qq ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh RUN chmod +x /usr/local/bin/wait-for-it.sh +RUN ln -s /home/mesh/.local/bin/meshtastic /bin/meshtastic USER mesh COPY --chown=mesh:mesh meshtastic-matrix-relay /home/mesh/app WORKDIR /home/mesh/app diff --git a/docker-compose.yaml b/docker-compose.yaml index d12dceb..58b144a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,8 +14,10 @@ services: - mesh entrypoint: ["sh", "-c", "meshtasticd"] environment: - MESHTASTIC_COMMAND_1: "network.wifi_enabled true --set network.wifi_ssid 'my network' --set network.wifi_psk mypassword" - # You can add as many MESHTASTIC_COMMAND_X as you need, they will be executed in order + MESHTASTIC_COMMAND_1: "--set-owner 'LongName' --set-owner-short 'SHRT' --set-url https://meshtastic.org/e/#CgMSAQESDAgBOAFAA0gBUB5oAQ" + MESHTASTIC_COMMAND_2: "--set mqtt.enabled true --set mqtt.address mqtt.meshtastic.org --set mqtt.username meshdev --set mqtt.password large4cats" + MESHTASTIC_COMMAND_3: "--ch-set uplink_enabled true --ch-set downlink_enabled true --ch-index 3" + # You can add as many MESHTASTIC_COMMAND_X as you need, they will be executed in order with a 30 second delay between mmrelayapp: build: app @@ -33,7 +35,14 @@ services: MATRIX_HOMESERVER: "https://example.matrix.org" MATRIX_ACCESS_TOKEN: "your_access_token" MATRIX_BOT_USER_ID: "@botuser:example.matrix.org" - MATRIX_ROOMS_ID_1: "#someroomalias1:example.matrix.org" # Need at least 1 room & channel mapped, maximum 8 + MESHTASTIC_CONNECTION_TYPE: "serial" # "serial" or "network" + MESHTASTIC_SERIAL_PORT: "/dev/ttyUSB0" + MESHTASTIC_HOST: "meshtastic.local" + MESHTASTIC_MESHNET_NAME: "Your Meshnet Name" + MESHTASTIC_BROADCAST_ENABLED: "true" + LOGGING_LEVEL: "info" + # Need at least 1 room & channel mapped, maximum 8 + MATRIX_ROOMS_ID_1: "#someroomalias1:example.matrix.org" MATRIX_ROOMS_MESHTASTIC_CHANNEL_1: "0" # MATRIX_ROOMS_ID_2: "#someroomalias2:example.matrix.org" # MATRIX_ROOMS_MESHTASTIC_CHANNEL_2: "1" @@ -49,12 +58,7 @@ services: # MATRIX_ROOMS_MESHTASTIC_CHANNEL_7: "6" # MATRIX_ROOMS_ID_8: "#someroomalias8:example.matrix.org" # MATRIX_ROOMS_MESHTASTIC_CHANNEL_8: "7" - MESHTASTIC_CONNECTION_TYPE: "serial" # "serial" or "network" - MESHTASTIC_SERIAL_PORT: "/dev/ttyUSB0" - MESHTASTIC_HOST: "meshtastic.local" - MESHTASTIC_MESHNET_NAME: "Your Meshnet Name" - MESHTASTIC_BROADCAST_ENABLED: "true" - LOGGING_LEVEL: "info" + # Plugin environment variables # HEALTH_PLUGIN_ACTIVE: "true" # MAP_PLUGIN_ACTIVE: "true"