2018-04-28 EPC安装脚本

#!/bin/bash

 set -e

 

 BASE_DIR=$(cd "$(dirname "$0")"; pwd); cd ${BASE_DIR}

 SCRIPT_DIR=$BASE_DIR/scripts

 RPM_DIR=$BASE_DIR/files

 TMP_DIR=$BASE_DIR/tmp

 SLEEP_SEC=10

 

 ######### Variable ##########

 CURRENT_USER="$(id -un 2>/dev/null || true)"

 BASH_C="bash -c"

 

 ######### Constant ##########

 SUPPORT_DISTRO=(centos)

 CENTOS_VER=(7.0 7.2)

 

 # Error Message

 ERR_ROOT_PRIVILEGE_REQUIRED=(10 "This install script need root privilege, please retry use 'sudo' or root user!")

 ERR_NOT_SUPPORT_PLATFORM=(20 "Sorry, EPC only support x86_64 platform!")

 ERR_NOT_SUPPORT_DISTRO=(21 "Sorry, EPC only support centos now!")

 ERR_NOT_SUPPORT_DISTRO_VERSION=(22 "Sorry, EPC only support centos(>=7.0) now!")

 ERR_FETCH_INST_PKG_FAILED=(60 "Fetch install package failed, please retry!")

 ERR_UNTAR_PKG_FAILED=(62 "Untar install package failed!")

 ERR_EXEC_INSTALL_FAILED=(70 "Install EPC failed!")

 ERR_INSTALL_SERVICE_FAILED=(71 "Install service failed!")

 ERR_UNKNOWN_MSG_TYPE=98

 ERR_UNKNOWN=99

 

 # Color Constant

 RED=`tput setaf 1 || true`

 GREEN=`tput setaf 2 || true`

 YELLOW=`tput setaf 3 || true`

 BLUE=`tput setaf 4 || true`

 WHITE=`tput setaf 7 || true`

 LIGHT=`tput bold || true`

 RESET=`tput sgr0 || true`

 

 ######### Function Definition ##########

 i_base() {

   log info "Welcome to Install Baicells EPC...\n"

   check_system

   configure_system

   install_common_deps

 }

 

 check_system() {

   log info "1. check user"

   check_user

   log done "check user...done"

 

   log info "2. check os platform"

   check_os_platform

   log done "check os platform...done"

 

   #log info "3. check os distro"

   #check_os_distro

   #log done "check os distro...done"

 }

 

 check_user() {

   if [[ "${CURRENT_USER}" != "root" ]];then

     if (command_exist sudo);then

       BASH_C="sudo -E bash -c"

     else

       log error "${ERR_ROOT_PRIVILEGE_REQUIRED[1]}" && exit ${ERR_ROOT_PRIVILEGE_REQUIRED[0]}

     fi

     log info "${WHITE}Hint: EPC installer need root privilege\n"

     ${BASH_C} "echo -n"

   fi

 }

 

 check_os_platform() {

   ARCH="$(uname -m)"

   if [[ "${ARCH}" != "x86_64" ]];then

     log error "${ERR_NOT_SUPPORT_PLATFORM[1]}" && exit ${ERR_NOT_SUPPORT_PLATFORM[0]}

   fi

 }

 

 check_os_distro() {

   #set -x

   LSB_DISTRO=""; LSB_VER=""; LSB_CODE=""

   if (command_exist lsb_release);then

     LSB_DISTRO="$(lsb_release -si)"

     LSB_VER="$(lsb_release -sr)"

     LSB_CODE="$(lsb_release -sc)"

   fi

 

   if [[ -z "${LSB_DISTRO}" ]];then

     if [[ -r /etc/lsb-release ]];then

       LSB_DISTRO="$(. /etc/lsb-release && echo "${DISTRIB_ID}")"

       LSB_VER="$(. /etc/lsb-release && echo "${DISTRIB_RELEASE}")"

       LSB_CODE="$(. /etc/lsb-release && echo "${DISTRIB_CODENAME}")"

     elif [[ -r /etc/fedora-release ]];then

       LSB_DISTRO="fedora"

     elif [[ -r /etc/debian_version ]];then

       LSB_DISTRO="Debian"

       LSB_VER="$(cat /etc/debian_version)"

     elif [[ -r /etc/centos-release ]];then

       LSB_DISTRO="CentOS"

       LSB_VER="$(cat /etc/centos-release | cut -d' ' -f4)"

     elif [[ -r /etc/os-release ]];then

       LSB_DISTRO="$(. /etc/os-release && echo "$ID")"

       LSB_VER="$(. /etc/os-release && echo "$VERSION_ID")"

     fi

   fi

 

   LSB_DISTRO=$(echo "${LSB_DISTRO}" | tr '[:upper:]' '[:lower:]')

   case "${LSB_DISTRO}" in

     centos)

       CVERSION=$( echo ${LSB_VER} | cut -d"." -f-2 )

       if [[  "${LSB_DISTRO}" == "centos" ]]

       then SUPPORT_VER_LIST="${CENTOS_VER[@]}";

       else SUPPORT_VER_LIST="${FEDORA_VER[@]}";

       fi

       if (echo "${SUPPORT_VER_LIST}" | grep -qvw "${CVERSION}");then

         log error "Baicells EPC support ${LSB_DISTRO}( ${SUPPORT_VER_LIST} ), but current is ${LSB_VER}"

         exit ${ERR_NOT_SUPPORT_DISTRO_VERSION[0]}

       fi

     ;;

     *) if [[ ! -z ${LSB_DISTRO} ]];then echo -e -n "\nCurrent OS is '${LSB_DISTRO} ${LSB_VER}(${LSB_CODE})'";

        else echo -e -n "\nCan not detect OS type"; fi

       log error "${ERR_NOT_SUPPORT_DISTRO[1]}"

       exit ${ERR_NOT_SUPPORT_DISTRO[0]}

     ;;

   esac

 }

 

 configure_system() {

     log info "4. configure system"

     log info "disable NetworkManager"

     # NetworkManager

     systemctl stop NetworkManager && systemctl disable NetworkManager || true

     log info "disable firewalld"

     # firewall

     systemctl stop firewalld && systemctl disable firewalld || true

     log info "disable selinux"

     # selinux

     setenforce 0 || true

     sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

     log info "enable ip forward"

     # disable libvirtd

     systemctl stop libvirtd 2>/dev/null && systemctl disable libvirtd 2>/dev/null || true

     ip link delete virbr0-nic 2>/dev/null || true

     ip link delete virbr0 2>/dev/null || true

     # ip forward

     echo 1 > /proc/sys/net/ipv4/ip_forward

     log done "configure system...done"

 }

 

 yum_install() {

     for pkg in $@; do

         rpm -q $pkg > /dev/null || yum install -y -q $pkg || true

     done

 }

 

 log_start() {

   log info "install $1"

 }

 

 log_end() {

   log done "install $1...done"

 }

 

 clean() {

   rm -rf $TMP_DIR

 }

 

 start_service() {

   systemctl enable $1 && systemctl start $1 && systemctl status $1 || true

 }

 

 command_exist() {

   type "$@" > /dev/null 2>&1

 }

 

 log() {

   case "$1" in

     debug)     echo -e "[${BLUE}DEBUG${RESET}] : $2\n";;

     info)      echo -e -n "${WHITE}$2${RESET}\n" ;;

     warn)      echo -e    "[${YELLOW}WARN${RESET}] : $2\n" ;;

     done|success)  echo -e "${LIGHT}${GREEN}$2${RESET}\n\n" ;;

     error|failure) echo -e "[${RED}ERROR${RESET}] : $2\n" ;;

   esac

 }

 

 install_common_deps() {

   log info "5. install common deps"

   yum_install epel-release

   yum_install gcc make automake autoconf libtool perl iptraf

   yum_install dmidecode libxml2 openssl openssl-devel

   yum_install iftop iperf htop vnc-server

   yum_install wireshark wireshark-gnome

   #  curl https://bootstrap.pypa.io/get-pip.py|python && pip install pyaml netaddr || true

   yum_install cronie

   log done "install common deps...done"

 }

  

 gc() {

   # generate configuration

   rpm -q PyYAML > /dev/null || yum install -y -q python-yaml || true

   yum_install python-netaddr

   python ./gen_cfg.py

   #sleep 1

   MME_PORT=$(cat $TMP_DIR/meta | grep "MME_PORT" | awk -F: '{print $2}')

   MME_IP=$(cat $TMP_DIR/meta | grep "MME_IP" | awk -F: '{print $2}')

   OMC_IP=$(cat $TMP_DIR/meta | grep "OMC_IP" | awk -F: '{print $2}')

   S1U_IP=$(cat $TMP_DIR/meta | grep "S1U_IP" | awk -F: '{print $2}')

   SGI_IP=$(cat $TMP_DIR/meta | grep "SGI_IP" | awk -F: '{print $2}')

   PLMN_ID=$(cat $TMP_DIR/meta | grep "PLMN_ID" | awk -F: '{print $2}')

   TACC=$(cat $TMP_DIR/meta | grep "TAC" | awk -F: '{print $2}')

   HA_ENABLED=$(cat $TMP_DIR/meta | grep "HA_ENABLED" | awk -F: '{print $2}')

   APN=$(cat $TMP_DIR/meta | grep "APN" | awk -F: '{print $2}')

 }

 

 i_epc() {

   log_start epc

   source $SCRIPT_DIR/install_epc.sh || true

 

   # configure epc

   sed -i "s#IFNAME=.*#IFNAME=$MME_PORT#g" /usr/local/epc/nwconfig.sh

   mysql -DMME -e"update MME_CONFIG set IP_ADDR='$MME_IP';"

   mysql -DMME -e"update SDN_CONFIG set SDN_CTRL_ADDR='$MME_IP';"

   mysql -DMME -e"update SDN_CONFIG set SDN_S1U_SGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_CONFIG set SDN_S5U_SGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_CONFIG set SDN_S5U_PGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_CONFIG set SDN_SGI_ADDR='$SGI_IP';"

   mysql -DMME -e"update MME_SUPPORTED_TAI_LIST set TAC='$TACC' where RID=1;"

   mysql -DMME -e"update SDN_TAC_GWUADDR_CONFIG set SDN_S1U_SGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_TAC_GWUADDR_CONFIG set SDN_S5U_SGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_TAC_GWUADDR_CONFIG set SDN_S5U_PGW_ADDR='$S1U_IP';"

   mysql -DMME -e"update SDN_TAC_GWUADDR_CONFIG set SDN_SGI_ADDR='$SGI_IP';"

   mysql -DMME -e"update SDN_TAC_GWUADDR_CONFIG set TAC='$TACC' where RID=1;"

   mysql -DMME -e"update MME_FOR_TAI set PLMN_ID='$PLMN_ID';"

   mysql -DMME -e"update MME_FOR_TAI set TAC='$TACC';"

   mysql -DMME -e"update MME_FOR_TAI set IP_ADDR='$MME_IP';"

   mysql -DMME -e"update GENERAL_CONFIG set PLMNID='$PLMN_ID';"

   mysql -DMME -e"update REDIS_PROFILE set REDIS_SERVER_IP='127.0.0.1';"

   mysql -DHSS -e"update REDIS_PROFILE set REDIS_SERVER_IP='127.0.0.1';"

   mysql -DPCRF -e"update REDIS_PROFILE set REDIS_SERVER_IP='127.0.0.1';"

   mysql -DPCRF -e"update DBP_PEER set IPADDR='172.16.7.27';"

   mysql -DPCRF -e"update DBP_SERVER set IPADDR='172.16.7.29';"

   mysql -DPCRF -e"update INTERFACE_CONFIG set SC_DPI_IP='$MME_IP';"

   mysql -DPCRF -e"update INTERFACE_CONFIG set PCRF_DPI_IP='$MME_IP';"

 

   # apn config

   for al in $(echo $APN|tr ',' ' '); do

     name=$(echo $al|cut -d\| -f1)

     start_ipv4=$(echo $al|cut -d\| -f2)

     end_ipv4=$(echo $al|cut -d\| -f3)

 

     exist1=$(mysql -DMME -e"select *from APN_CONFIG where APN_NAME='$name';")

     if [[ x$exist1 == x ]]; then

       if [[ x$RID1 == x ]]; then

         RID1=$(mysql -DMME -e"select count(*) from APN_CONFIG"|grep -v "count")

       fi

       RID1=$(awk -v rid=$RID1 'BEGIN{print rid + 1}')

       mysql -DMME -e"insert into APN_CONFIG values ($RID1,'$name',0,'DISABLE','DYNAMIC','IPv4','192.168.9.7',1,25,'IPv4','114.114.114.114','IPv4','8.8.8.8',1,'1','/opt/tmp/','/opt/cdr/',60,10000,1800,20000,30000,'IPv4','172.16.7.28',3883,'IPv4','172.16.7.28',3883);"

     fi

     exist2=$(mysql -DMME -e"select *from PGW_APN_IP_POOL_CONFIG where APN_NAME='$name';")

     if [[ x$exist2 == x ]]; then

       if [[ x$RID2 == x ]]; then

         RID2=$(mysql -DMME -e"select count(*) from PGW_APN_IP_POOL_CONFIG "|grep -v "count")

       fi

       RID2=$(awk -v rid=$RID2 'BEGIN{print rid + 1}')

       mysql -DMME -e"insert into PGW_APN_IP_POOL_CONFIG values ($RID2,'$name','IPv4','$start_ipv4','$end_ipv4','48','2001:db8::ff0','2009:db8::d53:f');"

     else

       mysql -DMME -e"update PGW_APN_IP_POOL_CONFIG set UE_START_IPV4ADDR='$start_ipv4' where APN_NAME='$name';"

       mysql -DMME -e"update PGW_APN_IP_POOL_CONFIG set UE_END_IPV4ADDR='$end_ipv4' where APN_NAME='$name';"

     fi

   done

 

   # configure hssBossAPI

   sed -i "/HSS/s/.*/    \"HSS\":  {\"IP\": \"$MME_IP\", \"PORT\": \"12880\"},/" /usr/local/epc/BOSS_CFG

   # configure omBossAPI

   sed -i "s#<alarm_api>http://192.168.5.3#<alarm_api>http://$OMC_IP#g" /etc/nginx/cgibin/om_boss_interface_config.xml || true

 

   start_service epc

   start_service lmt

   log_end epc

 }

 

 i_tomcat() {

   log_start tomcat

   source $SCRIPT_DIR/install_tomcat.sh || true

 

   start_service tomcat

   log_end tomcat

 }

 

 i_signaltrace() {

   log_start signaltrace

   source $SCRIPT_DIR/install_signaltrace.sh || true

 

   sed -i "s#REMOTE_IP=.*#REMOTE_IP=$OMC_IP#g" /usr/local/traced/start.sh

   # configure sniffer

   sed -i "s#REMOTE_IP=.*#REMOTE_IP=$MME_IP#g" /usr/local/sniffer/start.sh

   sed -i "s#LOCAL_IP=.*#LOCAL_IP=$MME_IP#g" /usr/local/sniffer/start.sh

 

   start_service traced

   start_service sniffer

   log_end signaltrace

 }

 

 i_onos() {

   log_start onos

   source $SCRIPT_DIR/install_onos.sh || true

 

   # configure onos

   mv $TMP_DIR/config.xml /etc/ctl/config.xml || true

 

   start_service onos

   log_end onos

 }

 

 i_ovs() {

   log_start ovs

   source $SCRIPT_DIR/install_ovs.sh || true

 

   # configure ovs

   chmod +x $TMP_DIR/*.sh || true

   mv $TMP_DIR/ovs-init.sh /usr/local/bin/ovs-init.sh || true

   if [[ "${HA_ENABLED}" == "1" ]];then

     mv $TMP_DIR/keepalived.conf ./scripts/ovs/keepalived.conf || true

     mv $TMP_DIR/report_ha_state.py ./scripts/ovs/report_ha_state.py || true

     mv $TMP_DIR/addip_sendarp.sh ./scripts/ovs/addip_sendarp.sh || true

     mv $TMP_DIR/delip.sh ./scripts/ovs/delip.sh || true

   fi

 

   start_service openvswitch

   start_service ovs-init

   sleep 3

   # change dpid in config.xml, then restart onos

   br0_dpid=$(ovs-ofctl show br0 |grep dpid | awk -F: '{print $3}' | sed 's/\(..\)/\0:/g' | sed 's/.$//')

   sed -i "s#{{ DPID }}#$br0_dpid#g" /etc/ctl/config.xml || true

   systemctl restart onos || true

 

   log_end ovs

 }

 

 i_all() {

   i_base || true

   log info "6. install"

   (

   i_epc

   i_tomcat

   i_signaltrace

   i_onos

   i_ovs

   ) | tee ./epc-install-`date +%Y-%m-%d-%H-%M`.log

   log info "install...done"

 }

 

 usage() {

     cat << EOF

 NAME

     install.sh - Install Baicells EPC

 

 SYNOPSIS

     $prog COMMAND [OPTIONS]

 

 DESCRIPTION

     A tool to install Baicells EPC, include components such as

     epc, onos, ovs, tomcat, signaltrace.

 

 OPTIONS

     -h        Show this help

     -i        Service to install   (default: all)

 

 EOF

 exit 0

 }

 

 run() {

   gc

   $1

   clean

 }

 

 # trap ctrl-c and call ctrl_c()

 trap ctrl_c INT

 

 ctrl_c() {

   echo "** CTRL-C **"

   exit 1

 }

 

 

 SERVICE=all

 while getopts ":i:h" opt

 do

   case "$opt" in

     h ) usage ;;

     i ) SERVICE=${OPTARG} ;;

     * ) usage ;;

   esac

 done

 case "$SERVICE" in

   base)

     run i_base

     ;;

   epc)

     run i_epc

     ;;

   tomcat)

     run i_tomcat

     ;;

   signaltrace)

     run i_signaltrace

     ;;

   onos)

     run i_onos

     ;;

   ovs)

     run i_ovs

     ;;

   all)

     run i_all

     ;;

   *)

     usage

     ;;

 esac

 

    原文作者:随风化作雨
    原文地址: https://www.jianshu.com/p/8aa22f062438
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞