07env-install-centos.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #!/usr/bin/env bash
  2. #-----------------------------------------------------------------------------------------------------
  3. # OracleJDK 下载地址
  4. # 镜像1: https://www.injdk.cn/ (https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz)
  5. # 镜像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)
  6. #
  7. #
  8. #
  9. #-----------------------------------------------------------------------------------------------------
  10. # 配置
  11. ADD_USER_NAME="www"
  12. ADD_USER_PASSWORD="MoXF2Zi6u7f7lqZu"
  13. BASE_DIR="/opt"
  14. # 软件
  15. #--- jdk
  16. JDK_URL="https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz"
  17. JDK_FILE_NAME="jdk-11.0.16.1_linux-x64_bin.tar.gz"
  18. JDK_DIR_NAME="jdk-11.0.16.1"
  19. #--- gradle
  20. #--- maven
  21. #--- jenkins
  22. # 安装标识
  23. #--- SSH用户
  24. SSH_OPTIONS="-o ConnectTimeout=600 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  25. SSH_USER=""
  26. SSH_PASSWORD=""
  27. SSH_PRIVATE_KEY=""
  28. SSH_PORT=""
  29. #--- SUDO用户
  30. SUDO_TAG="0"
  31. SUDO_USER=""
  32. SUDO_PASSWORD=""
  33. #--- 指令
  34. INIT_TAG="0"
  35. #--- 指令参数
  36. ADD_USER_TAG="1"
  37. GIT_TAG="1"
  38. DSTAT_TAG="1"
  39. HTOP_TAG="1"
  40. NGINX_TAG="1"
  41. NODEJS_TAG="1"
  42. JAVA_TAG="1"
  43. GRADLE_TAG="1"
  44. MAVEN_TAG="1"
  45. JENKINS_TAG="1"
  46. # 脚本设置
  47. TMP_DIR="$(rm -rf /tmp/env-install* && mktemp -d -t env-install.XXXXXXXXXX)"
  48. LOG_FILE="${TMP_DIR}/env-install.log"
  49. ERROR_INFO="\n\033[31mERROR Summary: \033[0m\n "
  50. ACCESS_INFO="\n\033[32mACCESS Summary: \033[0m\n "
  51. SCRIPT_PARAMETER="$*"
  52. COMMAND_OUTPUT=""
  53. ######################################################################################################
  54. # 通用函数
  55. ######################################################################################################
  56. # 信号处理
  57. function trap::info() {
  58. [[ ${#ERROR_INFO} -gt 37 ]] && echo -e "$ERROR_INFO"
  59. [[ ${#ACCESS_INFO} -gt 38 ]] && echo -e "$ACCESS_INFO"
  60. [ -f "$LOG_FILE" ] && echo -e "\n\n See detailed log >>> cat $LOG_FILE \n\n"
  61. trap '' EXIT
  62. exit
  63. }
  64. # 错误日志
  65. function log::error() {
  66. local item; item="[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')]: \033[31mERROR: \033[0m$*"
  67. ERROR_INFO="${ERROR_INFO}${item}\n "
  68. echo -e "${item}" | tee -a "$LOG_FILE"
  69. }
  70. # 基础日志
  71. function log::info() {
  72. printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  73. }
  74. # 警告日志
  75. function log::warning() {
  76. printf "[%s]: \033[33mWARNING: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  77. }
  78. # 访问信息
  79. function log::access() {
  80. ACCESS_INFO="${ACCESS_INFO}$*\n "
  81. printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
  82. }
  83. # 执行日志
  84. function log::exec() {
  85. printf "[%s]: \033[34mEXEC: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" >> "$LOG_FILE"
  86. }
  87. # 检查返回码
  88. function check::exit_code() {
  89. local code=${1:-}
  90. local app=${2:-}
  91. local desc=${3:-}
  92. local exit_script=${4:-}
  93. if [[ "${code}" == "0" ]]; then
  94. log::info "[${app}]" "${desc} succeeded."
  95. else
  96. log::error "[${app}]" "${desc} failed."
  97. [[ "$exit_script" == "exit" ]] && exit "$code"
  98. fi
  99. }
  100. # 重试
  101. function utils::retry() {
  102. local retries=$1
  103. shift
  104. local count=0
  105. until eval "$*"; do
  106. exit=$?
  107. wait=$((2 ** count))
  108. count=$((count + 1))
  109. if [ "$count" -lt "$retries" ]; then
  110. echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
  111. sleep $wait
  112. else
  113. echo "Retry $count/$retries exited $exit, no more retries left."
  114. return $exit
  115. fi
  116. done
  117. return 0
  118. }
  119. # 转义引号
  120. function utils::quote() {
  121. # shellcheck disable=SC2046
  122. if [ $(echo "$*" | tr -d "\n" | wc -c) -eq 0 ]; then
  123. echo "''"
  124. elif [ $(echo "$*" | tr -d "[a-z][A-Z][0-9]:,.=~_/\n-" | wc -c) -gt 0 ]; then
  125. 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"
  126. else
  127. echo "$*"
  128. fi
  129. }
  130. # 下载文件
  131. function utils::download_file() {
  132. local url="$1"
  133. local dest="$2"
  134. local unzip_tag="${3:-1}"
  135. local dest_dirname; dest_dirname=$(dirname "$dest")
  136. local filename; filename=$(basename "$dest")
  137. log::info "[download]" "${filename}"
  138. command::exec "${MGMT_NODE}" "
  139. set -e
  140. if [ ! -f \"${dest}\" ]; then
  141. [ ! -d \"${dest_dirname}\" ] && mkdir -pv \"${dest_dirname}\"
  142. wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate \"${url}\" -O \"${dest}\"
  143. if [[ \"${unzip_tag}\" == \"unzip\" ]]; then
  144. command -v unzip 2>/dev/null || yum install -y unzip
  145. unzip -o \"${dest}\" -d \"${dest_dirname}\"
  146. fi
  147. else
  148. echo \"${dest} is exists!\"
  149. fi
  150. "
  151. local status="$?"
  152. check::exit_code "$status" "download" "${filename}" "exit"
  153. return "$status"
  154. }
  155. # 检查命令是否存在
  156. function check::command_exists() {
  157. local cmd=${1}
  158. local package=${2}
  159. if command -V "$cmd" > /dev/null 2>&1; then
  160. log::info "[check]" "$cmd command exists."
  161. else
  162. log::warning "[check]" "I require $cmd but it's not installed."
  163. log::warning "[check]" "install $package package."
  164. command::exec "127.0.0.1" "yum install -y ${package}"
  165. check::exit_code "$?" "check" "$package install" "exit"
  166. fi
  167. }
  168. # 执行命令
  169. function command::exec() {
  170. local host=${1:-"127.0.0.1"}
  171. shift
  172. local command="$*"
  173. if [[ "${SUDO_TAG:-}" == "1" ]]; then
  174. sudo_options="sudo -H -n -u ${SUDO_USER}"
  175. if [[ "${SUDO_PASSWORD:-}" != "" ]]; then
  176. sudo_options="${sudo_options// -n/} -p \"\" -S <<< \"${SUDO_PASSWORD}\""
  177. fi
  178. command="$sudo_options bash -c $(utils::quote "$command")"
  179. fi
  180. command="$(utils::quote "$command")"
  181. if [[ "${host}" == "127.0.0.1" ]]; then
  182. # 本地执行
  183. log::exec "[command]" "bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")"
  184. # shellcheck disable=SC2094
  185. COMMAND_OUTPUT=$(eval bash -c "${command}" 2>> "$LOG_FILE" | tee -a "$LOG_FILE")
  186. local status=$?
  187. else
  188. # 远程执行
  189. local ssh_cmd="ssh"
  190. if [[ "${SSH_PASSWORD}" != "" ]]; then
  191. ssh_cmd="sshpass -p \"${SSH_PASSWORD}\" ${ssh_cmd}"
  192. elif [[ "$SSH_PRIVATE_KEY" != "" ]]; then
  193. [ -f "${SSH_PRIVATE_KEY}" ] || { log::error "[exec]" "ssh private_key:${SSH_PRIVATE_KEY} not found."; exit 1; }
  194. ssh_cmd="${ssh_cmd} -i $SSH_PRIVATE_KEY"
  195. fi
  196. log::exec "[command]" "${ssh_cmd//${SSH_PASSWORD:-}/zzzzzz} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT} bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")"
  197. # shellcheck disable=SC2094
  198. COMMAND_OUTPUT=$(eval "${ssh_cmd} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT}" bash -c '"${command}"' 2>> "$LOG_FILE" | tee -a "$LOG_FILE")
  199. local status=$?
  200. fi
  201. return $status
  202. }
  203. ######################################################################################################
  204. # 安装函数
  205. ######################################################################################################
  206. # 新增用户
  207. function init:add_user() {
  208. log::info "[add-user]" "新增用户[${ADD_USER_NAME}]..."
  209. local host="127.0.0.1"
  210. local add_sudoers="${ADD_USER_NAME} ALL=(ALL) NOPASSWD:ALL"
  211. command::exec "${host}" "
  212. id -u ${ADD_USER_NAME} >/dev/null 2>&1
  213. if [ \$? -ne '0' ]; then
  214. # 创建用户
  215. adduser ${ADD_USER_NAME}
  216. # 设置密码
  217. echo ${ADD_USER_NAME}:${ADD_USER_PASSWORD} | chpasswd
  218. fi
  219. if [ \`grep -c '$add_sudoers' '/etc/sudoers'\` == '0' ];then
  220. # 新增修改权限
  221. chmod -v u+w /etc/sudoers
  222. echo -e '$add_sudoers' >> /etc/sudoers
  223. # 收回修改权限
  224. chmod -v u-w /etc/sudoers
  225. fi
  226. "
  227. check::exit_code "$?" "add-user" "$host 新增用户[${ADD_USER_NAME}]" "exit"
  228. log::info "[add-user]" "新增用户[${ADD_USER_NAME}]成功!"
  229. }
  230. # 安装git环境
  231. function init:git() {
  232. log::info "[git]" "安装git环境..."
  233. local host="127.0.0.1"
  234. command::exec "${host}" "
  235. yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
  236. yum -y install git
  237. git --version
  238. git config --global credential.helper store
  239. "
  240. check::exit_code "$?" "git" "$host 安装git环境" "exit"
  241. log::info "[git]" "git环境安装成功!"
  242. if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
  243. command::exec "${host}" "
  244. sudo su - ${ADD_USER_NAME}
  245. git config --global credential.helper store
  246. "
  247. check::exit_code "$?" "git" "$host git配置" "exit"
  248. log::info "[git]" "git配置成功!"
  249. fi
  250. }
  251. # 安装dstat
  252. function init:dstat() {
  253. log::info "[dstat]" "安装dstat..."
  254. local host="127.0.0.1"
  255. command::exec "${host}" "
  256. yum -y install dstat
  257. dstat -V
  258. "
  259. check::exit_code "$?" "dstat" "$host 安装dstat" "exit"
  260. log::info "[dstat]" "dstat安装成功!"
  261. }
  262. # 安装htop
  263. function init:htop() {
  264. log::info "[htop]" "安装htop..."
  265. local host="127.0.0.1"
  266. command::exec "${host}" "
  267. yum -y install htop
  268. htop -v
  269. "
  270. check::exit_code "$?" "htop" "$host 安装htop" "exit"
  271. log::info "[htop]" "htop安装成功!"
  272. }
  273. # 安装nginx
  274. function init:nginx() {
  275. log::info "[nginx]" "安装nginx..."
  276. local host="127.0.0.1"
  277. command::exec "${host}" "
  278. yum -y install nginx
  279. nginx -v
  280. # sudo systemctl enable nginx
  281. "
  282. check::exit_code "$?" "nginx" "$host 安装nginx" "exit"
  283. log::info "[nginx]" "nginx安装成功!"
  284. }
  285. # 安装nodejs环境
  286. function init:nodejs() {
  287. log::info "[nodejs]" "安装nodejs环境..."
  288. local host="127.0.0.1"
  289. command::exec "${host}" "
  290. curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
  291. yum -y install nodejs
  292. npm install --global yarn
  293. # 配置npm源
  294. npm config set registry https://registry.npm.taobao.org
  295. npm config set disturl https://npm.taobao.org/dist
  296. npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
  297. npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
  298. npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
  299. # 配置yarn源
  300. yarn config set registry https://registry.npm.taobao.org -g
  301. yarn config set disturl https://npm.taobao.org/dist -g
  302. yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g
  303. yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g
  304. yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g
  305. yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g
  306. yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g
  307. yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g
  308. "
  309. check::exit_code "$?" "nodejs" "$host 安装nodejs环境" "exit"
  310. log::info "[nodejs]" "安装nodejs环境成功!"
  311. }
  312. # 安装java环境
  313. function init:java() {
  314. log::info "[java]" "安装java环境..."
  315. local host="127.0.0.1"
  316. }
  317. # 安装gradle环境
  318. function init:gradle() {
  319. log::info "[gradle]" "安装gradle环境..."
  320. local host="127.0.0.1"
  321. }
  322. # 安装maven环境
  323. function init:maven() {
  324. log::info "[maven]" "安装maven环境..."
  325. local host="127.0.0.1"
  326. }
  327. # 安装jenkins环境
  328. function init:jenkins() {
  329. log::info "[jenkins]" "安装jenkins环境..."
  330. local host="127.0.0.1"
  331. }
  332. ######################################################################################################
  333. # 主调用逻辑
  334. ######################################################################################################
  335. trap trap::info 1 2 3 15 EXIT
  336. # 使用帮助
  337. function help::usage() {
  338. cat << EOF
  339. Usage:
  340. env-install-centos.sh [command]
  341. Available Commands:
  342. init 初始化系统
  343. Flag:
  344. -add-user 是否新增用户, 默认: '1'
  345. -user-name 新增用户名, 默认: 'www'
  346. -base-dir 新增用户名, 默认: '/opt'
  347. -git 是否安装git环境, 默认: '1'
  348. -dstat 是否安装dstat, 默认: '1'
  349. -htop 是否安装htop, 默认: '1'
  350. -nginx 是否安装nginx, 默认: '1'
  351. -nodejs 是否安装nodejs环境, 默认: '1'
  352. -java 是否安装java环境, 默认: '1'
  353. -gradle 是否安装gradle环境, 默认: '1'
  354. -maven 是否安装maven环境, 默认: '1'
  355. -jenkins 是否安装jenkins环境, 默认: '1'
  356. Example:
  357. env-install-centos.sh init \\
  358. -add-user \\
  359. -nodejs \\
  360. -java \\
  361. -gradle 0 \\
  362. -maven
  363. EOF
  364. exit 1
  365. }
  366. # 参数处理
  367. [ "$#" == "0" ] && help::usage
  368. while [ "${1:-}" != "" ]; do
  369. case $1 in
  370. # -------------------------------------------------------------- 指令
  371. init ) INIT_TAG="1"
  372. ;;
  373. # -------------------------------------------------------------- 指令参数
  374. -add-user ) shift
  375. ADD_USER_TAG=${1:-ADD_USER_TAG}
  376. ;;
  377. -user-name ) shift
  378. ADD_USER_NAME=${1:-ADD_USER_NAME}
  379. ;;
  380. -base-dir ) shift
  381. BASE_DIR=${1:-BASE_DIR}
  382. ;;
  383. -git ) shift
  384. GIT_TAG=${1:-GIT_TAG}
  385. ;;
  386. -dstat ) shift
  387. DSTAT_TAG=${1:-DSTAT_TAG}
  388. ;;
  389. -htop ) shift
  390. HTOP_TAG=${1:-HTOP_TAG}
  391. ;;
  392. -nginx ) shift
  393. NGINX_TAG=${1:-NGINX_TAG}
  394. ;;
  395. -nodejs ) shift
  396. NODEJS_TAG=${1:-NODEJS_TAG}
  397. ;;
  398. -java ) shift
  399. JAVA_TAG=${1:-JAVA_TAG}
  400. ;;
  401. -gradle ) shift
  402. GRADLE_TAG=${1:-GRADLE_TAG}
  403. ;;
  404. -maven ) shift
  405. MAVEN_TAG=${1:-MAVEN_TAG}
  406. ;;
  407. -jenkins ) shift
  408. JENKINS_TAG=${1:-JENKINS_TAG}
  409. ;;
  410. * ) help::usage
  411. exit 1
  412. esac
  413. shift
  414. done
  415. # 开始
  416. log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}"
  417. # 动作
  418. if [[ "${INIT_TAG:-}" == "1" ]]; then
  419. [[ "${ADD_USER_TAG:-}" == "1" ]] && { init:add_user; }
  420. [[ "${GIT_TAG:-}" == "1" ]] && { init:git; }
  421. [[ "${DSTAT_TAG:-}" == "1" ]] && { init:dstat; }
  422. [[ "${HTOP_TAG:-}" == "1" ]] && { init:htop; }
  423. [[ "${NGINX_TAG:-}" == "1" ]] && { init:nginx; }
  424. [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; }
  425. [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; }
  426. [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; }
  427. [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; }
  428. [[ "${JENKINS_TAG:-}" == "1" ]] && { init:jenkins; }
  429. else
  430. help::usage
  431. fi
  432. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/07env-install-centos.sh) [cmd]