diff --git a/README.md b/README.md index db4fadc..10b9d68 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,37 @@ -# ansible-ha-bridge-docker -Manages ha-bridge Docker container with systemd on Raspbian and Debian OS +# Ansible Role: ha-bridge-docker + +An Ansible role that manages ha-bridge Docker container with systemd on Raspbian and Debian OS. +This role is compatible with OS version Stretch (9) and Jessie (8). + +## Requirements + +The Docker daemon should be already installed and running on the host side. +I recommend using of the Ansible Galaxy role [angstwad.docker_ubuntu](https://github.com/angstwad/docker.ubuntu) to manage the Docker daemon. + +This role is tested with Ansible version greater equal 2.4. + +## Install + +``` +$ ansible-galaxy install escalate.ha-bridge-docker +``` + +## Role Variables + +Please see [defaults/main.yml](https://github.com/escalate/ansible-ha-bridge-docker/blob/master/defaults/main.yml) for a complete list of variables that can be overridden. + +## Dependencies + +None + +## Example Playbook + +``` +- hosts: all + roles: + - escalate.ha-bridge-docker +``` + +## License + +MIT diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..5804c77 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,13 @@ +--- + +ha_bridge_docker_image_version: latest +ha_bridge_docker_image_name: aptalca/home-automation-bridge +ha_bridge_docker_run_options: "--volume={{ha_bridge_data_path}}:/ha-bridge/data --volume={{ha_bridge_exec_path}}:/ha-bridge/exec" +ha_bridge_docker_restart_container: always + +ha_bridge_http_port: 8080 +ha_bridge_security_key: 'secret' + +ha_bridge_path: /opt/ha-bridge +ha_bridge_data_path: "{{ha_bridge_path}}/data" +ha_bridge_exec_path: "{{ha_bridge_path}}/exec" diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..f649f37 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,10 @@ +--- + +- name: Reload Systemd daemon + systemd: + daemon_reload: yes + +- name: Restart HA-Bridge service + service: + name: docker.ha-bridge + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..6d6877e --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,23 @@ +galaxy_info: + author: Felix Boerner + description: Manages ha-bridge Docker container with systemd on Raspbian and Debian OS + company: Felix Boerner + license: MIT + min_ansible_version: 2.4 + github_branch: master + platforms: + - name: Debian + versions: + - jessie + - stretch + galaxy_tags: + - raspbian + - raspberry + - pi + - raspberrypi + - docker + - systemd + - ha + - bridge + - habridge +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..9c9673a --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,55 @@ +--- + +- name: Load HA-Bridge settings for ARMv7 systems + include_vars: + file: armv7.yml + when: ansible_architecture == 'armv7l' + tags: + - ha-bridge + +- name: Create HA-Bridge folder structure + file: + path: "{{item}}" + state: directory + owner: root + group: root + mode: 0755 + with_items: + - "{{ha_bridge_path}}" + tags: + - ha-bridge + +- name: Create HA-Bridge startup script + template: + src: docker.ha-bridge.service.j2 + dest: /etc/systemd/system/docker.ha-bridge.service + owner: root + group: root + mode: 0644 + notify: + - Reload Systemd daemon + - Restart HA-Bridge service + tags: + - ha-bridge + +- name: Force all notified handlers to run at this point + meta: flush_handlers + +- name: Start HA-Bridge service + service: + name: docker.ha-bridge + state: started + enabled: yes + tags: + - ha-bridge + +- name: Wait for HA-Bridge service startup + uri: + url: "http://localhost:{{ha_bridge_http_port}}/" + status_code: 200 + register: result + until: result.status == 200 + retries: 60 + delay: 5 + tags: + - ha-bridge diff --git a/templates/docker.ha-bridge.service.j2 b/templates/docker.ha-bridge.service.j2 new file mode 100644 index 0000000..6e840e6 --- /dev/null +++ b/templates/docker.ha-bridge.service.j2 @@ -0,0 +1,16 @@ +[Unit] +Description=Docker HA-Bridge Container +After=docker.service +Requires=docker.service + +[Service] +ExecStartPre=-/usr/bin/docker stop %n +ExecStartPre=-/usr/bin/docker rm %n +ExecStartPre=/usr/bin/docker pull {{ha_bridge_docker_image_name}}:{{ha_bridge_docker_image_version}} +ExecStart=/usr/bin/docker run --init --name %n --net=host --volume=/etc/localtime:/etc/localtime:ro --volume=/etc/timezone:/etc/timezone:ro {{ha_bridge_docker_run_options}} {{ha_bridge_docker_image_name}}:{{ha_bridge_docker_image_version}} -Dserver.port={{ha_bridge_http_port}} -Djava.net.preferIPv4Stack=true -Dexec.garden=/ha-bridge/exec -Dsecurity.key={{ha_bridge_security_key}} +ExecStop=/usr/bin/docker stop %n +TimeoutStartSec=0 +Restart={{ha_bridge_docker_restart_container}} + +[Install] +WantedBy=multi-user.target diff --git a/vars/armv7.yml b/vars/armv7.yml new file mode 100644 index 0000000..f749ec4 --- /dev/null +++ b/vars/armv7.yml @@ -0,0 +1,3 @@ +--- + +ha_bridge_docker_image_name: habridge/ha-bridge-raspberrypi3