commit 498dc473108d76c579e40f8f38a931116f8fcdbd Author: Robert Kaussow Date: Sat Apr 27 13:52:50 2019 +0200 first commit diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..87e0641 --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,110 @@ +local AnsibleVersions(version="latest", package="ansible") = { + name: "ansible-" + version, + image: "python:3.7", + pull: "always", + environment: { + PY_COLORS: 1 + }, + commands: [ + "pip install " + package + " ansible-later -qq", + "git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy", + "git ls-files *[^LICENSE,.md,molecule,.ini] | xargs ansible-later -c ~/policy/config.ini" + ], + depends_on: [ + "clone", + ], +}; + +local PipelineLinting = { + kind: "pipeline", + name: "linting", + platform: { + os: "linux", + arch: "amd64", + }, + steps: [ + AnsibleVersions(version="latest", package="ansible"), + AnsibleVersions(version="master", package="git+https://github.com/ansible/ansible.git@devel"), + ], + trigger: { + ref: ["refs/heads/master", "refs/tags/**", "refs/pull/**"], + }, +}; + +local PipelineDeployment = { + kind: "pipeline", + name: "deployment", + platform: { + os: "linux", + arch: "amd64", + }, + workspace: { + base: "/drone/src", + path: "xoxys.nginx" + }, + steps: [ + { + name: "molecule", + image: "xoxys/molecule:gce-linux-amd64", + pull: "always", + environment: { + GCE_SSH_KEY: { "from_secret": "gce_ssh_key" }, + GCE_SERVICE_ACCOUNT_EMAIL: { "from_secret": "gce_service_account_email" }, + GCE_PROJECT_ID: { "from_secret": "gce_project_id" }, + GCE_CREDENTIALS_JSON: { "from_secret": "gce_credentials_json" }, + GCE_SSH_USER: { "from_secret": "gce_ssh_user" }, + GCE_CREDENTIALS_FILE: "/root/ansible-testing.json", + MOLECULE_CUSTOM_MODULES_REPO: "https://gitea.rknet.org/ansible/custom_modules", + PY_COLORS: 1 + }, + commands: [ + "/bin/bash /docker-entrypoint.sh", + "molecule create --scenario-name gce-centos-7", + "molecule converge --scenario-name gce-centos-7", + "molecule verify --scenario-name gce-centos-7", + "molecule destroy --scenario-name gce-centos-7", + ], + }, + ], + depends_on: [ + "linting", + ], + trigger: { + ref: ["refs/heads/master", "refs/tags/**"], + }, +}; + +local PipelineNotifications = { + kind: "pipeline", + name: "notifications", + platform: { + os: "linux", + arch: "amd64", + }, + steps: [ + { + name: "matrix", + image: "plugins/matrix", + settings: { + homeserver: "https://matrix.rknet.org", + roomid: "MtidqQXWWAtQcByBhH:rknet.org", + template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}", + username: { "from_secret": "matrix_username" }, + password: { "from_secret": "matrix_password" }, + }, + }, + ], + depends_on: [ + "deployment", + ], + trigger: { + status: [ "success", "failure" ], + ref: ["refs/heads/master", "refs/tags/**"], + }, +}; + +[ + PipelineLinting, + PipelineDeployment, + PipelineNotifications, +] diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..097555f --- /dev/null +++ b/.drone.yml @@ -0,0 +1,116 @@ +--- +kind: pipeline +name: linting + +platform: + os: linux + arch: amd64 + +steps: +- name: ansible-latest + pull: always + image: python:3.7 + commands: + - pip install ansible ansible-later -qq + - git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy + - "git ls-files *[^LICENSE,.md,molecule,.ini] | xargs ansible-later -c ~/policy/config.ini" + environment: + PY_COLORS: 1 + depends_on: + - clone + +- name: ansible-master + pull: always + image: python:3.7 + commands: + - "pip install git+https://github.com/ansible/ansible.git@devel ansible-later -qq" + - git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy + - "git ls-files *[^LICENSE,.md,molecule,.ini] | xargs ansible-later -c ~/policy/config.ini" + environment: + PY_COLORS: 1 + depends_on: + - clone + +trigger: + ref: + - refs/heads/master + - "refs/tags/**" + - "refs/pull/**" + +--- +kind: pipeline +name: deployment + +platform: + os: linux + arch: amd64 + +workspace: + base: /drone/src + path: xoxys.nginx + +steps: +- name: molecule + pull: always + image: xoxys/molecule:gce-linux-amd64 + commands: + - /bin/bash /docker-entrypoint.sh + - molecule create --scenario-name gce-centos-7 + - molecule converge --scenario-name gce-centos-7 + - molecule verify --scenario-name gce-centos-7 + - molecule destroy --scenario-name gce-centos-7 + environment: + GCE_CREDENTIALS_FILE: /root/ansible-testing.json + GCE_CREDENTIALS_JSON: + from_secret: gce_credentials_json + GCE_PROJECT_ID: + from_secret: gce_project_id + GCE_SERVICE_ACCOUNT_EMAIL: + from_secret: gce_service_account_email + GCE_SSH_KEY: + from_secret: gce_ssh_key + GCE_SSH_USER: + from_secret: gce_ssh_user + MOLECULE_CUSTOM_MODULES_REPO: https://gitea.rknet.org/ansible/custom_modules + PY_COLORS: 1 + +trigger: + ref: + - refs/heads/master + - "refs/tags/**" + +depends_on: +- linting + +--- +kind: pipeline +name: notifications + +platform: + os: linux + arch: amd64 + +steps: +- name: matrix + image: plugins/matrix + settings: + homeserver: https://matrix.rknet.org + password: + from_secret: matrix_password + roomid: MtidqQXWWAtQcByBhH:rknet.org + template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}" + username: + from_secret: matrix_username + +trigger: + ref: + - refs/heads/master + - "refs/tags/**" + status: + - success + - failure + +depends_on: +- deployment + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c199eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# ---> Ansible +*.retry + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3f28a5f --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc72dc6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# homeassistant + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..8a590d8 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,44 @@ +--- +homeassistant_version: 0.92.1 + +homeassistant_user: homeassistant +homeassistant_user_home: "/home/{{ homeassistant_user }}" +# homeassistant_uid: # defaults to not set +homeassistant_group: "{{ homeassistant_user }}" +# homeassistant_gid: # defaults to not set +homeassistant_extra_groups: + - tty + - dialout + +homeassistant_base_dir: /opt/homeassistant +homeassistant_conf_dir: "{{ homeassistant_base_dir }}/config" + +homeassistant_dependencies: [] + +homeassistant_http_bind_port: 8123 +homeassistant_client_url: https://hassio.example.com + +homeassistant_tls_enabled: False +homeassistant_tls_dhparam_path: "{{ homeassistant_base_dir }}/tls/dhparam.pem" +homeassistant_tls_dhparam_size: 2048 +homeassistant_tls_cert_path: "{{ homeassistant_base_dir }}/tls/certs/mycert.pem" +homeassistant_tls_key_path: "{{ homeassistant_base_dir }}/tls/private/mykey.pem" +homeassistant_tls_cert_source: mycert.pem +homeassistant_tls_key_source: mykey.pem + +homeassistant_iptables_enabled: False +homeassistant_open_ports: + - name: allow_homeassistant_web + rules: | + -A INPUT -m state --state NEW -p tcp --dport {{ homeassistant_http_bind_port }} -j ACCEPT + state: present + +homeassistant_nginx_vhost_enabled: False +homeassistant_nginx_server: localhost +homeassistant_nginx_vhost_dir: /etc/nginx/sites-available +homeassistant_nginx_vhost_symlink: /etc/nginx/sites-enabled +homeassistant_nginx_iptables_enabled: False +homeassistant_nginx_tls_enabled: False +homeassistant_nginx_tls_cert_file: homeassistant-cert.pem +homeassistant_nginx_tls_key_file: homeassistant-key.pem +homeassistant_nginx_proxy_url: "https://1.2.3.4:{{ homeassistant_http_bind_port }}" diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..a977605 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,9 @@ +--- +- name: Restart homeassistant service + systemd: + state: restarted + daemon_reload: yes + name: homeassistant + listen: __homeassistant_restart + become: True + become_user: root diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..ebb00cd --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,17 @@ +# Standards: 0.1 +--- +galaxy_info: + author: xoxys + description: Role to setup homeassistant with Python venv + license: MIT + min_ansible_version: 2.4 + platforms: + - name: EL + versions: + - 7 + galaxy_tags: + - homeassistant + - hassio + - iot + - smart home +dependencies: diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..950c2b2 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,56 @@ +--- +- name: Prepare base folders + file: + path: "{{ item }}" + state: directory + owner: "{{ homeassistant_user }}" + group: "{{ homeassistant_user }}" + mode: 0750 + loop: + - "{{ homeassistant_base_dir }}" + - "{{ homeassistant_conf_dir }}" + become: True + become_user: root + +- block: + - name: Upgrade python dependencies + pip: + name: "{{ item }}" + virtualenv: "{{ homeassistant_base_dir }}/env" + virtualenv_command: /usr/bin/python3 -m venv + extra_args: --upgrade + loop: + - pip + - setuptools + - wheel + + - name: Install with pip and virtualenv + pip: + name: homeassistant + version: "{{ homeassistant_version }}" + virtualenv: "{{ homeassistant_base_dir }}/env" + virtualenv_command: /usr/bin/python3 -m venv + notify: __homeassistant_restart + become: True + become_user: "{{ homeassistant_user }}" + +- block: + - name: Copy systemd unit file + template: + src: "etc/systemd/system/homeassistant.service.j2" + dest: "/etc/systemd/system/homeassistant.service" + notify: __homeassistant_restart + + - name: Open ports in iptables + iptables_raw: + name: "{{ item.name }}" + rules: "{{ item.rules }}" + state: "{{ item.state }}" + weight: "{{ item.weight | default(omit) }}" + table: "{{ item.table | default(omit) }}" + loop: "{{ homeassistant_open_ports }}" + loop_control: + label: "{{ item.name }}" + when: homeassistant_iptables_enabled + become: True + become_user: root diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..afe66d2 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,9 @@ +--- +- import_tasks: prepare.yml +- import_tasks: install.yml +- import_tasks: tls.yml + when: homeassistant_tls_enabled + tags: tls_renewal +- import_tasks: nginx.yml + when: homeassistant_nginx_vhost_enabled +- import_tasks: post_tasks.yml diff --git a/tasks/nginx.yml b/tasks/nginx.yml new file mode 100644 index 0000000..bb978ac --- /dev/null +++ b/tasks/nginx.yml @@ -0,0 +1,48 @@ +--- +- block: + - name: Copy certs and private key to nginx proxy + copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode }}" + loop: + - { src: "{{ homeassistant_tls_key_source }}", dest: '/etc/pki/tls/private/{{ homeassistant_nginx_tls_key_file }}', mode: '0600' } + - { src: "{{ homeassistant_tls_cert_source }}", dest: '/etc/pki/tls/certs/{{ homeassistant_nginx_tls_cert_file }}', mode: '0750' } + loop_control: + label: "{{ item.dest }}" + notify: __nginx_reload + delegate_to: "{{ homeassistant_nginx_server }}" + when: homeassistant_nginx_tls_enabled + become: True + become_user: root + tags: tls_renewal + +- block: + - name: Add vhost configuration file + template: + src: nginx/vhost.j2 + dest: "{{ homeassistant_nginx_vhost_dir }}/homeassistant" + owner: root + group: root + mode: 0640 + notify: __nginx_reload + + - name: Enable homeassistant vhost + file: + src: "{{ homeassistant_nginx_vhost_dir }}/homeassistant" + dest: "{{ homeassistant_nginx_vhost_symlink }}/homeassistant" + owner: root + group: root + state: link + notify: __nginx_reload + when: homeassistant_nginx_vhost_symlink is defined + + - name: Open ports in iptables + iptables_raw: + name: allow_homeassistant_nginx_proxy + state: present + rules: "-A OUTPUT -m state --state NEW -p tcp -d {{ homeassistant_nginx_proxy_url | urlsplit('hostname') }} --dport {{ homeassistant_nginx_proxy_url | urlsplit('port') }} -j ACCEPT" + when: homeassistant_nginx_iptables_enabled and (not homeassistant_nginx_server == inventory_hostname or not homeassistant_nginx_server == "localhost") + delegate_to: "{{ homeassistant_nginx_server }}" + become: True + become_user: root diff --git a/tasks/post_tasks.yml b/tasks/post_tasks.yml new file mode 100644 index 0000000..b21bfe0 --- /dev/null +++ b/tasks/post_tasks.yml @@ -0,0 +1,9 @@ +--- +- name: Ensure homeassistant service is up and running + systemd: + state: started + daemon_reload: yes + enabled: yes + name: homeassistant + become: True + become_user: root diff --git a/tasks/prepare.yml b/tasks/prepare.yml new file mode 100644 index 0000000..06a6e34 --- /dev/null +++ b/tasks/prepare.yml @@ -0,0 +1,24 @@ +--- +- block: + - name: Create group '{{ homeassistant_group }}' + group: + name: "{{ homeassistant_group }}" + state: present + gid: "{{ homeassistant_gid | default(omit) }}" + + - name: Create user '{{ homeassistant_user }}' + user: + comment: homeassistant + name: "{{ homeassistant_user }}" + home: "{{ homeassistant_user_home }}" + uid: "{{ homeassistant_uid | default(omit) }}" + group: "{{ homeassistant_group }}" + groups: "{{ homeassistant_extra_groups | join(',') }}" + + - name: Install dependencies + package: + name: "{{ item }}" + state: present + loop: "{{ homeassistant_dependencies }}" + become: True + become_user: root diff --git a/tasks/tls.yml b/tasks/tls.yml new file mode 100644 index 0000000..3244330 --- /dev/null +++ b/tasks/tls.yml @@ -0,0 +1,37 @@ +--- +- block: + - name: Create tls folder structure + file: + path: "{{ item }}" + state: directory + owner: "{{ homeassistant_user }}" + group: "{{ homeassistant_group }}" + recurse: True + loop: + - "{{ homeassistant_tls_dhparam_path | dirname }}" + - "{{ homeassistant_tls_cert_path | dirname }}" + - "{{ homeassistant_tls_key_path | dirname }}" + become: True + become_user: root + +- block: + - name: Copy certs and private key + copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode }}" + loop: + - { src: "{{ homeassistant_tls_key_source }}", dest: '{{ homeassistant_tls_key_path }}', mode: '0600' } + - { src: "{{ homeassistant_tls_cert_source }}", dest: '{{ homeassistant_tls_cert_path }}', mode: '0650' } + loop_control: + label: "{{ item.dest }}" + notify: __homeassistant_restart + + - name: Create Diffie-Hellman Parameter + openssl_dhparam: + path: "{{ homeassistant_tls_dhparam_path }}" + size: "{{ homeassistant_tls_dhparam_size }}" + when: homeassistant_tls_dhparam_path is defined + notify: __homeassistant_restart + become: True + become_user: "{{ homeassistant_user }}" diff --git a/templates/etc/systemd/system/homeassistant.service.j2 b/templates/etc/systemd/system/homeassistant.service.j2 new file mode 100644 index 0000000..070fbcf --- /dev/null +++ b/templates/etc/systemd/system/homeassistant.service.j2 @@ -0,0 +1,15 @@ +#jinja2: lstrip_blocks: True +# {{ ansible_managed }} +[Unit] +Description=Home Assistant +After=network-online.target + +[Service] +Type=simple +User=homeassistant +ExecStart={{ homeassistant_base_dir }}/env/bin/hass -c {{ homeassistant_conf_dir }} +Restart=on-failure +RestartSec=5s + +[Install] +WantedBy=multi-user.targe diff --git a/templates/nginx/vhost.j2 b/templates/nginx/vhost.j2 new file mode 100644 index 0000000..6b9dd1b --- /dev/null +++ b/templates/nginx/vhost.j2 @@ -0,0 +1,48 @@ +#jinja2: lstrip_blocks: True +# {{ ansible_managed }} +upstream backend_homeassistant { + server {{ homeassistant_nginx_proxy_url | urlsplit('hostname') }}:{{ homeassistant_nginx_proxy_url | urlsplit('port') }}; +} + +server { + listen 80; + server_name {{ homeassistant_client_url | urlsplit('hostname') }}; + + client_max_body_size 200M; + + {% if homeassistant_nginx_tls_enabled %} + return 301 https://$server_name$request_uri; + {% else %} + location / { + proxy_pass {{ homeassistant_nginx_proxy_url | urlsplit('scheme') }}://backend_homeassistant; + proxy_set_header Host $host; + proxy_redirect http:// https://; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + } + {% endif %} +} + +{% if homeassistant_nginx_tls_enabled %} +server { + listen 443 ssl; + server_name {{ homeassistant_client_url | urlsplit('hostname') }}; + + client_max_body_size 200M; + + location / { + proxy_pass {{ homeassistant_nginx_proxy_url | urlsplit('scheme') }}://backend_homeassistant; + proxy_set_header Host $host; + proxy_redirect http:// https://; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + } + + ssl_certificate /etc/pki/tls/certs/{{ homeassistant_nginx_tls_cert_file }}; + ssl_certificate_key /etc/pki/tls/private/{{ homeassistant_nginx_tls_key_file }}; +} +{% endif %}