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

docker-compose execute scripts not working #1411

Open
solderchicken opened this issue Jan 16, 2025 · 9 comments
Open

docker-compose execute scripts not working #1411

solderchicken opened this issue Jan 16, 2025 · 9 comments

Comments

@solderchicken
Copy link

Hi all

Had habridge working great on a Pi, then on Ubuntu but now trying to get to work in docker-compose.

I have shutdown/reboot and WOL scripts that execute when I right click and 'run as program' in Ubuntu.

Have got ha-bridge running and can execute these scripts (which are stored in /config/scripts) but nothing happens when I do.

Permissions issue? I'm not sure how to fix as there is nothing in the log.

Thanks in advance

@solderchicken
Copy link
Author

here is an example of the script.

ha-bridge says it executes, but nothing happens. If I run the script manually in Ubuntu it works. The code also runs in terminal as user.

#!/bin/bash
ssh [email protected] sudo shutdown now &> /dev/null
exit 0

@arnieSkyNet
Copy link

Don't forget habridge runs as root so try running your script via su- rather than sudo

I've created a GitHub called launchprog that allows you to put into the habridge on/dim/off launchprog ssh [email protected] sudo shutdown now

I'm just finishing it with a -d (debug) option that will leave a window open, so you can see any errors as it runs from root but not pass on the -d as A parameter to the next program, launchprog passes it onto

@arnieSkyNet
Copy link

I've released it

https://github.com/arnieSkyNet/habrunasuser

If I shouldn't of posted here, please delete or tell me to delete and I will

@BuxtonCalvin
Copy link

You should check the container to make sure it includes SSH. If not, you'll have to add ssh to the container. If you're using the "lscr.io/linuxserver/habridge:latest" image, you can add ssh by including the following volume in your compose file.

home/custom-services:/custom-cont-init.d:ro # ssh service

Then in your local custom-services folder, add the file "add-ssh.sh" set the permissions to execute and add the following inside the file:

#!/bin/bash

echo "**** installing ssh client ****"
apk add --no-cache openssh-client

That should do it. There is info on the net for lscr containers on how to add custom services to their containers.

here is an example of the script.

ha-bridge says it executes, but nothing happens. If I run the script manually in Ubuntu it works. The code also runs in terminal as user.

#!/bin/bash ssh [email protected] sudo shutdown now &> /dev/null exit 0

@arnieSkyNet
Copy link

Basically the SSH keys are for the user not root user, hence ssh works via sudo in the terminal but not as root via habridge as root hasn't got the user keys.

That's why I wrote launchprog so habridge root can use the users keys

@solderchicken
Copy link
Author

solderchicken commented Jan 21, 2025

Hi both

Thank you so much for your input, sorry couldn't respond more promptly.

Have tried the method from @BuxtonCalvin but still no dice :(

I now have a folder called 'custom-services' in /home/username/Compose/habridge

In there is the file 'add-ssh.sh' which is executable (chmod +x)

Contents of the file are;

#!/bin/bash

echo "**** installing ssh client ****"
apk add --no-cache openssh-client

In habridge, my 'off items' Type is Execute Program/Script

The 'Target Items' is shutdown.sh

In /home/username/Compose/habridge/scripts there is an executable file called shutdown.sh

If I run as program, it works, if I run fro terminal (./shutdown.sh) it works

In habridge, it says it executes but nothing happens still.

Script is as follows

#!/bin/bash
ssh [email protected] sudo shutdown now &> /dev/null
exit 0

I tried various options swapping sudo for su- but stil didn't get anywhere.

Completely stumped,,, :(

@arnieSkyNet
Copy link

arnieSkyNet commented Jan 21, 2025

It's the ssh keys

habridge is running as root
root doesn't use your users ssh keys (and you probably shouldn't try to make it either) but doing sudo Will miss guide you, as it works as that user because that user has access to the ssh keys

You can test it failing by doing su -
then run your program and see what goes wrong (in effect, you are being HA bridge)

Note: root on the local machine habridge is running on

Took me ages in fact years to work out. This was the issue.

So then I wrote that software to make it easier for me and so it would work

The other easier way is su -
Then create root ssh keys for the remote machine, including doing the fingerprint.
Then run your command to see if it works as root (while still in "su -" session

If it works fine, then you can then test it with HA bridge

Like I said, I've created a program called launch product that does all this for you if you don't want to create SSH keys for root, which is a real pain because every time you add a new remote machine, do your user, you have to do the same again for root.

@BuxtonCalvin
Copy link

BuxtonCalvin commented Jan 21, 2025

You also need to add a volume for the ssh keys:

  • /root/.ssh:/root/.ssh

Since you know the script works, try running the script from inside the container command line. An easy way to get to it is to use the portainer console feature for the given container. You should be able to change directories from inside the container to your volume where you are holding your action scrips.

@arnieSkyNet
Copy link

That will work but worth thinking about Root SSH keys, especially in Docker environments, come with significant risks like privilege escalation, weak key management, and lack of isolation. Minimising root access, enforcing restrictions, and securing key storage are critical steps to mitigate risks.

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

No branches or pull requests

3 participants