Update from Github and restart

Hello all,

after a quick read through the forum, I couldn’t find an example on how to update the config from GitHub (or similar) and restart Hass after. So here goes my fairly simple solution:

I’ve added this part to my config:

shell_command:
  get_latest_config: bash /home/hass/.homeassistant/pullconfig.sh

script:
  update_config:
    sequence:
      - service: shell_command.get_latest_config
      - service: homeassistant.restart

This adds a button on the UI, which will execute a small bash script that pulls the latest commits from my Github account (after getting a repo set up and in sync) and call the service to restart Hass afterwards.
You can check the script here: https://github.com/Tommatheussen/Home-Assistant-Configuration/blob/master/bin/update_config_from_github.sh

What I still plan on doing:

  • Don’t restart when there’s no updates
    -> This could be done by either disabling the button (script condition) or possibly stop the execution if git pull returns no changes
  • Possibly allow the option to disable restart altogether (switch)

The main reason for me doing this, is the fact that I don’t always have physical access to my home server (when I’m at my GF’s house, at work, somewhere else,…), which allows me to quickly edit my config inside my Github repo and restarting the home assistant without having to SSH into my server.

EDIT: Updated my repo a bit, I now got a Travis CI integration set up.
I protected my master branch, only allowing it to be updated from PR’s.
New commits to a PR branch trigger the Travis builds, which will install HA and run the check_config script.
This way I know for sure my config will work and not crash HA on restart.

Any comments, questions, suggestions welcome :smiley:

9 Likes

That’s awesome – maybe even more convenient than automated config backups if you are using a remote repository.

How did you set up Travis CI?

Never mind, I’ve ripped off your github set-up for Travis

You can indeed get it from my Github page.
the procedure is fairly easy, since it just install hass and runs the checking script.

The only downside is that it needs to be wrapped in the bash script for processing, since the script doesn’t output anything to stderr

i’m trying to do the same but travis fails everytime on following message:
No module named ‘RFXtrx’. Of course the RFXtrx module is not connected but do you know how can I get past that?

I don’t use the RFXtrx component myself, but looking to the docs, you might be able to work-around this with a dummy device (created during Travis install, like the secrets file).

If the check-config actually tries to connect to the device and requires a correct response, then I’m afraid I can’t help you with that…

Edit: I’ve also just tried to integrate the RFXtrx component in my configuration, and for some reason it does work for me, perhaps I’m missing something though

Edit 2: Adding the switch as RFXtrx does indeed show the error and creating something at the device’s location doesn’t seem to work. Maybe somebody else might know how to mock this device?

Edit 3: Final edit :stuck_out_tongue: apparently HA doesn’t install the pyRFXtrx module. Adding pip install pyRFXtrx to your installation procedure of Travis seems to fix it.

2 Likes

Nice adding install works thank you strange it wont install automatically

@tmatheussen – Thanks! When I was looking at https://github.com/Tommatheussen/Home-Assistant-Configuration/blob/master/bin/pullconfig.sh I have a question:

I’m using the AiO Raspberry Pi installer and generally would SSH as pi and then if I want to update my config files inside of /home/.hass/homeassistant I have to first run sudo su hass prior to running a git pull.

Would I have to add this to your bash script to ensure it’s using the correct user?

Thanks!

hey @brianjking you don’t need to use sudo because the script is ran by hass user.
When you manually do a git pull you’re using the pi user and the pi user doesn’t have any rights on the homeassistant folder

Could you elaborate on that part? I set up a configuration on my Windows laptop and put it on github. I tried a git clone in my home assistant directory on the Raspberry Pi and it completely killed Home Assistant. HA won’t even start.

I think it’s partially related to owners (I did it as user pi instead of user home assistant), but will rwx permissions get messed up too even if I had been the correct user?

I realized another issue with using git clone: by default, the .gitignore file excludes some things like .HA_VERSION, home-assistant.txt, and the /deps directory. I don’t know if cloning those from a Windows configuration is going to work on a Raspberry Pi, so even if they were included, the process of deleting the entire /.homeassistant directory and cloning from github might still not work.

I’ll dig around and see if I can get a synced to github without deleting that entire directory first. Or if I can delete the entire directory but do something to get those other folders back safely.

yes, I think the users would be the issue here, try to do the git clone as the home assistant user.

I’ve set those items to not be included, since they are generated by HA itself.
The deps folder will install pip packages that are needed by the configured platforms, HA installs them on the first run.

Are you using git clone only the first time you set up the repo?
If you already have the code (a previous version), you can just use git pull to get the latest commits and changes

Yesterday I deleted the /.homeassistant directory and then did a git clone as the pi user. This replaced the /.homeassistant folder with everything from git hub, but the owner was pi and home assistant didn’t seem to start. Even when I changed the owner of everything back to homeassistant, it still didn’t seem to work. I couldn’t the HA page to appear when I went to 'ipaddres:8123`

I started over with the HASSbian image, and today I will try to do git clone as the homeassistant user. Is it okay to delete the entire /.homeassistant directory and remake it, or should I be trying to do this in some way that leaves the existing files (like /deps) alone?

I got it to work! Here’s what I did for anybody as clueless as I am who ends up here after me. I don’t know which steps were strictly necessary, but this is what worked for me. Somebody who knows what they’re doing could probably cut this down or fix some errors in my commands.

I would recommend making an image of your Pi’s SD card before starting. That makes it easy to go back if anything gets messed up.

1. As pi user, install git.

$ sudo apt-get install git

2. Still as pi user, run the following commands to stop the home assistant service and then switch to the home assistant user.

$ sudo systemctl stop [email protected] $ sudo su -s /bin/bash homeassistant

3. Now that you’re working as the home assistant user, navigate to your home directory (I think that’s what it’s called) and copy your existing .homeassistant directory (along with everything in it) to a back up.

$ cd $ cp -r .homeassistant .haReplaceAfterGit

4. Delete the .homeassistant directory, then use git clone to have it remade with whatever you have on git hub.

$ rm -rf .homeassistant $ git clone [put your URL from github here]

5. Now you have a new .homeassistant directory, but it’s missing a lot of things like the /deps directory, home-assistant.txt, and whatever else you put in your .gitignore. You’ll copy all those missing files back from the backup you made before. Start by going to your backup and deleting everything you don’t want to copy back. Since I started from a blank image, configuration.yaml was the only file where I wanted to keep the github version instead of my old version.

$ rm .haReplaceAfterGit/configuration.yaml

6. Since you have removed the files in your backup that you don’t want to keep, copy the rest of your backup back into the .homeassistant directory. The formatting below also copies all of the hidden files (the ones that start with a period).

$ cp -r ~/.haReplaceAfterGit/. ~/.homeassistant

7. Exit to get back to the pi user, then start the homeassistant service back up.

$ exit $ sudo systemctl start [email protected]

I made a tweak to this.

I made a custom component on my Windows machine and got it working, but I had a few issues when I moved it to the Pi. I used SSH to directly tinker with the files on the Pi to get it going and I updated my PC files alongside them. But that meant that the two got out of sync, so I couldn’t do a git pull from the Pi any more because of the conflicts. If you SSH into the Pi and run your pull from the command line it will tell you what the problems are, but if you are trying to run it from a script (like in this case). it will simply fail to work without giving you any indication that it failed. At that point the “update and restart” script just becomes a “restart” script.

I changed the script so that instead of git pull it has this:

git fetch git reset --hard origin/master

This pair of commands essentially says “overwrite all of the local changes with the remote versions.” Any tinkering you’ve done on the Pi gets replaced with what’s on Git Hub. This probably isn’t a good update script if you do frequent or significant edits directly on the Pi, as you’re liable to overwrite your work. But I do all of my code building on the PC and I only access files on the Pi for a minimal amount of Pi-specific debugging, so this works really well for me.

Yes offcourse. That will work, and if you don’t have any critical changes on your pi it won’t be a problem.
I’m currently in the process of improving this system, catching error codes and sending events to my HA instance.
These events will then trigger notifications, so I will know whenever there’s an issue executing something.

1 Like

@tmatheussen – It looks like pullconfig.sh is now missing from your repo. :cry:

Oops yes, I renamed some files to be more descriptive, I’ll update my post :wink:

2 Likes