Update from Github and restart

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

Confirmed as solution to the Travis build problem. Thank You!

For some reason, my update script does not work.
This is the script I try to call via HASS to update the config and then reload HASS:
https://github.com/TribuneX/home_assistant/blob/master/bash/git_pull.sh

Extract from my HASS config:
git_pull: 'sudo bash /opt/home_assistant/bash/git_pull.sh'

get_latest_config:
   alias: 'Get Latest Config'
   sequence:
      - service: shell_command.git_pull

I also added this script to my sudoers file, in order to let HASS use sudo:

homeassistant ALL = NOPASSWD: /opt/home_assistant/bash/git_pull.sh

The script works without any issue, if I execute it from the command line.
There is no error in the log. Any ideas?

Got if fixed. I had to clean some file and folder privileges, in order to pull the repository from within the env where HASS is installed.

1 Like

Would you be so kind to tell me what you did to get to get the pull working? I’m really struggling with this one,

Nevermind! I fixed it, It turns out some files weren’t owned by the homeassistant user

1 Like

@tmatheussen found this from yonks ago, are you still using it? :slight_smile: - I couldn’t find any info in your repo.

I’m not using it anymore, we moved to a new house 2 years ago and I’ve completely re-done my setup, including remote access :wink:
I do still use github for my config, but more and more stuff is moving to UI which reduces my repo setup, HA also has build-in protection to prevent starting with a breaking config, which totally negates the need for Travis even further.

If you really want to see the old stuff, you can look at this page:

this is basically my repo before removing a lot of legacy stuff