Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create environment variable for the config file #2519

Merged
merged 1 commit into from Dec 16, 2019

Conversation

wilmardo
Copy link
Contributor

@wilmardo wilmardo commented Dec 13, 2019

Introduces an environment variable for the configuration.yaml location.

Since the devices and groups can be split off this can be used to build the configuration into a Docker image since it doesn't require writing to.

For example:

# Home Assistant integration (MQTT discovery)
homeassistant: false

# allow new devices to join
permit_join: true

# MQTT settings
mqtt:
  # MQTT base topic for zigbee2mqtt MQTT messages
  base_topic: zigbee2mqtt
  # MQTT server URL
  server: "mqtt://localhost"
  # MQTT server authentication, uncomment if required:
  # user: my_user
  # password: my_password

# Serial settings
serial:
  # Location of CC2531 USB sniffer
  port: /dev/ttyACM0

# Map devices to persistent /data directory
devices: /data/devices.yaml
groups: /data/groups.yaml

advanced:
  log_output: ["console"]

This still persists the devices and groups to a volume but the configuration can be built into the image since it doesn't require changing.

I need this for to be able to use a Kubernetes configmap for the configuration. Kubernetes makes the folder where the configmap is mounted to read-only. In the current scenario this means that the whole /data directory becomes read-only. Zigbee2Mqtt tries to write the state.json to there so that results in an error.
This env var allows the configuration to exists in /config and keep the /data directory writable.

Signed-off-by: wilmardo <info@wilmardenouden.nl>
@wilmardo
Copy link
Contributor Author

BTW, when you are open to merging this I will add some documentation :)

@Koenkk
Copy link
Owner

Koenkk commented Dec 14, 2019

Currently it is already possible to set a different location for the data directory (which contains configuration.yaml) ZIGBEE2MQTT_DATA, does this have any added value?

@wilmardo
Copy link
Contributor Author

Yes it does, I will try to elaborate it a bit more.

This is an example deployment for the container on Kubernetes (simplified, full example here)

---
apiVersion: apps/v1
kind: Statefulset
metadata:
  name: zigbee2mqtt
spec:
  template:
    spec:
      containers:
        - name: zigbee2mqtt
          image: lansible/zigbee2mqtt:master
          env:
            - name: ZIGBEE2MQTT_DATA
              value: /data
          volumeMounts:
            - name: zigbee2mqtt-data
              mountPath: /data
            - name: zigbee-usb
              mountPath: /dev/ttyACM0
      volumes:
        - name: zigbee2mqtt-config
          configMap:
            name: zigbee2mqtt-config
        - name: zigbee2mqtt-data
          hostPath:
            path: /data/zigbee2mqtt-zigbee2mqtt-data
        - name: zigbee-usb
          hostPath:
            path: /dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B00194AD7EC-if00
            type: CharDevice

This works as expected, the only thing is, I need to login to the node to edit the configuration.yaml. Kubernetes has a nice resource dedicated to this named configmap (docs) which can be deployed with the app.

Example:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: zigbee2mqtt-config
data:
  configuration.yaml: |
    # Home Assistant integration (MQTT discovery)
    homeassistant: true

    # allow new devices to join
    permit_join: true

    # MQTT settings
    mqtt:
      # MQTT base topic for zigbee2mqtt MQTT messages
      base_topic: zigbee2mqtt
      # MQTT server URL
      server: 'mqtt://mosquitto:1883'
      user: zigbee2mqtt
      password: password

    # Serial settings
    serial:
      # Location of CC2531 USB sniffer
      port: /dev/ttyACM0

    # Map devices to persistant storage
    devices: /data/devices.yaml
    groups: /data/groups.yaml

    advanced:
      log_output: ["console"]
      log_level: debug

The problem here is that the folder where the configmap gets mounted will become readonly by design (issue).
So the current setup with /data/configuration.yaml does not work because /data will become readonly. Zigbee2Mqtt tries to write its state.json/devices.yaml/groups.yaml but errors since /data has become readonly.
This variable makes it possible to keep /data writable by mounting the config somewhere else (/config for example).

The patched deployment with /config readonly and the configmap mounted and /data still writeable:

---
apiVersion: apps/v1
kind: Statefulset
metadata:
  name: zigbee2mqtt
spec:
  template:
    spec:
      containers:
        - name: zigbee2mqtt
          image: lansible/zigbee2mqtt:master
          env:
            - name: ZIGBEE2MQTT_DATA
              value: /data
            - name: ZIGBEE2MQTT_CONFIG
              value: /config
          volumeMounts:
            - name: zigbee2mqtt-config
              mountPath: /config
            - name: zigbee2mqtt-data
              mountPath: /data
            - name: zigbee-usb
              mountPath: /dev/ttyACM0
      volumes:
        - name: zigbee2mqtt-config
          configMap:
            name: zigbee2mqtt-config
        - name: zigbee2mqtt-data
          hostPath:
            path: /data/zigbee2mqtt-zigbee2mqtt-data
        - name: zigbee-usb
          hostPath:
            path: /dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B00194AD7EC-if00
            type: CharDevice

@Koenkk
Copy link
Owner

Koenkk commented Dec 16, 2019

Definitely a valid case indeed, thanks for your comprehensive explanation!

@Koenkk Koenkk merged commit 5083bbc into Koenkk:dev Dec 16, 2019
Koenkk added a commit to Koenkk/zigbee2mqtt.io that referenced this pull request Dec 16, 2019
Koenkk added a commit to Koenkk/zigbee2mqtt.io that referenced this pull request Jan 3, 2020
Koenkk added a commit to Koenkk/zigbee2mqtt.io that referenced this pull request Jan 7, 2020
Koenkk added a commit to Koenkk/zigbee2mqtt.io that referenced this pull request Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants