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

Unable to source bash script on login #735

Open
DocSalvager opened this issue May 17, 2024 · 3 comments
Open

Unable to source bash script on login #735

DocSalvager opened this issue May 17, 2024 · 3 comments

Comments

@DocSalvager
Copy link

DocSalvager commented May 17, 2024

Problem/Motivation

Attempting to customize login shell such as having a bash alias invoked to list files the way I want, but the only way this works is to type out the command after ssh login. I have SFTP enabled so login user has to be root. Login correctly puts me in the Docker container's copy of /root.

Expected behavior

Successful login and sourcing of the .bashrc file.

Actual behavior

Any file created in /root is lost on restart but can create persistent files in ./config, ./homeassistant, etc. so did that and set the init_commands field in the addon configuration to source that file...

. ./config/docsalvager/.bashrc

(yes the "docsalvager" directory I created persisted)

The .bashrc file contains only 1 line...
alias ll='ls -la'

After restart of the addon, it refuses to start in Home Assistant and any attempt to login remotely (which was working before) fails with...

ssh: connect to host ha01 port 22: Connection refused

NOTE that zsh is turned off in the configuration.

Relevant lines in the addon log...
/etc/s6-overlay/s6-rc.d/init-user/run: line 112: ./config/docsalvager/.bashrc: No such file or directory [16:33:56] FATAL: Failed executing init command: . ./config/docsalvager/.bashrc s6-rc: warning: unable to start service init-user: command exited 1 /run/s6/basedir/scripts/rc.init: warning: s6-rc failed to properly bring all the services up! Check your logs (in /run/uncaught-logs/current if you have in-container logging) for more information. /run/s6/basedir/scripts/rc.init: fatal: stopping the container.

Further experimentation revealed that the init_commands field requires full pathnames so the correct entry is...

. /root/config/docsalvager/.bashrc

On using this, the addon started but still failed to source the .bashrc file.

Steps to reproduce

See above.

Proposed changes

The addon should start and display relevant error messages if there is a problem with the sourced file. Otherwise, it should source the file.

@sinclairpaul
Copy link
Member

sinclairpaul commented May 17, 2024

What path did you place ./config/docsalvager/.bashrc' ?

Edit - To be clear was it within the addon or added externally?

@DocSalvager
Copy link
Author

I guess it is in addon since the /root/config/docsalvager/.bashrc file appears both when accessed via the HA Addon in HA as well as via sftp. I know of no other way to get Linux commandline access to the HomeAssistant OS 12.3 host. Though I've been programming Unix/Linux systems for over 40 years, I'm new to Docker containers and very confused by all the security that prevents access to the real operating system.

@paulwarwicker
Copy link

paulwarwicker commented May 26, 2024

I had the same issue and worked around it by writing a file by using the init_commands for the addon.
You need to write it as one long string and I used the echo extended character sequence to indent the file to make it more readable. I did initially try to use a multi-line approach in yaml, but it ran into issues. This was ever so slightly cryptic but simpler. You need to edit in the yaml editor rather than the UI.

For example, I had this:

ssh:
  username: root
  password: ""
  authorized_keys:
    - >-
      ssh-ed25519 
      AA.......8T
  sftp: true
  compatibility_mode: false
  allow_agent_forwarding: false
  allow_remote_port_forwarding: true
  allow_tcp_forwarding: true
zsh: false
share_sessions: false
packages: []
init_commands:
  - >-
    echo -e '# written from advanced ssh & web terminal config\n\nalias
    ll="docker logs addon_a0d7b954_appdaemon --follow"\nalias vii="vi
    ~/.profile"\nalias tt="cd /addon_configs/a0d7b954_appdaemon/tests; pytest
    test_automation.py"\nalias ii=". ~/.profile"\nalias\n\nif [[ ! -d ~/venv ]];
    then\n\techo -n "initialising venv..."\n\tpython -m venv ~/venv\n\techo
    "done"\n\tapk add pytest\n\tpip install pytest-freezer ics arrow pytest
    requests\nelif [[ -f ~/venv/bin/activate ]]; then\n\t.
    ~/venv/bin/activate\nfi\n' > /root/.profile

which gave me a .profile of:

# written from advanced ssh & web terminal config

alias ll="docker logs addon_a0d7b954_appdaemon --follow"
alias vii="vi ~/.profile"
alias tt="cd /addon_configs/a0d7b954_appdaemon/tests; pytest test_automation.py"
alias ii=". ~/.profile"
alias

if [[ ! -d ~/venv ]]; then
        echo -n "initialising venv..."
        python -m venv ~/venv
        echo "done"
        apk add pytest
        pip install pytest-freezer ics arrow pytest requests
elif [[ -f ~/venv/bin/activate ]]; then
        . ~/venv/bin/activate
fi

You could write .bashrc if you like, I preferred .profile.

I realise that I could have installed the system package pytest via the addon package mechanism, but I preferred this approach.

-paul

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