commit 2fb4d4dae91ed83df1346775bc275e3c3342cd5f
parent edc824f8b81ba88ae3424dbe720453ff195af270
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 3 Feb 2021 18:19:32 +0100
parent edc824f8b81ba88ae3424dbe720453ff195af270
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 3 Feb 2021 18:19:32 +0100
smarthome: add role
8 files changed, 240 insertions(+), 0 deletions(-)
A
|
63
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/roles/smarthome/tasks/checks.yml b/roles/smarthome/tasks/checks.yml @@ -0,0 +1,43 @@ +--- + +- fail: msg="This role currently only supports ArchLinux!" + when: + - ansible_distribution != "Archlinux" + +- fail: msg="This Role only works when Option 'system.enableOwnRepos' is true!" + when: + - system.enableOwnRepos is false + + +- fail: msg="Option 'services.smartied.configFile' has to be set!" + when: + - services.smartied.configFile is not defined + + +- fail: msg="Option 'services.smartied.nginx.domain' has to be set when using nginx!" + when: + - services.smartied.nginx.enable is defined + - services.smartied.nginx.enable is true + - services.smartied.nginx.domain is not defined + +- fail: msg="Option 'services.smartied.nginx.sslOnly' has to be set when using nginx!" + when: + - services.smartied.nginx.enable is defined + - services.smartied.nginx.enable is true + - services.smartied.nginx.sslOnly is not defined + +- fail: msg="Option 'services.smartied.nginx.ssl.cert' has to be set when using nginx with ssl!" + when: + - services.smartied.nginx.enable is defined + - services.smartied.nginx.enable is true + - services.smartied.nginx.ssl.enable is defined + - services.smartied.nginx.ssl.enable is true + - services.smartied.nginx.ssl.cert is not defined + +- fail: msg="Option 'services.smartied.nginx.ssl.privkey' has to be set when using nginx with ssl!" + when: + - services.smartied.nginx.enable is defined + - services.smartied.nginx.enable is true + - services.smartied.nginx.ssl.enable is defined + - services.smartied.nginx.ssl.enable is true + - services.smartied.nginx.ssl.privkey is not defined
diff --git a/roles/smarthome/tasks/configure.yml b/roles/smarthome/tasks/configure.yml @@ -0,0 +1,7 @@ +--- + +- name: "Copy config-file to: /etc/smartied.json" + copy: + src: "{{ services.smartied.configFile }}" + dest: /etc/smartied.json + mode: 0755
diff --git a/roles/smarthome/tasks/install.yml b/roles/smarthome/tasks/install.yml @@ -0,0 +1,13 @@ +--- + +- name: "[Archlinux] Install packages: smartied smartie-pwa" + pacman: + name: + - smartied + - smartie-pwa + state: present + update_cache: yes + when: + - ansible_distribution == "Archlinux" + +-+ \ No newline at end of file
diff --git a/roles/smarthome/tasks/main.yml b/roles/smarthome/tasks/main.yml @@ -0,0 +1,38 @@ +--- + +- include: checks.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is true + +- include: install.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is true + +- include: configure.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is true + +- include: start.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is true + +- include: nginx.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is true + - services.smartied.nginx is defined + - services.smartied.nginx.enable is true + +- include: remove.yml + when: + - services.smartied.enable is defined + - services.smartied.enable is false + + + + +
diff --git a/roles/smarthome/tasks/nginx.yml b/roles/smarthome/tasks/nginx.yml @@ -0,0 +1,23 @@ +--- + +- name: "[nginx] Create vhost" + template: + src: nginx-vhost.conf.j2 + dest: /etc/nginx/conf.d/smartied.conf + mode: 0644 + owner: nginx + group: nginx + +- name: "[OpenRC] Restart service: nginx" + service: + name: nginx + state: restarted + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Restart service: nginx" + systemd: + name: nginx + state: restarted + when: + - ansible_service_mgr == "systemd"
diff --git a/roles/smarthome/tasks/remove.yml b/roles/smarthome/tasks/remove.yml @@ -0,0 +1,34 @@ +--- + +- name: "[OpenRC] Disable and stop service: smartied" + service: + name: smartied + enabled: no + state: stopped + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Disable and stop service: smartied" + systemd: + name: smartied + enabled: no + state: stopped + when: + - ansible_service_mgr == "systemd" + +- name: "[Archlinux] Remove packages: smartied smartie-pwa" + pacman: + name: + - smartied + - smartie-pwa + state: absent + when: + - ansible_distribution == "Archlinux" + +- name: "Delete leftovers" + file: + path: "{{item}}" + state: absent + with_items: + - /etc/nginx/conf.d/smartied.conf + - /etc/smartied.json
diff --git a/roles/smarthome/tasks/start.yml b/roles/smarthome/tasks/start.yml @@ -0,0 +1,17 @@ +--- + +- name: "[OpenRC] Restart and enable service: smartied" + service: + name: smartied + enabled: yes + state: restarted + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Restart and enable service: smartied" + systemd: + name: smartied + enabled: yes + state: restarted + when: + - ansible_service_mgr == "systemd"+ \ No newline at end of file
diff --git a/roles/smarthome/templates/nginx-vhost.conf.j2 b/roles/smarthome/templates/nginx-vhost.conf.j2 @@ -0,0 +1,63 @@ +# +# !!! This file is managed by Ansible !!! +# + +{% if services.smartied.nginx.sslOnly is not defined or services.smartied.nginx.sslOnly is false %} +server { + listen 80 ; + listen [::]:80; + + server_name {{ services.smartied.nginx.domain }}; + + location / { + root /usr/share/webapps/smartie-pwa; + } + + location /ws { + proxy_pass http://127.0.0.1:5002/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + location /smarthome-exporter { + proxy_pass http://127.0.0.1:5003/metrics; + } + + location /archive { + alias /var/lib/powermeter-archive; + } +} + +{% endif %} +{% if services.smartied.nginx.ssl.enable is true %} +server { + listen 443 ssl; + listen [::]:443 ssl; + + ssl_certificate "{{ services.smartied.nginx.ssl.cert }}"; + ssl_certificate_key "{{ services.smartied.nginx.ssl.privkey }}"; + include /etc/nginx/ssl.conf; + + server_name {{ services.smartied.nginx.domain }}; + + location / { + root /usr/share/webapps/smartie-pwa; + } + + location /ws { + proxy_pass http://127.0.0.1:5002/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + location /smarthome-exporter { + proxy_pass http://127.0.0.1:5003/metrics; + } + + location /archive { + alias /var/lib/powermeter-archive; + } +} +{% endif %}