07env-install-centos.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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.org/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="0"
  60. CONFIG_TAG="0"
  61. ADD_USER_TAG="0"
  62. GIT_TAG="0"
  63. DSTAT_TAG="0"
  64. HTOP_TAG="0"
  65. NGINX_TAG="0"
  66. NODEJS_TAG="0"
  67. JAVA_TAG="0"
  68. GRADLE_TAG="0"
  69. MAVEN_TAG="0"
  70. JENKINS_TAG="0"
  71. REDIS_TAG="0"
  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. yum -y install npm
  335. npm install --global yarn
  336. # 配置npm源
  337. npm config set registry https://registry.npm.taobao.org
  338. npm config set disturl https://npm.taobao.org/dist
  339. npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
  340. npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
  341. npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
  342. # 配置yarn源
  343. yarn config set registry https://registry.npm.taobao.org -g
  344. yarn config set disturl https://npm.taobao.org/dist -g
  345. yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g
  346. yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g
  347. yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g
  348. yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g
  349. yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g
  350. yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g
  351. # 验证
  352. node -v
  353. npm -v
  354. yarn -v
  355. "
  356. check::exit_code "$?" "nodejs" "$host 安装nodejs环境" "exit"
  357. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  358. command::exec "${host}" "
  359. # 配置npm源
  360. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set registry https://registry.npm.taobao.org'
  361. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set disturl https://npm.taobao.org/dist'
  362. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set electron_mirror https://npm.taobao.org/mirrors/electron/'
  363. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/'
  364. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/'
  365. # 配置yarn源
  366. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set registry https://registry.npm.taobao.org -g'
  367. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set disturl https://npm.taobao.org/dist -g'
  368. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g'
  369. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g'
  370. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g'
  371. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g'
  372. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g'
  373. echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c 'yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g'
  374. "
  375. check::exit_code "$?" "nodejs" "$host nodejs配置" "exit"
  376. log::info "[nodejs]" "nodejs配置成功!"
  377. fi
  378. log::info "[nodejs]" "安装nodejs环境成功!"
  379. }
  380. # 安装java环境
  381. function init:java() {
  382. log::info "[java]" "安装java环境..."
  383. local host="127.0.0.1"
  384. local java_path="${BASE_DIR}/java/${JDK_DIR_NAME}"
  385. local line="\n"
  386. local add_profile=`echo -e "${line}JAVA_HOME=${java_path}${line}PATH=\\\$JAVA_HOME/bin:\\\$PATH${line}export JAVA_HOME PATH"`
  387. command::exec "${host}" "
  388. yum -y remove *openjdk*
  389. if [ ! -d "$java_path" ]; then
  390. mkdir -p ${BASE_DIR}/java
  391. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JDK_URL} -O ${BASE_DIR}/java/${JDK_FILE_NAME}
  392. cd ${BASE_DIR}/java
  393. tar -zxvf ${JDK_FILE_NAME}
  394. echo '$add_profile' >> /etc/profile
  395. source /etc/profile
  396. fi
  397. java -version
  398. "
  399. check::exit_code "$?" "java" "$host 安装java环境" "exit"
  400. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  401. command::exec "${host}" "
  402. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/java
  403. "
  404. fi
  405. log::info "[java]" "java环境安装成功!"
  406. }
  407. # 安装gradle环境
  408. function init:gradle() {
  409. log::info "[gradle]" "安装gradle环境..."
  410. local host="127.0.0.1"
  411. local gradle_path="${BASE_DIR}/gradle/${GRADLE_DIR_NAME}"
  412. local line="\n"
  413. local add_profile=`echo -e "${line}GRADLE_HOME=${gradle_path}${line}PATH=\\\$GRADLE_HOME/bin:\\\$PATH${line}export GRADLE_HOME PATH"`
  414. command::exec "${host}" "
  415. if [ ! -d "$gradle_path" ]; then
  416. mkdir -p ${BASE_DIR}/gradle
  417. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${GRADLE_URL} -O ${BASE_DIR}/gradle/${GRADLE_FILE_NAME}
  418. cd ${BASE_DIR}/gradle
  419. unzip ${GRADLE_FILE_NAME}
  420. echo '$add_profile' >> /etc/profile
  421. source /etc/profile
  422. fi
  423. gradle -v
  424. "
  425. check::exit_code "$?" "gradle" "$host 安装gradle环境" "exit"
  426. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  427. command::exec "${host}" "
  428. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/gradle
  429. "
  430. fi
  431. log::info "[gradle]" "gradle环境安装成功!"
  432. }
  433. # 安装maven环境
  434. function init:maven() {
  435. log::info "[maven]" "安装maven环境..."
  436. local host="127.0.0.1"
  437. local maven_path="${BASE_DIR}/maven/${MAVEN_DIR_NAME}"
  438. local line="\n"
  439. local add_profile=`echo -e "${line}MAVEN_HOME=${maven_path}${line}PATH=\\\$MAVEN_HOME/bin:\\\$PATH${line}export MAVEN_HOME PATH"`
  440. command::exec "${host}" "
  441. if [ ! -d "$maven_path" ]; then
  442. mkdir -p ${BASE_DIR}/maven
  443. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${MAVEN_URL} -O ${BASE_DIR}/maven/${MAVEN_FILE_NAME}
  444. cd ${BASE_DIR}/maven
  445. tar -zxvf ${MAVEN_FILE_NAME}
  446. echo '$add_profile' >> /etc/profile
  447. source /etc/profile
  448. fi
  449. mvn -v
  450. "
  451. check::exit_code "$?" "maven" "$host 安装maven环境" "exit"
  452. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  453. command::exec "${host}" "
  454. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/maven
  455. "
  456. fi
  457. log::info "[maven]" "maven环境安装成功!"
  458. }
  459. # 安装jenkins环境
  460. function init:jenkins() {
  461. log::info "[jenkins]" "安装jenkins环境..."
  462. local host="127.0.0.1"
  463. local java_path="${BASE_DIR}/jenkins/${JENKINS_JAVA_DIR_NAME}"
  464. local jenkins_file="${BASE_DIR}/jenkins/${JENKINS_FILE_NAME}"
  465. local java_mem_opts=" -DappName=jenkins -server -Xmx1g"
  466. local database_opts=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  467. local java_opts_ext=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  468. command::exec "${host}" "
  469. if [ ! -d "$java_path" ]; then
  470. mkdir -p ${BASE_DIR}/jenkins
  471. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JENKINS_JAVA_URL} -O ${BASE_DIR}/jenkins/${JENKINS_JAVA_FILE_NAME}
  472. cd ${BASE_DIR}/jenkins
  473. tar -zxvf ${JENKINS_JAVA_FILE_NAME}
  474. $java_path/bin/java -version
  475. fi
  476. if [ ! -f "$jenkins_file" ]; then
  477. mkdir -p ${BASE_DIR}/jenkins
  478. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JENKINS_URL} -O ${BASE_DIR}/jenkins/${JENKINS_FILE_NAME}
  479. cd ${BASE_DIR}/jenkins
  480. fi
  481. # 写文件start.sh文件
  482. cat << EOF > ${BASE_DIR}/jenkins/start.sh
  483. #!/bin/bash
  484. httpPort=\\\${1:-'30003'}
  485. #-------------------------------------------------------------------
  486. java='$java_path/bin/java'
  487. jar_file='${BASE_DIR}/jenkins/jenkins.war'
  488. java_mem_opts=' -DappName=jenkins $java_mem_opts'
  489. database_opts='$database_opts'
  490. java_opts_ext='$java_opts_ext'
  491. #-------------------------------------------------------------------
  492. pid=\\\`ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  493. if [ -z \\\$pid ];then
  494. nohup \\\$java \\\$java_mem_opts \\\$database_opts \\\$java_opts_ext -jar \\\$jar_file --httpPort=\\\$httpPort >${BASE_DIR}/jenkins/logs.log 2>&1 &
  495. echo 'jenkins启动成功!'
  496. else
  497. echo 'jenkins正在运行...'
  498. fi
  499. echo '查看日志: tail -F ${BASE_DIR}/jenkins/logs.log -n 100'
  500. EOF
  501. # 写文件kill.sh文件
  502. cat << EOF > ${BASE_DIR}/jenkins/kill.sh
  503. #!/bin/bash
  504. pid=\\\`ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  505. if [ -z \\\$pid ];then
  506. echo 'jenkins未运行'
  507. else
  508. ps -ef | grep 'DappName=jenkins ' | grep -v 'grep' | awk '{print \\\$2}' | xargs kill
  509. echo 'jenkins已停止!'
  510. fi
  511. EOF
  512. chmod +x ${BASE_DIR}/jenkins/start.sh
  513. chmod +x ${BASE_DIR}/jenkins/kill.sh
  514. "
  515. check::exit_code "$?" "jenkins" "$host 安装jenkins环境" "exit"
  516. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  517. command::exec "${host}" "
  518. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/jenkins
  519. "
  520. fi
  521. log::info "[jenkins]" "jenkins环境安装成功!"
  522. log::info "[jenkins]" "启动jenkins: ${BASE_DIR}/jenkins/start.sh 30003"
  523. log::info "[jenkins]" "停止jenkins: ${BASE_DIR}/jenkins/kill.sh"
  524. }
  525. # 安装redis环境
  526. function init:redis() {
  527. log::info "[redis]" "安装redis环境..."
  528. local host="127.0.0.1"
  529. local redis_path="${BASE_DIR}/redis/${REDIS_DIR_NAME}"
  530. local redis_install_path="${BASE_DIR}/redis/${REDIS_DIR_NAME}-install"
  531. command::exec "${host}" "
  532. if [ ! -d "$redis_path" ]; then
  533. mkdir -p ${BASE_DIR}/redis
  534. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${REDIS_URL} -O ${BASE_DIR}/redis/${REDIS_FILE_NAME}
  535. cd ${BASE_DIR}/redis
  536. tar -zxvf ${REDIS_FILE_NAME}
  537. cd $redis_path
  538. yum -y install gcc
  539. yum -y install centos-release-scl
  540. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  541. echo 'source /opt/rh/devtoolset-9/enable' >> /etc/profile
  542. source /etc/profile
  543. gcc -v
  544. mkdir -p $redis_install_path
  545. cd $redis_path
  546. make install PREFIX=$redis_install_path
  547. # 写文件start.sh文件
  548. cat << EOF > $redis_install_path/redis.conf
  549. # bind 127.0.0.1
  550. protected-mode yes
  551. port 6379
  552. tcp-backlog 511
  553. timeout 300
  554. tcp-keepalive 300
  555. daemonize yes
  556. supervised auto
  557. pidfile $redis_install_path/redis.pid
  558. loglevel notice
  559. logfile $redis_install_path/redis.log
  560. databases 16
  561. always-show-logo yes
  562. save 900 1
  563. save 300 10
  564. save 60 10000
  565. stop-writes-on-bgsave-error yes
  566. rdbcompression yes
  567. rdbchecksum yes
  568. dbfilename dump.rdb
  569. rdb-del-sync-files no
  570. dir $redis_install_path/
  571. replica-serve-stale-data yes
  572. replica-read-only yes
  573. repl-diskless-sync no
  574. repl-diskless-sync-delay 5
  575. repl-diskless-load disabled
  576. repl-disable-tcp-nodelay no
  577. replica-priority 100
  578. acllog-max-len 128
  579. requirepass admin123456
  580. # 限制内存4GB = 4294967296 bytes
  581. maxmemory 4294967296
  582. maxmemory-policy allkeys-lfu
  583. lazyfree-lazy-eviction no
  584. lazyfree-lazy-expire no
  585. lazyfree-lazy-server-del no
  586. replica-lazy-flush no
  587. lazyfree-lazy-user-del no
  588. oom-score-adj no
  589. oom-score-adj-values 0 200 800
  590. appendonly no
  591. appendfilename \"appendonly.aof\"
  592. appendfsync everysec
  593. no-appendfsync-on-rewrite no
  594. auto-aof-rewrite-percentage 100
  595. auto-aof-rewrite-min-size 64mb
  596. aof-load-truncated yes
  597. aof-use-rdb-preamble yes
  598. lua-time-limit 5000
  599. slowlog-log-slower-than 10000
  600. slowlog-max-len 128
  601. latency-monitor-threshold 0
  602. notify-keyspace-events \"\"
  603. hash-max-ziplist-entries 512
  604. hash-max-ziplist-value 64
  605. list-max-ziplist-size -2
  606. list-compress-depth 0
  607. set-max-intset-entries 512
  608. zset-max-ziplist-entries 128
  609. zset-max-ziplist-value 64
  610. hll-sparse-max-bytes 3000
  611. stream-node-max-bytes 4096
  612. stream-node-max-entries 100
  613. activerehashing yes
  614. client-output-buffer-limit normal 0 0 0
  615. client-output-buffer-limit replica 256mb 64mb 60
  616. client-output-buffer-limit pubsub 32mb 8mb 60
  617. hz 10
  618. dynamic-hz yes
  619. aof-rewrite-incremental-fsync yes
  620. rdb-save-incremental-fsync yes
  621. jemalloc-bg-thread yes
  622. EOF
  623. cat << EOF > $redis_install_path/start.sh
  624. #!/bin/bash
  625. #set -x
  626. set -e
  627. pid=\\\`ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  628. cd $redis_install_path
  629. if [ -z \\\$pid ];then
  630. $redis_install_path/bin/redis-server $redis_install_path/redis.conf
  631. echo 'redis启动成功!'
  632. else
  633. echo 'redis正在运行...'
  634. fi
  635. echo '查看日志: tail -F $redis_install_path/redis.log -n 100'
  636. EOF
  637. # 写文件kill.sh文件
  638. cat << EOF > $redis_install_path/kill.sh
  639. #!/bin/bash
  640. #set -x
  641. set -e
  642. pid=\\\`ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}'\\\`
  643. if [ -z \\\$pid ];then
  644. echo 'redis未运行'
  645. else
  646. ps -ef | grep '$redis_install_path/bin/redis-server' | grep -v 'grep' | awk '{print \\\$2}' | xargs kill
  647. echo 'redis已停止!'
  648. fi
  649. EOF
  650. chmod +x $redis_install_path/start.sh
  651. chmod +x $redis_install_path/kill.sh
  652. fi
  653. "
  654. check::exit_code "$?" "redis" "$host 安装redis环境" "exit"
  655. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  656. command::exec "${host}" "
  657. sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/redis
  658. "
  659. fi
  660. log::info "[redis]" "redis环境安装成功!"
  661. log::info "[redis]" "启动redis: $redis_install_path/start.sh"
  662. log::info "[redis]" "停止redis: $redis_install_path/kill.sh"
  663. }
  664. ######################################################################################################
  665. # 主调用逻辑
  666. ######################################################################################################
  667. trap trap::info 1 2 3 15 EXIT
  668. # 使用帮助
  669. function help::usage() {
  670. cat << EOF
  671. Usage:
  672. env-install-centos.sh [command]
  673. Available Commands:
  674. init 初始化系统
  675. Flag:
  676. -update-yum-repos 是否更新yum源, 默认: '1'
  677. -config 配置系统, 默认: '1'
  678. -add-user 是否新增用户, 默认: '1'
  679. -user-name 新增用户名, 默认: 'www'
  680. -base-dir 新增用户名, 默认: '/opt'
  681. -git 是否安装git环境, 默认: '1'
  682. -dstat 是否安装dstat, 默认: '1'
  683. -htop 是否安装htop, 默认: '1'
  684. -nginx 是否安装nginx, 默认: '1'
  685. -nodejs 是否安装nodejs环境, 默认: '1'
  686. -java 是否安装java环境, 默认: '1'
  687. -gradle 是否安装gradle环境, 默认: '1'
  688. -maven 是否安装maven环境, 默认: '1'
  689. -jenkins 是否安装jenkins环境, 默认: '1'
  690. -redis 是否安装redis环境, 默认: '1'
  691. Example:
  692. env-install-centos.sh init \\
  693. -update-yum-repos 0 \\
  694. -config 1 \\
  695. -add-user 1 \\
  696. -user-name www \\
  697. -git 1 \\
  698. -dstat 0 \\
  699. -htop 0 \\
  700. -nginx 0 \\
  701. -nodejs 0 \\
  702. -java 1 \\
  703. -gradle 0 \\
  704. -maven 0 \\
  705. -jenkins 0 \\
  706. -redis 0
  707. EOF
  708. exit 1
  709. }
  710. # 参数处理
  711. [ "$#" == "0" ] && help::usage
  712. while [ "${1:-}" != "" ]; do
  713. case $1 in
  714. # -------------------------------------------------------------- 指令
  715. init ) INIT_TAG="1"
  716. ;;
  717. # -------------------------------------------------------------- 指令参数
  718. -update-yum-repos ) shift
  719. UPDATE_YUM_REPOS=${1:-UPDATE_YUM_REPOS}
  720. ;;
  721. -config ) shift
  722. CONFIG_TAG=${1:-CONFIG_TAG}
  723. ;;
  724. -add-user ) shift
  725. ADD_USER_TAG=${1:-ADD_USER_TAG}
  726. ;;
  727. -user-name ) shift
  728. ADD_USER_NAME=${1:-ADD_USER_NAME}
  729. ;;
  730. -base-dir ) shift
  731. BASE_DIR=${1:-BASE_DIR}
  732. ;;
  733. -git ) shift
  734. GIT_TAG=${1:-GIT_TAG}
  735. ;;
  736. -dstat ) shift
  737. DSTAT_TAG=${1:-DSTAT_TAG}
  738. ;;
  739. -htop ) shift
  740. HTOP_TAG=${1:-HTOP_TAG}
  741. ;;
  742. -nginx ) shift
  743. NGINX_TAG=${1:-NGINX_TAG}
  744. ;;
  745. -nodejs ) shift
  746. NODEJS_TAG=${1:-NODEJS_TAG}
  747. ;;
  748. -java ) shift
  749. JAVA_TAG=${1:-JAVA_TAG}
  750. ;;
  751. -gradle ) shift
  752. GRADLE_TAG=${1:-GRADLE_TAG}
  753. ;;
  754. -maven ) shift
  755. MAVEN_TAG=${1:-MAVEN_TAG}
  756. ;;
  757. -jenkins ) shift
  758. JENKINS_TAG=${1:-JENKINS_TAG}
  759. ;;
  760. -redis ) shift
  761. REDIS_TAG=${1:-REDIS_TAG}
  762. ;;
  763. * ) help::usage
  764. exit 1
  765. esac
  766. shift
  767. done
  768. # 开始
  769. log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}"
  770. # 动作
  771. if [[ "${INIT_TAG:-}" == "1" ]]; then
  772. check::command
  773. [[ "${UPDATE_YUM_REPOS:-}" == "1" ]] && { init:update_yum_repos; }
  774. [[ "${CONFIG_TAG:-}" == "1" ]] && { init:config; }
  775. [[ "${ADD_USER_TAG:-}" == "1" ]] && { init:add_user; }
  776. [[ "${GIT_TAG:-}" == "1" ]] && { init:git; }
  777. [[ "${DSTAT_TAG:-}" == "1" ]] && { init:dstat; }
  778. [[ "${HTOP_TAG:-}" == "1" ]] && { init:htop; }
  779. [[ "${NGINX_TAG:-}" == "1" ]] && { init:nginx; }
  780. [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; }
  781. [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; }
  782. [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; }
  783. [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; }
  784. [[ "${JENKINS_TAG:-}" == "1" ]] && { init:jenkins; }
  785. [[ "${REDIS_TAG:-}" == "1" ]] && { init:redis; }
  786. else
  787. help::usage
  788. fi
  789. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/07env-install-centos.sh) [cmd]