diff --git a/files/wittgenstein/reboot.sh b/files/wittgenstein/reboot.sh new file mode 100755 index 0000000..e463089 --- /dev/null +++ b/files/wittgenstein/reboot.sh @@ -0,0 +1,22 @@ +#!/bin/sh +PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games' + +echo 'switch-on.sh' +/home/pi/switch-on.sh + +echo 'start i2c-foo' +sudo modprobe i2c_dev +sudo modprobe i2c_bcm2708 + +# wait for network devices +sleep 30 + +cd /home/pi +echo 'start ampel controller' +tmux new-session -s ampel 'cd /home/pi/netz39_space_notification/raspberry/ledcontrol && ./ledcontrol' + +echo 'start lever controller' +tmux new-window -t ampel:1 'cd /home/pi/netz39_space_notification/raspberry/statusswitch && ./statusswitch' + +#echo 'start spaceapi controller' +#tmux new-window -t ampel:2 'cd /home/pi/netz39_space_notification/SpaceAPI && ./update-json.py --server=platon' diff --git a/files/wittgenstein/switch-off.sh b/files/wittgenstein/switch-off.sh new file mode 100755 index 0000000..40a081e --- /dev/null +++ b/files/wittgenstein/switch-off.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +gpio write 2 0 +gpio write 3 0 + +gpio mode 2 tri +gpio mode 3 tri diff --git a/files/wittgenstein/switch-on.sh b/files/wittgenstein/switch-on.sh new file mode 100755 index 0000000..aae9e2e --- /dev/null +++ b/files/wittgenstein/switch-on.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# INT +gpio mode 0 tri + +# Power +gpio mode 2 out +gpio mode 3 out + +gpio write 2 1 +gpio write 3 1 diff --git a/files/wittgenstein/unstuck.sh b/files/wittgenstein/unstuck.sh new file mode 100755 index 0000000..29da941 --- /dev/null +++ b/files/wittgenstein/unstuck.sh @@ -0,0 +1,7 @@ +#!/bin/bash +logger -t unstuck "unstuck $(date)" + +killall tmux + +sleep 1 +/home/pi/reboot.sh diff --git a/host-pottwal.yml b/host-pottwal.yml index 8d7ea6d..f2cff3e 100644 --- a/host-pottwal.yml +++ b/host-pottwal.yml @@ -634,6 +634,8 @@ site_name: "spaceapi.n39.eu" proxy_preserve_host: "On" notify: Restart apache2 + tags: + - spaceapi - name: Ensure renovate bot cronjob is present ansible.builtin.template: diff --git a/host-wittgenstein.yml b/host-wittgenstein.yml new file mode 100644 index 0000000..f4b2190 --- /dev/null +++ b/host-wittgenstein.yml @@ -0,0 +1,172 @@ +--- +- hosts: wittgenstein.n39.eu + become: true + + roles: + - role: docker_setup + vars: + docker_data_root: "/srv/docker" + - role: apache + - role: apache_letsencrypt # Uses configuration from dehydrated setup + - role: ansible-role-dehydrated + vars: + dehydrated_contact_email: "{{ server_admin }}" + - role: penguineer.dehydrated_cron + + tasks: + - name: Install packages needed for the system + # This is a list of all packages, + # unless they are installed by a specific role + ansible.builtin.apt: + state: present + name: + # This is needed for the user-executed tasks + - acl + # Regular packages + - tmux + - git-core + - cmake + - build-essential + - libmosquitto-dev + - libconfig-dev + - mosquitto-clients + - python3-paho-mqtt + - i2c-tools + + + - name: Set MAC address for proper DHCP recognition + # Uses mac variable from inventory + ansible.builtin.template: + src: templates/network-interfaces-dhcp-mac.j2 + dest: /etc/network/interfaces.d/wittgenstein-mac + owner: root + group: root + mode: '0644' + + - name: Disable IPv6 + # Because it is not working.... + ansible.builtin.copy: + src: files/sysctl-no-ipv6.conf + dest: /etc/sysctl.d/99-systcl-no-ipv6.conf + owner: root + group: root + mode: '0644' + + + ### Gatekeeper user (pi for now) + # + # All the gatekeeping / door control stuff is here! + + - name: Ensure gatekeeper user is there + ansible.builtin.user: + name: "{{ gatekeeper_user }}" + groups: dialout,audio,plugdev,input,netdev,i2c,gpio + append: yes + + - name: Copy management scripts + ansible.builtin.copy: + src: "files/wittgenstein/{{ item }}" + dest: "/home/{{ gatekeeper_user }}/{{ item }}" + owner: "{{ gatekeeper_user }}" + group: "{{ gatekeeper_user }}" + mode: "0750" + loop: + - reboot.sh + - unstuck.sh + - switch-on.sh + - switch-off.sh + + - name: Install start-up cron + ansible.builtin.cron: + name: Start the gatekeeper services + job: "/home/{{ gatekeeper_user }}/reboot.sh" + user: "{{ gatekeeper_user }}" + special_time: reboot + + + - name: Download wiringPi library + # WiringPi needs to be installed, but that library seems to be + # obsolete. We download something and hope it works... + ansible.builtin.get_url: + url: https://project-downloads.drogon.net/wiringpi-latest.deb + dest: "/home/{{ gatekeeper_user }}/wiringpi-latest.deb" + mode: "0644" + force: no + register: wiringPi_download + + - name: Install wiringPi library # noqa 503 + ansible.builtin.apt: + state: present + deb: "/home/{{ gatekeeper_user }}/wiringpi-latest.deb" + when: wiringPi_download.changed + + + ### Ampelsteuerung + - name: Clone netz39_space_notification initial checkout + # Do this as the gatekeeper user! + become: yes + become_user: "{{ gatekeeper_user }}" + ansible.builtin.git: + repo: https://github.com/netz39/space_notification.git + dest: "/home/{{ gatekeeper_user }}/netz39_space_notification" + clone: yes + update: no + + - name: Compile ledcontrol agent + # Do this as the gatekeeper user! + become: yes + become_user: "{{ gatekeeper_user }}" + ansible.builtin.shell: + chdir: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/ledcontrol" + cmd: make + creates: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/ledcontrol/ledcontrol" + + - name: Compile statusswitch agent + # Do this as the gatekeeper user! + become: yes + become_user: "{{ gatekeeper_user }}" + ansible.builtin.shell: + chdir: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/statusswitch" + cmd: make + creates: "/home/{{ gatekeeper_user }}/netz39_space_notification/raspberry/statusswitch/statusswitch" + + ### Space API + - name: Setup the SpaceAPI Docker container + docker_container: + name: spaceapi + image: "{{ spaceapi_image }}" + pull: true + state: started + detach: yes + restart_policy: unless-stopped + ports: + - "0.0.0.0:{{ spaceapi_host_port }}:8080" # Must be reached by pottwal +# - "127.0.0.1:{{ spaceapi_host_port }}:8080" + env: + TZ: "{{ timezone }}" + MQTT_BROKER: "platon.n39.eu" + MQTT_TOPIC_STATUS: "{{ spaceapi_topic_status }}" + MQTT_TOPIC_LASTCHANGE: "{{ spaceapi_topic_lastchange }}" + tags: + - spaceapi + + - name: Setup the Ampel Controller Docker container + docker_container: + name: ampelcontroller + image: "{{ ampelcontroller_image }}" + pull: true + state: started + detach: yes + restart_policy: unless-stopped + env: + TZ: "{{ timezone }}" + MQTT_BROKER: "platon.n39.eu" + MQTT_LEVER_STATE_TOPIC: "{{ topic_lever_state }}" + MQTT_DOOR_EVENTS_TOPIC: "{{ topic_door_events }}" + MQTT_SPACESTATUS_ISOPEN_TOPIC: "{{ spaceapi_topic_status }}" + MQTT_SPACESTATUS_LASTCHANGE_TOPIC: "{{ spaceapi_topic_lastchange }}" + MQTT_TRAFFIC_LIGHT_TOPIC: "{{ topic_traffic_light }}" + tags: + - spaceapi + + handlers: diff --git a/host_vars/wittgenstein.n39.eu/vars.yml b/host_vars/wittgenstein.n39.eu/vars.yml new file mode 100644 index 0000000..914da6d --- /dev/null +++ b/host_vars/wittgenstein.n39.eu/vars.yml @@ -0,0 +1,16 @@ +server_admin: "admin+wittgenstein@netz39.de" +mac: "b8:27:eb:48:f1:59" +ansible_python_interpreter: /usr/bin/python3 +gatekeeper_user: pi +data_dir: "/srv/data" + +spaceapi_host_port: 8001 +spaceapi_domain_name: spaceapi.n39.eu +spaceapi_image: netz39/spaceapi-service:0.1.0 +spaceapi_topic_status: "Netz39/SpaceAPI/isOpen" +spaceapi_topic_lastchange: "Netz39/SpaceAPI/lastchange" + +ampelcontroller_image: netz39/ampel-controller:0.1.0 +topic_lever_state: "Netz39/Things/StatusSwitch/Lever/State" +topic_door_events: "Netz39/Things/Door/Events" +topic_traffic_light: "Netz39/Things/Ampel/Light" diff --git a/inventory.yml b/inventory.yml index b1513b4..4263588 100644 --- a/inventory.yml +++ b/inventory.yml @@ -12,6 +12,7 @@ all: pottwal.n39.eu: radon.n39.eu: unicorn.n39.eu: + wittgenstein.n39.eu: k3s-c1.n39.eu: k3s-c2.n39.eu: k3s-c3.n39.eu: diff --git a/main.yml b/main.yml index 5af6b86..177bccc 100644 --- a/main.yml +++ b/main.yml @@ -42,3 +42,6 @@ - name: Plumbum specific setup import_playbook: host-plumbum.yml + +- name: Wittgenstein specific setup + import_playbook: host-wittgenstein.yml diff --git a/templates/pottwal/spaceapi-apache-site.j2 b/templates/pottwal/spaceapi-apache-site.j2 index 5d6961d..9959e90 100644 --- a/templates/pottwal/spaceapi-apache-site.j2 +++ b/templates/pottwal/spaceapi-apache-site.j2 @@ -44,9 +44,9 @@ RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS} ProxyPreserveHost {{ proxy_preserve_host | default("Off") }} - ProxyPass /json http://172.23.48.7/spaceapi - ProxyPass /text http://172.23.48.7/state.txt - ProxyPass /state.png http://172.23.48.7/state.png + ProxyPass /json http://172.23.48.7:8001/json + ProxyPass /text http://172.23.48.7:8001/text + ProxyPass /state.png http://172.23.48.7:8001/state.png