alpine-install.func 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf
  25. $STD sysctl -p
  26. fi
  27. }
  28. catch_errors() {
  29. set -Eeuo pipefail
  30. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  31. }
  32. error_handler() {
  33. local exit_code="$?"
  34. local line_number="$1"
  35. local command="$2"
  36. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  37. echo -e "\n$error_message\n"
  38. }
  39. msg_info() {
  40. local msg="$1"
  41. echo -ne " ${HOLD} ${YW}${msg}..."
  42. }
  43. msg_ok() {
  44. local msg="$1"
  45. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  46. }
  47. msg_error() {
  48. local msg="$1"
  49. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  50. }
  51. setting_up_container() {
  52. msg_info "Setting up Container OS"
  53. while [ $i -gt 0 ]; do
  54. if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" != "" ]; then
  55. break
  56. fi
  57. echo 1>&2 -en "${CROSS}${RD} No Network! "
  58. sleep $RETRY_EVERY
  59. i=$((i - 1))
  60. done
  61. if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" = "" ]; then
  62. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  63. echo -e " 🖧 Check Network Settings"
  64. exit 1
  65. fi
  66. cat <<EOF >/etc/apk/repositories
  67. https://dl-cdn.alpinelinux.org/alpine/edge/main
  68. https://dl-cdn.alpinelinux.org/alpine/edge/community
  69. https://dl-cdn.alpinelinux.org/alpine/edge/testing
  70. EOF
  71. msg_ok "Set up Container OS"
  72. msg_ok "Network Connected: ${BL}$(ip addr show | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 | tail -n1)${CL}"
  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 apk update
  95. $STD apk 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. if [[ "${SSH_ROOT}" == "yes" ]]; then
  102. $STD rc-update add sshd
  103. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  104. $STD /etc/init.d/sshd start
  105. fi
  106. }
  107. customize() {
  108. if [[ "$PASSWORD" == "" ]]; then
  109. msg_info "Customizing Container"
  110. bash -c "passwd -d root" >/dev/null 2>&1
  111. msg_ok "Customized Container"
  112. fi
  113. }