微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bash:学习

#!/usr/bin/env bash
#
# ${SF_copYRIGHT}
#
# author: chunyang liu
# 
# this script used to install skyform monitor production
#
#

# check it the production directory. make sure package are OK.
#
Dirs=(bin conf lib scripts)
for d in ${Dirs[*]};do
    if [ ! -e $d ];then
        echo "ERROR: missing product module $d."
        echo "INFO: you may need to download well-formated package and make sure it's OK."
        exit -1;
    fi
done

# global env
export DRIVER_ROOT=/opt/vmware-driver

DRIVER_JAR=vmware-driver.jar
NVC_TAR=noVNC.tar.gz
RETCODE=0

setup_menu()
{
    # setup menu
    MENU_KEYS=(I U Q)
    MENU_disPLAY=('[I]nstall and Config SkyForm Vmware Driver'
        '[U]ninstall SkyForm Vmware Driver'
        '[Q]uit installation.')
    MENU_ACTION=(install_driver uninstall_driver quit)
}

install_driver()
{
    # check the old installation.
    if [ -d "$DRIVER_ROOT" ];then
        echo "WARN: found an old mointor server installation in $DRIVER_ROOT"
        echo "If you continue installation, the old one will be erased."
        printf "Continue installation (Y/N)?: "
        read YN
        case "$YN" in
            "Y") ;;
    "y") ;;
            *)
                echo "You have chosen 'No'. Quit the installation."
                exit 0;
        esac
        
        rm -rf "$DRIVER_ROOT"
    fi

    echo "It will need severial minutes, please waiting"

    mkdir -p "$DRIVER_ROOT"
    
    cp -rf ./conf $DRIVER_ROOT/.
    cp -rf ./lib $DRIVER_ROOT/.
    cp -rf ./bin $DRIVER_ROOT/.
    cp -rf ./scripts $DRIVER_ROOT/.
    cp -rf ./$DRIVER_JAR $DRIVER_ROOT/.
    mkdir -p $DRIVER_ROOT/logs
    chmod 777 $DRIVER_ROOT/logs -R
    chmod -R 755 $DRIVER_ROOT/bin/*
    chmod -R 755 $DRIVER_ROOT/scripts/*
    chmod -R 755 $DRIVER_ROOT/$DRIVER_JAR
    
    rpm -e jdk-1.7.0_02-fcs.x86_64 2>/dev/null
    rpm -qa | grep java | xargs rpm -e --nodeps 2>/dev/null
    rpm -qa | grep jre | xargs rpm -e --nodeps 2>/dev/null
    rpm -qa | grep jdk | xargs rpm -e --nodeps 2>/dev/null
    cd $DRIVER_ROOT/lib
    tar zxvf jdk-7u2-linux-x86_64.tar.gz
    cd jdk-7u2-linux-x86_64
    sh scripts/install.sh 2>/dev/snull

    vncinfo=`cat /etc/rc.local|grep "vnc"`
    
    if [ x"$vncinfo" = x ]
    then
        mkdir -p /root/tokens
        cd /opt
        tar -zxvf $DRIVER_ROOT/lib/$NVC_TAR
        echo "cd /opt/noVNC/utils" >> /etc/rc.local
        echo "nohup ./websockify --web /opt/noVNC/ --target-config=/root/tokens 6080 >> /opt/noVNC/novnc.log 2>&1 &" >> /etc/rc.local
        cd /opt/noVNC/utils
        nohup ./websockify --web /opt/noVNC/ --target-config=/root/tokens 6080 >> /opt/noVNC/novnc.log 2>&1 &
    fi
    
    echo "Install vmware-driver Succeed!\n"
    setup_menu;
    menu;
}

uninstall_driver()
{
    if [ -d "$DRIVER_ROOT" ] ; then
        rm -rf "$DRIVER_ROOT"
    fi
    
    rpm -e jdk-1.7.0_02-fcs.x86_64 2>/dev/null
    rpm -qa | grep java | xargs rpm -e --nodeps 2>/dev/null
    rpm -qa | grep jre | xargs rpm -e --nodeps 2>/dev/null
    rpm -qa | grep jdk | xargs rpm -e --nodeps 2>/dev/null
    
    /bin/sh /opt/vmware-driver/bin/vmware-driver-stop.sh -r all

    echo "Uninstall vmware-driver Succeed!\n"
    setup_menu;
    menu;
}

menu()
{
    local MENU_LEN=${#MENU_KEYS[@]}
    local ACTION_LEN=${#MENU_ACTION[@]}
    if [ $MENU_LEN != $ACTION_LEN ];then
        echo "Assert error: the menu item doesnot match the action item"
        exit 1;
    fi

    # header
    [ ! -z "${MENU_HEADER}" ] && printf "${MENU_HEADER}"

    # menu body
    for ((i=0; i < $MENU_LEN; i++ )); do
        printf "\t${MENU_KEYS[$i]}) ${MENU_disPLAY[$i]}\n"
    done
    
    printf "\t> "
    read CHOICE
    [ -z "$CHOICE" ] && echo "Aarning: empty choice. Quit." && exit 1;
    
    HIT=0
    for ((i=0; i < $MENU_LEN; i++)); do
        CHOICE=`echo $CHOICE | tr [A-Z] [a-z]`
        KEY=`echo ${MENU_KEYS[$i]} | tr [A-Z] [a-z]`
        [ "${CHOICE}" == "${KEY}" ] && HIT=1 && eval ${MENU_ACTION[$i]}
    done
    [ ${HIT} == 0 ] && echo "Invalid choice '$CHOICE'. Quit."; exit 1;
    exit $RETCODE
}

quit()
{
    exit $RETCODE
}

##############################
# main
setup_menu;
menu;

  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐