mqtt_setup.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. # Setup script environment
  3. set -o errexit #Exit immediately if a pipeline returns a non-zero status
  4. set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
  5. set -o nounset #Treat unset variables as an error
  6. set -o pipefail #Pipe will exit with last non-zero status if applicable
  7. shopt -s expand_aliases
  8. alias die='EXIT=$? LINE=$LINENO error_exit'
  9. trap die ERR
  10. trap 'die "Script interrupted."' INT
  11. function error_exit() {
  12. trap - ERR
  13. local DEFAULT='Unknown failure occured.'
  14. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  15. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  16. msg "$FLAG $REASON"
  17. exit $EXIT
  18. }
  19. function msg() {
  20. local TEXT="$1"
  21. echo -e "$TEXT"
  22. }
  23. # Prepare container OS
  24. msg "Setting up Container OS..."
  25. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  26. locale-gen >/dev/null
  27. # Update container OS
  28. msg "Updating Container OS..."
  29. apt update &>/dev/null
  30. apt-get -qqy upgrade &>/dev/null
  31. # Install prerequisites
  32. msg "Installing Prerequisites..."
  33. apt-get update &>/dev/null
  34. apt-get -qqy install \
  35. curl \
  36. gnupg \
  37. sudo &>/dev/null
  38. # Installing Mosquitto MQTT broker
  39. msg "Installing Mosquitto MQTT broker.."
  40. wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key &>/dev/null
  41. apt-key add mosquitto-repo.gpg.key &>/dev/null
  42. cd /etc/apt/sources.list.d/
  43. wget http://repo.mosquitto.org/debian/mosquitto-bullseye.list &>/dev/null
  44. apt-get update >/dev/null
  45. apt-get -y install mosquitto &>/dev/null
  46. apt-get -y install mosquitto-clients &>/dev/null
  47. # Customize container
  48. msg "Customizing LXC..."
  49. rm /etc/motd # Remove message of the day after login
  50. rm /etc/update-motd.d/10-uname # Remove kernel information after login
  51. touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
  52. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  53. mkdir -p $(dirname $GETTY_OVERRIDE)
  54. cat << EOF > $GETTY_OVERRIDE
  55. [Service]
  56. ExecStart=
  57. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  58. EOF
  59. systemctl daemon-reload
  60. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  61. # Cleanup container
  62. msg "Cleanup..."
  63. rm -rf /mqtt_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*