腾讯TARS框架安装脚本

腾讯TARS框架安装脚本

  • 腾讯TARS框架安装脚本
    • 依赖环境的安装
    • JDK的安装
    • TARS框架编译
    • TARS数据库初始化
    • TARS核心服务安装
    • TarsWeb管理平台安装
    • TARS框架安装总控脚本

腾讯TARS框架安装脚本

本文主要记录腾讯TARS框架安装的脚本,当前时间为2019-03-27,如果腾讯的TARS框架的源码做了结构上或者大的改动,脚本可能需要适当修改。

依赖环境的安装

software_install.sh

#!/bin/bash

echo "========DEPENDENCE SOFTWARE INSTALL START=========="

# 安装软件源,为了后续软件的成功安装,特别是mysql-server、mysql-devel
yum -y install https://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm
yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 安装管理repository及扩展包的工具
yum -y install yum-utils
# 安装依赖的软件
yum -y install gcc gcc-c++
yum -y install git
yum -y install cmake
yum -y install flex
yum -y install bison
yum -y install glibc-devel
yum -y install ncurses-devel
yum -y install zlib-devel
yum -y install mysql-server mysql-devel
# expect不是安装tars必要的工具,本地mysql初始化时需要
yum -y install expect

# 清除已安装的rpm
yum clean all && rm -rf /var/cache/yum

echo "========DEPENDENCE SOFTWARE INSTALL END=========="

JDK的安装

运行tars-java需要依赖JDK,此处我已经将一个固定版本的JDK放在了git上
jdk_install.sh

#!/bin/bash

echo "========START INSTALL JDK=========="

# 获取传入的GIT账户用户名密码
GitUser=$1
GitPass=$2

echo "删除原有的JDK环境变量和包"

# 删除原有的JDK环境变量
sed -i '/JAVA_HOME/d' /etc/profile
# 删除原有的JDK安装包
rm -rf /usr/JDK

echo "从GIT下载安装JDK,并设置环境变量"

# 从GIT下载JDK
cd /usr
git clone https://${GitUser}:${GitPass}@git.xxxx.xxx.xxx.git
cd JDK
tar zxvf jdk-8u191-linux-x64.tar.gz
rm -rf jdk-8u191-linux-x64.tar.gz

# 设置JDK环境变量
echo "export JAVA_HOME=/usr/JDK/jdk1.8.0_191" >> /etc/profile
echo "export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile

# 刷新环境变量
source /etc/profile

echo "========END INSTALL JDK=========="

TARS框架编译

framework_install.sh

#!/bin/bash

echo "========FRAMEWORK INSTALL START=========="

echo "固定mysql的头文件和开发库"

# 连接mysql的头文件和库到固定的目录,用于tars编译
mkdir -p /usr/local/mysql
ln -s /usr/lib64/mysql /usr/local/mysql/lib
ln -s /usr/include/mysql /usr/local/mysql/include
# 确保mysql的动态库能够被系统加载
echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf && ldconfig

echo "TarsFramework源码下载"

# TarsFramework源码下载
cd /root
git clone https://github.com/TarsCloud/TarsFramework.git

echo "TarsFramework编译"

# 编译TarsFramework
cd TarsFramework/build
chmod u+x build.sh
./build.sh prepare
./build.sh all

echo "TarsFramework安装"

# 安装TarsFramework(C++开发环境也在上面)
mkdir -p /usr/local/tars
./build.sh install

echo "Tars基础服务打包"

# 框架核心基础服务打包
make framework-tar

# 框架普通基础服务打包
make tarsstat-tar
make tarsnotify-tar
make tarsproperty-tar
make tarslog-tar
make tarsquerystat-tar
make tarsqueryproperty-tar

echo "========FRAMEWORK INSTALL END=========="

TARS数据库初始化

setup_database.sh

本地MySQL版

# 数据库初始化必要的参数
DBIP=$1
DBPort=$2
DBUser=$3
DBPassword=$4
DBTarsPass=$5

MachineIp=$(ip addr | grep inet | grep eth0 | awk '{print $2;}' | sed 's|/.*$||')
MachineName=$(hostname)

echo "启动mysql服务,初始化root用户密码,授权root@%用户"

# 启动mysql服务
systemctl enable mysqld
systemctl start mysqld

# 初始化root用户密码初始化
/usr/bin/expect << EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    "password:" { send "$DBPassword\r"; exp_continue}
    "new password:" { send "$DBPassword\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF

# mysql数据库添加"root@%"用户,并授权,否则在腾讯云上连接本地mysql数据库的时候指定-h为非localhost都无法连接
mysql -hlocalhost -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant all on *.* to 'root'@'%' identified by '${DBPassword}' with grant option;"
mysql -hlocalhost -P${DBPort} -u${DBUser} -p${DBPassword} -e "flush privileges;"

echo "mysql数据库授权tars相关用户"

# mysql数据库添加tars相关用户,并授权
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant all on *.* to 'tars'@'%' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant all on *.* to 'tars'@'localhost' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant all on *.* to 'tars'@'${MachineName}' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant all on *.* to 'tars'@'${MachineIp}' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "flush privileges;"

echo "替换数据库操作文件的必要参数"

# 数据库操作文件修改
cd /root/TarsFramework/sql
sed -i "s/192.168.2.131/${MachineIp}/g" `grep 192.168.2.131 -rl ./*`
sed -i "s/db.tars.com/${DBIP}/g" `grep db.tars.com -rl ./*`
sed -i "s/tars2015/${DBTarsPass}/g" `grep tars2015 -rl ./*`
sed -i "s/3306/${DBPort}/g" `grep 3306 -rl ./*`

sed -i "s/proot@appinside/h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} /g" `grep proot@appinside -rl ./exec-sql.sh`

echo "初始化数据库"

# 执行数据库初始化语句
chmod u+x exec-sql.sh
./exec-sql.sh

echo "========DATABASE INIT END=========="

腾讯云MySQL版

#!/bin/bash

echo "========DATABASE INIT START=========="

# 数据库初始化必要的参数
DBIP=$1
DBPort=$2
DBUser=$3
DBPassword=$4
DBTarsPass=$5

MachineIp=$(ip addr | grep inet | grep eth0 | awk '{print $2;}' | sed 's|/.*$||')
MachineName=$(hostname)

echo "mysql数据库授权tars相关用户"

# 使用云数据库,必须保证数据库可以远程连接,腾讯的mysql云数据库,不能赋予所有权限,需要注意
# mysql数据库添加tars相关用户,并授权
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,PROCESS,REFERENCES,INDEX,ALTER,SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES,EXECUTE,CREATE VIEW,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,EVENT,TRIGGER on *.* to 'tars'@'%' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,PROCESS,REFERENCES,INDEX,ALTER,SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES,EXECUTE,CREATE VIEW,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,EVENT,TRIGGER on *.* to 'tars'@'localhost' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,PROCESS,REFERENCES,INDEX,ALTER,SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES,EXECUTE,CREATE VIEW,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,EVENT,TRIGGER on *.* to 'tars'@'${MachineName}' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,PROCESS,REFERENCES,INDEX,ALTER,SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES,EXECUTE,CREATE VIEW,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,EVENT,TRIGGER on *.* to 'tars'@'${MachineIp}' identified by '${DBTarsPass}' with grant option;"
mysql -h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} -e "flush privileges;"

echo "替换数据库操作文件的必要参数"

# 数据库操作文件修改
cd /root/TarsFramework/sql
sed -i "s/192.168.2.131/${MachineIp}/g" `grep 192.168.2.131 -rl ./*`
sed -i "s/db.tars.com/${DBIP}/g" `grep db.tars.com -rl ./*`
sed -i "s/tars2015/${DBTarsPass}/g" `grep tars2015 -rl ./*`
sed -i "s/3306/${DBPort}/g" `grep 3306 -rl ./*`

sed -i "s/proot@appinside/h${DBIP} -P${DBPort} -u${DBUser} -p${DBPassword} /g" `grep proot@appinside -rl ./exec-sql.sh`

echo "初始化数据库"

# 执行数据库初始化语句
chmod u+x exec-sql.sh
./exec-sql.sh

echo "========DATABASE INIT END=========="

TARS核心服务安装

tars_service_install.sh

#!/bin/bash

echo "========TARS BASIC SERVICE INSTALL START=========="

# 脚本必要的几个参数
DBIP=$1
DBPort=$2
DBUser=$3
DBPassword=$4
DBTarsPass=$5

MachineIp=$(ip addr | grep inet | grep eth0 | awk '{print $2;}' | sed 's|/.*$||')

echo "解压核心基础服务包"

# 创建服务安装目录,拷贝核心基础服务包,并解压
mkdir -p /usr/local/app/tars
cp -f /root/TarsFramework/build/framework.tgz /usr/local/app/tars/
cd /usr/local/app/tars
tar xzfv framework.tgz
rm -rf framework.tgz

echo "修改数据库操作必要参数"

# 修改config文件以及其他文件中,相关的数据库以及主控地址等参数
sed -i "s/dbhost.*=.*192.168.2.131/dbhost=${DBIP}/g" `grep dbhost -rl ./*`
sed -i "s/192.168.2.131/${MachineIp}/g" `grep 192.168.2.131 -rl ./*`
sed -i "s/db.tars.com/${DBIP}/g" `grep db.tars.com -rl ./*`
sed -i "s/dbport.*=.*3306/dbport=${DBPort}/g" `grep dbport -rl ./*`
sed -i "s/registry.tars.com/${MachineIp}/g" `grep registry.tars.com -rl ./*`
sed -i "s/web.tars.com/${MachineIp}/g" `grep web.tars.com -rl ./*`
sed -i "s/tars2015/${DBTarsPass}/g" `grep tars2015 -rl ./*`

echo "启动Tars框架核心基础服务"

# 启动tars框架服务
chmod u+x tars_install.sh
./tars_install.sh
tarspatch/util/init.sh

# crontab里面配置tarsnodede的监控,避免不小心挂了以后会启动
echo "* * * * * /usr/local/app/tars/tarsnode/util/monitor.sh" >> /etc/crontab

echo "========TARS BASIC SERVICE INSTALL END=========="

TarsWeb管理平台安装

web_mgr_install.sh

#!/bin/bash

echo "========START INSTALL WEB MANAGEMENT=========="

DBIP=$1
DBPort=$2
DBTarsPass=$3

MachineIp=$(ip addr | grep inet | grep eth0 | awk '{print $2;}' | sed 's|/.*$||')

# web管理系统开发环境安装
cd /root
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
source ~/.bashrc
nvm install v8.11.3
npm install -g pm2 --registry=https://registry.npm.taobao.org

# TarsWeb源码下载
git clone https://github.com/TarsCloud/TarsWeb.git

# 修改TarsWeb配置文件中的必要参数
cd TarsWeb
sed -i "s/db.tars.com/${DBIP}/g" config/webConf.js
sed -i "s/registry.tars.com/${MachineIp}/g" config/tars.conf
sed -i "s/3306/${DBPort}/g" config/webConf.js
sed -i "s/tars2015/${DBTarsPass}/g" config/webConf.js

# 安装web管理页面依赖,启动web
npm install --registry=https://registry.npm.taobao.org
npm run prd

# 创建日志目录(这一步暂时没用。TarsWeb的日志还是在TarWeb源码目录的log目录下)
mkdir -p /data/log/tars

echo "========END INSTALL WEB MANAGEMENT=========="

TARS框架安装总控脚本

tars_install.sh

#!/bin/bash

# 数据库地址(理论上来说使用127.0.0.1也行,建议使用内网地址)
DBIP="xxx.xxx.xxx.xxx"
# 数据库端口
DBPort=3306
# 数据库用户名
DBUser=root
# 数据库用户的密码
DBPassword="root"
# tars相关数据库用户的密码
DBTarsPass="T@rs2015"

# GIT账户的用户密码
GitUser="xxx"
GitPass="xxx"

# 授权其余需要执行的脚本
chmod u+x jdk_install.sh
chmod u+x software_install.sh
chmod u+x framework_install.sh
chmod u+x setup_database.sh
chmod u+x tars_service_install.sh
chmod u+x web_mgr_install.sh

# 执行脚本安装Tars框架
# 安装依赖的第三方工具
sh ./software_install.sh
# 安装JDK
sh ./jdk_install.sh ${GitUser} ${GitPass}
# 安装c++ 开发环境
sh ./framework_install.sh
# 数据库环境初始化
sh ./setup_database.sh ${DBIP} ${DBPort} ${DBUser} ${DBPassword} ${DBTarsPass}
# 框架基础服务安装
sh ./tars_service_install.sh ${DBIP} ${DBPort} ${DBUser} ${DBPassword} ${DBTarsPass}
# WEB管理系统安装
sh ./web_mgr_install.sh ${DBIP} ${DBPort} ${DBTarsPass}

你可能感兴趣的:(腾讯TARS)