install.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env bash
  2. # Data Path
  3. # DataPath=/usr/local/etc/nginx-ui
  4. # Bin Path
  5. # BinPath=/usr/local/bin/nginx-ui
  6. # Service Path
  7. ServicePath=/usr/lib/systemd/system/nginx-ui.service
  8. PROXY=""
  9. ## Demo function for processing parameters
  10. judgment_parameters() {
  11. while [[ "$#" -gt '0' ]]; do
  12. case "$1" in
  13. '-p' | '--proxy')
  14. if [[ -z "$2" ]]; then
  15. echo "error: Please specify the proxy server address."
  16. exit 1
  17. fi
  18. PROXY="$2"
  19. shift
  20. ;;
  21. esac
  22. shift
  23. done
  24. }
  25. check_if_running_as_root() {
  26. # If you want to run as another user, please modify $EUID to be owned by this user
  27. if [[ "$EUID" -ne '0' ]]; then
  28. echo "error: You must run this script as root!"
  29. exit 1
  30. fi
  31. }
  32. identify_the_operating_system_and_architecture() {
  33. if [[ "$(uname)" == 'Linux' ]]; then
  34. case "$(uname -m)" in
  35. 'i386' | 'i686')
  36. MACHINE='32'
  37. ;;
  38. 'amd64' | 'x86_64')
  39. MACHINE='64'
  40. ;;
  41. 'armv8' | 'aarch64')
  42. MACHINE='arm64-v8a'
  43. ;;
  44. *)
  45. echo "error: The architecture is not supported."
  46. exit 1
  47. ;;
  48. esac
  49. if [[ ! -f '/etc/os-release' ]]; then
  50. echo "error: Don't use outdated Linux distributions."
  51. exit 1
  52. fi
  53. # Do not combine this judgment condition with the following judgment condition.
  54. ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  55. if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  56. true
  57. elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  58. true
  59. else
  60. echo "error: Only Linux distributions using systemd are supported."
  61. exit 1
  62. fi
  63. if [[ "$(type -P apt)" ]]; then
  64. PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  65. # PACKAGE_MANAGEMENT_REMOVE='apt purge'
  66. elif [[ "$(type -P dnf)" ]]; then
  67. PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  68. # PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  69. elif [[ "$(type -P yum)" ]]; then
  70. PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  71. # PACKAGE_MANAGEMENT_REMOVE='yum remove'
  72. elif [[ "$(type -P zypper)" ]]; then
  73. PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  74. # PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  75. elif [[ "$(type -P pacman)" ]]; then
  76. PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  77. # PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  78. else
  79. echo "error: The script does not support the package manager in this operating system."
  80. exit 1
  81. fi
  82. else
  83. echo "error: This operating system is not supported."
  84. exit 1
  85. fi
  86. }
  87. install_software() {
  88. package_name="$1"
  89. file_to_detect="$2"
  90. type -P "$file_to_detect" >/dev/null 2>&1 && return
  91. if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  92. echo "info: $package_name is installed."
  93. else
  94. echo "error: Installation of $package_name failed, please check your network."
  95. exit 1
  96. fi
  97. }
  98. download() {
  99. LATEST_RELEASE=$(curl -L -s -H 'Accept: application/json' "$PROXY"https://github.com/0xJacky/nginx-ui/releases/latest)
  100. # shellcheck disable=SC2001
  101. LATEST_VERSION=$(echo "$LATEST_RELEASE" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
  102. DOWNLOAD_LINK=$PROXY"https://github.com/0xJacky/nginx-ui/releases/download/$LATEST_VERSION/nginx-ui-linux-$MACHINE.tar.gz"
  103. echo "Downloading NginxUI archive: $DOWNLOAD_LINK"
  104. if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$TAR_FILE" "$DOWNLOAD_LINK"; then
  105. echo 'error: Download failed! Please check your network or try again.'
  106. return 1
  107. fi
  108. return 0
  109. }
  110. decompression() {
  111. if ! unzip -q "$1" -d "$TMP_DIRECTORY"; then
  112. echo 'error: Nginx UI decompression failed.'
  113. "rm" -r "$TMP_DIRECTORY"
  114. echo "removed: $TMP_DIRECTORY"
  115. exit 1
  116. fi
  117. echo "info: Extract the Nginx UI package to $TMP_DIRECTORY and prepare it for installation."
  118. }
  119. install_bin() {
  120. NAME="nginx-ui"
  121. install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  122. }
  123. install_service() {
  124. install -m 644 "${TMP_DIRECTORY}/nginx-ui.service" "$ServicePath"
  125. }
  126. start_nginx_ui() {
  127. if [[ -f ServicePath ]]; then
  128. systemctl start nginx-ui
  129. sleep 1s
  130. if systemctl -q is-active nginx-ui; then
  131. echo 'info: Start the Nginx UI service.'
  132. else
  133. echo 'error: Failed to start the Nginx UI service.'
  134. exit 1
  135. fi
  136. fi
  137. }
  138. stop_nginx_ui() {
  139. if ! systemctl stop nginx-ui; then
  140. echo 'error: Failed to stop the Nginx UI service.'
  141. exit 1
  142. fi
  143. echo 'info: Nginx UI service Stopped.'
  144. }
  145. main() {
  146. check_if_running_as_root
  147. judgment_parameters "$@"
  148. # TMP
  149. TMP_DIRECTORY="$(mktemp -d)"
  150. # Tar
  151. TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$ARCH.tar.gz"
  152. install_software 'curl' 'curl'
  153. download
  154. decompression "$TAR_FILE"
  155. install_bin
  156. install_service
  157. start_nginx_ui
  158. stop_nginx_ui
  159. systemctl start nginx-ui
  160. systemctl enable nginx-ui
  161. sleep 1s
  162. if systemctl -q is-active nginx-ui; then
  163. echo "info: Start and enable the Nginx UI service."
  164. else
  165. echo "warning: Failed to enable and start the Nginx UI service."
  166. fi
  167. }
  168. main "$@"