add python venv iotscripts master
authorBernhard Tittelbach <bernhard@tittelbach.org>
Wed, 13 May 2026 00:48:17 +0000 (02:48 +0200)
committerBernhard Tittelbach <bernhard@tittelbach.org>
Wed, 13 May 2026 00:48:17 +0000 (02:48 +0200)
ansible/roles/iotscripts/defaults/main.yml
ansible/roles/iotscripts/tasks/main.yml
ansible/roles/iotscripts/templates/dostuff_switch_lights.service.j2
ansible/roles/iotscripts/templates/olga_freezer_sensordata_forwarder.service.j2

index d539af2..00b08ec 100644 (file)
@@ -12,6 +12,10 @@ go_target_goos: "linux"
 # Local temporary build directory (on the Ansible controller)
 local_build_dir: "/tmp/ansible_iotscripts_build"
 
+
+python_requirements_url: "https://github.com/realraum/door_and_sensors/raw/refs/heads/master/scripts/requirements.txt"
+iotscripts_python_venv: /home/{{ realraum_user }}/venv
+
 python_scripts:
   - name: dostuff_switch_lights.py
     url: "https://github.com/realraum/door_and_sensors/raw/refs/heads/master/scripts/dostuff_switch_lights.py"
index ef55c79..7349cff 100644 (file)
     mode: "0600"
   no_log: true
 
+- name: Ensure python3-venv is installed
+  ansible.builtin.package:
+    name:
+      - python3
+      - python3-venv
+      - python3-pip
+    state: present
+
+- name: Download requirements.txt from upstream
+  ansible.builtin.get_url:
+    url: "{{ python_requirements_url }}"
+    dest: "{{ realraum_config_dir }}/requirements.txt"
+    owner: "{{ realraum_user }}"
+    group: "{{ realraum_user }}"
+    mode: "0644"
+    force: true          # always refresh so upstream changes are picked up
+  register: requirements_download
+
+- name: Create Python virtualenv
+  ansible.builtin.command:
+    cmd: python3 -m venv {{ iotscripts_python_venv }}
+    creates: "{{ iotscripts_python_venv }}/bin/python"
+  become: true
+  become_user: "{{ realraum_user }}"
+
+- name: Install Python dependencies into virtualenv
+  ansible.builtin.pip:
+    requirements: "{{ realraum_config_dir }}/requirements.txt"
+    virtualenv: "{{ iotscripts_python_venv }}"
+    state: present
+  become: true
+  become_user: "{{ realraum_user }}"
+
 - name: Download Python scripts
   ansible.builtin.get_url:
     url: "{{ item.url }}"
index a08a40c..569ad98 100644 (file)
@@ -7,7 +7,7 @@ Nice=2
 Type=simple
 Restart=always
 WorkingDirectory={{ realraum_home }}
-ExecStart={{ realraum_bin }}/dostuff_switch_lights.py
+ExecStart={{ iotscripts_python_venv }}/bin/python {{ realraum_bin }}/dostuff_switch_lights.py
 SyslogIdentifier=%i
 
 [Install]
index 9cb9d75..1177287 100644 (file)
@@ -6,9 +6,10 @@ Wants=network.target
 [Service]
 Type=simple
 Restart=always
+RestartSec=40
 WorkingDirectory={{ realraum_home }}
 EnvironmentFile={{ realraum_config_dir }}/smsgw.env
-ExecStart={{ realraum_bin }}/olga_freezer_sensordata_forwarder.py
+ExecStart={{ iotscripts_python_venv }}/bin/python {{ realraum_bin }}/olga_freezer_sensordata_forwarder.py
 SyslogIdentifier=%i
 
 [Install]