07env-install-centos.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. #!/usr/bin/env bash
  2. #-----------------------------------------------------------------------------------------------------
  3. # Nodejs 下载地址
  4. # 镜像1: https://nodejs.org/dist/ (https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.gz)
  5. # OracleJDK 下载地址
  6. # 镜像1: https://www.injdk.cn/ (https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz) (https://d6.injdk.cn/oraclejdk/8/jdk-8u341-linux-x64.tar.gz)
  7. # 镜像2: http://www.codebaoku.com/jdk/jdk-index.html (https://114-233-56-217.d.cjjd09.com:30443/123-511/85b2a0b8/1661483-0/85b2a0b8c16863b3ad55e742ac2ba9ff?v=3&t=1667188559&s=f2df45083bb69864446f801bf25b725c&i=dde8a980&filename=jdk-11.0.15.1_linux-x64_bin.tar.gz&d=6ee52da1)
  8. #-----------------------------------------------------------------------------------------------------
  9. # 配置
  10. ADD_USER_NAME="www"
  11. ADD_USER_PASSWORD="MoXF2Zi6u7f7lqZu"
  12. BASE_DIR="/opt"
  13. # 软件
  14. YUM_REPOS="http://mirrors.aliyun.com/repo/Centos-7.repo"
  15. #--- jdk(安装)
  16. JDK_URL="https://d6.injdk.cn/oraclejdk/8/jdk-8u341-linux-x64.tar.gz"
  17. JDK_FILE_NAME="jdk-8u341-linux-x64.tar.gz"
  18. JDK_DIR_NAME="jdk1.8.0_341"
  19. #--- jdk8(参考)
  20. JDK8_URL="https://d6.injdk.cn/oraclejdk/8/jdk-8u341-linux-x64.tar.gz"
  21. JDK8_FILE_NAME="jdk-8u341-linux-x64.tar.gz"
  22. JDK8_DIR_NAME="jdk1.8.0_341"
  23. #--- jdk11(参考)
  24. JDK11_URL="https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz"
  25. JDK11_FILE_NAME="jdk-11.0.16.1_linux-x64_bin.tar.gz"
  26. JDK11_DIR_NAME="jdk-11.0.16.1"
  27. #--- gradle (6.8.3 | 6.9.3 | 7.4.2 | 7.5.1)
  28. GRADLE_URL="https://downloads.gradle-dn.com/distributions/gradle-6.8.3-bin.zip"
  29. GRADLE_FILE_NAME="gradle-6.8.3-bin.zip"
  30. GRADLE_DIR_NAME="gradle-6.8.3"
  31. #--- maven (3.8.6 | 3.6.3 | 3.5.4 | 3.3.9)
  32. MAVEN_URL="https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz"
  33. MAVEN_FILE_NAME="apache-maven-3.8.6-bin.tar.gz"
  34. MAVEN_DIR_NAME="apache-maven-3.8.6"
  35. #--- jenkins (http://mirrors.jenkins.io/war-stable/latest/)
  36. JENKINS_URL="https://ftp-nyc.osuosl.org/pub/jenkins/war-stable/latest/jenkins.war"
  37. JENKINS_FILE_NAME="jenkins.war"
  38. JENKINS_JAVA_URL="https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz"
  39. JENKINS_JAVA_FILE_NAME="jdk-11.0.16.1_linux-x64_bin.tar.gz"
  40. JENKINS_JAVA_DIR_NAME="jdk-11.0.16.1"
  41. #--- redis Latest Stable
  42. REDIS_URL="https://download.redis.io/redis-stable.tar.gz"
  43. REDIS_FILE_NAME="redis-stable.tar.gz"
  44. REDIS_DIR_NAME="redis-stable"
  45. # 安装标识
  46. #--- SSH用户
  47. SSH_OPTIONS="-o ConnectTimeout=600 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  48. SSH_USER=""
  49. SSH_PASSWORD=""
  50. SSH_PRIVATE_KEY=""
  51. SSH_PORT=""
  52. #--- SUDO用户
  53. SUDO_TAG="0"
  54. SUDO_USER=""
  55. SUDO_PASSWORD=""
  56. #--- 指令
  57. INIT_TAG="0"
  58. #--- 指令参数
  59. UPDATE_YUM_REPOS="1"
  60. CONFIG_TAG="1"
  61. ADD_USER_TAG="1"
  62. GIT_TAG="1"
  63. DSTAT_TAG="1"
  64. HTOP_TAG="1"
  65. NGINX_TAG="1"
  66. NODEJS_TAG="1"
  67. JAVA_TAG="1"
  68. GRADLE_TAG="1"
  69. MAVEN_TAG="1"
  70. JENKINS_TAG="1"
  71. REDIS_TAG="1"
  72. # 脚本设置
  73. TMP_DIR="$(rm -rf /tmp/env-install* && mktemp -d -t env-install.XXXXXXXXXX)"
  74. LOG_FILE="${TMP_DIR}/env-install.log"
  75. ERROR_INFO="\n\033[31mERROR Summary: \033[0m\n "
  76. ACCESS_INFO="\n\033[32mACCESS Summary: \033[0m\n "
  77. SCRIPT_PARAMETER="$*"
  78. COMMAND_OUTPUT=""
  79. ######################################################################################################
  80. # 通用函数
  81. ######################################################################################################
  82. # 信号处理
  83. function trap::info() {
  84. [[ ${#ERROR_INFO} -gt 37 ]] && echo -e "$ERROR_INFO"
  85. [[ ${#ACCESS_INFO} -gt 38 ]] && echo -e "$ACCESS_INFO"
  86. [ -f "$LOG_FILE" ] && echo -e "\n\n See detailed log >>> cat $LOG_FILE \n\n"
  87. trap '' EXIT
  88. exit
  89. }
  90. # 错误日志
  91. function log::error() {
  92. local item; item="[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')]: \033[31mERROR: \033[0m$*"
  93. ERROR_INFO="${ERROR_INFO}${item}\n "
  94. echo -e "${item}" | tee -a "$LOG_FILE"
  95. }
  96. # 基础日志
  97. function log::info() {
  98. printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  99. }
  100. # 警告日志
  101. function log::warning() {
  102. printf "[%s]: \033[33mWARNING: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  103. }
  104. # 访问信息
  105. function log::access() {
  106. ACCESS_INFO="${ACCESS_INFO}$*\n "
  107. printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  108. }
  109. # 执行日志
  110. function log::exec() {
  111. printf "[%s]: \033[34mEXEC: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" >> "$LOG_FILE"
  112. }
  113. # 检查返回码
  114. function check::exit_code() {
  115. local code=${1:-}
  116. local app=${2:-}
  117. local desc=${3:-}
  118. local exit_script=${4:-}
  119. if [[ "${code}" == "0" ]]; then
  120. log::info "[${app}]" "${desc} succeeded."
  121. else
  122. log::error "[${app}]" "${desc} failed."
  123. [[ "$exit_script" == "exit" ]] && exit "$code"
  124. fi
  125. }
  126. # 重试
  127. function utils::retry() {
  128. local retries=$1
  129. shift
  130. local count=0
  131. until eval "$*"; do
  132. exit=$?
  133. wait=$((2 ** count))
  134. count=$((count + 1))
  135. if [ "$count" -lt "$retries" ]; then
  136. echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
  137. sleep $wait
  138. else
  139. echo "Retry $count/$retries exited $exit, no more retries left."
  140. return $exit
  141. fi
  142. done
  143. return 0
  144. }
  145. # 转义引号
  146. function utils::quote() {
  147. # shellcheck disable=SC2046
  148. if [ $(echo "$*" | tr -d "\n" | wc -c) -eq 0 ]; then
  149. echo "''"
  150. elif [ $(echo "$*" | tr -d "[a-z][A-Z][0-9]:,.=~_/\n-" | wc -c) -gt 0 ]; then
  151. printf "%s" "$*" | sed -e "1h;2,\$H;\$!d;g" -e "s/'/\'\"\'\"\'/g" | sed -e "1h;2,\$H;\$!d;g" -e "s/^/'/g" -e "s/$/'/g"
  152. else
  153. echo "$*"
  154. fi
  155. }
  156. # 检查命令是否存在
  157. function check::command_exists() {
  158. local cmd=${1}
  159. local package=${2}
  160. if command -V "$cmd" > /dev/null 2>&1; then
  161. log::info "[check]" "$cmd command exists."
  162. else
  163. log::warning "[check]" "I require $cmd but it's not installed."
  164. log::warning "[check]" "install $package package."
  165. command::exec "127.0.0.1" "yum install -y ${package}"
  166. check::exit_code "$?" "check" "$package install" "exit"
  167. fi
  168. }
  169. # 执行命令
  170. function command::exec() {
  171. local host=${1:-"127.0.0.1"}
  172. shift
  173. local command="$*"
  174. if [[ "${SUDO_TAG:-}" == "1" ]]; then
  175. sudo_options="sudo -H -n -u ${SUDO_USER}"
  176. if [[ "${SUDO_PASSWORD:-}" != "" ]]; then
  177. sudo_options="${sudo_options// -n/} -p \"\" -S <<< \"${SUDO_PASSWORD}\""
  178. fi
  179. command="$sudo_options bash -c $(utils::quote "$command")"
  180. fi
  181. command="$(utils::quote "$command")"
  182. if [[ "${host}" == "127.0.0.1" ]]; then
  183. # 本地执行
  184. log::exec "[command]" "bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")"
  185. # shellcheck disable=SC2094
  186. COMMAND_OUTPUT=$(eval bash -c "${command}" 2>> "$LOG_FILE" | tee -a "$LOG_FILE")
  187. local status=$?
  188. else
  189. # 远程执行
  190. local ssh_cmd="ssh"
  191. if [[ "${SSH_PASSWORD}" != "" ]]; then
  192. ssh_cmd="sshpass -p \"${SSH_PASSWORD}\" ${ssh_cmd}"
  193. elif [[ "$SSH_PRIVATE_KEY" != "" ]]; then
  194. [ -f "${SSH_PRIVATE_KEY}" ] || { log::error "[exec]" "ssh private_key:${SSH_PRIVATE_KEY} not found."; exit 1; }
  195. ssh_cmd="${ssh_cmd} -i $SSH_PRIVATE_KEY"
  196. fi
  197. log::exec "[command]" "${ssh_cmd//${SSH_PASSWORD:-}/zzzzzz} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT} bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")"
  198. # shellcheck disable=SC2094
  199. COMMAND_OUTPUT=$(eval "${ssh_cmd} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT}" bash -c '"${command}"' 2>> "$LOG_FILE" | tee -a "$LOG_FILE")
  200. local status=$?
  201. fi
  202. return $status
  203. }
  204. ######################################################################################################
  205. # 安装函数
  206. ######################################################################################################
  207. # 检查用到的命令
  208. function check::command() {
  209. check::command_exists ssh openssh-clients
  210. check::command_exists tar tar
  211. check::command_exists wget wget
  212. check::command_exists unzip unzip
  213. }
  214. # 更新yum源
  215. function init:update_yum_repos() {
  216. log::info "[update-yum-repos]" "更新yum源..."
  217. local host="127.0.0.1"
  218. local repo="/etc/yum.repos.d/CentOS-Base.repo"
  219. local repo_bak="/etc/yum.repos.d/CentOS-Base.repo_bak"
  220. command::exec "${host}" "
  221. if [ ! -f "$repo_bak" ]; then
  222. mv $repo $repo_bak
  223. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${YUM_REPOS} -O $repo
  224. yum clean all
  225. yum makecache
  226. yum -y install epel-release
  227. fi
  228. "
  229. check::exit_code "$?" "update-yum-repos" "$host 更新yum源" "exit"
  230. log::info "[update-yum-repos]" "yum源更新成功!"
  231. }
  232. # 系统配置
  233. function init:config() {
  234. log::info "[config]" "关闭系统防火墙..."
  235. local host="127.0.0.1"
  236. command::exec "${host}" "
  237. # 停止防火墙
  238. systemctl stop firewalld
  239. # 彻底关闭防火墙
  240. systemctl disable firewalld
  241. "
  242. log::info "[config]" "系统防火墙关闭成功!"
  243. }
  244. # 新增用户
  245. function init:add_user() {
  246. log::info "[add-user]" "新增用户[${ADD_USER_NAME}]..."
  247. local host="127.0.0.1"
  248. local add_sudoers="${ADD_USER_NAME} ALL=(ALL) NOPASSWD:ALL"
  249. command::exec "${host}" "
  250. id -u ${ADD_USER_NAME} >/dev/null 2>&1
  251. if [ \$? -ne '0' ]; then
  252. # 创建用户
  253. adduser ${ADD_USER_NAME}
  254. # 设置密码
  255. echo ${ADD_USER_NAME}:${ADD_USER_PASSWORD} | chpasswd
  256. fi
  257. if [ \`grep -c '$add_sudoers' '/etc/sudoers'\` == '0' ];then
  258. # 新增修改权限
  259. chmod -v u+w /etc/sudoers
  260. echo -e '$add_sudoers' >> /etc/sudoers
  261. # 收回修改权限
  262. chmod -v u-w /etc/sudoers
  263. fi
  264. "
  265. check::exit_code "$?" "add-user" "$host 新增用户[${ADD_USER_NAME}]" "exit"
  266. log::info "[add-user]" "新增用户[${ADD_USER_NAME}]成功!"
  267. }
  268. # 安装git环境
  269. function init:git() {
  270. log::info "[git]" "安装git环境..."
  271. local host="127.0.0.1"
  272. local exc_cmd="git config --global credential.helper store"
  273. command::exec "${host}" "
  274. yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
  275. yum -y install git
  276. git --version
  277. $exc_cmd
  278. "
  279. check::exit_code "$?" "git" "$host 安装git环境" "exit"
  280. log::info "[git]" "git环境安装成功!"
  281. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  282. command::exec "${host}" "
  283. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c '$exc_cmd'
  284. "
  285. check::exit_code "$?" "git" "$host git配置" "exit"
  286. log::info "[git]" "git配置成功!"
  287. fi
  288. }
  289. # 安装dstat
  290. function init:dstat() {
  291. log::info "[dstat]" "安装dstat..."
  292. local host="127.0.0.1"
  293. command::exec "${host}" "
  294. yum -y install dstat
  295. dstat -V
  296. "
  297. check::exit_code "$?" "dstat" "$host 安装dstat" "exit"
  298. log::info "[dstat]" "dstat安装成功!"
  299. }
  300. # 安装htop
  301. function init:htop() {
  302. log::info "[htop]" "安装htop..."
  303. local host="127.0.0.1"
  304. command::exec "${host}" "
  305. yum -y install htop
  306. htop -v
  307. "
  308. check::exit_code "$?" "htop" "$host 安装htop" "exit"
  309. log::info "[htop]" "htop安装成功!"
  310. }
  311. # 安装nginx
  312. function init:nginx() {
  313. log::info "[nginx]" "安装nginx..."
  314. local host="127.0.0.1"
  315. command::exec "${host}" "
  316. yum -y install nginx
  317. nginx -v
  318. "
  319. check::exit_code "$?" "nginx" "$host 安装nginx" "exit"
  320. log::info "[nginx]" "nginx安装成功!"
  321. log::info "[nginx]" "配置文件路径: /etc/nginx"
  322. log::info "[nginx]" "设置开机启动: sudo systemctl enable nginx"
  323. log::info "[nginx]" "启动/停止/重启: sudo service nginx start/stop/restart"
  324. log::info "[nginx]" "检查nginx配置: sudo nginx -t"
  325. log::info "[nginx]" "加载nginx配置: sudo nginx -s reload"
  326. }
  327. # 安装nodejs环境
  328. function init:nodejs() {
  329. log::info "[nodejs]" "安装nodejs环境..."
  330. local host="127.0.0.1"
  331. command::exec "${host}" "
  332. curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
  333. yum -y install nodejs
  334. npm install --global yarn
  335. # 配置npm源
  336. npm config set registry https://registry.npm.taobao.org
  337. npm config set disturl https://npm.taobao.org/dist
  338. npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
  339. npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
  340. npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
  341. # 配置yarn源
  342. yarn config set registry https://registry.npm.taobao.org -g
  343. yarn config set disturl https://npm.taobao.org/dist -g
  344. yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g
  345. yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g
  346. yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g
  347. yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g
  348. yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g
  349. yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g
  350. # 验证
  351. node -v
  352. npm -v
  353. yarn -v
  354. "
  355. check::exit_code "$?" "nodejs" "$host 安装nodejs环境" "exit"
  356. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  357. command::exec "${host}" "
  358. # 配置npm源
  359. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set registry https://registry.npm.taobao.org'
  360. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set disturl https://npm.taobao.org/dist'
  361. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set electron_mirror https://npm.taobao.org/mirrors/electron/'
  362. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/'
  363. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/'
  364. # 配置yarn源
  365. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set registry https://registry.npm.taobao.org -g'
  366. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set disturl https://npm.taobao.org/dist -g'
  367. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g'
  368. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g'
  369. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g'
  370. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g'
  371. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g'
  372. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g'
  373. "
  374. check::exit_code "$?" "nodejs" "$host nodejs配置" "exit"
  375. log::info "[nodejs]" "nodejs配置成功!"
  376. fi
  377. log::info "[nodejs]" "安装nodejs环境成功!"
  378. }
  379. # 安装java环境
  380. function init:java() {
  381. log::info "[java]" "安装java环境..."
  382. local host="127.0.0.1"
  383. local java_path="${BASE_DIR}/java/${JDK_DIR_NAME}"
  384. local line="\n"
  385. local add_profile=`echo -e "${line}JAVA_HOME=${java_path}${line}PATH=\\\$JAVA_HOME/bin:\\\$PATH${line}export JAVA_HOME PATH"`
  386. command::exec "${host}" "
  387. yum -y remove *openjdk*
  388. if [ ! -d "$java_path" ]; then
  389. mkdir -p ${BASE_DIR}/java
  390. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JDK_URL} -O ${BASE_DIR}/java/${JDK_FILE_NAME}
  391. cd ${BASE_DIR}/java
  392. tar -zxvf ${JDK_FILE_NAME}
  393. echo '$add_profile' >> /etc/profile
  394. source /etc/profile
  395. fi
  396. java -version
  397. "
  398. check::exit_code "$?" "java" "$host 安装java环境" "exit"
  399. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  400. command::exec "${host}" "
  401. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/java
  402. "
  403. fi
  404. log::info "[java]" "java环境安装成功!"
  405. }
  406. # 安装gradle环境
  407. function init:gradle() {
  408. log::info "[gradle]" "安装gradle环境..."
  409. local host="127.0.0.1"
  410. local gradle_path="${BASE_DIR}/gradle/${GRADLE_DIR_NAME}"
  411. local line="\n"
  412. local add_profile=`echo -e "${line}GRADLE_HOME=${gradle_path}${line}PATH=\\\$GRADLE_HOME/bin:\\\$PATH${line}export GRADLE_HOME PATH"`
  413. command::exec "${host}" "
  414. if [ ! -d "$gradle_path" ]; then
  415. mkdir -p ${BASE_DIR}/gradle
  416. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${GRADLE_URL} -O ${BASE_DIR}/gradle/${GRADLE_FILE_NAME}
  417. cd ${BASE_DIR}/gradle
  418. unzip ${GRADLE_FILE_NAME}
  419. echo '$add_profile' >> /etc/profile
  420. source /etc/profile
  421. fi
  422. gradle -v
  423. "
  424. check::exit_code "$?" "gradle" "$host 安装gradle环境" "exit"
  425. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  426. command::exec "${host}" "
  427. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/gradle
  428. "
  429. fi
  430. log::info "[gradle]" "gradle环境安装成功!"
  431. }
  432. # 安装maven环境
  433. function init:maven() {
  434. log::info "[maven]" "安装maven环境..."
  435. local host="127.0.0.1"
  436. local maven_path="${BASE_DIR}/maven/${MAVEN_DIR_NAME}"
  437. local line="\n"
  438. local add_profile=`echo -e "${line}MAVEN_HOME=${maven_path}${line}PATH=\\\$MAVEN_HOME/bin:\\\$PATH${line}export MAVEN_HOME PATH"`
  439. command::exec "${host}" "
  440. if [ ! -d "$maven_path" ]; then
  441. mkdir -p ${BASE_DIR}/maven
  442. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${MAVEN_URL} -O ${BASE_DIR}/maven/${MAVEN_FILE_NAME}
  443. cd ${BASE_DIR}/maven
  444. tar -zxvf ${MAVEN_FILE_NAME}
  445. echo '$add_profile' >> /etc/profile
  446. source /etc/profile
  447. fi
  448. mvn -v
  449. "
  450. check::exit_code "$?" "maven" "$host 安装maven环境" "exit"
  451. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  452. command::exec "${host}" "
  453. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/maven
  454. "
  455. fi
  456. log::info "[maven]" "maven环境安装成功!"
  457. }
  458. # 安装jenkins环境
  459. function init:jenkins() {
  460. log::info "[jenkins]" "安装jenkins环境..."
  461. local host="127.0.0.1"
  462. local java_path="${BASE_DIR}/jenkins/${JENKINS_JAVA_DIR_NAME}"
  463. local jenkins_file="${BASE_DIR}/jenkins/${JENKINS_FILE_NAME}"
  464. local java_mem_opts=" -DappName=jenkins -server -Xmx1g"
  465. local database_opts=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  466. local java_opts_ext=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  467. command::exec "${host}" "
  468. if [ ! -d "$java_path" ]; then
  469. mkdir -p ${BASE_DIR}/jenkins
  470. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JENKINS_JAVA_URL} -O ${BASE_DIR}/jenkins/${JENKINS_JAVA_FILE_NAME}
  471. cd ${BASE_DIR}/jenkins
  472. tar -zxvf ${JENKINS_JAVA_FILE_NAME}
  473. $java_path/bin/java -version
  474. fi
  475. if [ ! -f "$jenkins_file" ]; then
  476. mkdir -p ${BASE_DIR}/jenkins
  477. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JENKINS_URL} -O ${BASE_DIR}/jenkins/${JENKINS_FILE_NAME}
  478. cd ${BASE_DIR}/jenkins
  479. fi
  480. # 写文件start.sh文件
  481. cat << EOF > ${BASE_DIR}/jenkins/start.sh
  482. #!/bin/bash
  483. httpPort=\\\${1:-'30003'}
  484. #-------------------------------------------------------------------
  485. java='$java_path/bin/java'
  486. jar_file='${BASE_DIR}/jenkins/jenkins.war'
  487. java_mem_opts=' -DappName=jenkins $java_opts_ext'
  488. database_opts='$database_opts'
  489. java_opts_ext='$java_opts_ext'
  490. #-------------------------------------------------------------------
  491. pid=\\\`ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  492. if [ -z \\\$pid ];then
  493. nohup \\\$java \\\$java_mem_opts \\\$database_opts \\\$java_opts_ext -jar \\\$jar_file --httpPort=\\\$httpPort >${BASE_DIR}/jenkins/logs.log 2>&1 &
  494. echo 'jenkins启动成功!'
  495. else
  496. echo 'jenkins正在运行...'
  497. fi
  498. echo '查看日志: tail -F ${BASE_DIR}/jenkins/logs.log -n 100'
  499. EOF
  500. # 写文件kill.sh文件
  501. cat << EOF > ${BASE_DIR}/jenkins/kill.sh
  502. #!/bin/bash
  503. pid=\\\`ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  504. if [ -z \\\$pid ];then
  505. echo 'jenkins未运行'
  506. else
  507. ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}' | xargs kill
  508. echo 'jenkins已停止!'
  509. fi
  510. EOF
  511. chmod +x ${BASE_DIR}/jenkins/start.sh
  512. chmod +x ${BASE_DIR}/jenkins/kill.sh
  513. "
  514. check::exit_code "$?" "jenkins" "$host 安装jenkins环境" "exit"
  515. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  516. command::exec "${host}" "
  517. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/jenkins
  518. "
  519. fi
  520. log::info "[jenkins]" "jenkins环境安装成功!"
  521. log::info "[jenkins]" "启动jenkins: ${BASE_DIR}/jenkins/start.sh 30003"
  522. log::info "[jenkins]" "停止jenkins: ${BASE_DIR}/jenkins/kill.sh"
  523. }
  524. # 安装redis环境
  525. function init:redis() {
  526. log::info "[redis]" "安装redis环境..."
  527. local host="127.0.0.1"
  528. local redis_path="${BASE_DIR}/redis/${REDIS_DIR_NAME}"
  529. local redis_install_path="${BASE_DIR}/redis/${REDIS_DIR_NAME}-install"
  530. command::exec "${host}" "
  531. if [ ! -d "$redis_path" ]; then
  532. mkdir -p ${BASE_DIR}/redis
  533. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${REDIS_URL} -O ${BASE_DIR}/redis/${REDIS_FILE_NAME}
  534. cd ${BASE_DIR}/redis
  535. tar -zxvf ${REDIS_FILE_NAME}
  536. cd $redis_path
  537. yum -y install gcc
  538. yum -y install centos-release-scl
  539. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  540. echo 'source /opt/rh/devtoolset-9/enable' >> /etc/profile
  541. source /etc/profile
  542. gcc -v
  543. mkdir -p $redis_install_path
  544. cd $redis_path
  545. make install PREFIX=$redis_install_path
  546. # 写文件start.sh文件
  547. cat << EOF > $redis_install_path/redis.conf
  548. # bind 127.0.0.1
  549. protected-mode yes
  550. port 6379
  551. tcp-backlog 511
  552. timeout 300
  553. tcp-keepalive 300
  554. daemonize yes
  555. supervised auto
  556. pidfile $redis_install_path/redis.pid
  557. loglevel notice
  558. logfile $redis_install_path/redis.log
  559. databases 16
  560. always-show-logo yes
  561. save 900 1
  562. save 300 10
  563. save 60 10000
  564. stop-writes-on-bgsave-error yes
  565. rdbcompression yes
  566. rdbchecksum yes
  567. dbfilename dump.rdb
  568. rdb-del-sync-files no
  569. dir $redis_install_path/
  570. replica-serve-stale-data yes
  571. replica-read-only yes
  572. repl-diskless-sync no
  573. repl-diskless-sync-delay 5
  574. repl-diskless-load disabled
  575. repl-disable-tcp-nodelay no
  576. replica-priority 100
  577. acllog-max-len 128
  578. requirepass admin123456
  579. # 限制内存4GB = 4294967296 bytes
  580. maxmemory 4294967296
  581. maxmemory-policy allkeys-lfu
  582. lazyfree-lazy-eviction no
  583. lazyfree-lazy-expire no
  584. lazyfree-lazy-server-del no
  585. replica-lazy-flush no
  586. lazyfree-lazy-user-del no
  587. oom-score-adj no
  588. oom-score-adj-values 0 200 800
  589. appendonly no
  590. appendfilename \"appendonly.aof\"
  591. appendfsync everysec
  592. no-appendfsync-on-rewrite no
  593. auto-aof-rewrite-percentage 100
  594. auto-aof-rewrite-min-size 64mb
  595. aof-load-truncated yes
  596. aof-use-rdb-preamble yes
  597. lua-time-limit 5000
  598. slowlog-log-slower-than 10000
  599. slowlog-max-len 128
  600. latency-monitor-threshold 0
  601. notify-keyspace-events \"\"
  602. hash-max-ziplist-entries 512
  603. hash-max-ziplist-value 64
  604. list-max-ziplist-size -2
  605. list-compress-depth 0
  606. set-max-intset-entries 512
  607. zset-max-ziplist-entries 128
  608. zset-max-ziplist-value 64
  609. hll-sparse-max-bytes 3000
  610. stream-node-max-bytes 4096
  611. stream-node-max-entries 100
  612. activerehashing yes
  613. client-output-buffer-limit normal 0 0 0
  614. client-output-buffer-limit replica 256mb 64mb 60
  615. client-output-buffer-limit pubsub 32mb 8mb 60
  616. hz 10
  617. dynamic-hz yes
  618. aof-rewrite-incremental-fsync yes
  619. rdb-save-incremental-fsync yes
  620. jemalloc-bg-thread yes
  621. EOF
  622. cat << EOF > $redis_install_path/start.sh
  623. #!/bin/bash
  624. #set -x
  625. set -e
  626. pid=\\\`ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  627. cd $redis_install_path
  628. if [ -z \\\$pid ];then
  629. $redis_install_path/bin/redis-server $redis_install_path/redis.conf
  630. echo 'redis启动成功!'
  631. else
  632. echo 'redis正在运行...'
  633. fi
  634. echo '查看日志: tail -F $redis_install_path/redis.log -n 100'
  635. EOF
  636. # 写文件kill.sh文件
  637. cat << EOF > $redis_install_path/kill.sh
  638. #!/bin/bash
  639. #set -x
  640. set -e
  641. pid=\\\`ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  642. if [ -z \\\$pid ];then
  643. echo 'redis未运行'
  644. else
  645. ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}' | xargs kill
  646. echo 'redis已停止!'
  647. fi
  648. EOF
  649. chmod +x $redis_install_path/start.sh
  650. chmod +x $redis_install_path/kill.sh
  651. fi
  652. "
  653. check::exit_code "$?" "redis" "$host 安装redis环境" "exit"
  654. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  655. command::exec "${host}" "
  656. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/redis
  657. "
  658. fi
  659. log::info "[redis]" "redis环境安装成功!"
  660. log::info "[redis]" "启动redis: $redis_install_path/start.sh"
  661. log::info "[redis]" "停止redis: $redis_install_path/kill.sh"
  662. }
  663. ######################################################################################################
  664. # 主调用逻辑
  665. ######################################################################################################
  666. trap trap::info 1 2 3 15 EXIT
  667. # 使用帮助
  668. function help::usage() {
  669. cat << EOF
  670. Usage:
  671. env-install-centos.sh [command]
  672. Available Commands:
  673. init 初始化系统
  674. Flag:
  675. -update-yum-repos 是否更新yum源, 默认: '1'
  676. -config 配置系统, 默认: '1'
  677. -add-user 是否新增用户, 默认: '1'
  678. -user-name 新增用户名, 默认: 'www'
  679. -base-dir 新增用户名, 默认: '/opt'
  680. -git 是否安装git环境, 默认: '1'
  681. -dstat 是否安装dstat, 默认: '1'
  682. -htop 是否安装htop, 默认: '1'
  683. -nginx 是否安装nginx, 默认: '1'
  684. -nodejs 是否安装nodejs环境, 默认: '1'
  685. -java 是否安装java环境, 默认: '1'
  686. -gradle 是否安装gradle环境, 默认: '1'
  687. -maven 是否安装maven环境, 默认: '1'
  688. -jenkins 是否安装jenkins环境, 默认: '1'
  689. -redis 是否安装redis环境, 默认: '1'
  690. Example:
  691. env-install-centos.sh init \\
  692. -update-yum-repos 0 \\
  693. -config 1 \\
  694. -add-user 1 \\
  695. -user-name www \\
  696. -git 1 \\
  697. -dstat 0 \\
  698. -htop 0 \\
  699. -nginx 0 \\
  700. -nodejs 0 \\
  701. -java 1 \\
  702. -gradle 0 \\
  703. -maven 0 \\
  704. -jenkins 0 \\
  705. -redis 0
  706. EOF
  707. exit 1
  708. }
  709. # 参数处理
  710. [ "$#" == "0" ] && help::usage
  711. while [ "${1:-}" != "" ]; do
  712. case $1 in
  713. # -------------------------------------------------------------- 指令
  714. init ) INIT_TAG="1"
  715. ;;
  716. # -------------------------------------------------------------- 指令参数
  717. -update-yum-repos ) shift
  718. UPDATE_YUM_REPOS=${1:-UPDATE_YUM_REPOS}
  719. ;;
  720. -config ) shift
  721. CONFIG_TAG=${1:-CONFIG_TAG}
  722. ;;
  723. -add-user ) shift
  724. ADD_USER_TAG=${1:-ADD_USER_TAG}
  725. ;;
  726. -user-name ) shift
  727. ADD_USER_NAME=${1:-ADD_USER_NAME}
  728. ;;
  729. -base-dir ) shift
  730. BASE_DIR=${1:-BASE_DIR}
  731. ;;
  732. -git ) shift
  733. GIT_TAG=${1:-GIT_TAG}
  734. ;;
  735. -dstat ) shift
  736. DSTAT_TAG=${1:-DSTAT_TAG}
  737. ;;
  738. -htop ) shift
  739. HTOP_TAG=${1:-HTOP_TAG}
  740. ;;
  741. -nginx ) shift
  742. NGINX_TAG=${1:-NGINX_TAG}
  743. ;;
  744. -nodejs ) shift
  745. NODEJS_TAG=${1:-NODEJS_TAG}
  746. ;;
  747. -java ) shift
  748. JAVA_TAG=${1:-JAVA_TAG}
  749. ;;
  750. -gradle ) shift
  751. GRADLE_TAG=${1:-GRADLE_TAG}
  752. ;;
  753. -maven ) shift
  754. MAVEN_TAG=${1:-MAVEN_TAG}
  755. ;;
  756. -jenkins ) shift
  757. JENKINS_TAG=${1:-JENKINS_TAG}
  758. ;;
  759. -redis ) shift
  760. REDIS_TAG=${1:-REDIS_TAG}
  761. ;;
  762. * ) help::usage
  763. exit 1
  764. esac
  765. shift
  766. done
  767. # 开始
  768. log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}"
  769. # 动作
  770. if [[ "${INIT_TAG:-}" == "1" ]]; then
  771. check::command
  772. [[ "${UPDATE_YUM_REPOS:-}" == "1" ]] && { init:update_yum_repos; }
  773. [[ "${CONFIG_TAG:-}" == "1" ]] && { init:config; }
  774. [[ "${ADD_USER_TAG:-}" == "1" ]] && { init:add_user; }
  775. [[ "${GIT_TAG:-}" == "1" ]] && { init:git; }
  776. [[ "${DSTAT_TAG:-}" == "1" ]] && { init:dstat; }
  777. [[ "${HTOP_TAG:-}" == "1" ]] && { init:htop; }
  778. [[ "${NGINX_TAG:-}" == "1" ]] && { init:nginx; }
  779. [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; }
  780. [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; }
  781. [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; }
  782. [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; }
  783. [[ "${JENKINS_TAG:-}" == "1" ]] && { init:jenkins; }
  784. [[ "${REDIS_TAG:-}" == "1" ]] && { init:redis; }
  785. else
  786. help::usage
  787. fi
  788. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/07env-install-centos.sh) [cmd]