install-dependencies.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. set -ex
  19. function detect_aur_helper() {
  20. if [[ $(command -v yay) ]]; then
  21. AUR_HELPER=yay
  22. elif [[ $(command -v pacaur) ]]; then
  23. AUR_HELPER=pacaur
  24. else
  25. echo No available AUR helpers found. Please specify your AUR helper by AUR_HELPER.
  26. exit 255
  27. fi
  28. }
  29. function install_dependencies_with_aur() {
  30. detect_aur_helper
  31. $AUR_HELPER -S openresty --noconfirm
  32. sudo pacman -S openssl --noconfirm
  33. export OPENRESTY_PREFIX=/opt/openresty
  34. sudo mkdir $OPENRESTY_PREFIX/openssl
  35. sudo ln -s /usr/include $OPENRESTY_PREFIX/openssl/include
  36. sudo ln -s /usr/lib $OPENRESTY_PREFIX/openssl/lib
  37. }
  38. # Install dependencies on centos and fedora
  39. function install_dependencies_with_yum() {
  40. sudo yum install -y yum-utils
  41. sudo yum-config-manager --add-repo "https://openresty.org/package/${1}/openresty.repo"
  42. if [[ "${1}" == "centos" ]]; then
  43. sudo yum -y install centos-release-scl
  44. sudo yum -y install devtoolset-9 patch wget
  45. set +eu
  46. source scl_source enable devtoolset-9
  47. set -eu
  48. fi
  49. sudo yum install -y \
  50. gcc gcc-c++ curl wget unzip xz gnupg perl-ExtUtils-Embed cpanminus patch libyaml-devel \
  51. perl perl-devel pcre pcre-devel openldap-devel \
  52. openresty-zlib-devel openresty-pcre-devel
  53. }
  54. # Install dependencies on ubuntu and debian
  55. function install_dependencies_with_apt() {
  56. # add OpenResty source
  57. sudo apt-get update
  58. sudo apt-get -y install software-properties-common wget lsb-release gnupg patch
  59. wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
  60. arch=$(uname -m | tr '[:upper:]' '[:lower:]')
  61. arch_path=""
  62. if [[ $arch == "arm64" ]] || [[ $arch == "aarch64" ]]; then
  63. arch_path="arm64/"
  64. fi
  65. if [[ "${1}" == "ubuntu" ]]; then
  66. sudo add-apt-repository -y "deb http://openresty.org/package/${arch_path}ubuntu $(lsb_release -sc) main"
  67. elif [[ "${1}" == "debian" ]]; then
  68. sudo add-apt-repository -y "deb http://openresty.org/package/${arch_path}debian $(lsb_release -sc) openresty"
  69. fi
  70. sudo apt-get update
  71. # install some compilation tools
  72. sudo apt-get install -y curl make gcc g++ cpanminus libpcre3 libpcre3-dev libldap2-dev libyaml-dev unzip openresty-zlib-dev openresty-pcre-dev
  73. }
  74. # Identify the different distributions and call the corresponding function
  75. function multi_distro_installation() {
  76. if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
  77. install_dependencies_with_yum "centos"
  78. elif grep -Eqi -e "Red Hat" -e "rhel" /etc/*-release; then
  79. install_dependencies_with_yum "rhel"
  80. elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
  81. install_dependencies_with_yum "fedora"
  82. elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
  83. install_dependencies_with_apt "debian"
  84. elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
  85. install_dependencies_with_apt "ubuntu"
  86. elif grep -Eqi "Arch" /etc/issue || grep -Eqi "EndeavourOS" /etc/issue || grep -Eq "Arch" /etc/*-release; then
  87. install_dependencies_with_aur
  88. else
  89. echo "Non-supported distribution, APISIX is only supported on Linux-based systems"
  90. exit 1
  91. fi
  92. install_apisix_runtime
  93. }
  94. function multi_distro_uninstallation() {
  95. if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
  96. sudo yum autoremove -y openresty-zlib-devel openresty-pcre-devel
  97. elif grep -Eqi -e "Red Hat" -e "rhel" /etc/*-release; then
  98. sudo yum autoremove -y openresty-zlib-devel openresty-pcre-devel
  99. elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
  100. sudo yum autoremove -y openresty-zlib-devel openresty-pcre-devel
  101. elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
  102. sudo apt-get autoremove -y openresty-zlib-dev openresty-pcre-dev
  103. elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
  104. sudo apt-get autoremove -y openresty-zlib-dev openresty-pcre-dev
  105. else
  106. echo "Non-supported distribution, APISIX is only supported on Linux-based systems"
  107. exit 1
  108. fi
  109. }
  110. function install_apisix_runtime() {
  111. export runtime_version=${APISIX_RUNTIME:?}
  112. wget "https://raw.githubusercontent.com/api7/apisix-build-tools/apisix-runtime/${APISIX_RUNTIME}/build-apisix-runtime.sh"
  113. chmod +x build-apisix-runtime.sh
  114. ./build-apisix-runtime.sh latest
  115. rm build-apisix-runtime.sh
  116. }
  117. # Install LuaRocks
  118. function install_luarocks() {
  119. if [ -f "./utils/linux-install-luarocks.sh" ]; then
  120. ./utils/linux-install-luarocks.sh
  121. elif [ -f "./linux-install-luarocks.sh" ]; then
  122. ./linux-install-luarocks.sh
  123. else
  124. echo "Installing luarocks from remote master branch"
  125. curl https://raw.githubusercontent.com/apache/apisix/master/utils/linux-install-luarocks.sh -sL | bash -
  126. fi
  127. }
  128. # Entry
  129. function main() {
  130. OS_NAME=$(uname -s | tr '[:upper:]' '[:lower:]')
  131. if [[ "$#" == 0 ]]; then
  132. if [[ "${OS_NAME}" == "linux" ]]; then
  133. multi_distro_installation
  134. install_luarocks
  135. return
  136. else
  137. echo "Non-supported distribution, APISIX is only supported on Linux-based systems"
  138. exit 1
  139. fi
  140. fi
  141. case_opt=$1
  142. case "${case_opt}" in
  143. "install_luarocks")
  144. install_luarocks
  145. ;;
  146. "uninstall")
  147. if [[ "${OS_NAME}" == "linux" ]]; then
  148. multi_distro_uninstallation
  149. else
  150. echo "Non-supported distribution, APISIX is only supported on Linux-based systems"
  151. fi
  152. ;;
  153. *)
  154. echo "Unsupported method: ${case_opt}"
  155. ;;
  156. esac
  157. }
  158. main "$@"