mariadb_setup.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. sudo &>/dev/null
  53. # Installing MariaDB
  54. msg "Installing MariaDB..."
  55. curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash &>/dev/null
  56. apt-get update >/dev/null
  57. apt-get install -y mariadb-server &>/dev/null
  58. # Customize container
  59. msg "Customizing container..."
  60. rm /etc/motd # Remove message of the day after login
  61. rm /etc/update-motd.d/10-uname # Remove kernel information after login
  62. touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
  63. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  64. mkdir -p $(dirname $GETTY_OVERRIDE)
  65. cat << EOF > $GETTY_OVERRIDE
  66. [Service]
  67. ExecStart=
  68. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  69. EOF
  70. systemctl daemon-reload
  71. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  72. # Cleanup container
  73. msg "Cleanup..."
  74. rm -rf /mariadb_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*