From 1be54c2b9c8324576a8e231e008b162656ce53ca Mon Sep 17 00:00:00 2001 From: lizw <1183409807@qq.com> Date: Mon, 31 Oct 2022 22:18:16 +0800 Subject: [PATCH] 07env-install-centos.sh --- 00base/07env-install-centos.sh | 27 +- 00base/08jenkins-agent-install-centos.sh | 427 +++++++++++++++++++++++++++++++ 2 files changed, 441 insertions(+), 13 deletions(-) create mode 100644 00base/08jenkins-agent-install-centos.sh diff --git a/00base/07env-install-centos.sh b/00base/07env-install-centos.sh index 196efa4..65b0361 100644 --- a/00base/07env-install-centos.sh +++ b/00base/07env-install-centos.sh @@ -613,7 +613,7 @@ while [ "${1:-}" != "" ]; do -base-dir ) shift BASE_DIR=${1:-BASE_DIR} ;; - -git ) shift + -git ) shift GIT_TAG=${1:-GIT_TAG} ;; -dstat ) shift @@ -650,18 +650,19 @@ done log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}" # 动作 if [[ "${INIT_TAG:-}" == "1" ]]; then - [[ "${UPDATE_YUM_REPOS:-}" == "1" ]] && { init:update_yum_repos; } - [[ "${CONFIG_TAG:-}" == "1" ]] && { init:config; } - [[ "${ADD_USER_TAG:-}" == "1" ]] && { init:add_user; } - [[ "${GIT_TAG:-}" == "1" ]] && { init:git; } - [[ "${DSTAT_TAG:-}" == "1" ]] && { init:dstat; } - [[ "${HTOP_TAG:-}" == "1" ]] && { init:htop; } - [[ "${NGINX_TAG:-}" == "1" ]] && { init:nginx; } - [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; } - [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; } - [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; } - [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; } - [[ "${JENKINS_TAG:-}" == "1" ]] && { init:jenkins; } + check::command + [[ "${UPDATE_YUM_REPOS:-}" == "1" ]] && { init:update_yum_repos; } + [[ "${CONFIG_TAG:-}" == "1" ]] && { init:config; } + [[ "${ADD_USER_TAG:-}" == "1" ]] && { init:add_user; } + [[ "${GIT_TAG:-}" == "1" ]] && { init:git; } + [[ "${DSTAT_TAG:-}" == "1" ]] && { init:dstat; } + [[ "${HTOP_TAG:-}" == "1" ]] && { init:htop; } + [[ "${NGINX_TAG:-}" == "1" ]] && { init:nginx; } + [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; } + [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; } + [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; } + [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; } + [[ "${JENKINS_TAG:-}" == "1" ]] && { init:jenkins; } else help::usage fi diff --git a/00base/08jenkins-agent-install-centos.sh b/00base/08jenkins-agent-install-centos.sh new file mode 100644 index 0000000..00ea86f --- /dev/null +++ b/00base/08jenkins-agent-install-centos.sh @@ -0,0 +1,427 @@ +#!/usr/bin/env bash + +#----------------------------------------------------------------------------------------------------- +# Nodejs 下载地址 +# 镜像1: https://nodejs.org/dist/ (https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.gz) +# OracleJDK 下载地址 +# 镜像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) +# 镜像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) +#----------------------------------------------------------------------------------------------------- + +# 配置 +BASE_DIR="/data/jenkins-agent/env" + +# 软件 +#--- jdk8 +JDK8_URL="https://d6.injdk.cn/oraclejdk/8/jdk-8u341-linux-x64.tar.gz" +JDK8_FILE_NAME="jdk-8u341-linux-x64.tar.gz" +JDK8_DIR_NAME="jdk1.8.0_341" +#--- jdk11 +JDK11_URL="https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz" +JDK11_FILE_NAME="jdk-11.0.16.1_linux-x64_bin.tar.gz" +JDK11_DIR_NAME="jdk-11.0.16.1" +#--- jdk17 +JDK17_URL="https://d6.injdk.cn/oraclejdk/17/jdk-17_linux-x64_bin.tar.gz" +JDK17_FILE_NAME="jdk-17_linux-x64_bin.tar.gz" +JDK17_DIR_NAME="jdk-17.0.4.1" +#--- gradle (6.8.3 | 6.9.3 | 7.4.2 | 7.5.1) +GRADLE_VERSION="6.8.3" +GRADLE_URL="https://downloads.gradle-dn.com/distributions/gradle-6.8.3-bin.zip" +GRADLE_FILE_NAME="gradle-6.8.3-bin.zip" +GRADLE_DIR_NAME="gradle-6.8.3" +#--- maven (3.8.6 | 3.6.3 | 3.5.4 | 3.3.9) +MAVEN_VERSION="3.8.6" +MAVEN_URL="https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz" +MAVEN_FILE_NAME="apache-maven-3.8.6-bin.tar.gz" +MAVEN_DIR_NAME="apache-maven-3.8.6" +#--- nodejs (12.22.9 | 14.20.1 | 16.18.0 | 18.9.1) +NODEJS_URL="https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.gz" +NODEJS_FILE_NAME="node-v16.18.0-linux-x64.tar.gz" +NODEJS_DIR_NAME="node-v16.18.0-linux-x64" + +# 安装标识 +#--- SSH用户 +SSH_OPTIONS="-o ConnectTimeout=600 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" +SSH_USER="" +SSH_PASSWORD="" +SSH_PRIVATE_KEY="" +SSH_PORT="" +#--- SUDO用户 +SUDO_TAG="0" +SUDO_USER="" +SUDO_PASSWORD="" +#--- 指令 +INIT_TAG="0" +#--- 指令参数 +GIT_TAG="1" +JAVA_TAG="1" +GRADLE_TAG="6.8.3" +MAVEN_TAG="3.8.6" +NODEJS_TAG="16.18.0" + +# 脚本设置 +TMP_DIR="$(rm -rf /tmp/jenkins-agent-install* && mktemp -d -t jenkins-agent-install.XXXXXXXXXX)" +LOG_FILE="${TMP_DIR}/jenkins-agent-install.log" +ERROR_INFO="\n\033[31mERROR Summary: \033[0m\n " +ACCESS_INFO="\n\033[32mACCESS Summary: \033[0m\n " +SCRIPT_PARAMETER="$*" +COMMAND_OUTPUT="" + +###################################################################################################### +# 通用函数 +###################################################################################################### +# 信号处理 +function trap::info() { + [[ ${#ERROR_INFO} -gt 37 ]] && echo -e "$ERROR_INFO" + [[ ${#ACCESS_INFO} -gt 38 ]] && echo -e "$ACCESS_INFO" + [ -f "$LOG_FILE" ] && echo -e "\n\n See detailed log >>> cat $LOG_FILE \n\n" + trap '' EXIT + exit +} + +# 错误日志 +function log::error() { + local item; item="[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')]: \033[31mERROR: \033[0m$*" + ERROR_INFO="${ERROR_INFO}${item}\n " + echo -e "${item}" | tee -a "$LOG_FILE" +} + +# 基础日志 +function log::info() { + printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE" +} + +# 警告日志 +function log::warning() { + printf "[%s]: \033[33mWARNING: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE" +} + +# 访问信息 +function log::access() { + ACCESS_INFO="${ACCESS_INFO}$*\n " + printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE" +} + +# 执行日志 +function log::exec() { + printf "[%s]: \033[34mEXEC: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" >> "$LOG_FILE" +} + +# 检查返回码 +function check::exit_code() { + local code=${1:-} + local app=${2:-} + local desc=${3:-} + local exit_script=${4:-} + if [[ "${code}" == "0" ]]; then + log::info "[${app}]" "${desc} succeeded." + else + log::error "[${app}]" "${desc} failed." + [[ "$exit_script" == "exit" ]] && exit "$code" + fi +} + +# 重试 +function utils::retry() { + local retries=$1 + shift + local count=0 + until eval "$*"; do + exit=$? + wait=$((2 ** count)) + count=$((count + 1)) + if [ "$count" -lt "$retries" ]; then + echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." + sleep $wait + else + echo "Retry $count/$retries exited $exit, no more retries left." + return $exit + fi + done + return 0 +} + +# 转义引号 +function utils::quote() { + # shellcheck disable=SC2046 + if [ $(echo "$*" | tr -d "\n" | wc -c) -eq 0 ]; then + echo "''" + elif [ $(echo "$*" | tr -d "[a-z][A-Z][0-9]:,.=~_/\n-" | wc -c) -gt 0 ]; then + 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" + else + echo "$*" + fi +} + +# 检查命令是否存在 +function check::command_exists() { + local cmd=${1} + local package=${2} + if command -V "$cmd" > /dev/null 2>&1; then + log::info "[check]" "$cmd command exists." + else + log::warning "[check]" "I require $cmd but it's not installed." + log::warning "[check]" "install $package package." + command::exec "127.0.0.1" "yum install -y ${package}" + check::exit_code "$?" "check" "$package install" "exit" + fi +} + +# 执行命令 +function command::exec() { + local host=${1:-"127.0.0.1"} + shift + local command="$*" + if [[ "${SUDO_TAG:-}" == "1" ]]; then + sudo_options="sudo -H -n -u ${SUDO_USER}" + if [[ "${SUDO_PASSWORD:-}" != "" ]]; then + sudo_options="${sudo_options// -n/} -p \"\" -S <<< \"${SUDO_PASSWORD}\"" + fi + command="$sudo_options bash -c $(utils::quote "$command")" + fi + command="$(utils::quote "$command")" + if [[ "${host}" == "127.0.0.1" ]]; then + # 本地执行 + log::exec "[command]" "bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")" + # shellcheck disable=SC2094 + COMMAND_OUTPUT=$(eval bash -c "${command}" 2>> "$LOG_FILE" | tee -a "$LOG_FILE") + local status=$? + else + # 远程执行 + local ssh_cmd="ssh" + if [[ "${SSH_PASSWORD}" != "" ]]; then + ssh_cmd="sshpass -p \"${SSH_PASSWORD}\" ${ssh_cmd}" + elif [[ "$SSH_PRIVATE_KEY" != "" ]]; then + [ -f "${SSH_PRIVATE_KEY}" ] || { log::error "[exec]" "ssh private_key:${SSH_PRIVATE_KEY} not found."; exit 1; } + ssh_cmd="${ssh_cmd} -i $SSH_PRIVATE_KEY" + fi + log::exec "[command]" "${ssh_cmd//${SSH_PASSWORD:-}/zzzzzz} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT} bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")" + # shellcheck disable=SC2094 + COMMAND_OUTPUT=$(eval "${ssh_cmd} ${SSH_OPTIONS} ${SSH_USER}@${host} -p ${SSH_PORT}" bash -c '"${command}"' 2>> "$LOG_FILE" | tee -a "$LOG_FILE") + local status=$? + fi + return $status +} + +###################################################################################################### +# 安装函数 +###################################################################################################### +# 检查用到的命令 +function check::command() { + check::command_exists ssh openssh-clients + check::command_exists tar tar + check::command_exists wget wget + check::command_exists unzip unzip +} + +# 创建文件夹 +function init:mkdir() { + log::info "[mkdir]" "创建目录..." + local host="127.0.0.1" + command::exec "${host}" " + mkdir -p ${BASE_DIR} + sudo chown -R \\\$(id -u):\\\$(id -g) ${BASE_DIR} + " + check::exit_code "$?" "git" "$host 创建目录:${BASE_DIR}" "exit" + log::info "[mkdir]" "创建目录成功!" +} + +# 安装git环境 +function init:git() { + log::info "[git]" "安装git环境..." + local host="127.0.0.1" + command::exec "${host}" " + yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm + yum -y install git + git --version + git config --global credential.helper store + " + check::exit_code "$?" "git" "$host 安装git环境" "exit" + log::info "[git]" "git环境安装成功!" +} + +# 安装java环境 +function init:java() { + log::info "[java]" "安装java环境..." + local host="127.0.0.1" +# local java_path="${BASE_DIR}/java/${JDK_DIR_NAME}" +# command::exec "${host}" " +# if [ ! -d "$java_path" ]; then +# mkdir -p ${BASE_DIR}/java +# wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JDK_URL} -O ${BASE_DIR}/java/${JDK_FILE_NAME} +# cd ${BASE_DIR}/java +# tar -zxvf ${JDK_FILE_NAME} +# sudo chown -R $(id -u):$(id -g) ${BASE_DIR}/java +# fi +# " +# check::exit_code "$?" "java" "$host 安装java环境" "exit" +# if [[ "${ADD_USER_TAG:-}" == "1" ]]; then +# command::exec "${host}" " +# sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/java +# " +# fi + log::info "[java]" "java环境安装成功!" +} + +# 安装gradle环境 +function init:gradle() { + log::info "[gradle]" "安装gradle环境..." + local host="127.0.0.1" +# local gradle_path="${BASE_DIR}/gradle/${GRADLE_DIR_NAME}" +# local line="\n" +# local add_profile=`echo -e "${line}GRADLE_HOME=${gradle_path}${line}PATH=\\\$GRADLE_HOME/bin:\\\$PATH${line}export GRADLE_HOME PATH"` +# command::exec "${host}" " +# if [ ! -d "$gradle_path" ]; then +# mkdir -p ${BASE_DIR}/gradle +# wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${GRADLE_URL} -O ${BASE_DIR}/gradle/${GRADLE_FILE_NAME} +# cd ${BASE_DIR}/gradle +# unzip ${GRADLE_FILE_NAME} +# echo '$add_profile' >> /etc/profile +# source /etc/profile +# fi +# gradle -v +# " +# check::exit_code "$?" "gradle" "$host 安装gradle环境" "exit" +# if [[ "${ADD_USER_TAG:-}" == "1" ]]; then +# command::exec "${host}" " +# sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/gradle +# " +# fi + log::info "[gradle]" "gradle环境安装成功!" +} + +# 安装maven环境 +function init:maven() { + log::info "[maven]" "安装maven环境..." + local host="127.0.0.1" +# local maven_path="${BASE_DIR}/maven/${MAVEN_DIR_NAME}" +# local line="\n" +# local add_profile=`echo -e "${line}MAVEN_HOME=${maven_path}${line}PATH=\\\$MAVEN_HOME/bin:\\\$PATH${line}export MAVEN_HOME PATH"` +# command::exec "${host}" " +# if [ ! -d "$maven_path" ]; then +# mkdir -p ${BASE_DIR}/maven +# wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${MAVEN_URL} -O ${BASE_DIR}/maven/${MAVEN_FILE_NAME} +# cd ${BASE_DIR}/maven +# tar -zxvf ${MAVEN_FILE_NAME} +# echo '$add_profile' >> /etc/profile +# source /etc/profile +# fi +# mvn -v +# " +# check::exit_code "$?" "maven" "$host 安装maven环境" "exit" +# if [[ "${ADD_USER_TAG:-}" == "1" ]]; then +# command::exec "${host}" " +# sudo chown -R \$(id -u ${ADD_USER_NAME}):\$(id -g ${ADD_USER_NAME}) ${BASE_DIR}/maven +# " +# fi + log::info "[maven]" "maven环境安装成功!" +} + +# 安装nodejs环境 +function init:nodejs() { + log::info "[nodejs]" "安装nodejs环境..." + local host="127.0.0.1" +# command::exec "${host}" " +# curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash - +# yum -y install nodejs +# npm install --global yarn +# # 配置npm源 +# npm config set registry https://registry.npm.taobao.org +# npm config set disturl https://npm.taobao.org/dist +# npm config set electron_mirror https://npm.taobao.org/mirrors/electron/ +# npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ +# npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ +# # 配置yarn源 +# yarn config set registry https://registry.npm.taobao.org -g +# yarn config set disturl https://npm.taobao.org/dist -g +# yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g +# yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g +# yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g +# yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g +# yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g +# yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g +# " + check::exit_code "$?" "nodejs" "$host 安装nodejs环境" "exit" + log::info "[nodejs]" "安装nodejs环境成功!" +} + +###################################################################################################### +# 主调用逻辑 +###################################################################################################### +trap trap::info 1 2 3 15 EXIT + +# 使用帮助 +function help::usage() { + cat << EOF +Usage: + jenkins-agent-install-centos.sh [command] + +Available Commands: + init 初始化系统 +Flag: + -base-dir 新增用户名, 默认: '/data/jenkins-agent/env' + -git 是否安装git, 默认: '1' + -java 是否安装java, 默认: '1' + -gradle 安装gradle版本, 默认: '6.8.3' + -maven 是否安装maven环境, 默认: '3.8.6' + -nodejs 是否安装nodejs环境, 默认: '16.18.0' + +Example: + jenkins-agent-install-centos.sh init \\ + -git 0 \\ + -java 0 \\ + -gradle '6.8.3' \\ + -maven '3.8.6' \\ + -nodejs '16.18.0' + +EOF + exit 1 +} + +# 参数处理 +[ "$#" == "0" ] && help::usage +while [ "${1:-}" != "" ]; do + case $1 in + # -------------------------------------------------------------- 指令 + init ) INIT_TAG="1" + ;; + # -------------------------------------------------------------- 指令参数 + -base-dir ) shift + BASE_DIR=${1:-BASE_DIR} + ;; + -git ) shift + GIT_TAG=${1:-GIT_TAG} + ;; + -java ) shift + JAVA_TAG=${1:-JAVA_TAG} + ;; + -gradle ) shift + GRADLE_TAG=${1:-GRADLE_TAG} + ;; + -maven ) shift + MAVEN_TAG=${1:-MAVEN_TAG} + ;; + -nodejs ) shift + NODEJS_TAG=${1:-NODEJS_TAG} + ;; + * ) help::usage + exit 1 + esac + shift +done + +# 开始 +log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}" +# 动作 +if [[ "${INIT_TAG:-}" == "1" ]]; then + check::command + [[ "${GIT_TAG:-}" == "1" ]] && { init:git; } + [[ "${JAVA_TAG:-}" == "1" ]] && { init:java; } + [[ "${GRADLE_TAG:-}" == "1" ]] && { init:gradle; } + [[ "${MAVEN_TAG:-}" == "1" ]] && { init:maven; } + [[ "${NODEJS_TAG:-}" == "1" ]] && { init:nodejs; } +else + help::usage +fi + +# bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/08jenkins-agent-install-centos.sh) [cmd]