You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
493 lines
16 KiB
493 lines
16 KiB
#!/usr/bin/env bash
|
|
|
|
#-----------------------------------------------------------------------------------------------------
|
|
# OracleJDK 下载地址
|
|
# 镜像1: https://www.injdk.cn/ (https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.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)
|
|
#
|
|
#
|
|
#
|
|
#-----------------------------------------------------------------------------------------------------
|
|
|
|
# 配置
|
|
ADD_USER_NAME="www"
|
|
ADD_USER_PASSWORD="MoXF2Zi6u7f7lqZu"
|
|
BASE_DIR="/opt"
|
|
|
|
# 软件
|
|
YUM_REPOS="http://mirrors.aliyun.com/repo/Centos-7.repo"
|
|
#--- jdk
|
|
JDK_URL="https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz"
|
|
JDK_FILE_NAME="jdk-11.0.16.1_linux-x64_bin.tar.gz"
|
|
JDK_DIR_NAME="jdk-11.0.16.1"
|
|
#--- gradle
|
|
|
|
#--- maven
|
|
|
|
#--- jenkins
|
|
|
|
|
|
# 安装标识
|
|
#--- 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"
|
|
#--- 指令参数
|
|
UPDATE_YUM_REPOS="1"
|
|
ADD_USER_TAG="1"
|
|
GIT_TAG="1"
|
|
DSTAT_TAG="1"
|
|
HTOP_TAG="1"
|
|
NGINX_TAG="1"
|
|
NODEJS_TAG="1"
|
|
JAVA_TAG="1"
|
|
GRADLE_TAG="1"
|
|
MAVEN_TAG="1"
|
|
JENKINS_TAG="1"
|
|
|
|
# 脚本设置
|
|
TMP_DIR="$(rm -rf /tmp/env-install* && mktemp -d -t env-install.XXXXXXXXXX)"
|
|
LOG_FILE="${TMP_DIR}/env-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 utils::download_file() {
|
|
local url="$1"
|
|
local dest="$2"
|
|
local unzip_tag="${3:-1}"
|
|
local dest_dirname; dest_dirname=$(dirname "$dest")
|
|
local filename; filename=$(basename "$dest")
|
|
log::info "[download]" "${filename}"
|
|
command::exec "${MGMT_NODE}" "
|
|
set -e
|
|
if [ ! -f \"${dest}\" ]; then
|
|
[ ! -d \"${dest_dirname}\" ] && mkdir -pv \"${dest_dirname}\"
|
|
wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate \"${url}\" -O \"${dest}\"
|
|
if [[ \"${unzip_tag}\" == \"unzip\" ]]; then
|
|
command -v unzip 2>/dev/null || yum install -y unzip
|
|
unzip -o \"${dest}\" -d \"${dest_dirname}\"
|
|
fi
|
|
else
|
|
echo \"${dest} is exists!\"
|
|
fi
|
|
"
|
|
local status="$?"
|
|
check::exit_code "$status" "download" "${filename}" "exit"
|
|
return "$status"
|
|
}
|
|
|
|
# 检查命令是否存在
|
|
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
|
|
}
|
|
|
|
######################################################################################################
|
|
# 安装函数
|
|
######################################################################################################
|
|
# 更新yum源
|
|
function init:update_yum_repos() {
|
|
log::info "[update-yum-repos]" "更新yum源..."
|
|
local host="127.0.0.1"
|
|
local repo="/etc/yum.repos.d/CentOS-Base.repo"
|
|
local repo_bak="/etc/yum.repos.d/CentOS-Base.repo_bak"
|
|
command::exec "${host}" "
|
|
if [ ! -f "$repo_bak" ]; then
|
|
mv $repo $repo_bak
|
|
wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${YUM_REPOS} -O $repo
|
|
yum makecache
|
|
fi
|
|
"
|
|
check::exit_code "$?" "update-yum-repos" "$host 更新yum源" "exit"
|
|
log::info "[update-yum-repos]" "yum源更新成功!"
|
|
}
|
|
|
|
# 新增用户
|
|
function init:add_user() {
|
|
log::info "[add-user]" "新增用户[${ADD_USER_NAME}]..."
|
|
local host="127.0.0.1"
|
|
local add_sudoers="${ADD_USER_NAME} ALL=(ALL) NOPASSWD:ALL"
|
|
command::exec "${host}" "
|
|
id -u ${ADD_USER_NAME} >/dev/null 2>&1
|
|
if [ \$? -ne '0' ]; then
|
|
# 创建用户
|
|
adduser ${ADD_USER_NAME}
|
|
# 设置密码
|
|
echo ${ADD_USER_NAME}:${ADD_USER_PASSWORD} | chpasswd
|
|
fi
|
|
if [ \`grep -c '$add_sudoers' '/etc/sudoers'\` == '0' ];then
|
|
# 新增修改权限
|
|
chmod -v u+w /etc/sudoers
|
|
echo -e '$add_sudoers' >> /etc/sudoers
|
|
# 收回修改权限
|
|
chmod -v u-w /etc/sudoers
|
|
fi
|
|
"
|
|
check::exit_code "$?" "add-user" "$host 新增用户[${ADD_USER_NAME}]" "exit"
|
|
log::info "[add-user]" "新增用户[${ADD_USER_NAME}]成功!"
|
|
}
|
|
|
|
# 安装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环境安装成功!"
|
|
if [[ "${ADD_USER_TAG:-}" == "1" ]]; then
|
|
command::exec "${host}" "
|
|
echo '${ADD_USER_PASSWORD}' | su - ${ADD_USER_NAME} -c \"git config --global credential.helper store\"
|
|
"
|
|
check::exit_code "$?" "git" "$host git配置" "exit"
|
|
log::info "[git]" "git配置成功!"
|
|
fi
|
|
}
|
|
|
|
# 安装dstat
|
|
function init:dstat() {
|
|
log::info "[dstat]" "安装dstat..."
|
|
local host="127.0.0.1"
|
|
command::exec "${host}" "
|
|
yum -y install dstat
|
|
dstat -V
|
|
"
|
|
check::exit_code "$?" "dstat" "$host 安装dstat" "exit"
|
|
log::info "[dstat]" "dstat安装成功!"
|
|
}
|
|
|
|
# 安装htop
|
|
function init:htop() {
|
|
log::info "[htop]" "安装htop..."
|
|
local host="127.0.0.1"
|
|
command::exec "${host}" "
|
|
yum -y install htop
|
|
htop -v
|
|
"
|
|
check::exit_code "$?" "htop" "$host 安装htop" "exit"
|
|
log::info "[htop]" "htop安装成功!"
|
|
}
|
|
|
|
# 安装nginx
|
|
function init:nginx() {
|
|
log::info "[nginx]" "安装nginx..."
|
|
local host="127.0.0.1"
|
|
command::exec "${host}" "
|
|
yum -y install nginx
|
|
nginx -v
|
|
# sudo systemctl enable nginx
|
|
"
|
|
check::exit_code "$?" "nginx" "$host 安装nginx" "exit"
|
|
log::info "[nginx]" "nginx安装成功!"
|
|
}
|
|
|
|
# 安装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环境成功!"
|
|
}
|
|
|
|
# 安装java环境
|
|
function init:java() {
|
|
log::info "[java]" "安装java环境..."
|
|
local host="127.0.0.1"
|
|
}
|
|
|
|
# 安装gradle环境
|
|
function init:gradle() {
|
|
log::info "[gradle]" "安装gradle环境..."
|
|
local host="127.0.0.1"
|
|
}
|
|
|
|
# 安装maven环境
|
|
function init:maven() {
|
|
log::info "[maven]" "安装maven环境..."
|
|
local host="127.0.0.1"
|
|
}
|
|
|
|
# 安装jenkins环境
|
|
function init:jenkins() {
|
|
log::info "[jenkins]" "安装jenkins环境..."
|
|
local host="127.0.0.1"
|
|
}
|
|
|
|
######################################################################################################
|
|
# 主调用逻辑
|
|
######################################################################################################
|
|
trap trap::info 1 2 3 15 EXIT
|
|
|
|
# 使用帮助
|
|
function help::usage() {
|
|
cat << EOF
|
|
Usage:
|
|
env-install-centos.sh [command]
|
|
|
|
Available Commands:
|
|
init 初始化系统
|
|
Flag:
|
|
-update-yum-repos 是否更新yum源, 默认: '1'
|
|
-add-user 是否新增用户, 默认: '1'
|
|
-user-name 新增用户名, 默认: 'www'
|
|
-base-dir 新增用户名, 默认: '/opt'
|
|
-git 是否安装git环境, 默认: '1'
|
|
-dstat 是否安装dstat, 默认: '1'
|
|
-htop 是否安装htop, 默认: '1'
|
|
-nginx 是否安装nginx, 默认: '1'
|
|
-nodejs 是否安装nodejs环境, 默认: '1'
|
|
-java 是否安装java环境, 默认: '1'
|
|
-gradle 是否安装gradle环境, 默认: '1'
|
|
-maven 是否安装maven环境, 默认: '1'
|
|
-jenkins 是否安装jenkins环境, 默认: '1'
|
|
|
|
Example:
|
|
env-install-centos.sh init \\
|
|
-add-user \\
|
|
-nodejs \\
|
|
-java \\
|
|
-gradle 0 \\
|
|
-maven
|
|
|
|
EOF
|
|
exit 1
|
|
}
|
|
|
|
# 参数处理
|
|
[ "$#" == "0" ] && help::usage
|
|
while [ "${1:-}" != "" ]; do
|
|
case $1 in
|
|
# -------------------------------------------------------------- 指令
|
|
init ) INIT_TAG="1"
|
|
;;
|
|
# -------------------------------------------------------------- 指令参数
|
|
-update-yum-repos ) shift
|
|
UPDATE_YUM_REPOS=${1:-UPDATE_YUM_REPOS}
|
|
;;
|
|
-add-user ) shift
|
|
ADD_USER_TAG=${1:-ADD_USER_TAG}
|
|
;;
|
|
-user-name ) shift
|
|
ADD_USER_NAME=${1:-ADD_USER_NAME}
|
|
;;
|
|
-base-dir ) shift
|
|
BASE_DIR=${1:-BASE_DIR}
|
|
;;
|
|
-git ) shift
|
|
GIT_TAG=${1:-GIT_TAG}
|
|
;;
|
|
-dstat ) shift
|
|
DSTAT_TAG=${1:-DSTAT_TAG}
|
|
;;
|
|
-htop ) shift
|
|
HTOP_TAG=${1:-HTOP_TAG}
|
|
;;
|
|
-nginx ) shift
|
|
NGINX_TAG=${1:-NGINX_TAG}
|
|
;;
|
|
-nodejs ) shift
|
|
NODEJS_TAG=${1:-NODEJS_TAG}
|
|
;;
|
|
-java ) shift
|
|
JAVA_TAG=${1:-JAVA_TAG}
|
|
;;
|
|
-gradle ) shift
|
|
GRADLE_TAG=${1:-GRADLE_TAG}
|
|
;;
|
|
-maven ) shift
|
|
MAVEN_TAG=${1:-MAVEN_TAG}
|
|
;;
|
|
-jenkins ) shift
|
|
JENKINS_TAG=${1:-JENKINS_TAG}
|
|
;;
|
|
* ) help::usage
|
|
exit 1
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# 开始
|
|
log::info "[start]" "bash $0 ${SCRIPT_PARAMETER}"
|
|
# 动作
|
|
if [[ "${INIT_TAG:-}" == "1" ]]; then
|
|
[[ "${UPDATE_YUM_REPOS:-}" == "1" ]] && { init:update_yum_repos; }
|
|
[[ "${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
|
|
|
|
# bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/07env-install-centos.sh) [cmd]
|
|
|