first commit

This commit is contained in:
Robert Kaussow 2019-04-27 13:52:50 +02:00
commit 498dc47310
16 changed files with 556 additions and 0 deletions

110
.drone.jsonnet Normal file
View File

@ -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 }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> 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,
]

116
.drone.yml Normal file
View File

@ -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 }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}"
username:
from_secret: matrix_username
trigger:
ref:
- refs/heads/master
- "refs/tags/**"
status:
- success
- failure
depends_on:
- deployment
...

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# ---> Ansible
*.retry

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
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.

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# homeassistant

44
defaults/main.yml Normal file
View File

@ -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 }}"

9
handlers/main.yml Normal file
View File

@ -0,0 +1,9 @@
---
- name: Restart homeassistant service
systemd:
state: restarted
daemon_reload: yes
name: homeassistant
listen: __homeassistant_restart
become: True
become_user: root

17
meta/main.yml Normal file
View File

@ -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:

56
tasks/install.yml Normal file
View File

@ -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

9
tasks/main.yml Normal file
View File

@ -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

48
tasks/nginx.yml Normal file
View File

@ -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

9
tasks/post_tasks.yml Normal file
View File

@ -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

24
tasks/prepare.yml Normal file
View File

@ -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

37
tasks/tls.yml Normal file
View File

@ -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 }}"

View File

@ -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

48
templates/nginx/vhost.j2 Normal file
View File

@ -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 %}