alpine-install.func 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. i=$RETRY_NUM
  12. CM="${GN}✓${CL}"
  13. CROSS="${RD}✗${CL}"
  14. BFR="\\r\\033[K"
  15. HOLD="-"
  16. }
  17. verb_ip6() {
  18. if [ "$VERBOSE" = "yes" ]; then
  19. set -x
  20. STD=""
  21. else STD="silent"; fi
  22. silent() { "$@" >/dev/null 2>&1; }
  23. if [ "$DISABLEIPV6" == "yes" ]; then
  24. $STD sysctl -w net.ipv6.conf.all.disable_ipv6=1
  25. echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf
  26. $STD rc-update add sysctl default
  27. fi
  28. }
  29. catch_errors() {
  30. set -Eeuo pipefail
  31. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  32. }
  33. error_handler() {
  34. local exit_code="$?"
  35. local line_number="$1"
  36. local command="$2"
  37. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  38. echo -e "\n$error_message\n"
  39. }
  40. msg_info() {
  41. local msg="$1"
  42. echo -ne " ${HOLD} ${YW}${msg}..."
  43. }
  44. msg_ok() {
  45. local msg="$1"
  46. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  47. }
  48. msg_error() {
  49. local msg="$1"
  50. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  51. }
  52. setting_up_container() {
  53. msg_info "Setting up Container OS"
  54. while [ $i -gt 0 ]; do
  55. if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" != "" ]; then
  56. break
  57. fi
  58. echo 1>&2 -en "${CROSS}${RD} No Network! "
  59. sleep $RETRY_EVERY
  60. i=$((i - 1))
  61. done
  62. if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" = "" ]; then
  63. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  64. echo -e " 🖧 Check Network Settings"
  65. exit 1
  66. fi
  67. cat <<EOF >>/etc/apk/repositories
  68. https://dl-cdn.alpinelinux.org/alpine/edge/main
  69. https://dl-cdn.alpinelinux.org/alpine/edge/community
  70. https://dl-cdn.alpinelinux.org/alpine/edge/testing
  71. EOF
  72. msg_ok "Set up Container OS"
  73. msg_ok "Network Connected: ${BL}$(ip addr show | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 | tail -n1)${CL}"
  74. }
  75. network_check() {
  76. set +e
  77. trap - ERR
  78. if ping -c 1 -W 1 1.1.1.1 &>/dev/null; then msg_ok "Internet Connected"; else
  79. msg_error "Internet NOT Connected"
  80. read -r -p "Would you like to continue anyway? <y/N> " prompt
  81. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  82. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  83. else
  84. echo -e " 🖧 Check Network Settings"
  85. exit 1
  86. fi
  87. fi
  88. RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  89. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  90. set -e
  91. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  92. }
  93. update_os() {
  94. msg_info "Updating Container OS"
  95. $STD apk update
  96. $STD apk upgrade
  97. msg_ok "Updated Container OS"
  98. }
  99. motd_ssh() {
  100. echo "export TERM='xterm-256color'" >>/root/.bashrc
  101. echo -e "$APPLICATION LXC provided by https://tteck.github.io/Proxmox/\n" >/etc/motd
  102. if [[ "${SSH_ROOT}" == "yes" ]]; then
  103. $STD rc-update add sshd
  104. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  105. $STD /etc/init.d/sshd start
  106. fi
  107. }
  108. customize() {
  109. if [[ "$PASSWORD" == "" ]]; then
  110. msg_info "Customizing Container"
  111. bash -c "passwd -d root" >/dev/null 2>&1
  112. msg_ok "Customized Container"
  113. fi
  114. }