From 6b3ced47551eed8342ee94843fd9c42e78e72bd2 Mon Sep 17 00:00:00 2001 From: Matthew Macdonald-Wallace Date: Sun, 1 May 2016 12:44:50 +0100 Subject: [PATCH] Initial import of the role after tidying it up --- .gitignore | 2 + .kitchen.yml | 19 ++++ README.md | 98 +++++++++++++++++++ defaults/main.yml | 32 ++++++ files/items/README.md | 2 + files/persistence/README.md | 2 + files/rules/README.md | 2 + files/sitemaps/README.md | 2 + handlers/main.yml | 6 ++ meta/main.yml | 15 +++ tasks/debian.yml | 41 ++++++++ tasks/main.yml | 59 +++++++++++ tasks/rhel.yml | 1 + templates/openhab.cfg.j2 | 49 ++++++++++ test/integration/openhab/default.yml | 8 ++ test/integration/openhab/serverspec/Gemfile | 3 + .../openhab/serverspec/openhab_spec.rb | 20 ++++ vars/main.yml | 2 + 18 files changed, 363 insertions(+) create mode 100644 .gitignore create mode 100644 .kitchen.yml create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 files/items/README.md create mode 100644 files/persistence/README.md create mode 100644 files/rules/README.md create mode 100644 files/sitemaps/README.md create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/debian.yml create mode 100644 tasks/main.yml create mode 100644 tasks/rhel.yml create mode 100644 templates/openhab.cfg.j2 create mode 100644 test/integration/openhab/default.yml create mode 100644 test/integration/openhab/serverspec/Gemfile create mode 100644 test/integration/openhab/serverspec/openhab_spec.rb create mode 100644 vars/main.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30a19d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.kitchen/** +**/*.swp diff --git a/.kitchen.yml b/.kitchen.yml new file mode 100644 index 0000000..26a6910 --- /dev/null +++ b/.kitchen.yml @@ -0,0 +1,19 @@ +--- +platforms: + - name: ubuntu-14.04 + provision_command: sudo pip install netaddr + driver: + instance_type: c4.large + tags: + - Name: ubuntu-14.04-test-kitchen-openhab +# - name: centos-7 +# provision_command: yum install -y rubygem-rake +# tags: +# - Name: centos-7-test-kitchen-nginx + +suites: + - name: openhab + +verifier: + sudo_command: sudo + diff --git a/README.md b/README.md new file mode 100644 index 0000000..71bd112 --- /dev/null +++ b/README.md @@ -0,0 +1,98 @@ +# OpenHAB + +Install and configure the OpenHAB (www.openhab.org) software for Home +Automation + +## Requirements + +To ensure that IP Address Ranges are mapped to their correct CIDR equivalents, +you must install "netaddr" on the device that you use to run this Ansible role. + +This is easily done from PIP with the following command on Linux: + +```pip install netaddr``` + +## Usage + +### Bindings, Actions, Persistence and IO Plugins + +To install and configure bindings, you need to set variables either at a group, +host or role level. + +Each section of the variables is named after the configuration file section +name, for example, the following installs and configures the "pushover" +action, the rrd4j persistence layer and the lightwaverf and milight bindings: + +``` +openhab_actions: + pushover: + - { key: defaultToken, value: pushover_token } + - { key: defaultUser, value: pushover_user_token } +openhab_persistence: + rrd4j: + - { key: der5min.def, value: "DERIVE,900,0,U,300" } + - { key: der5min.archives, value: "AVERAGE,0.5,1,365:AVERAGE,0.5,7,300"} + - { key: der5min.items, value: "Speedtest*,LW_WifiL*"} +openhab_bindings: + milight: + - { key: "milight1.host", value: "192.168.1.102" } + - { key: "milight1.port", value: "8899" } + plex: + - { key: host, value: 192.168.1.104 } + - { key: port, value: 32400 } + - { key: refresh, value: 5000 } + lightwaverf: + - { key: ip, value: 192.168.1.103 } + - { key: receiveport, value: 9761 } + - { key: sendport, value: 9760 } + - { key: registeronstartup, value: true } + - { key: senddelay, value: 2000 } + - { key: okTimeout, value: 1000 } +``` + +Note that the format is: + +``` +openhab_: + : + - { key: key_name, value: value_of_variable } +``` + +### Sitemaps, rules, and other configuration files + +I've not found a way to automate this nicely at the present time, so for now, +create the files as you would if installing manually and place them in the +relevant directory under `files`. They will automatically be copied in to +place. + +## Dependencies + +Make sure that you have a role that installs Java. People appear to have +widely differing opinions on which JVM should be installed (Oracle/OpenJDK etc) +so I have left this choice up to you! + +## Example Playbook + +This playbook can be run in isolation, however, I would recommend that you +ensure your baseline play installs and configures a firewall and that you +enable the `openhab_use_firewall` variable to make your installation more +secure. + +To run the role, create a play with the following content: + + - hosts: openhab_servers + roles: + - { role: } + - { role: openhab } + +License +------- + +MIT + +Author Information +------------------ + +Matthew Macdonald-Wallace +http://doics.co +http://hackerdads.wordpress.com diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..94c7f1c --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,32 @@ +--- +# defaults file for openhab +openhab_security: OFF +general_net_mask: "{{ ansible_default_ipv4.network }}/{{ ansible_default_ipv4.netmask }}" +openhab_netmask: "{{ general_net_mask | ipaddr('net') }}" +openhab_default_persistence: rrd4j +openhab_configuration_refresh: 5 +openhab_service_discovery_bind_address: "{{ ansible_default_ipv4['address'] }}" +openhab_chart_provider: rrd4j +openhab_chart_height: 240 +openhab_chart_width: 480 +openhab_chart_scale: 1 + +## Configure your Actions here +openhab_actions: + mail: + - { key: hostname, value: localhost } + - { key: port, value: 25 } + +## Configure Persistence here +openhab_persistence: + rrd4j: + - { key: der5min.def, value: 'DERIVE,900,0,U,300' } + - { key: der5min.archives, value: 'AVERAGE,0.5,1,365:AVERAGE,0.5,7,300' } + - { key: der5min.items, value: 'Demo*' } + logging: + - {key: pattern, value: '%date{ISO8601} - %-25logger: %msg%n' } + +## Configure your Bindings here +openhab_bindings: + bluetooth: + - { key: refresh, value: 20 } diff --git a/files/items/README.md b/files/items/README.md new file mode 100644 index 0000000..ac89195 --- /dev/null +++ b/files/items/README.md @@ -0,0 +1,2 @@ +# items +Put your OpenHAB items configurations in this directory diff --git a/files/persistence/README.md b/files/persistence/README.md new file mode 100644 index 0000000..857c8f9 --- /dev/null +++ b/files/persistence/README.md @@ -0,0 +1,2 @@ +# persistence +Put your OpenHAB persistence configurations in this directory diff --git a/files/rules/README.md b/files/rules/README.md new file mode 100644 index 0000000..57ba316 --- /dev/null +++ b/files/rules/README.md @@ -0,0 +1,2 @@ +# rules +Put your OpenHAB rules configurations in this directory diff --git a/files/sitemaps/README.md b/files/sitemaps/README.md new file mode 100644 index 0000000..4097180 --- /dev/null +++ b/files/sitemaps/README.md @@ -0,0 +1,2 @@ +# sitemaps +Put your OpenHAB sitemaps configurations in this directory diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..93d25c5 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,6 @@ +--- +# handlers file for openhab +- name: Restart OpenHAB + service: + name: openhab + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..d745a9c --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,15 @@ +--- +galaxy_info: + author: Matthew Macdonald-Wallae + description: Install the OpenHAB Home Automation Suite + company: + license: MIT + min_ansible_version: 1.9 + platforms: + - name: Ubuntu + versions: + - all + - name: Debian + versions: + - all +dependencies: [] diff --git a/tasks/debian.yml b/tasks/debian.yml new file mode 100644 index 0000000..3738d2d --- /dev/null +++ b/tasks/debian.yml @@ -0,0 +1,41 @@ +--- +# Debian specfic tasks for OpenHAB +- name: Add APT Key + apt_key: + url: "https://bintray.com/user/downloadSubjectPublicKey?username=openhab" + state: present + +- name: Add the APT Repo + apt_repository: + repo: "deb http://dl.bintray.com/openhab/apt-repo stable main" + state: present + +- name: Install OpenHAB + apt: + name: openhab-runtime + state: present + update_cache: yes + +- name: Install OpenHAB Actions + apt: + name: "openhab-addon-action-{{ item.key }}" + state: present + update_cache: yes + with_dict: "{{ openhab_actions }}" + ignore_errors: True + +- name: Install OpenHAB Bindings + apt: + name: "openhab-addon-binding-{{ item.key }}" + state: present + update_cache: yes + with_dict: "{{ openhab_bindings }}" + ignore_errors: True + +- name: Install OpenHAB Persistence + apt: + name: "openhab-addon-persistence-{{ item.key }}" + state: present + update_cache: yes + with_dict: "{{ openhab_persistence }}" + ignore_errors: True diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..21dae4d --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,59 @@ +--- +# tasks file for openhab +# include the distro-specific stuff +- name: Include the Debian config + include: debian.yml + when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' + +- name: Use the variables to build out the configuration file + template: + src: openhab.cfg.j2 + dest: /etc/openhab/configurations/openhab.cfg + owner: openhab + group: openhab + +- name: Copy in the items files + copy: + src: "{{ item }}" + dest: "/etc/openhab/configurations/items/" + owner: openhab + group: openhab + mode: 0644 + with_fileglob: + - items/* + +- name: Copy in the Sitemaps + copy: + src: "{{ item }}" + dest: "/etc/openhab/configurations/sitemaps/" + owner: openhab + group: openhab + mode: 0644 + with_fileglob: + - sitemaps/* + +- name: Copy in the Persistence Config + copy: + src: "{{ item }}" + dest: "/etc/openhab/configurations/persistence/" + owner: openhab + group: openhab + mode: 0644 + with_fileglob: + - persistence/* + +- name: Copy in the Rules + copy: + src: "{{ item }}" + dest: "/etc/openhab/configurations/rules/" + owner: openhab + group: openhab + mode: 0644 + with_fileglob: + - rules/* + +- name: Enable and start the OpenHAB service + service: + name: openhab + state: running + enabled: True diff --git a/tasks/rhel.yml b/tasks/rhel.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/tasks/rhel.yml @@ -0,0 +1 @@ +--- diff --git a/templates/openhab.cfg.j2 b/templates/openhab.cfg.j2 new file mode 100644 index 0000000..9462c94 --- /dev/null +++ b/templates/openhab.cfg.j2 @@ -0,0 +1,49 @@ +### +# {{ ansible_managed }} +### + +## General configurations +folder:items=10,items +folder:sitemaps=10,sitemap +folder:rules=10,rules +folder:scripts=10,script +folder:persistence=10,persist + +security:option={{ openhab_security | default("OFF") }} +security:netmask={{ openhab_netmask | default("192.168.1.0/24") }} +persistence:default={{ openhab_default_persistence | default("rrd4j") }} +mainconfig:refresh={{ openhab_configuration_refresh | default("-1") }} + +servicediscovery:bind_address={{ openhab_service_discovery_bind_address | default(ansible_default_ipv4['address']) }} + +################################## Chart Servlet ###################################### +chart:provider={{ openhab_chart_provider | default("rrd4j") }} +chart:defaultHeight={{ openhab_chart_height | default("240") }} +chart:defaultWidth={{ openhab_chart_width | default("480") }} +chart:scale={{ openhab_chart_scale | default("1") }} + + +##### Action configurations + +{% for action in openhab_actions %} +########### {{ action }} ################## +{% for acfg_item in openhab_actions[action] %} +{{ action }}:{{ acfg_item['key'] }}={{ acfg_item['value'] }} +{% endfor %} +{% endfor %} + +##### Persistence configurations +{% for persist in openhab_persistence %} +########### {{ persist }} ################## +{% for pcfg_item in openhab_persistence[persist] %} +{{ persist }}:{{ pcfg_item['key'] }}={{ pcfg_item['value'] }} +{% endfor %} +{% endfor %} + +##### Binding configurations +{% for binding in openhab_bindings %} +########### {{ binding }} ################## +{% for bcfg_item in openhab_bindings[binding] %} +{{ binding }}:{{ bcfg_item['key'] }}={{ bcfg_item['value'] }} +{% endfor %} +{% endfor %} diff --git a/test/integration/openhab/default.yml b/test/integration/openhab/default.yml new file mode 100644 index 0000000..cbaad71 --- /dev/null +++ b/test/integration/openhab/default.yml @@ -0,0 +1,8 @@ +# +# Copyright (c) 2015 by DevOpsGuys. All rights reserved. +# +--- +- hosts: all + become: true + roles: + - openhab diff --git a/test/integration/openhab/serverspec/Gemfile b/test/integration/openhab/serverspec/Gemfile new file mode 100644 index 0000000..3fb1fb2 --- /dev/null +++ b/test/integration/openhab/serverspec/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org/' +gem 'rake' +gem 'busser' diff --git a/test/integration/openhab/serverspec/openhab_spec.rb b/test/integration/openhab/serverspec/openhab_spec.rb new file mode 100644 index 0000000..39ec80f --- /dev/null +++ b/test/integration/openhab/serverspec/openhab_spec.rb @@ -0,0 +1,20 @@ +require 'serverspec' + +# Required by serverspec +set :backend, :exec + +describe package('openhab') do + it { should be_disabled } +end + +describe service('openhab') do + it { should be_enabled } + it { should be_running } +end + +describe port(80) do + it { should be_listening } +end +describe port(443) do + it { should be_listening } +end diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..d36f276 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for openhab