install.fairie/home/dot_local/bin/post-installx/executable_post-endlessh.sh

51 lines
2.4 KiB
Bash
Raw Normal View History

2024-05-03 19:40:44 -07:00
#!/usr/bin/env bash
# @file Endlessh Configuration
# @brief Applies the Endlessh configuration and starts the service on Linux systems
2024-05-27 20:45:14 -07:00
set -Eeuo pipefail
2024-05-27 20:50:11 -07:00
trap "gum log -sl error 'Script encountered an error!'" ERR
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
function configureEndlessh() {
### Update the service configuration file
2024-05-27 20:50:11 -07:00
gum log -sl info 'Updating endlessh service configuration file'
2024-05-03 19:40:44 -07:00
sudo sed -i 's/^.*#AmbientCapabilities=CAP_NET_BIND_SERVICE/AmbientCapabilities=CAP_NET_BIND_SERVICE/' /usr/lib/systemd/system/endlessh.service
sudo sed -i 's/^.*PrivateUsers=true/#PrivateUsers=true/' /usr/lib/systemd/system/endlessh.service
2024-05-27 20:50:11 -07:00
gum log -sl info 'Reloading systemd' && sudo systemctl daemon-reload
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
### Update capabilities of `endlessh`
2024-05-27 20:50:11 -07:00
gum log -sl info 'Updating capabilities of endlessh' && sudo setcap 'cap_net_bind_service=+ep' /usr/bin/endlessh
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
### Restart / enable Endlessh
2024-05-27 20:50:11 -07:00
gum log -sl info 'Enabling the endlessh service' && sudo systemctl enable endlessh
gum log -sl info 'Restarting the endlessh service' && sudo systemctl restart endlessh
2024-05-03 19:40:44 -07:00
}
### Update /etc/endlessh/config if environment is not WSL
if [[ ! "$(test -d proc && grep Microsoft /proc/version > /dev/null)" ]]; then
if command -v endlessh > /dev/null; then
if [ -d /etc/endlessh ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Copying ~/.ssh/endlessh/config to /etc/endlessh/config' && sudo cp -f "$HOME/.ssh/endlessh/config" /etc/endlessh/config
2024-05-03 19:40:44 -07:00
configureEndlessh || CONFIGURE_EXIT_CODE=$?
2024-05-27 20:45:14 -07:00
if [ -n "${CONFIGURE_EXIT_CODE:-}" ]; then
2024-05-27 20:50:11 -07:00
gum log -sl error 'Configuring endlessh service failed' && exit 1
2024-05-03 19:40:44 -07:00
else
2024-05-27 23:55:42 -07:00
gum log -sl info 'Successfully configured endlessh service'
2024-05-03 19:40:44 -07:00
fi
elif [ -f /etc/endlessh.conf ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Copying ~/.ssh/endlessh/config to /etc/endlessh.conf' && sudo cp -f "$HOME/.ssh/endlessh/config" /etc/endlessh.conf
2024-05-03 19:40:44 -07:00
configureEndlessh || CONFIGURE_EXIT_CODE=$?
2024-05-27 20:45:14 -07:00
if [ -n "${CONFIGURE_EXIT_CODE:-}" ]; then
2024-05-27 20:50:11 -07:00
gum log -sl error 'Configuring endlessh service failed' && exit 1
2024-05-03 19:40:44 -07:00
else
2024-05-27 23:55:42 -07:00
gum log -sl info 'Successfully configured endlessh service'
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'Neither the /etc/endlessh folder nor the /etc/endlessh.conf file exist'
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Skipping Endlessh configuration because the endlessh executable is not available in the PATH'
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Skipping Endlessh configuration since environment is WSL'
2024-05-03 19:40:44 -07:00
fi