#!/bin/bash
echo "安装新增微服务应用服务器(全部环境)"
echo "请确认已设置主机名和挂载磁盘，本次不做检查"

#安装JAVA
        echo "准备安装JAVA，请稍候..."
        sleep 1
        curl -sO http://down.yadeaiot.net:8000/install/java.sh && sh java.sh;
#安装prometheus客户端
        echo "准备安装prometheus-client,请稍候..."
        sleep 1
        curl -sO http://down.yadeaiot.net:8000/install/prometheus-client.sh && sh prometheus-client.sh;
#服务器建立目录
        echo "server服务器建立目录,修改host，请稍候..."
        sleep 1
        curl -sO http://down.yadeaiot.net:8000/install/serverfile.sh && sh serverfile.sh;
#加入堡垒机
        echo "准备将本主机加入堡垒机,请稍候..."
        sleep 1
        curl -sO http://down.yadeaiot.net:8000/install/jumpserver.sh && sh jumpserver.sh;
#安装filebeat
#        echo "准备安装filebeat,请稍候..."
#        sleep 1
#        curl -sO http://down.yadeaiot.net:8000/install/filebeat.sh && sh filebeat.sh;

#配置启动脚本
echo "接下来将进行配置，请按照问题输入正确答案，按任意键开始..."
read -n1
echo "请输入需要部署的java应用服务名称(全称)，如soa-blog、soa-vehicle-base"
read server_1
echo "你输入的服务是：$server_1"
echo "请确认是否执行此操作？（Y/N）"
read confirm

if [ "$confirm" == "Y" ] || [ "$confirm" == "y" ]
then
    echo "已确认"
else
    echo "请重新输入需要部署的java应用服务名称(全称)，如soa-blog、soa-vehicle-base"
    read server_1
    echo "你输入的服务是：$server_1"
fi
echo "请输入这是第几个java应用服务，如3(根据已部署服务递增)"
read nu_1
echo "你输入的序号是：$nu_1"
echo "请确认是否执行此操作？（Y/N）"
read confirm

if [ "$confirm" == "Y" ] || [ "$confirm" == "y" ]
then
    echo "已确认执行此操作。"
else
    echo "请输入这是第几个java应用服务，如3(根据已部署服务递增)"
    read nu_1
    echo "你输入的序号是：$nu_1"
fi
echo "请输入这个服务的端口号，如：8088"
read port_1
echo "你输入的端口号是：$port_1"
echo "请确认是否执行此操作？（Y/N）"
read confirm

if [ "$confirm" == "Y" ] || [ "$confirm" == "y" ]
then
    echo "已确认执行此操作。"
else
    echo "请输入这个服务的端口号，如：8088"
    read port_1
    echo "你输入的序号是：$port_1"
fi
#判断是socket服务，需要单独处理
if [ "$server_1" == "portal-socket" ]
then
	mkdir -p /opt/settings
	echo "server.id=$nu_1" >>/opt/settings/tspServer.properties
else 
	echo "socket"
fi
#判断是collect服务，需要单独处理
if [ "$server_1" == "soa-collect" ]
then
        mkdir -p /opt/settings
	cd /opt/settings
	wget http://down.yadeaiot.net:8000/serverfile/ip_china.db
else
        echo "collect"
fi



#下载部署脚本
mkdir -p /opt/TSP
mkdir -p /opt/TSP/restart
cd /opt/TSP
wget http://down.yadeaiot.net:8000/serverfile/TSP/$server_1.sh
wget http://down.yadeaiot.net:8000/serverfile/restart/$server_1.sh
sed -i "s/$server_1-1/$server_1-$nu_1/g" /opt/TSP/$server_1.sh
sed -i "s/$server_1-1/$server_1-$nu_1/g" /opt/TSP/restart/$server_1.sh
#配置jenkins并下载jar包到服务器
echo 'ssh-rsa [payload removed: 372 characters] root@tsp-prod-jenkins' >>/root/.ssh/authorized_keys
echo "配置jenkins并下载jar包到服务器"

echo
echo  -e "\033[5;4;45;30m  注意！jenkins的发版项目: $server_1 需要手动去修改 \033[0m"
hostname1=`hostname`
ip1=`ip addr |grep eth0 | grep inet | awk -F " " '{print $2}'|awk -F "/" '{print $1}'`
echo "请确认本机主机名是：$hostname1"
echo "请确认本机IP是：$ip1"
echo "请确认上述信息是否正确？（Y/N）"
read confirm

if [ "$confirm" == "Y" ] || [ "$confirm" == "y" ]
then
    echo "已确认执行此操作。"
else
    echo "请输入主机名称"
    read hostname1
    echo "请输入IP地址"
    read ip1
fi
echo "请移步jenkins服务器执行下列命令，请复制"
echo "echo $ip1 $hostname1 >>/etc/hosts"
echo "sed -i '/# merge/i\\${hostname1} ansible_user=root' /etc/ansible/hosts"
#echo "echo $hostname1 ansible_user=root >>/etc/ansible/hosts"
echo "scp /data/jenkins/workspace/aliyun-tsp-prod-$server_1/$server_1/target/*.jar $hostname1:/data/TSP/$server_1-v0.0.1.jar.1"
echo 
echo
echo "请确认已经在jenkins执行了上述操作：设置host、设置ansbile、传送jar包，前提是jenkins可以连接到本机"
echo "请确认上述操作，按任意键继续..."
read -n1
#判断java文件是否存在，并启动java服务
echo "判断java文件是否存在，并启动java服务"
ecoo "请手动启动，这里不做启动"
#以下注释
:<<EOF
echo "接下来会花费一些时间，请耐心，对于java启动的错误可以忽略"
file="/data/TSP/$server_1-v0.0.1.jar.1"
if [ -f "$file" ]
then 
	echo "jar文件传输完成，下面开始启动jar应用"
	/bin/sh /opt/TSP/$server_1.sh
	sleep 40
	#启动健康检查和监控
	/bin/sh /opt/TSP/health_check.sh $port_1 >/tmp/$server_1.log
	result=`sed -n "1, 1p" /tmp/$server_1.log | awk '{print $3}'`
	echo $result
	if [ "$result" = "FAILED" ]; then
  		echo  -e "\033[5;4;45;30m  启动失败了，请查看 \033[0m"
	else
  		echo -e "\033[36m  启动成功\033[0m"
	fi
	#开启监控
	/bin/sh /data/monitor/status_control.sh open $server_1 $port_1
	
else
	echo "jar文件没有传输完成，请确认,暂时不部署java服务，继续部署别的服务了"
fi

#配置filebeat
echo "配置filebeat"
cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml-$(date +%F_%T)
sed -i "s/portal-socket/$server_1/g" /etc/filebeat/filebeat.yml
sed -i "s/socket004/$hostname1/g" /etc/filebeat/filebeat.yml
systemctl restart filebeat


EOF
#注释结束


#添加文件服务器客户端
curl https://dl.minio.org.cn/client/mc/release/linux-amd64/mc \
  --create-dirs \
  -o /usr/local/minio-binaries/mc

chmod +x /usr/local/minio-binaries/mc
echo 'export PATH=$PATH:/usr/local/minio-binaries/' >>/etc/profile
source /etc/profile

mc config host add alarm http://10.20.10.118:9000 KTVhxGr2Joz3fFEb t9Djv07irkIVJOPcQwphVcTAjHtNtn4N

#添加SLS
echo "添加SLS，请移步阿里云添加机器组"
wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/linux64/logtail.sh -O logtail.sh; chmod 755 logtail.sh
./logtail.sh install auto

echo "sls别忘记去阿里云增加"



#修改prometheus监控JVM和主机
echo  -e "\033[5;4;45;30m  注意！prometheus需要手动修改 \033[0m"
echo "修改prometheus监控JVM和主机"
echo "请移步promethues服务器修改配置，执行如下命令" 
echo "echo $ip1 $hostname1 >>/etc/hosts 执行这个命令添加hosts"
echo "修改/usr/local/prometheus/prometheus.yml中的9100这个是主机的监控"
echo "修改/usr/local/prometheus/prometheus.yml中的服务jvm这个是java的监控"
echo "systemctl restart prometheus执行这个命令重启prometheus"

#修改jumpserver
echo  -e "\033[5;4;45;30m  注意！jumoserver需要手动修改 \033[0m"
echo "在资产列表-创建资产里加上新增的节点"

#安装阿里云ossutil
        echo "准备安装阿里云ossutil,请稍候..."
        sleep 1
        curl -sO http://down.yadeaiot.net:8000/install/ossutil64.sh && sh ossutil64.sh;
#修改crontab定时计划
(crontab -l 2>/dev/null; echo '1 0 * * * /bin/bash /data/monitor/arch_log.sh "/data/TSP_LOG/'${server_1}'"') | crontab -
echo
(crontab -l 2>/dev/null; echo "0 3 * * * /bin/bash /data/monitor/oss_log.sh &") | crontab -
echo
(crontab -l 2>/dev/null; echo "30 0 * * * systemctl restart filebeat") | crontab -
echo
(crontab -l 2>/dev/null; echo '0 0 * * * for i in `ls /data/logs/*.log`; do echo "''" > $i ;done') | crontab -
echo
(crontab -l 2>/dev/null; echo '*/1 * * * * sh /data/monitor/err_log.sh "'$server_1'" "13812197122"') | crontab -
echo
#安装lrzsz
yum -y install lrzsz
#设置开机启动
echo "设置开机启动"
#start_1=`cat /opt/TSP/$server_1.sh |grep -m1 nohup`
start_1=`/bin/bash /opt/TSP/restart/$server_1.sh`
echo $start_1 >>/etc/rc.local
chmod +x /etc/rc.local

echo "完成"

echo "请手动启动java和配置jenkins\prometheus,修改oss配置"
exit 0















