#!/bin/bash

echo "<<=========== 下载工具[开始] ============>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "<<=========== 下载工具[开始] ============>>" >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log

initPath=`pwd`

mkdir /etc/yum.repos.d/backup
yum -y install wget 
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
if [ $? -eq 0 ] ; then 
  echo -e "yum源下载                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "yum源下载----------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "yum源下载                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "yum源下载----------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
yum clean all 
yum makecache 
if [ $? -eq 0 ] ; then 
  echo -e "yum缓存建立                    \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "yum缓存建立--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "yum缓存建立                    \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "yum缓存建立--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi

yum -y install ntpdate 
if [ $? -eq 0 ] ; then 
  echo -e "ntpdate安装                    \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "ntpdate安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "ntpdate安装                    \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "ntpdate安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi

yum -y install keepalived 
if [ $? -eq 0 ] ; then 
  echo -e "keepalived安装                 \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "keepalived安装-----------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "keepalived安装                 \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "keepalived安装-----------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
fi

yum -y install htop 
if [ $? -eq 0 ] ; then 
  echo -e "htop安装                       \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "htop安装-----------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "htop安装                       \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "htop安装-----------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
fi

yum -y install gcc-c++ make
gcc --version
if [ $? -eq 0 ] ; then 
  echo -e "gcc安装                        \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gcc安装------------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  mkdir /opt/redis
  cp ./rar/redis-4.0.11.tar.gz /opt/redis/
  cd /opt/redis/
  tar -zxvf redis-4.0.11.tar.gz
  cd redis-4.0.11/
  make MALLOC=libc
  cd src && make install
  mkdir /etc/redis
  cd ${initPath}
  cp ./doc/6379.conf /etc/redis/6379.conf
  cp ./doc/redisd /etc/init.d/redisd
  chmod +x /etc/init.d/redisd
  chkconfig --add redisd
  chkconfig --list redisd
  service redisd start
  if [ $? -eq 0 ] ; then 
    echo -e "redis安装                      \033[32m 完成 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "redis安装----------------------------[完成]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
  else 
    echo -e "redis安装                      \033[31m 失败 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "redis安装----------------------------[失败]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
    exit 1;
  fi
else 
  echo -e "gcc安装                        \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gcc安装------------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi

systemctl stop firewalld.service
systemctl disable firewalld.service 

setenforce 0
# sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux
sed -i 's/enforcing/disabled/g' /etc/selinux/config

echo "<<=========== 下载工具[完成] ============>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "<<=========== 下载工具[完成] ============>>" >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
