Attempt at upgrade script failing

I’m trying to write what I though would be a simple script to update hass, it won’t work…it doesn’t activate the virtual environment.

#!/bin/bash

cd /home/homeassistant/.homeassistant/
sudo su -s /bin/bash homeassistant
source /srv/homeassistant/bin/activate
pip3 install --upgrade homeassistant

Permissions as below:

-rwxr-xr-x  1 homeassistant nogroup  439 Mar  3 12:28 upgrade-hass.sh

It switches the user and moves to the home assistant dir bit doesn’t activate the virtual environment?
I’ve checked all my paths etc and all work in their own right from the CL?

Are you trying to run this from inside homeassistant, or as the pi user?

As the pi user from CL

Try:

sudo su -s /bin/bash homeassistant << EOM
source /srv/homeassistant/bin/activate
pip3 install --upgrade homeassistant
EOM

You need to run it with sudo from the pi user as all the permissions will be for the homeassistant user.

would you mind elaborating on this a little…specifically what does the << EOM do?

Is this because the ownership of the .sh file lies with my home assistant user?
Would chown’ing the file to pi help?

Yeah, homeassistant owns the file, so to run it from the pi user will require sudo.

OK, that makes absolute sense. thank you.
however, I think this may be my lack of Linux knowledge…
ive hashed out the pip --upgrade line for testing purposes for now, so when I run the script from within its directory location, as the pi user by sudo ./update.sh the script runs (minus actually upgrading) and then returns to the CL prompt, only know im logged in as user homeassistant and have moved to my home assistant dir /home/homeassistant/.homeassistant
as the script incudes the activate virtual env. id expect for the virtual environment to be active also, but it is not…should it be?

Not sure if I’m honest, the whole permissions thing is very important so it may be that because you’re running the script as sudo pi, but have then changed user to homeassistant, you don’t have the permissions as pi to activate the venv.

Realistically, you need to give permission to the user that’s going to be completing the actions, so in this case you should change to the homeassistant user first, then run the script (obviously the script would no longer need to change user). Or you should give the script permissions to run as the pi user, and in the script change user, activate the venv, perform the upgrade, deactivate the venv and exit the homeassistant user.

I think the latter is where @Tinkerer is heading with <<<EOM but I’m not familiar with that syntax.

I suspect it’s the crossing user permissions that’s causing the issue.

Thanks for the help, but still no luck.
I changed the .sh file ownership to user pi and it still doesn’t seem to activate the virtual environment. :confused:

Did you try my approach? Your script does a sudo, but the following command are not run under the sudo, they’ll happen when you end that sudo session.

Mine means that the activation of the venv, and the upgrade, happen within the sudo session.

yes I did indeed try your approach and it also did not work.
it went through the motions of the script, but failed at the pip3 install --upgrade homeassistant step as the virtual env wasn’t activated (Im assuming)

Details? Errors? Anything to help us help you?

“It did not work” is, I’m afraid, about as much use as saying “my car doesn’t start”.

1 Like

Yeah sorry, should have said I’m not at home so don’t have the particular errors to hand, will get them later this evening

Here’s the error I get…

pi@media-pi:/home/homeassistant/.homeassistant/bash_scripts $ sudo ./upgrade-hass.sh
Requirement already up-to-date: homeassistant in /srv/homeassistant/lib/python3.5/site-packages
Collecting exit
  Could not find a version that satisfies the requirement exit (from versions: )
No matching distribution found for exit

You shouldn’t need to sudo that script, since it calls sudo itself.

i tried running without sudo and still no joy.
to be honest its not worth the hassle :rofl:

That error tells me that you are trying to install a package called exit, which doesn’t exist.