migrate to docker container

This commit is contained in:
Robert Kaussow 2020-11-29 21:59:41 +01:00
parent b9150ed19b
commit 7c5d9a4bd2
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61
14 changed files with 228 additions and 166 deletions

View File

@ -1,17 +1,67 @@
--- ---
zigbee2mqtt_version: 1.5.1 zigbee2mqtt_version: latest
zigbee2mqtt_image: "koenkk/zigbee2mqtt:{{ zigbee2mqtt_version }}"
zigbee2mqtt_user: zigbee2mqtt zigbee2mqtt_service_directory: /var/lib/docker/services/zigbee2mqtt
zigbee2mqtt_group: zigbee2mqtt zigbee2mqtt_container_name: zigbee2mqtt
zigbee2mqtt_secondary_groups: zigbee2mqtt_restart_policy: always
- tty zigbee2mqtt_service_stopped: False
- dialout # @var zigbee2mqtt_networks:example: >
# zigbee2mqtt_networks:
# - name: default
# # optional network driver, defaults to 'bride'
# driver: host
# @end
zigbee2mqtt_networks:
- name: default
zigbee2mqtt_service_state: started zigbee2mqtt_networks_applied:
zigbee2mqtt_service_restart_state: restarted - default
# @var zigbee2mqtt_volumes:description: Define required docker volumes.
# @var zigbee2mqtt_volumes:example: >
# zigbee2mqtt_volumes:
# # Instead of the name you could specify a path on the container host system,
# # but you also have to enable bind mount for this volume
# - name: data
# # target location inside the container
# dest: /var/www/app/data
# # enable bind mount, if false volume will be configured as named volume
# # keep in mind you MUST set bind in any case
# bind: True
# # Options for bind mounts
# bind_opt: "ro,z"
# @end
zigbee2mqtt_volumes:
- name: "{{ zigbee2mqtt_data_volume }}"
dest: /app/data
bind: True
# @var zigbee2mqtt_devices:description: Define required devices e.g. `/dev/ttyACM0`.
# @var zigbee2mqtt_devices:example: >
# zigbee2mqtt_devices:
# - src: "{{ zigbee2mqtt_serial_port }}"
# dest: "{{ zigbee2mqtt_serial_port }}"
# opt: z
# @end
zigbee2mqtt_data_volume: "/opt/zigbee2mqtt/data"
zigbee2mqtt_exposed_ports: []
zigbee2mqtt_extra_hosts: []
# @var zigbee2mqtt_memory_limit: $ "_unset_"
# @var zigbee2mqtt_memory_limit:example: $ "512m"
# @var zigbee2mqtt_memory_reservation: $ "_unset_"
# @var zigbee2mqtt_memory_reservation:example: $ "256m"
# @var zigbee2mqtt_cpu_shares: $ "_unset_"
# @var zigbee2mqtt_cpu_shares:example: $ "1024"
zigbee2mqtt_cap_add: []
zigbee2mqtt_cap_drop: []
zigbee2mqtt_security_opt: []
# @var zigbee2mqtt_pids_limit: $ "_unset_"
zigbee2mqtt_base_dir: /opt/zigbee2mqtt
zigbee2mqtt_log_dir: "{{ zigbee2mqtt_base_dir }}/log"
zigbee2mqtt_log_level: info zigbee2mqtt_log_level: info
zigbee2mqtt_homeassistant_enabled: False zigbee2mqtt_homeassistant_enabled: False
@ -26,7 +76,7 @@ zigbee2mqtt_mqtt_reject_unauthorized: False
zigbee2mqtt_mqtt_include_device_information: False zigbee2mqtt_mqtt_include_device_information: False
# @var zigbee2mqtt_serial_port: $ "_unset_" # @var zigbee2mqtt_serial_port: $ "_unset_"
# @var zigbee2mqtt_serial_port:example: $ "/dev/ttyACM0" # @var zigbee2mqtt_serial_port:example: $ ""
zigbee2mqtt_serial_disable_led: False zigbee2mqtt_serial_disable_led: False
zigbee2mqtt_pan_id: "0x1a62" zigbee2mqtt_pan_id: "0x1a62"
@ -45,3 +95,5 @@ zigbee2mqtt_homeassistant_status_topic: "hass/status"
zigbee2mqtt_devices_ban: [] zigbee2mqtt_devices_ban: []
zigbee2mqtt_devices_whitelist: [] zigbee2mqtt_devices_whitelist: []
zigbee2mqtt_timezone: Europe/Berlin

View File

@ -1,9 +1,11 @@
--- ---
- name: Restart zigbee2mqtt service - name: Restart zigbee2mqtt service
systemd: docker_compose:
state: "{{ zigbee2mqtt_service_restart_state }}" project_src: "{{ zigbee2mqtt_service_directory }}"
daemon_reload: yes pull: yes
name: zigbee2mqtt remove_orphans: yes
stopped: "{{ zigbee2mqtt_service_stopped }}"
restarted: "{{ not zigbee2mqtt_service_stopped }}"
listen: __zigbee2mqtt_restart listen: __zigbee2mqtt_restart
become: True become: True
become_user: root become_user: root

View File

@ -1,14 +1,23 @@
--- ---
- name: Converge - name: Converge (Stage 1)
hosts: all hosts: all
vars: vars:
mosquitto_packages_extra: dockerengine_packages_extra:
- epel-release - epel-release
- python-pip
- python-virtualenv
roles:
- role: xoxys.docker_engine
- name: Converge (Stage 2)
hosts: all
environment:
PYTHONPATH: /opt/python2/ansible-deps/lib/python2.7/site-packages
vars:
mosquitto_bind_address: "127.0.0.1" mosquitto_bind_address: "127.0.0.1"
zigbee2mqtt_service_state: stopped zigbee2mqtt_service_stopped: True
zigbee2mqtt_service_restart_state: stopped
roles: roles:
- role: xoxys.mosquitto - role: xoxys.mosquitto
- role: xoxys.nodejs - role: xoxys.zigbee2mqtt_docker
- role: xoxys.zigbee2mqtt

View File

@ -15,8 +15,3 @@ def test_hosts_file(host):
f = host.file('/etc/hosts') f = host.file('/etc/hosts')
assert f.exists assert f.exists
# def test_zigbee2mqtt_running_and_enabled(host):
# zigbee2mqtt = host.service("zigbee2mqtt")
# assert zigbee2mqtt.is_running
# assert zigbee2mqtt.is_enabled

View File

@ -4,12 +4,12 @@ collections:
- name: community.general - name: community.general
roles: roles:
- src: https://gitea.rknet.org/ansible/xoxys.docker_engine.git
name: xoxys.docker_engine
scm: git
version: master
- src: https://gitea.rknet.org/ansible/xoxys.mosquitto.git - src: https://gitea.rknet.org/ansible/xoxys.mosquitto.git
name: xoxys.mosquitto name: xoxys.mosquitto
scm: git scm: git
version: master version: master
- src: https://gitea.rknet.org/ansible/xoxys.nodejs.git
name: xoxys.nodejs
scm: git
version: master

View File

@ -1,76 +0,0 @@
---
- name: Create directories
file:
path: "{{ item }}"
state: directory
owner: "{{ zigbee2mqtt_user }}"
group: "{{ zigbee2mqtt_group }}"
mode: 0750
loop:
- "{{ zigbee2mqtt_base_dir }}"
- "{{ zigbee2mqtt_base_dir }}/data"
- "{{ zigbee2mqtt_log_dir }}"
become: True
become_user: root
- block:
- name: Download release 'v{{ zigbee2mqtt_version }}'
unarchive:
src: "https://github.com/Koenkk/zigbee2mqtt/archive/{{ zigbee2mqtt_version }}.tar.gz"
dest: "{{ zigbee2mqtt_base_dir }}"
remote_src: yes
exclude: "{{ __zigbee2mqtt_release_exclude }}"
extra_opts:
- --strip-components=1
- name: Delete existing node_modules folder
file:
path: "{{ zigbee2mqtt_base_dir }}/node_modules"
state: absent
- name: Install nodejs dependencies
npm:
path: "{{ zigbee2mqtt_base_dir }}"
environment:
PATH: "{{ __zigbee2mqtt_npm_executable | dirname }}:{{ ansible_env.PATH }}"
notify: __zigbee2mqtt_restart
when: zigbee2mqtt_version is version(zigbee2mqtt_current_version, ">")
become: True
become_user: "{{ zigbee2mqtt_user }}"
- block:
- name: Deploy configuration file
template:
src: "zigbee2mqtt/data/configuration.yaml.j2"
dest: "{{ zigbee2mqtt_base_dir }}/data/configuration.yaml"
mode: 0600
notify: __zigbee2mqtt_restart
- name: Ensure device files exists
file:
path: "{{ item }}"
state: touch
access_time: preserve
modification_time: preserve
loop:
- "{{ zigbee2mqtt_base_dir }}/data/devices.yaml"
- "{{ zigbee2mqtt_base_dir }}/data/groups.yaml"
become: True
become_user: "{{ zigbee2mqtt_user }}"
- block:
- name: Create systemd unit files
template:
src: "etc/systemd/system/zigbee2mqtt.service.j2"
dest: "/etc/systemd/system/zigbee2mqtt.service"
mode: 0644
notify: __zigbee2mqtt_restart
- name: Set current version to custom fact
template:
src: etc/ansible/facts.d/zigbee2mqtt.fact.j2
dest: /etc/ansible/facts.d/zigbee2mqtt.fact
mode: 0644
when: zigbee2mqtt_version is version(zigbee2mqtt_current_version, ">") or zigbee2mqtt_current_version is version('0.0.0', "=")
become: True
become_user: root

View File

@ -1,4 +1,2 @@
--- ---
- import_tasks: prepare.yml - include_tasks: setup.yml
- import_tasks: install.yml
- import_tasks: post_tasks.yml

View File

@ -1,9 +0,0 @@
---
- name: Ensure zigbee2mqtt service is up and running
systemd:
state: "{{ zigbee2mqtt_service_state }}"
daemon_reload: yes
enabled: yes
name: zigbee2mqtt
become: True
become_user: root

View File

@ -1,26 +0,0 @@
---
- block:
- name: Create local facts dir
file:
path: /etc/ansible/facts.d
state: directory
- name: Set current zigbee2mqtt version
set_fact:
zigbee2mqtt_current_version: "{{ (ansible_local.zigbee2mqtt | default (dict(version='0.0.0')))['version'] }}"
- debug:
msg: Current version is '{{ zigbee2mqtt_current_version }}'
- name: Create group '{{ zigbee2mqtt_group }}'
group:
name: "{{ zigbee2mqtt_group }}"
state: present
- name: Create user '{{ zigbee2mqtt_user }}'
user:
name: "{{ zigbee2mqtt_user }}"
group: "{{ zigbee2mqtt_group }}"
groups: "{{ zigbee2mqtt_secondary_groups }}"
become: True
become_user: root

52
tasks/setup.yml Normal file
View File

@ -0,0 +1,52 @@
---
- block:
- name: Ensure service directory exists
file:
path: "{{ zigbee2mqtt_service_directory }}"
state: directory
mode: 0755
- name: Create bind mount source directory
file:
path: "{{ item.name }}"
state: directory
loop: "{{ zigbee2mqtt_volumes }}"
loop_control:
label: "{{ item.name }}"
when: item.bind | bool
- name: Deploy compose file to '{{ zigbee2mqtt_service_directory }}'
template:
src: "services/zigbee2mqtt_compose.yml.j2"
dest: "{{ zigbee2mqtt_service_directory }}/docker-compose.yml"
owner: root
group: root
mode: 0640
validate: "docker-compose -f %s config -q"
- name: Deploy configuration file
template:
src: "zigbee2mqtt/data/configuration.yaml.j2"
dest: "{{ zigbee2mqtt_data_volume }}/configuration.yaml"
mode: 0600
notify: __zigbee2mqtt_restart
- name: Ensure device files exists
file:
path: "{{ item }}"
state: touch
access_time: preserve
modification_time: preserve
loop:
- "{{ zigbee2mqtt_data_volume }}/devices.yaml"
- "{{ zigbee2mqtt_data_volume }}/groups.yaml"
- name: Ensure service is up and running
docker_compose:
project_src: "{{ zigbee2mqtt_service_directory }}"
pull: yes
remove_orphans: yes
stopped: "{{ zigbee2mqtt_service_stopped }}"
state: present
become: True
become_user: root

View File

@ -1,4 +0,0 @@
{
"comment" : "{{ ansible_managed }}",
"version" : "{{ zigbee2mqtt_version }}"
}

View File

@ -1,16 +0,0 @@
#jinja2:lstrip_blocks: True
{{ ansible_managed | comment }}
[Unit]
Description=zigbee2mqtt
After=network.target
[Service]
ExecStart=/usr/local/bin/npm start
WorkingDirectory={{ zigbee2mqtt_base_dir }}
StandardError=inherit
Restart=always
User={{ zigbee2mqtt_user }}
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,86 @@
#jinja2:lstrip_blocks: True
{{ ansible_managed | comment }}
version: "2.4"
services:
zigbee2mqtt:
container_name: {{ zigbee2mqtt_container_name }}
image: {{ zigbee2mqtt_image }}
restart: {{ zigbee2mqtt_restart_policy }}
{% if zigbee2mqtt_exposed_ports | default([]) %}
ports:
{% for port in zigbee2mqtt_exposed_ports %}
- {{ port | quote }}
{% endfor %}
{% endif %}
{% if zigbee2mqtt_volumes | default([]) %}
volumes:
{% for volume in zigbee2mqtt_volumes %}
- "{{ volume.name }}:{{ volume.dest }}{% if volume.bind_opt is defined %}:{{ volume.bind_opt }}{% endif %}"
{% endfor %}
{% endif %}
{% if zigbee2mqtt_devices | default([]) %}
devices:
{% for device in zigbee2mqtt_devices %}
- "{{ device.src }}:{{ device.dest }}{% if device.opt is defined %}:{{ device.opt }}{% endif %}"
{% endfor %}
{% endif %}
{% if zigbee2mqtt_networks_applied | default([]) %}
networks:
{% for network in zigbee2mqtt_networks_applied %}
- {{ network }}
{% endfor %}
{% endif %}
{% if zigbee2mqtt_extra_hosts | default([]) %}
extra_hosts:
{% for host in zigbee2mqtt_extra_hosts %}
- {{ host | quote }}
{% endfor %}
{% endif %}
environment:
- TZ={{ zigbee2mqtt_timezone }}
{% if zigbee2mqtt_memory_limit is defined %}
mem_limit: {{ zigbee2mqtt_memory_limit }}
{% endif %}
{% if zigbee2mqtt_memory_reservation is defined %}
mem_reservation: {{ zigbee2mqtt_memory_reservation }}
{% endif %}
{% if zigbee2mqtt_cpu_shares is defined %}
cpu_shares: {{ zigbee2mqtt_cpu_shares }}
{% endif %}
{% if not zigbee2mqtt_cap_add | length == 0 %}
cap_add:
{% for item in zigbee2mqtt_cap_add %}
- {{ item }}
{% endfor %}
{% endif %}
{% if not zigbee2mqtt_cap_drop | length == 0 %}
cap_drop:
{% for item in zigbee2mqtt_cap_drop %}
- {{ item }}
{% endfor %}
{% endif %}
{% if not zigbee2mqtt_security_opt | length == 0 %}
security_opt:
{% for item in zigbee2mqtt_security_opt %}
- {{ item }}
{% endfor %}
{% endif %}
{% if zigbee2mqtt_pids_limit is defined %}
pids_limit: {{ zigbee2mqtt_pids_limit }}
{% endif %}
{% if zigbee2mqtt_volumes | default([]) | rejectattr("bind") | list | length > 0 %}
volumes:
{% for volume in zigbee2mqtt_volumes | rejectattr("bind") %}
{{ volume.name }}:
{% endfor %}
{% endif %}
{% if zigbee2mqtt_networks | default([]) | length > 0 %}
networks:
{% for network in zigbee2mqtt_networks %}
{{ network.name }}:
driver: {{ network.backend | default("bridge") }}
{% endfor %}
{% endif %}

View File

@ -43,7 +43,6 @@ advanced:
channel: {{ zigbee2mqtt_channel }} channel: {{ zigbee2mqtt_channel }}
cache_state: {{ zigbee2mqtt_cache_state }} cache_state: {{ zigbee2mqtt_cache_state }}
log_level: "{{ zigbee2mqtt_log_level }}" log_level: "{{ zigbee2mqtt_log_level }}"
log_directory: "{{ zigbee2mqtt_log_dir }}"
{% if zigbee2mqtt_network_key is defined and zigbee2mqtt_network_key %} {% if zigbee2mqtt_network_key is defined and zigbee2mqtt_network_key %}
network_key: {{ zigbee2mqtt_network_key }} network_key: {{ zigbee2mqtt_network_key }}
{% endif %} {% endif %}