#!/bin/bash
#This is Open-Falcon install script
#Data is 2018/1/10
#Editer Zhanxing 
#本脚本使用Red 7和CentOS 7系统安装Open-Falcon
install_preparation()
{
yum install -y wget vim
yum install -y git  pip epel-release golang  redis 
python get-pip.py
pip install gunicorn
systemctl start redis
systemctl enable redis
systemctl stop firewalld
systemctl disable firewalld
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0 
}
install_mysql()
{
wget -P /tmp http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    if [ $? -eq 0 ]; then
        echo "mysql yum源下载成功"
    else
        echo "mysql yum源下载失败"
        exit
    fi
rpm -ivh /tmp/mysql-community-release-el7-5.noarch.rpm
yum install mysql-server -y
    if [ $? -eq 0 ]; then
        echo "mysql 安装成功"
    else
        echo "mysql 安装失败"
        exit
    fi
systemctl start mysql
systemctl enable mysql
echo "自行设置MySQL密码,方便记录"
read -p "please input mysql password:"pwd
mysqladmin -u root password "$pwd"
export GOROOT=/usr/lib/golang
export GOPATH=/home
mkdir -p $GOPATH/src/github.com/open-falcon
cd $GOPATH/src/github.com/open-falcon
git clone https://github.com/open-falcon/falcon-plus.git
    if [ $? -eq 0 ]; then
        echo "git源码成功"
    else
        echo "git源码失败"
        exit
    fi
cd $GOPATH/src/github.com/open-falcon/falcon-plus/scripts/mysql/db_schema/
#mysql -u root -p123.com  < /home/src/github.com/open-falcon/falcon-plus/scripts/mysql/db_schema/1_uic-db-schema.sql
mysql -h 127.0.0.1 -u root -p$pwd < 1_uic-db-schema.sql
mysql -h 127.0.0.1 -u root -p$pwd < 2_portal-db-schema.sql
mysql -h 127.0.0.1 -u root -p$pwd < 3_dashboard-db-schema.sql
mysql -h 127.0.0.1 -u root -p$pwd < 4_graph-db-schema.sql
mysql -h 127.0.0.1 -u root -p$pwd < 5_alarms-db-schema.sql
cd $GOPATH/src/github.com/open-falcon/falcon-plus/
go get github.com/open-falcon/rrdlite
make all
make pack
}
install_after()
{
export WORKSPACE= 
mkdir -p $WORKSPACE
cd $GOPATH/src/github.com/open-falcon/falcon-plus/
tar -xzvf *.tar.gz -C $WORKSPACE
sed -i 's/root:/root:$pwd/g' /home/work/aggregator/config/cfg.json
sed -i 's/root:/root:$pwd/g' /home/work/graph/config/cfg.json
sed -i 's/root:/root:$pwd/g' /home/work/hbs/config/cfg.json
sed -i 's/root:/root:$pwd/g' /home/work/nodata/config/cfg.json
sed -i 's/root:/root:$pwd/g' /home/work/api/config/cfg.json
sed -i 's/root:/root:$pwd/g' /home/work/alarm/config/cfg.json
cd $WORKSPACE
./open-falcon start
    if [ $? -eq 0 ]; then
        echo "open-falcon启动成功"
    else
        echo "open-falcon启动失败"
        exit
    fi
}
install_before()
{
export FRONTSPACE=/home/front/open-falcon
mkdir -p $FRONTSPACE
cd $FRONTSPACE
git clone https://github.com/open-falcon/dashboard.git
    if [ $? -eq 0 ]; then
        echo "git 前端代码成功"
    else
        echo "git 前端代码失败"
        exit
    fi
    
yum install -y python-virtualenv python-devel openldap-devel mysql-devel
yum groupinstall "Development tools" -y
cd $FRONTSPACE/dashboard/
virtualenv ./env
./env/bin/pip install -r pip_requirements.txt
bash control start
    if [ $? -eq 0 ]; then
        echo "dashboard启动成功"
    else
        echo "dashboard启动失败"
        exit
    fi
}
#检测网络
ping -c 1 -t 1 www.baidu.com >/dev/null 2>&1
if [[ \$? == 2 ]]; then
    echo "nameserver 114.114.114.114
nameserver 8.8.8.8" > /etc/resolv.conf
fi
ping -c 1 -t 1 www.baidu.com >/dev/null 2>&1
if [[ $? == 2 ]]; then
    echo "Network problems,Please check!"
    exit
fi
#更换阿里YUM源和fedora的epel仓库
yum install wget vim net-tools -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum install -y epel-release
#开始安装
echo "1 安装准备环境"
echo "2 安装MySQL"
echo "3 安装后端组件"
echo "4 安装前端组件"
echo "5 单机组合安装"
echo "输入您需要安装的组件编号[1-5]"
read aNum
case  $aNum in
    1) 
    install_preparation
    ;;
    2) 
    install_mysql
    ;;
    3) 
    install_after
    ;;
    4) 
    install_before
    ;;
    5) 
    install_preparation 
    install_mysql
    install_after
    install_before
    echo "安装完成,请通过http://IP:8081进行访问"
    ;;
esac