码迷,mamicode.com
首页 > 系统相关 > 详细

记录学习linux下shell用法

时间:2019-11-04 18:10:14      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:日志文件   stdin   alarm   mode   set   哪些   oba   pool   脚本   

#!/bin/sh

#set -u

[[ "$1" == "-v" ]] &&
{
cat <<EOF
version:
# 2.0 进行增量升级
1. 2016.7.13 比较稳定的版本
2. 2016.7.20 检查必须以root进行登录才能进行升级
3. 2016.8.8 增加软链接的处理
4. 2016.9.12 升级时对quote-calc/etc/block目录进行忽略
根据配置文件只升级指定的模块
升级前,清除所有的log文件
5. 2016.9.26 修改版本号的判断
6. 2016.11.2 修改版本号的判断,以空格为分隔, 取第二个值
可自动检测并进行脚本的更新
用颜色显示忽略的文件,是否要进行更新
7. 2016.11.10 增加记录升级列表
8. 2016.12.22 Feat: 记录整个脚本的运行过程到日志文件
关闭脚本自动更新的功能
EOF
exit 0
}


NOW_TIME=`date ‘+%Y-%m-%d-%H%M‘`
CUR_TIME=`date +%Y%m%d%H%M%S`
CURRENT_PWD=`pwd`
KDS_CONF="${CURRENT_PWD}/kds.conf"
out_list_file=/opt/.setup/Upgrade_instractions_${CUR_TIME}.log
update_log=/opt/.setup/Upgrade_${CUR_TIME}.log
all_log=/opt/.setup/Upgrade_update_process_${CUR_TIME}.log
ignore_list_file=/opt/.setup/Upgrade_file_${CUR_TIME}.log
ignore_list_diff=/opt/.setup/Upgrade_diff_${CUR_TIME}.log
new_mobile_stock_path=${PWD}/mobile-stock
old_mobile_stock_path=/opt/kds/mobile-stock
backup_dir=/opt/.backup
upgrade_list=./upgrade.list
OPT_DEBUG=4
args="$@"


# 以下文件需要对版进行对比
compare_version_file_lists=(
web-admin/bin/web-admin
auth-agent/bin/auth-agent
auth-service/bin/auth-service
news-service/bin/news-service
news-transform/bin/news-transform
quote-calc/bin/quote-calc
quote-receive/bin/quote-receive
quote-service/bin/quote-service
quote-transform/bin/quote-transform
system-service/bin/system-service
trade-service/bin/trade-service
)


# 以下文件需要用md5进行对比
compare_md5_file_lists=(
kds-agent/bin/get_monitor.pl
news-distribute/bin/news-receive.sh
quote-common/etc/index_conf.txt
common-soft/test-tools/bin/common-soft
trade-service/etc/business.kds
tou-gu/bin/tou-gu
trade-service/etc/backend_business.kds
trade-service/etc/backend.kds
trade-service/etc/business_program.kds
trade-service/etc/dict.kds
web-switch/bin/web-switch
)

compare_ignore_file_list=(
send-message/etc/config.json
system-service/etc/config.json
system-service/etc/nginx-upstream.conf
system-service/etc/nginx-location.conf
web-admin/etc/nginx-upstream-manage.conf
web-admin/etc/nginx-location-manage.conf
web-admin/etc/nginx-upstream.conf
web-admin/etc/nginx-location.conf
news-service/etc/config.json
news-service/etc/nginx-upstream.conf
news-service/etc/nginx-location.conf
tou-gu/etc/config.json
tou-gu/etc/nginx-upstream.conf
tou-gu/etc/nginx-location.conf
web-switch/etc/nginx.conf
alarm-service/etc/config.json
alarm-service/etc/nginx-upstream.conf
alarm-service/etc/nginx-location.conf
redis-pool/etc/config.redis
news-staticize/etc/config.json
quote-service/etc/nginx-upstream.conf
quote-service/etc/nginx-location.conf
trade-service/etc/config.json.default
trade-service/etc/config.json
trade-service/etc/redis.conf
trade-service/etc/nginx-upstream.conf
trade-service/etc/nginx-location.conf
auto-deploy/etc/redis.conf
config-service/etc/nginx-upstream.conf
config-service/etc/nginx-location.conf
auth-agent/etc/config.json
auth-agent/etc/nginx-upstream.conf
auth-agent/etc/nginx-location.conf
news-transform/etc/config.json
auth-service/etc/config.json
auth-service/etc/nginx-upstream.conf
auth-service/etc/nginx-location.conf
quote-common/etc/config.json
3rd-soft/etc/redis.conf
bin/auth-agent-redis-start.sh
bin/auth-agent-redis-stop.sh
bin/auth-agent-redis-status.sh
bin/trade-service-redis-start.sh
bin/trade-service-redis-stop.sh
bin/trade-service-redis-status.sh
bin/memcached-start.sh
bin/gearman-job-server-start.sh
bin/redis-start.sh
bin/redis-stop.sh
bin/redis-backup.sh
)

# 输出日志信息
lg()
{
[[ -n "$OPT_DEBUG" && $1 -ge $OPT_DEBUG ]] && {
shift
echo -e $@
}
}

# 打印错误信息
err()
{
echo -e $@ >&2
exit 1
}

fun_need_root()
{
[[ root == $LOGNAME ]] && return 0
return 1
}

write_upgrade_list()
{
[[ $1 ]] || lg 5 "$FUNCNAME <package name>"
echo "[`date +%F-%T`] upgrade $1" >> $upgrade_list
}

# 判断文件是否存在指定的文件列表中
if_in_lists()
{
# $1 file which to check
# $2~.. lists

[[ -z "$2" ]] && err "$FUNCNAME <file> <lists[]>"

local file=$1
lg 3 file: $file
shift
local s=0

for s in $@; do
lg 3 "file: $file <> list: $s "
[[ $s == $file ]] && return 0
done

return 1
}

# 二进制文件按版本进行对比
compare_exe_file()
{
# $1 filename

[[ -z "$1" ]] && err "$FUNCNAME <file>"
# ??? 是否需要对库文件进行检查?
#local old_ver=`su - kds -c "$old_mobile_stock_path/$1 -v |grep -o \"v[0-9\.\-a-zA-Z]\+\""`
#local new_ver=`su - kds -c "$new_mobile_stock_path/$1 -v |grep -o \"v[0-9\.\-a-zA-Z]\+\""`

local old_ver=`su - kds -c "$old_mobile_stock_path/$1 -v |awk ‘{print $2}‘"`
local new_ver=`su - kds -c "$new_mobile_stock_path/$1 -v |awk ‘{print $2}‘"`

lg 3 $1
lg 3 old_ver=$old_ver new_ver=$new_ver

[[ "$old_ver$new_ver" == "" ]] && return 1
[[ "$old_ver" == "$new_ver" ]] && return 0

return 1
}

# 普通文件按md5对比 ??? 是否直接对比?
compare_oth_file()
{
# $1 filename

[[ -z "$1" ]] && err "$FUNCNAME <file>"

#local old_md5=`md5sum "$old_mobile_stock_path/$1" |awk ‘{print $1}‘`
#local new_md5=`md5sum "$new_mobile_stock_path/$1" |awk ‘{print $1}‘`

#[[ "$old_md5" == "$new_md5" ]] && return 0

diff "$old_mobile_stock_path/$1" "$new_mobile_stock_path/$1" > /dev/null
}

deal_old_log()
{
# $1 要过滤的通配符
# $2 名字前缀
# $3 要保存的目录
local list=`ls $1 2>/dev/null`
[[ -z "$list" ]] && return 1
[[ -d $3 ]] || mkdir -p $3

local time=`echo $list |awk ‘{print $1}‘ |grep -o "[0-9]\+"`
echo 备份 $list 到 $3/${2}_${time}.tgz
tar czf $3/${2}_${time}.tgz $list
rm -f $list
}

fun_display()
{
# 显示信息
# $1 显示类别
# $2 显示信息

[[ $# -ne 2 ]] && err "$LINENO $FUNCNAME <显示类别> <显示信息>"
local type=$1
local msg=$2

case $type in
R|r)
printf "\033[31;1m$2\033[0m\n"
;;
G|g)
printf "\033[32;1m$2\033[0m\n"
;;
Y|y)
printf "\033[33;1m$2\033[0m\n"
;;
*)
err "$LINENO $FUNCNAME 错误的显示类别"
esac
}

fun_diff()
{

[[ -z "$1" ]] && err "$FUNCNAME <file name>"

local file1=$old_mobile_stock_path/$1
local file2=$new_mobile_stock_path/$1

[[ -f "$file1" ]] || { lg 4 线上不存在: "$1"; return 1; }
if ! diff -q -w -B -E "$file1" "$file2" &> /dev/null ; then
if [[ `cat "$file1" |wc -l` == `cat "$file2" |wc -l` ]] ; then
#lg 4 内容不同: "$1"
fun_display y "内容不同: $1"
else
#lg 4 行数不同: $1
fun_display r "行数不同: $1"
fi
else
continue
fi

echo "diff -W 115 -y $file1 $file2" >> $ignore_list_diff
{ diff -W 115 -y -w -B -E --left-column $file1 $file2 |sed ‘s/\t\+$//g;s/\r//g‘;echo; } >> $ignore_list_diff
}

diff_file()
{
local file=
lg 4 ""
lg 4 ==== 检查忽略的文件是否有不同的 ====
while read file ; do
fun_diff $file
done < $ignore_list_file
}

# 输出列表到文件
out_list()
{
# 文件进行分类处理
# $1 操作类型(覆盖/新增)
# $2 file

[[ -z "$2" ]] && err "$FUNCNAME <file>"

local tmp_file_prefix=.setup2.0_process_file_one
local model_name=`echo "$2" |awk -F\/ ‘{print $1}‘`
lg 3 $1:$2
echo $1:$2 >> $tmp_file_prefix.$model_name


#echo "$1:$2" >> $out_list_file
#echo 4 "$1:$2"
}

# 按指定的模块,将其它的模块删除
delete_module()
{
local s=
local ss=
local oper=N
local module_name=
local oper1=N
local dir_name=
local dir_list=
local Ylist=README
local found=false

for s in ${list[@]}
do
module_name=`echo $s |awk -F= ‘{print $1}‘ |xargs`
oper=`echo $s |awk -F= ‘{print $2}‘ |xargs`
if [[ $oper == Y ]] ;then
dir_list=`eval echo \$\{$module_name[@]\}`
for ss in ${dir_list[@]}
do
dir_name=`echo $ss |awk -F= ‘{print $1}‘ |xargs`
oper1=`echo $ss |awk -F= ‘{print $2}‘ |xargs`
[[ $oper1 == Y ]] && {
Ylist="$Ylist $dir_name"
}
done
fi
done

for dirs in `ls $new_mobile_stock_path`
do
found=false
for y in $Ylist
do
[[ $dirs == $y ]] && {
found=true
break
}
done

[[ $found == false ]] && {
echo rm $new_mobile_stock_path/$dirs
rm -rf $new_mobile_stock_path/$dirs
}
done
}

# 处理文件并生成列表
process_file()
{
# $1 base path

#[[ -z "$1" ]] && err "$FUNCNAME <base path>"
#[[ -d "$1" ]] || err "$FUNCNAME $1 is not a directory"
echo 正在解压 mobile-stock ..
rm -rf mobile-stock
tar xzvf $INST_KDS_PACKAGE_NAME mobile-stock > /dev/null
chown -R kds.kds mobile-stock
echo 解压完成.

# 删除不需要的模块
delete_module
echo ============= 要升级的模块 ===============
ls $new_mobile_stock_path
echo ==========================================
sleep 3

local tmp_file=.setup2.0_process_file
local tmp_link_file=.setup2.0_process_link_file
local s=
local item=

# 比对文件列表
#(cd $new_mobile_stock_path && find . -type f ; find . -type f -name ".*" && cd - >/dev/null) |sed ‘s#^./##g‘ > $tmp_file
(cd $new_mobile_stock_path && find . -type f && cd - >/dev/null) |sed ‘s#^./##g‘ |sort -u > $tmp_file
(cd $new_mobile_stock_path && find . -type l && cd - >/dev/null) |sed ‘s#^./##g‘ |sort -u > $tmp_link_file

echo
echo ==== 正在生成文件列表 ====
# 忽略某些目录
for item in "^common-soft\/" "^system-service\/data\/" "^config-service\/data\/" "^quote-calc\/etc\/block"
do
lg 4 忽略目录 $item
# grep "$item" $tmp_file >> $ignore_list_file
sed -i "/$item/d" $tmp_file
done

while read s;do
lg 2 "compare: \t$old_mobile_stock_path/$s \n\t$new_mobile_stock_path/$s"
if [ ! -f "$old_mobile_stock_path/$s" ];then
out_list 新增 "$s"
elif if_in_lists "$s" ${compare_ignore_file_list[@]} > /dev/null;then
lg 5 忽略文件 "$s"
echo $s >> $ignore_list_file
elif if_in_lists "$s" ${compare_md5_file_lists[@]} > /dev/null;then
compare_oth_file "$s" || out_list 覆盖 "$s"
elif if_in_lists "$s" ${compare_version_file_lists[@]} > /dev/null;then
compare_exe_file "$s" || out_list 覆盖 "$s"
else
compare_oth_file "$s" || out_list 覆盖 "$s"
fi
done < $tmp_file
echo 生成文件完成.

diff_file
merge_file
}

merge_single_file()
{
# 处理模块文件
# $1 dir list
# $2 msg

lg 3 处理 $@
[[ "$#" -ne 2 ]] && err "$FUNCNAME <dir list> <msg>"

local tmp_file_prefix=.setup2.0_process_file_one
local flag=0
local s=

for s in $1
do
[[ -f $tmp_file_prefix.$s ]] && {
[[ $flag -eq 0 ]] && {
echo "$2" >> $out_list_file
flag=1
}
cat $tmp_file_prefix.$s >> $out_list_file
#rm -f $tmp_file_prefix.$s
}

done
}

merge_file()
{
# 文件名进行合并
local tmp_file_prefix=.setup2.0_process_file_one
local model_rz="auth-agent auth-service system-service"
local model_jy="trade-service"
local model_hq="quote-common quote-calc quote-globalIndex quote-receive quote-service quote-tool quote-transform"
local model_zx="news-distribute news-service news-staticize news-transform"
local all=`ls $new_mobile_stock_path`
local a=
local b=
local model_ot=
local found=

for b in $all ; do
found=0
for a in $model_rz $model_jy $model_hq $model_zx ; do
[[ $a == $b ]] && {
found=1
break
}
done
[[ $found == 0 ]] && model_ot="$model_ot $b"
done

rm -f $out_list_file
echo "# package name -- $INST_KDS_PACKAGE_NAME" > $out_list_file
merge_single_file "$model_rz" "# ------ 认证模块 ------"
merge_single_file "$model_jy" "# ------ 交易模块 ------"
merge_single_file "$model_hq" "# ------ 行情模块 ------"
merge_single_file "$model_zx" "# ------ 资讯模块 ------"
merge_single_file "$model_ot" "# ------ 其它模块 ------"
}

# 根据列表进行文件替换
apply_modify()
{
# 说明: 将process_file生成的需要修改列表,进行升级替换
# [[ -f $out_list_file ]] || return

local line=
local file=
local tmp_dir=.setup2.0_process_dir
local tmp_link_file=.setup2.0_process_link_file
echo

if [[ -f $out_list_file && `cat $out_list_file |wc -l` -ne 0 ]] ; then
echo =========== 更新列表 ===========
cat $out_list_file
echo ================================
echo -n 按[ENTER]开始升级:
read res

write_upgrade_list $INST_KDS_PACKAGE_NAME
# 处理目录结构,增加线上的环境不存在的目录
#(cd $new_mobile_stock_path && find . -type d ; find . -type d -name ".[^.]*" && cd - >/dev/null)|sed ‘s#^./##g‘ |sort -u > $tmp_dir
(cd $new_mobile_stock_path && find . -type d && cd - >/dev/null)|sed ‘s#^./##g‘ |sort -u > $tmp_dir
while read s ; do
[[ $s == "." || $s == ".." ]] && continue
lg 3 测试dir: $s
[[ -d "$old_mobile_stock_path/$s" ]] || {
lg 4 创建目录 $old_mobile_stock_path/$s
su - kds -c "mkdir -p $old_mobile_stock_path/$s"
}
done < $tmp_dir

# 升级文件
echo 正在升级..
while read line ;do
echo $line |grep "^#" && continue
#echo -n .
file=`echo $line |awk -F: ‘{print $2}‘`
[[ -f "$new_mobile_stock_path/$file" ]] && su - kds -c "cp -vf \"$new_mobile_stock_path/$file\" \"$old_mobile_stock_path/$file\"" >> $update_log
done < $out_list_file

echo 升级软件链接文件.. |tee -a $update_log
while read file
do
su - kds -c "\\cp -vfd \"$new_mobile_stock_path/$file\" \"$old_mobile_stock_path/$file\"" 2>&1 >> $update_log
done < $tmp_link_file

echo
echo 升级完成. |tee -a $update_log
else
echo 没有不同的文件,不需要升级 |tee -a $update_log
fi
}

# 加载部署方案
LoadSetupConfig() {
echo "加载部署方案"
if [ ! -f $KDS_CONF ]; then
echo "找不到 ${KDS_CONF} 文件。请撰写部署方案文件,并放置到文档指定目录。"
exit
fi
. $KDS_CONF

if [[ "`echo ${list[@]}`" == "" ]];then
echo 请使用最新的kds.conf配置文件, 里面有可以控制升级哪些模块的配置
exit 3
fi

}

CreateUser() {
echo "创建 kds 用户"
id kds
if [ $? -eq 0 ]; then
echo "用户已存在,将会重置密码"
echo $SYS_KDS_PASSWD | passwd --stdin kds
else
echo "创建 kds 用户"
useradd kds -m -d $SYS_KDS_USER_HOME
# 设置密码
echo $SYS_KDS_PASSWD | passwd --stdin kds
fi
}

DeleteUserKds()
{
id kds
if [ $? -eq 0 ]; then
killall -u kds
sleep 3
killall -9 -u kds
userdel kds
# 删除 sudo 相关信息
rm -rf /etc/sudoers.d/kds
rm -rf /opt/kds
echo "kds 用户已被删除"
exit
else
echo "已无 kds 用户存在!"
exit
fi
}

# 设置系统环境
SetupSystemEnv() {
echo "现在开始设置系统环境"
# 创建用户
CreateUser
# 设置服务器名称,重启服务器生效
sname=/etc/sysconfig/network
cp $sname $sname`date "+%Y%m%d%H%M$S"`
echo NETWORKING=yes > $sname
echo HOSTNAME=$SYS_HOSTNAME >> $sname
# 添加服务器外网DNS脚本
dns_root=/etc/resolv.conf
echo nameserver 114.114.114.114 >$dns_root
echo nameserver 114.114.115.115 >>$dns_root
echo nameserver 8.8.8.8 >>$dns_root
# 设置关闭服务器防火墙
service iptables stop
chkconfig iptables off
# 设置时区、网络对时
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#ntpdate us.pool.ntp.org ----- 当前需要手动设置时间
}

# 通过本地安装包安装工具软件
LocalInstallTools() {
echo "安装第三方常用工具"
if [ ! -f $CURRENT_PWD/$INST_RPM_PACKAGE ]; then
echo 请提供RPM安装包 $CURRENT_PWD/$INST_RPM_PACKAGE,并放置到当前目录。
exit
fi
tar xzvf $CURRENT_PWD/$INST_RPM_PACKAGE
cd $CURRENT_PWD/rpm
rpm -Uvh --aid --nodeps *.rpm
cd ../; rm -rf rpm
}

# 安装移动证券
InstallMobileStock() {
echo "安装移动证券"
if [ ! -f $CURRENT_PWD/$INST_KDS_PACKAGE_NAME ]; then
echo 请提供程序安装包 $CURRENT_PWD/$INST_KDS_PACKAGE_NAME,并放置到当前目录。
exit
fi
chown kds:kds $CURRENT_PWD/$INST_KDS_PACKAGE_NAME
su - kds -c "tar -C $INST_KDS_INSTALL_PATH -zxvf $CURRENT_PWD/$INST_KDS_PACKAGE_NAME mobile-stock"
}

# 部署 freetds
SetupFreetdsConf() {
rm -rf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/pool.conf
rm -rf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/locales.conf
rm -rf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/freetds.conf
su - kds -c "mkdir -p $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/"
su - kds -c "ln -s $INST_KDS_INSTALL_PATH/mobile-stock/etc/pool.conf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/pool.conf"
su - kds -c "ln -s $INST_KDS_INSTALL_PATH/mobile-stock/etc/locales.conf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/locales.conf"
su - kds -c "ln -s $INST_KDS_INSTALL_PATH/mobile-stock/etc/freetds.conf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/freetds.conf"
}

# 部署 nginx
SetupNginxConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 nginx_deploy.pl $CONF_NGINX_TT_FILE $CONF_NGINX_WORKER_PROCESSES $CONF_NGINX_HTTP_PORT $CONF_NGINX_HTTPS_PORT $CONF_NGINX_WORKER_RLIMIT_NOFILE $CONF_NGINX_WORKER_CONNECTIONS $CONF_NGINX_USER $CONF_NGINX_WORKER_CPU_AFFINITY > $CONF_NGINX_CONFIG_FILENAME"
}

# 部署 auth-service
SetupAuthServiceConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 auth_service_deploy.pl $CONF_AS_TT_FILE $CONF_YDZQ_DB_NAME $CONF_YDZQ_DB_USER $CONF_YDZQ_DB_PWD $CONF_YDZQ_DB_IP $CONF_YDZQ_DB_PORT $CONF_AS_SUPPORT_APPID $CONF_AS_SUPPORT_SWITCH > $CONF_AS_CONFIG_FILENAME"
}

# 部署 auth-agent
SetupAuthAgentConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 auth_agent_deploy.pl $CONF_AA_TT_FILE $CONF_AA_LOG_LEVEL $CONF_AA_REDIS_IP $CONF_AA_REDIS_PORT $CONF_AA_HOSTNAME > $CONF_AA_CONFIG_FILENAME"
}

# 部署 system-service
SetupSystemServiceConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 system_service_deploy.pl $CONF_SS_TT_FILE $CONF_YDZQ_DB_NAME $CONF_YDZQ_DB_USER $CONF_YDZQ_DB_PWD $CONF_YDZQ_DB_IP $CONF_YDZQ_DB_PORT > $CONF_SS_CONFIG_FILENAME"
}

# 部署
SetupRedisConf() {
su - kds -c "cp $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/etc/redis.conf $INST_KDS_INSTALL_PATH/mobile-stock/3rd-soft/etc/"
}

# 部署 trade-service
SetupTradeServiceConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 trade_service_deploy.pl $CONF_TS_TT_FILE $CONF_TS_LOG_LEVEL $CONF_TS_GEARMAN_IP $CONF_TS_GEARMAN_PORT $CONF_TS_OF_REDIS_IP $CONF_TS_OF_REDIS_PORT > $CONF_TS_CONFIG_FILENAME"
}


# 部署 quote-common
SetupQuoteCommConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 quote_comm_deploy.pl $CONF_QC_TT_FILE $CONF_QC_CALC_LOG_LEVEL $CONF_QC_RECEIVE_LOG_LEVEL $CONF_QC_TRANSFROM_LOG_LEVEL $CONF_QC_SERVICE_LOG_LEVEL $CONF_QC_QQGZ_REDIS_IP $CONF_QC_QQGZ_REDIS_PORT $CONF_QC_DBF_SERVER $CONF_QC_HOLIDAYS $CONF_QC_HK_HOLIDAYS > $CONF_QC_CONFIG_FILENAME"
}


# 部署 redis-pool
SetupRedisPoolConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 redis_deploy.pl $CONF_REDIS_TT_FILE $CONF_REDIS_IP $CONF_REDIS_PORT $CONF_REDIS_NUM > $CONF_REDIS_CONFIG_FILENAME"
}

# 部署 kds-agent
SetupKDSAgentConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 kds_agent_deploy.pl $CONF_KA_TT_FILE $CONF_KA_WA_ADDR $CONF_LOCAL_IP $CONF_OMS_DB_NAME $CONF_OMS_DB_IP $CONF_OMS_DB_PORT $CONF_OMS_DB_USER $CONF_OMS_DB_PWD > $CONF_KA_CONFIG_FILENAME"
}

# 部署 admin-web
SetupWebAdminConf() {
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 admin-web_deploy.pl $CONF_WA_TT_ADMIN_FILE $CONF_AW_PORT $CONF_OMS_DB_USER $CONF_OMS_DB_PWD $CONF_OMS_DB_IP $CONF_OMS_DB_PORT $CONF_OMS_DB_NAME $CONF_YDZQ_DB_USER $CONF_YDZQ_DB_PWD $CONF_YDZQ_DB_IP $CONF_YDZQ_DB_PORT $CONF_YDZQ_DB_NAME $CONF_REDIS_IP $CONF_REDIS_PORT $CONF_REDIS_NUM > $CONF_WA_ADMIN_CONFIG_FILENAME"
su - kds -c "cd $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/script; perl5.20.1 admin-comm_deploy.pl $CONF_WA_TT_COMM_FILE $CONF_AW_IP $CONF_AW_PORT > $CONF_WA_COMM_CONFIG_FILENAME"
}

# 设置操作系统配置
SetupSystem() {
cd $INST_KDS_INSTALL_PATH/mobile-stock/etc
# 设置环境变
echo ". `pwd`/dot-bashrc-kds-mobile-stock" >> $SYS_KDS_USER_HOME/.bashrc

# 修改操作系统 limits
rm -rf /etc/security/limits.d/99-kds-limits.conf
ln -s `pwd`/kds-limits.conf /etc/security/limits.d/99-kds-limits.conf

# 设置系统配置文件 SysctlConf
chown root:root $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/etc/sysctl.conf
cp $INST_KDS_INSTALL_PATH/mobile-stock/auto-deploy/etc/sysctl.conf /etc/

# 为 kds 用户设置 sudo 权限
IF_KDS_SUDOER=`ls /etc/sudoers.d/ | grep "kds"`
if [ -n "$IF_KDS_SUDOER" ]; then
echo "kds 用户已获取 sudo 权限"
else
echo "正在为 kds 用户获取 sudo 权限"
echo "kds ALL=(ALL) ALL" > /etc/sudoers.d/kds
fi

# 建立备份目录
[[ -d /opt/.backup ]] || mkdir /opt/.backup
chown -R kds:kds /opt/.backup
}

# 部署移动证券
SetupMobileStock() {
echo "部署移动证券"
if [ $MODEL = Install ]; then
SetupSystem
else
echo "无需重新配置系统参数!"
fi

# 设置 freetds
SetupFreetdsConf

# 设置 nginx
SetupNginxConf

# 设置 auth-service
SetupAuthServiceConf

# 部署 auth-agent
SetupAuthAgentConf

# 设置 system-service
SetupSystemServiceConf

# 设置 Redis
SetupRedisConf

# 设置 TradeService
SetupTradeServiceConf

# 设置 QuoteComm
SetupQuoteCommConf

# 部署 redis-pool
SetupRedisPoolConf

# 部署 kds-agent
SetupKDSAgentConf

# 部署 web-admin
SetupWebAdminConf
}

Checkpackage() {
PACKAGE_NAME=`echo $INST_KDS_PACKAGE_NAME | cut -d"-" -f 8 | cut -d"." -f 1`
local CONF_MODEL=
echo "$PACKAGE_NAME"
if [ $PACKAGE_NAME = all ]; then
KDS_USER=`id kds`
if [ -z "$KDS_USER" ]; then
PACK_MODEL=Install
else
PACK_MODEL=Upgrade_Deploy
fi
elif [ $PACKAGE_NAME = special ]; then
PACK_MODEL=Upgrade_Special
elif [ $PACKAGE_NAME = normal ]; then
PACK_MODEL=Upgrade_Normal
else
echo "包名获取异常"
fi


if [[ $MANUAL_MODE = Install ]]; then
CONF_MODEL=Install
elif [[ $MANUAL_MODE = Upgrade_Deploy ]]; then
CONF_MODEL=Upgrade_Deploy
elif [[ $MANUAL_MODE = Deploy ]]; then
CONF_MODEL=Deploy
elif [[ $MANUAL_MODE = Uninstall ]]; then
CONF_MODEL=Uninstall
elif [[ -z "$MANUAL_MODE" ]]; then
echo "默认使用自动化模式配置"
else
echo "模式异常"
exit 1
fi

if [[ $CONF_MODEL = $PACK_MODEL ]]; then
MODEL=$PACK_MODEL
else
if [[ -n "$CONF_MODEL" ]]; then
MODEL=$CONF_MODEL
else
MODEL=$PACK_MODEL
fi
fi
echo CONF_MODEL: $CONF_MODEL, PACK_MODEL: $PACK_MODEL, MODLE, "$MODEL"
}

BackupFile()
{
[[ -f $out_list_file ]] || return
[[ -d $backup_dir ]] || mkdir -p $backup_dir
su - kds -c "mkdir -p $backup_dir/mobile-stock-${NOW_TIME}"

local s=
echo
echo ==== 备份etc以及bin目录 ====
for s in `cat "$out_list_file" | awk -F: ‘{print $2}‘ |awk -F\/ ‘{print $1}‘ |sort -u` "."
do
echo "--> $s "
su - kds -c "mkdir -p \"$backup_dir/mobile-stock-${NOW_TIME}/$s\""
if [[ -d "$old_mobile_stock_path/$s/bin" ]] ;then
su - kds -c "cp -r \"$old_mobile_stock_path/$s/bin\" \"$backup_dir/mobile-stock-${NOW_TIME}/$s\""
else
echo $old_mobile_stock_path/$s/bin 不存在.
fi
if [[ -d "$old_mobile_stock_path/$s/etc" ]] ;then
su - kds -c "cp -r \"$old_mobile_stock_path/$s/etc\" \"$backup_dir/mobile-stock-${NOW_TIME}/$s\""
else
echo $old_mobile_stock_path/$s/etc 不存在.
fi
done
echo 备份文件结束..
echo
}

fun_main_init()
{
rm -f .setup2.0_process_*

if ! grep ‘ulimit -c unlimited‘ $SYS_KDS_USER_HOME/.bashrc ;then
echo ‘ulimit -c unlimited‘ >> $SYS_KDS_USER_HOME/.bashrc
fi
}

fun_update_sh()
{
local update_list=
local ans=
echo 检查脚本是否有更新..
rm -rf setup
tar xzf $INST_KDS_PACKAGE_NAME setup
chmod +x setup/*
for s in setup2.0.sh module-upgrade2.0.sh
do
if ! diff $s setup/$s &> /dev/null; then
echo "---------------- $s -------------------"
./setup/$s -v
update_list="$update_list $s"
fi
done
echo "-----------------------------------"


[[ `echo $update_list` ]] && {
echo -n "脚本有更新, 请查看上面的更新信息, 是否进行更新[N|n 不进行更新;直接回车进行更新]: "
read -n 1 ans
if [[ `echo $ans |tr ‘a-z‘ ‘A-Z‘` != N ]]; then
for s in $update_list; do
echo 备份 $s $s.backup
\cp $s{,.backup}
cp setup/$s .
done

rm -rf setup
echo "更新完成, 请按方向键 上 再次执行此脚本!"
exit 0
fi
}

rm -rf setup
}

init()
{
[[ -d Upgrade_dir_bak ]] || mkdir -p Upgrade_dir_bak
mv $(ls *.log |grep -v $(basename $all_log)) Upgrade_dir_bak
}

main() {

if ! fun_need_root ;then
echo 必须以root用户进行登录后,再操作!
echo 而不是用其它普通用户登录再切换到root进行操作!
exit 1
fi

# 加载部署方案
LoadSetupConfig

[[ -f $INST_KDS_PACKAGE_NAME ]] || {
echo $INST_KDS_PACKAGE_NAME 文件并不存在,请重新检查或者修改kds.conf
exit 2
}

# fun_update_sh

# 杀掉所有kds的进程
killall -u kds
sleep 3
killall -9 -u kds

# 确定包模式
Checkpackage

# 做一些初始化处理
fun_main_init

case "$MODEL" in
#######################################################
################### 新服务器安装 ######################
Install )
echo " Let\`s Install !"
# 安装工具软件
LocalInstallTools
# 设置系统环境
SetupSystemEnv
# 安装移动证券
InstallMobileStock
# 部署移动证券
SetupMobileStock
echo "MOBILE-STOCK SYSTEM INSTALL COMPLETE!"
;;
#######################################################
################### 系统升级 ##########################
Upgrade_Special )
echo " Let\`s Upgrade !"
# 生成升级列表
process_file
# 备份关键数据
BackupFile
# 升级新文件
apply_modify
echo "SYSTEM UPGRADE COMPLETE!"
;;
#######################################################
################### 系统升级 ##########################
Upgrade_Normal )
echo " Let\`s Upgrade !"
# 生成升级列表
process_file
# 备份关键数据
BackupFile
# 升级新文件
apply_modify
echo "SYSTEM UPGRADE COMPLETE!"
;;
#######################################################
################### 系统升级 ##########################
Upgrade_Deploy )
echo " 尚未实现 !"

;;
#######################################################
################### 配置移动证券 ######################
Deploy )
echo " Let\`s Deploy !"
# 部署移动证券
SetupMobileStock
echo "SYSTEM DEPLOY COMPLETE!"
;;
#######################################################
################### 卸载移动证券 ######################
Uninstall )
DeleteUserKds
echo "移动证券后台已卸载完毕,请使用 ps 命令查看 kds 用户是否被彻底删除"
;;
#######################################################
################### 未选择模式 ########################
* )
echo "配置文件尚未选择安装模式,检查 kds.conf 配置!"
exit
;;
esac

rm -rf $CURRENT_PWD/mobile-stock
rm -f .setup2.0_process_*
}

init
main 2>&1 |tee $all_log

exit

 

记录学习linux下shell用法

标签:日志文件   stdin   alarm   mode   set   哪些   oba   pool   脚本   

原文地址:https://www.cnblogs.com/xiaofeng0223/p/11792910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!