rkoshak / sensorReporter

A python based service that receives sensor inputs and publishes them in various ways.
Apache License 2.0
105 stars 41 forks source link

setup script #116

Closed DanielDecker closed 9 months ago

DanielDecker commented 1 year ago

I've just set up Pi OS Bookworm and made a fresh install of sensorReporter. Meanwhile there are many steps required, e. g. before setting up the virtualenv I had to install python3.11-venv (Pi OS lite).

I think a setup.sh, that installs the dependencies, creates the virtualenv and python packages, would simplify the initial setup. Moreover I think the requirements.txt should be split up for each plug-in group and be moved to the corresponding folder e. g. one requirements.txt for all GPIO plug-ins. @rkoshak: Do you agree with this?

rkoshak commented 1 year ago

I didn't install venv separately I think. But it may depend on how you installed. I usually install python3-full, python3-setuptools, and python3-pip and venv comes with one of those.

I don't have a problem with any of this. I'm a little bit wary of installing all the libraries for all the plug-ins so if there is a way to split up the requirements.txt in a way that pip can understand I'm all for it. Then the end users get to choose which ones they need.

I don't know if it helps but I use the following ansible playbook.

---
# tasks file for roles/sensor_reporter

- name: Create sensor_reporter user
  include_role:
    name: create-user
  vars:
    uid: "{{ sensor_reporter_uid }}"
    gid: "{{ sensor_reporter_uid }}"
    user_name: sensor_reporter
    create_home: False
    service: sensor_reporter

- name: Check to see if the gpio group exists
  command: grep gpio /etc/group
  register: rpi_groups
  failed_when: False
  changed_when: False

- name: Add the sensor_reporter user to the gpio group
  user:
    append: True
    groups: gpio
    name: sensor_reporter
  become: True
  when: ('gpio' in rpi_groups['stdout'])

- name: Install the prerequisites
  apt:
    name: [libglib2.0-dev, bluetooth, bluez, python3-bluez, libgpiod2, net-tools]
    update_cache: no
  become: True

- name: Checkout sensor_reporter from github
  ansible.builtin.git:
    accept_hostkey: True
    ssh_opts: -o StrictHostKeyChecking=no
    dest: "{{ sensor_reporter_home }}"
    repo: "{{ sensor_reporter_repo }}"
    version: main
    update: True
  register: checked_out
  notify:
    - Restart sensor_reporter if there was a change
  become: True

- name: Create the python venv
  ansible.builtin.pip:
    requirements: "{{ sensor_reporter_home }}/requirements.txt"
    virtualenv: "{{ sensor_reporter_home }}"
    virtualenv_command: /usr/bin/python -m venv
  become: True

- name: Install and configure the start script if there has been an update to the share_source
  block:
    - name: Install the start script
      copy:
        src: "{{ sensor_reporter_home }}/sensor_reporter.service"
        dest: /etc/systemd/system
        remote_src: True
        mode: a+rwx
      when: checked_out.changed

    - name: Make sure the service uses the user, home directory, and .yaml file
      lineinfile:
        dest: "/etc/systemd/system/sensor_reporter.service"
        regexp: "{{ item.regex }}"
        line: "{{ item.value }}"
        state: present
      loop:
        - { "regex": "^User=.*", "value": "User=sensor_reporter" }
        - { "regex": "^WorkingDirectory=.*", "value": "WorkingDirectory={{ sensor_reporter_home }}" }
        - { "regex": "^ExecStart=.*", "value": "ExecStart={{ sensor_reporter_home }}/bin/python sensor_reporter.py {{ sensor_reporter_configs_home }}/{{ ansible_hostname }}.yaml" }

    - name: If this is muninn, run it as root
      lineinfile:
        dest: "/etc/systemd/system/sensor_reporter.service"
        regexp: "^User=.*"
        line: "User=root"
        state: present
      when: ansible_hostname == "muninn"
  become: True
  when: checked_out.changed

- name: Checkout the sensor_reporter configs from git
  git:
    dest: "{{ sensor_reporter_configs_home }}"
    repo: "{{ sensor_reporter_configs_repo }}"
    update: True
    version: main
  notify:
    - Restart sensor_reporter if there was a change
  register: configs_checkedout
  become: True

- name: Enable and start sensor_reporter
  systemd:
    name: sensor_reporter
    state: started
    enabled: True
    daemon_reload: True
  become: True

# If it errors with the message "Operation not possible due to RF-kill" issue the following commands
# sudo rfkill unblock bluetooth
# sudo systemctl enable bluetooth.service
# sudo systemctl start bluetooth.service
DanielDecker commented 1 year ago

Thanks for pointing to ansible, it looks very useful for managing multiple Raspberry Pi installations.

I noticed you moved the install directory for sensorRepoter from /opt/ to /svr/, is this related with Bookworm? For me the opt directory works just fine.

rkoshak commented 1 year ago

Ansible is very useful. I use it to configure and maintain all of my machines. And thanks to Ansible I don't have to remember how to install each and every little thing, remember what I installed where, and rebuilding any machine is super easy (I had to rebuild all my RPis recently because of failed in place upgrades to bookworm and they were all done in an hour and most of that time was spent burning the images to the SD cards.

As for /opt verses /srv, you can use any directory you want. Even /mystuff would work.

I used to try to follow the FHS which says that site specific data should go under /srv and site specific software should be installed to /opt.

But I have some cases where there is a mix of data and software and it's not clear where is the most appropriate and since most of the time I'm dealing with containerized software, /srv is the right place.

It became annoying to have to remember which is the right place to look so I gradually ended up moving everything I install to /srv as most of the time in my systems /srv is more correct. It's only the rare cases (sensorReporter being one) where /opt would be more appropriate. But I'm the only one on these systems so what works for me is more important than following the standard.

I guess that's a long way to say /opt is more correct but I'm lazy and put everything /srv so I've only one place to look for stuff I've added to a machine.

DanielDecker commented 1 year ago

Thanks for explaining this in detail.

because of failed in place upgrades to bookworm

I also coudn't simply upgrade to bookworm and had to do a complete reinstall.

I used to try to follow the FHS which says that site specific data should go under /srv and site specific software should be installed to /opt.

In my research I came across similar information packed into a man-page. man hier will explain the folder hierarchy just like the link you posted. So I agree that /svr works also for sensorReporter too. I'll write the setup script to will work in any chosen directory.

DanielDecker commented 9 months ago

PR was merged, so this issue is solved!