• openstack常用shell脚本


    批量创建openstack云主机

    cat createServer.sh
    #/bin/bash
    
    help() {
    cat <<EOF
    # Start openstack cloud hosts in batches
    Usage:
      ./createServer.sh <OPTION> server..
    
    Options:
      -u, --user-data Use a custom template to create a cloud host. 
          Note: The long format cannot be used because the long format contains the "-" character
      -i, --image     Enter image name. For example: CentOS7 , Ubuntu20 ...
      -n, --network   Enter the network name. If there are multiple networks, separate them with commas. For example: selfnetwork,provider
      -f, --flavor    Enter the flavor name. For example: standard(disk 15G, RAM 4G, VCPU 4), luxury(disk 20G, RAM 8G, VCPU 8)
    
    Example:
      # Multiple single network
      ./createServer.sh --flavor standard --image CentOS7 --network selfnetwork test1 test2
    
      # Custom simulation host. The value of -u must be combined with the parameter
      ./createServer.sh -u./cloud-config-centos.txt --flavor standard --image CentOS7 --network selfnetwork test1 test2
    
      # Multiple stations and multiple networks
      ./createServer.sh --flavor standard --image CentOS7 --network selfnetwork,provider test1 test2
    EOF
    }
    
    # Define help information
    if [[ $# == 0 || "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
        help
        exit 1
    fi
    
    # Set script parameter information
    args=$(getopt -l "flavor:,image:,network:,server:,user-data::" -o "f:i:n:s:u::" -- "$@")
    eval set -- "$args"
    while [ $# -ge 1 ]; do
      case $1 in
        -f|--flavor)
          flavor="$2"
          shift
          ;;
    
        -i|--image)
          image="$2"
          shift
          ;;
    
        -n|--network)
          network="`echo $2 | sed 's/,/ --network /g'`"
          shift
          ;;
    
        -u|--user-data)
          Userdata="$2"
          shift
          ;;
    
        --)
          shift
          name=$*
          if [[ -z $name ]];then
             exit
          fi
          ;;
      esac
      shift
    done
    
    # Do you agree to create servers
    read -p "请确认是否需要创建 $name 服务器[yes,no]: " val
    if [[ $val == 'yes' || $val == 'y' ]]; then
        # Create servers
        source admin-openrc
        for i in $name; do
            echo "Create server $i..."
            if [[ ! -z $Userdata ]];then
              openstack server create --user-data $Userdata --flavor $flavor --network $network --image $image --security-group all $i &> /dev/null
              # echo openstack server create --user-data $Userdata --flavor $flavor --network $network --image $image --security-group all $i
            else
              openstack server create --flavor $flavor --network $network --image $image --security-group all $i &> /dev/null
            fi
        done
    
    elif [[ $val == 'no' || $val == 'n' ]]; then
        echo '[INFO] exit...' 
        exit
    
    else
        echo '[ERROR] The input format is incorrect, exit...'
        exit
    
    fi
    
    # View server creation
    echo -e "33[1;42;33mAll Server Create completed...33[0m
    "
    sleep 20
    echo '$ openstack server list'
    openstack server list
    

    一键启动云主机

    cat startServer.sh
    #/bin/bash
    
    help() {
    cat <<EOF
    # Start openstack cloud hosts in batches
    Usage:
      ./startServer.sh <OPTION> server..
    
    Options:
        start: Start the cloud server. Support one-click start, server designated as all.
        stop:  Shut down cloud server. Support one-click start, server designated as all.
    
    Example:
        # Start ceph and k8s cloud server
          ./startServer.sh start ceph k8s
        # Shut down all cloud servers
          ./startServer.sh stop all
    EOF
    }
    
    # Define help information
    if [[ $# == 0 || "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
        help
        exit
    fi
    
    source admin-openrc 
    
    # View service status information
    status_server(){
        # Cycle all parameters
        for i in $*;do
            # The host that loops all parameters
            for j in `openstack server list | egrep $i | awk -F'|' '{print $4}'`;do
                # The host is not started, return 255
                if [[ ! $j == 'ACTIVE' ]];then
                     return 255
                fi
            done
        done
    }
    
    jumpserver(){
        echo 'start server jumpserver...'
        openstack server start jumpserver &> /dev/null && sleep 30
    
        while true; do
          ping -c4 192.168.31.20 &> /dev/null && break
          if [ $? -ne 0 ]; then
            systemctl restart neutron-dhcp-agent.service neutron-linuxbridge-agent.service 
            openstack server reboot jumpserver &> /dev/null
            sleep 30
          fi
        done
    }
    
    
    start_server(){
        for k in $*;do
            eval status_server $k
    
            if [[ $? -ne 255 ]];then
                if [[ $k == 'jumpserver' ]];then
                    ping 192.168.31.20 -c2 &> /dev/null
                    if [ $? -ne 0 ]; then
                        openstack server stop jumpserver &> /dev/null && sleep 10
                        jumpserver
                    fi
                fi
    
                echo "$k server has started..."
                # echo -e "
    $ openstack server list"
                # openstack server list
                # exit
                continue
            fi
    
            if [[ $k == 'jumpserver' ]];then
                jumpserver
    
            elif [[ $k == 'k8s' ]];then
                status_server ceph
                if [[ $? -eq 255 ]];then
                    echo -e "[ERROR] The ceph service is not started. Start the ceph host first, then start the k8s host.
    "
                    exit
                fi
    
                
                echo 'start server k8s...'
                for z in `openstack server list | egrep k8s | awk -F'|' '{print $3}'`;do
                    openstack server start $z &> /dev/null
                done
    
            else
                echo -e "
    start server $k..."
                for z in `openstack server list | egrep $k | awk -F'|' '{print $3}'`;do
                    openstack server start $z &> /dev/null
                done
            fi
    
            sleep 15
            echo "$k server has started completed..."
        done
    
    
        echo -e "
    $ openstack server list"
        openstack server list
    }
    
    stop_server(){
        for k in $*;do
            if [[ $k == 'ceph' ]];then
                status_server k8s
                if [[ $? -ne 255 ]];then
                    echo -e "[ERROR] The k8s service is not shut down. Stop the k8s host first, and then stop the ceph host.
    "
                    exit
                fi
            fi
    
            eval status_server $k
            if [[ $? -ne 255 ]];then
                echo -e "stop server $k..."
                for z in `openstack server list | egrep $k | awk -F'|' '{print $3}'`;do
                    openstack server stop $z &> /dev/null
                done
            fi
            echo "$k server has stoped completed..."
        done
    
        sleep 10
        echo -e "
    $ openstack server list"
        openstack server list
    }
    
    
    if [[ $1 == 'start' ]];then
        shift
    
        if [[ $1 == 'all' ]];then
            start_server jumpserver ceph debian k8s
            exit
        fi
    
        eval start_server $*
    
    elif [[ $1 == 'stop' ]];then
        shift
    
        if [[ $1 == 'all' ]];then
            stop_server k8s debian ceph jumpserver
            exit
        fi
    
        eval stop_server $*
    
    else
        echo -e "[ERROR] Syntax error, view help through -h option
    "
        exit
    fi
    

    禁用安全端口

    cat disablePort.sh
    #!/bin/bash
    source ~/admin-openrc
    
    UserHelp() {
    cat <<EOF
    # Disable the security rules and ports of the cloud host
    
    Usage:
      ./disablePort.sh <network> server..
    
    Options:
      <network>   Enter the network name. For example: selfnetwork provider
    
    Example:
      ./disablePort.sh selfnetwork test01 test02
    EOF
    }
    
    if [[ $1 == 'selfnetwork' || $1 == 'provider' ]]; then
      network=$1
      shift
    
      for i in $*; do
        # 根据主机名,查IP地址
        # "/${i}/"'{print $5}' 两者之间不能有空格,如果过滤项是变量请使用双引号
        # '{sub("^ *","");sub(" *$","");print $2}' 显示第二项并删除前后空格
        IP_Field=`openstack server list | awk -F"|" "/ $i /"'{print $5}'`
        if [[ -z $IP_Field ]];then
          echo "$i Host does not exist"
          continue
        fi
    
        for j in $IP_Field;do
          if [[ $j =~ "$network" ]];then
            IP=`echo $j | awk -F"[,;=]" '{sub("^ *","");sub(" *$","");print $2}'`
            # echo $IP
          fi
        done
      
        # 根据IP地址,查port id
        if [[ ! -z $IP ]];then
          Port_id=`openstack port list | awk "/'${IP}'/"'{print $2}'`
          # echo $Port_id
        fi
      
        # 禁用安全规则和端口规格
        openstack port set --no-security-group --disable-port-security ${Port_id}
        echo "$i host set complete."
      done
    
    elif [[ $1 == '--help' || $1 == '-h' || $# == 0 ]];then
      UserHelp
    
    else
      echo -e "Wrong option
    
    "
      UserHelp
    
    fi
    
  • 相关阅读:
    Lambada. 计算和
    adb server version (31) doesn't match this client (39) 解决方案
    python爬虫beautifulsoup4系列2
    python爬虫beautifulsoup4系列1
    利用Python攻破12306的最后一道防线
    python自动化17-JS处理滚动条
    python多线程
    python接口自动化2-发送post请求
    python接口自动化1-发送get请求 前言
    jenkens其实是代码上传工具
  • 原文地址:https://www.cnblogs.com/mycloudedu/p/14998018.html
Copyright © 2020-2023  润新知