install.func 3.5 KB

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