install.func 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # This function sets color variables for formatting output in the terminal
  2. color() {
  3. YW=$(echo "\033[33m")
  4. BL=$(echo "\033[36m")
  5. RD=$(echo "\033[01;31m")
  6. BGN=$(echo "\033[4;92m")
  7. GN=$(echo "\033[1;92m")
  8. DGN=$(echo "\033[32m")
  9. CL=$(echo "\033[m")
  10. RETRY_NUM=10
  11. RETRY_EVERY=3
  12. CM="${GN}✓${CL}"
  13. CROSS="${RD}✗${CL}"
  14. BFR="\\r\\033[K"
  15. HOLD="-"
  16. }
  17. # This function enables IPv6 if it's not disabled and sets verbose mode if the global variable is set to "yes"
  18. verb_ip6() {
  19. if [ "$VERBOSE" = "yes" ]; then
  20. set -x
  21. STD=""
  22. else STD="silent"; fi
  23. silent() { "$@" >/dev/null 2>&1; }
  24. if [ "$DISABLEIPV6" == "yes" ]; then
  25. echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf
  26. $STD sysctl -p
  27. fi
  28. }
  29. # This function sets error handling options and defines the error_handler function to handle errors
  30. catch_errors() {
  31. set -Eeuo pipefail
  32. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  33. }
  34. # This function handles errors
  35. error_handler() {
  36. local exit_code="$?"
  37. local line_number="$1"
  38. local command="$2"
  39. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  40. echo -e "\n$error_message"
  41. if [[ "$line_number" -eq 24 ]]; then
  42. echo -e "The silent function has suppressed the error, run the script with verbose mode enabled, which will provide more detailed output.\n"
  43. fi
  44. }
  45. # This function prints an informational message
  46. msg_info() {
  47. local msg="$1"
  48. echo -ne " ${HOLD} ${YW}${msg}..."
  49. }
  50. # This function prints a success message
  51. msg_ok() {
  52. local msg="$1"
  53. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  54. }
  55. # This function prints an error message
  56. msg_error() {
  57. local msg="$1"
  58. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  59. }
  60. # This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
  61. setting_up_container() {
  62. msg_info "Setting up Container OS"
  63. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  64. locale-gen >/dev/null
  65. echo $tz >/etc/timezone
  66. ln -sf /usr/share/zoneinfo/$tz /etc/localtime
  67. for ((i = RETRY_NUM; i > 0; i--)); do
  68. if [ "$(hostname -I)" != "" ]; then
  69. break
  70. fi
  71. echo 1>&2 -en "${CROSS}${RD} No Network! "
  72. sleep $RETRY_EVERY
  73. done
  74. if [ "$(hostname -I)" = "" ]; then
  75. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  76. echo -e " 🖧 Check Network Settings"
  77. exit 1
  78. fi
  79. rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED
  80. msg_ok "Set up Container OS"
  81. msg_ok "Network Connected: ${BL}$(hostname -I)"
  82. }
  83. # This function checks the network connection by pinging a known IP address and prompts the user to continue if the internet is not connected
  84. network_check() {
  85. set +e
  86. trap - ERR
  87. if ping -c 1 -W 1 1.1.1.1 &>/dev/null; then msg_ok "Internet Connected"; else
  88. msg_error "Internet NOT Connected"
  89. read -r -p "Would you like to continue anyway? <y/N> " prompt
  90. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  91. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  92. else
  93. echo -e " 🖧 Check Network Settings"
  94. exit 1
  95. fi
  96. fi
  97. RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  98. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  99. set -e
  100. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  101. }
  102. # This function updates the Container OS by running apt-get update and upgrade
  103. update_os() {
  104. msg_info "Updating Container OS"
  105. $STD apt-get update
  106. $STD apt-get -y upgrade
  107. msg_ok "Updated Container OS"
  108. }
  109. # This function modifies the message of the day (motd) and SSH settings
  110. motd_ssh() {
  111. echo "export TERM='xterm-256color'" >>/root/.bashrc
  112. echo -e "$APPLICATION LXC provided by https://tteck.github.io/Proxmox/\n" >/etc/motd
  113. chmod -x /etc/update-motd.d/*
  114. if [[ "${SSH_ROOT}" == "yes" ]]; then
  115. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  116. systemctl restart sshd
  117. fi
  118. if [[ "$PCT_OSVERSION" == "12" ]]; then
  119. $STD systemctl disable systemd-networkd-wait-online.service
  120. fi
  121. }
  122. # This function customizes the container by modifying the getty service and enabling auto-login for the root user
  123. customize() {
  124. if [[ "$PASSWORD" == "" ]]; then
  125. msg_info "Customizing Container"
  126. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  127. mkdir -p $(dirname $GETTY_OVERRIDE)
  128. cat <<EOF >$GETTY_OVERRIDE
  129. [Service]
  130. ExecStart=
  131. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  132. EOF
  133. systemctl daemon-reload
  134. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  135. msg_ok "Customized Container"
  136. fi
  137. }