From 247568a58c0370b8cfae994c336901ffa3f82bdb Mon Sep 17 00:00:00 2001
From: Karol Hennessy <revkarol@gmail.com>
Date: Wed, 27 May 2020 18:43:30 +0200
Subject: [PATCH] ansible updates for apache, hevserver, hev-display

---
 ansible/playbooks/install_software.yml | 70 ++++++++++++++++++++++++--
 ansible/playbooks/lists.yml            |  5 +-
 ansible/playbooks/refresh_software.yml | 22 ++++++--
 ansible/playbooks/setup_apache.sh      | 10 ++++
 utils/hev.rules                        |  3 ++
 5 files changed, 101 insertions(+), 9 deletions(-)
 create mode 100644 ansible/playbooks/setup_apache.sh

diff --git a/ansible/playbooks/install_software.yml b/ansible/playbooks/install_software.yml
index 82aaf626..63a0fab1 100644
--- a/ansible/playbooks/install_software.yml
+++ b/ansible/playbooks/install_software.yml
@@ -1,5 +1,5 @@
 ---
-- hosts: hevpi
+- hosts: nuliv
   remote_user: pi
   vars:
         download_dir: /home/pi/Downloads
@@ -33,6 +33,9 @@
         
         - name: copy hev-display script
           template: src=hev-display.sh dest={{ download_dir }} owner=pi group=pi mode=0755
+
+        - name: copy apache script
+          template: src=setup_apache.sh dest={{ download_dir }} owner=pi group=pi mode=0755
         
         - name: install hev-display
           command: "{{ download_dir }}/hev-display.sh"
@@ -65,11 +68,25 @@
               executable: /usr/bin/pip3
               name: "{{ pip_list }}"  
         
-        - name: install hev-display via apt
+        - name: check pi version
+          shell: "pinout | grep 'BCM2711'"
+          register: pi4
+          ignore_errors: True
+
+        - name: install hev-display-pi4 via apt
+          apt: 
+              name: hev-display-pi4
+              state: present
+              update_cache: yes
+          when: pi4 is succeeded
+          become: yes
+
+        - name: install hev-display-pi1 via apt
           apt: 
-              name: hev-display
+              name: hev-display-pi1
               state: present
               update_cache: yes
+          when: pi4 is failed
           become: yes
         
         - name: link fonts for hev-display
@@ -79,7 +96,6 @@
               state: link
           become: yes
         
-       
         - name: install hev-display systemd service
           copy:
               src: "{{ ansible_env.HOME }}/hev-display/extras/systemd/hev-display.service"
@@ -87,6 +103,27 @@
               remote_src: yes
           become: yes
 
+        - name: mk user systemd dir
+          file:
+              path: "{{ ansible_env.HOME }}/.config/systemd/user/"
+              state: directory
+
+        - name: install hevserver systemd service
+          copy:
+              src: "{{ ansible_env.HOME }}/hev-sw/utils/hevserver.service"
+              dest: "{{ ansible_env.HOME }}/.config/systemd/user/hevserver.service"
+              remote_src: yes
+
+        - name: copy hev rules
+          copy: 
+              src: "{{ ansible_env.HOME }}/hev-sw/utils/hev.rules"
+              dest: "/etc/udev/rules.d/88-hev.rules"
+              remote_src: yes
+              owner: root 
+              group: root 
+              mode: 0644
+          become: yes
+
         #- name: enable systemd service
         #  systemd:
         #    enabled: yes
@@ -94,9 +131,34 @@
         #    name: hev-display
         #  become: yes
 
+        #- name: enable systemd service
+        #  systemd:
+        #    enabled: yes
+        #    daemon_reload: yes
+        #    name: hevserver
+        #    user: yes       
+
         - name: link libts
           file:
               src: "/usr/lib/arm-linux-gnueabihf/libts.so.0"
               dest: "/usr/lib/arm-linux-gnueabihf/libts-0.0.so.0"
               state: link
           become: yes
+
+        - name: Adding user pi to www-data
+          user: 
+              name: pi
+              groups: www-data
+              append: yes
+          become: yes
+
+        - name: install hevconf into apache 
+          copy:
+              src: "{{ ansible_env.HOME }}/hev-sw/raspberry-backend/share/hev.conf" 
+              dest: "/etc/apache2/sites-available/hev.conf"
+              remote_src: yes
+          become: yes
+
+        - name: setup apache
+          command: "{{ download_dir }}/setup_apache.sh"
+          become: yes
\ No newline at end of file
diff --git a/ansible/playbooks/lists.yml b/ansible/playbooks/lists.yml
index a8ef1193..2f6d4cfc 100644
--- a/ansible/playbooks/lists.yml
+++ b/ansible/playbooks/lists.yml
@@ -21,7 +21,10 @@ rpi_swlist:
   - libglib2.0-dev
   - socat
   - emacs
-
+  - apache2
+  - libapache2-mod-wsgi-py3 
+  - python3-pyqt5
+  - python3-pyqtgraph
 
 pip_list: 
   - libscrc
diff --git a/ansible/playbooks/refresh_software.yml b/ansible/playbooks/refresh_software.yml
index 5f80fc30..6fa3580d 100644
--- a/ansible/playbooks/refresh_software.yml
+++ b/ansible/playbooks/refresh_software.yml
@@ -1,5 +1,5 @@
 ---
-- hosts: hevpi1
+- hosts: nuliv 
   remote_user: pi
   vars:
         download_dir: /home/pi/Downloads
@@ -47,9 +47,23 @@
               replace: 'url =  https://gitlab.cern.ch/hev-sw/{{ item }}.git'
           loop: "{{ repos }}"
 
-        - name: upgrade hev-display via apt
+        - name: check pi version
+          shell: "pinout | grep 'BCM2711'"
+          register: pi4
+          ignore_errors: True
+
+        - name: install hev-display-pi4 via apt
           apt: 
-              name: hev-display
-              state: latest
+              name: hev-display-pi4
+              state: present
               update_cache: yes
+          when: pi4 is succeeded
           become: yes
+
+        - name: install hev-display-pi1 via apt
+          apt: 
+              name: hev-display-pi1
+              state: present
+              update_cache: yes
+          when: pi4 is failed
+          become: yes
\ No newline at end of file
diff --git a/ansible/playbooks/setup_apache.sh b/ansible/playbooks/setup_apache.sh
new file mode 100644
index 00000000..ce1fc85e
--- /dev/null
+++ b/ansible/playbooks/setup_apache.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+a2dismode wsgi
+a2enmode wsgi
+#update the locations in hev.conf if needed
+#disable default location
+a2dissite 000-default.conf
+#enable our hev site
+a2ensite hev.conf
+#reload apache
+sudo systemctl reload apache2
\ No newline at end of file
diff --git a/utils/hev.rules b/utils/hev.rules
index 73b3cf86..c0b001a8 100644
--- a/utils/hev.rules
+++ b/utils/hev.rules
@@ -1,4 +1,7 @@
 # Arduino uno
 ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0043", ACTION=="bind", SUBSYSTEM=="usb", DRIVERS=="cdc_acm", RUN+="/bin/su -c /home/pi/hev-sw/utils/hevcheck.sh"
 
+# NodeMCU 32s
+ATTRS{idVendor}=="10C4", ATTRS{idProduct}=="EA60", ACTION=="bind", SUBSYSTEM=="usb", DRIVERS=="cdc_acm", RUN+="/bin/su -c /home/pi/hev-sw/utils/hevcheck.sh"
+
 # Add other devices similarly. Find idVendor:idProduct from lsusb
-- 
GitLab