mqtt_setup.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. CROSS='\033[1;31m\xE2\x9D\x8C\033[0m'
  10. CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
  11. RETRY_NUM=5
  12. RETRY_EVERY=3
  13. NUM=$RETRY_NUM
  14. trap die ERR
  15. trap 'die "Script interrupted."' INT
  16. function error_exit() {
  17. trap - ERR
  18. local DEFAULT='Unknown failure occured.'
  19. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  20. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  21. msg "$FLAG $REASON"
  22. exit $EXIT
  23. }
  24. function msg() {
  25. local TEXT="$1"
  26. echo -e "$TEXT"
  27. }
  28. # Prepare container OS
  29. msg "Setting up Container OS..."
  30. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  31. locale-gen >/dev/null
  32. while [ "$(hostname -I)" = "" ]; do
  33. 1>&2 echo -e "${CROSS} \e[1;31m No Network: \e[0m $(date)"
  34. sleep $RETRY_EVERY
  35. ((NUM--))
  36. if [ $NUM -eq 0 ]
  37. then
  38. 1>&2 echo -e "${CROSS} \e[1;31m No Network After $RETRY_NUM Tries \e[0m"
  39. exit 1
  40. fi
  41. done
  42. echo -e "${CHECKMARK} \e[1;92m Network Connected: \e[0m $(hostname -I)"
  43. # Update container OS
  44. msg "Updating Container OS..."
  45. apt update &>/dev/null
  46. apt-get -qqy upgrade &>/dev/null
  47. # Install prerequisites
  48. msg "Installing Prerequisites..."
  49. apt-get update &>/dev/null
  50. apt-get -qqy install \
  51. curl \
  52. gnupg \
  53. sudo &>/dev/null
  54. # Installing Mosquitto MQTT broker
  55. msg "Installing Mosquitto MQTT broker.."
  56. wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key &>/dev/null
  57. apt-key add mosquitto-repo.gpg.key &>/dev/null
  58. cd /etc/apt/sources.list.d/
  59. wget http://repo.mosquitto.org/debian/mosquitto-bullseye.list &>/dev/null
  60. apt-get update >/dev/null
  61. apt-get -y install mosquitto &>/dev/null
  62. apt-get -y install mosquitto-clients &>/dev/null
  63. # Customize container
  64. msg "Customizing LXC..."
  65. rm /etc/motd # Remove message of the day after login
  66. rm /etc/update-motd.d/10-uname # Remove kernel information after login
  67. touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
  68. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  69. mkdir -p $(dirname $GETTY_OVERRIDE)
  70. cat << EOF > $GETTY_OVERRIDE
  71. [Service]
  72. ExecStart=
  73. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  74. EOF
  75. systemctl daemon-reload
  76. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  77. # Cleanup container
  78. msg "Cleanup..."
  79. rm -rf /mqtt_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*