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;

  

posted on 2021-01-12 03:36  lydstory  阅读(105)  评论(0)    收藏  举报

导航