07env-install-centos.sh 29 KB

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