Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOWTO: Automated update (proposal) #36

Open
Elarion245 opened this issue Oct 9, 2024 · 9 comments
Open

HOWTO: Automated update (proposal) #36

Elarion245 opened this issue Oct 9, 2024 · 9 comments
Labels
documentation Improvements or additions to documentation

Comments

@Elarion245
Copy link

Elarion245 commented Oct 9, 2024

Describe the issue you are experiencing

Hey all,

I wanted to share my way through which I intend to realise updates with the ease of a simple button click.

It is a bit hacky, but maybe someone likes it and wants to try... I am not going into all the details, but rather want to explain the high-level concept.

First, create the following file: /homeassistant/haos_update/haos_update_script.sh with the following content (mind unix formating and line-ending) with the following content:

#!/bin/bash

echo -e -n "#!/bin/bash\nrauc install $1\n" > /mnt/data/supervisor/homeassistant/haos_update/perform_update.sh
chmod 777 /mnt/data/supervisor/homeassistant/haos_update/perform_update.sh

Next, activate the Scrape addon to scrape this awesome repo of citruz.
grafik

The definitions are
RockPi HAOS Available Version:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

RockPi HAOS Update Command:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

RockPi HAOS URL:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

Next, define the following template sensor in configuration.yaml:

  - sensor:
      - name: "RockPi HAOS Current Version"
        unique_id: "RockPi_HAOS_Current"
        icon: "mdi:format-vertical-align-bottom"
        state: >-
          {{ (state_attr('update.home_assistant_operating_system_update', 'installed_version') | regex_findall("(^\d\d\.\d).*") | first) | float }}

This will yield the following entities:
grafik

Also, add the following shell commands to configuration.yaml (check out other tutorials on how to generate the private_key or accessing through ssh on port 22222 to get full access to the underlying OS):

  haos_update_script: 'ssh 127.0.0.1 -p22222 -i /config/misc/private_key -o StrictHostKeyChecking=no "/mnt/data/supervisor/homeassistant/haos_update/haos_update_script.sh {{URL}}"'
  haos_perform_update: 'ssh 127.0.0.1 -p22222 -i /config/misc/private_key -o StrictHostKeyChecking=no "/mnt/data/supervisor/homeassistant/haos_update/perform_update.sh"'

Next, create the following automation:

alias: HAOS Update Available
description: ""
triggers:
  - hours: "7"
    trigger: time_pattern
  - hours: "17"
    trigger: time_pattern
conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.rockpi_haos_version
        above: sensor.haos_current_version
actions:
  - data:
      message: >-
        Install via update button on 'Scenarios' or manually via ssh on port 22
        and {{ states('sensor.rockpi_haos_update_command') }}
      title: >-
        HAOS update to version {{ states('sensor.rockpi_haos_version') }}
        available
    enabled: true
    action: notify.persistent_notification
  - action: shell_command.haos_update_script
    data:
      URL: "{{ states('sensor.rockpi_haos_url') }}"
mode: single

Finally, you can create the following button as part of your dashboard:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: shell_command.haos_perform_update
name: Update RockPi HAOS
icon: mdi:update

So basically, the scrape extension checks this site for new releases. Whenever the released version is greater than the currently installed one, a notification will pop up and the shell script to perform the update will be written/updated.

When pressing the update button, the update script is triggered. Mind that there is no error checking and nothing visually will happen, apart from the CPU load spiking during the update. If you hit the button without an update being available, you will silently update to your current version.

The logical flow is thus to press the update button whenever the automation makes you aware of a new update of citruz. Then you wait for a minute and restart HAOS completely. You should then find the newest version in the info section.

I hope this helps. Feel free to improve the method.

@Elarion245 Elarion245 added the bug Something isn't working label Oct 9, 2024
@citruz citruz added documentation Improvements or additions to documentation and removed bug Something isn't working labels Oct 12, 2024
@citruz
Copy link
Owner

citruz commented Oct 12, 2024

Very cool! Thanks for sharing.

@honsma235
Copy link

Inspired by your proposal I thought quite a bit about automated updates and came up with the following idea:

Create a very basic addon called something like "HAOS - Update-Helper for Rock Pi 4" or so..
Probably better to have a new repo for it.

On every new OS release, update the version of the addon as well and release it (with the same version number as the OS)

The addon consists only of a relatively simple shell script which does de following:

  • check if the addon just got updated
  • if yes, fetch the correct *.raub for the board with the same version as the addon, install and restart (addon with SYS_ADMIN priviledge should be able to do so)

Done.

So whenever you release an new OS and the addon, all users will get notified that there is a new version availably (of the addon). And whit a simple click on "update" they will in fact get an OS update.

The addon description an the release comments should make very clear whats the intent of this addon and what will happen after an update of the addon.

This method takes full advantage of the allready existing update checking mechanism and makes it very simple for the user (no ssh into your box, search for the right link, look up the instructions...).

@Elarion245
Copy link
Author

Sounds like a simple, yet great improvement.
How do you intend to handle the different boards? As a config of the addon or would it be auto-detected?

In any case: thank you so much for caring about the rock series and Haos!

@honsma235
Copy link

Thank you too for your inspiration!

The board should be auto-detected. With access to the host system the addon can use the Homeassistant CLI.
The command ha os info should get you the board.

As a clarification to my previous post:
After updating the addon, the user needs to start it once to trigger the OS update.
Thats because addons aren't restarted automatically after an update, unless they where running allready. But in my opinion it shouldn't always be running in the background. It does nothing, just wastes resources.

Additionaly the script should check and react accordingly on some edge cases:

  • current version already installed? -> maybe don't reinstall be default, but allow override with config option
  • current addon version older then installed OS? -> probably the same as above
  • restore from backup? -> not sure how that works in generall.. if the addon is restores to the same version and state as when it was backed up, then we are good

I just checked and it seems there is no way to select which version of an addon gets installed. Its allways on latest, unless not updated yet. So a downgrade to an older version of the OS would not work out of the box. If needed, it could be implemented through a config option as well.

@honsma235
Copy link

So I got kind of motivated to try that idea out.
Unless somebody started already..?

@honsma235
Copy link

Well, it wasn't as easy as I thought, but finally I have a working addon: https://github.com/honsma235/haos-rockpi_update-helper
It still needs some polishing and documentation, but feel free to check it out and test/debug:)
Any suggestions welcome.

@Elarion245
Copy link
Author

Great initiative!

For me it ain't working yet. I receive the following error:

Download completed successfully.
mount: permission denied (are you root?)
cp: can't stat '/tmp/hassos-overlay/root/.ssh/authorized_keys': No such file or directory
umount: can't unmount /tmp/hassos-overlay: Invalid argument
umount: can't unmount /tmp/tmp: Invalid argument
losetup: /tmp/tmp.img: Operation not permitted

Quick question:
Is there any way for the addon to notify us when an update was released? I assume not, as it would need to be running always, right? So the sensors in my op would still make sense...

@honsma235
Copy link

Well, I didn't test it a lot, but never encountered this error. Did you disable the protection mode of the addon?
If the error persists, I will come back to you on how to debug. But I'm busy till beginning of next week.

Regarding the update:
Homeassistant regularly checks for new versions of all it components and addons. So as soon as the addon changes to a new version (which in future should be in sync with releases from haos-rockpi), you will get notified. Just like other updates.
Then you can update the addon and start it once. This will update the OS to the same version as the addon.
The addon just stops after the update and doesn't need to run in the background.

@Elarion245
Copy link
Author

Elarion245 commented Feb 6, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants