美文网首页
openstack重建虚机脚本

openstack重建虚机脚本

作者: waypec | 来源:发表于2022-09-25 16:21 被阅读0次

写了个重建虚机脚本

  • 目前只支持虚机挂载一个卷
  • 该脚本可以加入cmp_user,description
#editor:waypec
#version: V1.0
#date:2022-09-23
#########################################

#!/bin/bash
source /root/script/newshangjin/admin-openrc_new
ip=$1

info() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO]: $1"
}

get_server_info() {
    info "get server info"
    echo ""

    #server_id
    server_id=$(openstack server list | grep -w $ip | cut -d '|' -f '2')
    server_id=$(echo $server_id | sed -e 's/^[ \t]//g')
    echo "server_id:$server_id"

    openstack server show $server_id > _server_info
    sleep 1

    #server_name
    server_name=$(cat _server_info | grep -w "name " | cut -d '|' -f 3)
    server_name=$(echo $server_name | sed -e 's/^[ \t]//g')
    echo "server_name:$server_name"
    

    #availability_zone
    availability_zone=$(cat _server_info | grep availability_zone | cut -d '|' -f 3)
    availability_zone=$(echo $availability_zone | sed -e 's/^[ \t]//g')
    echo "availability_zone:$availability_zone"

    #flavor_id
    flavor_id=$(cat _server_info | grep flavor | cut -d '|' -f 3)
    flavor_id=${flavor_id#*\(}
    flavor_id=${flavor_id%\)*}
    echo "flavor_id:$flavor_id"

    #floating_ip
    addresses=$(cat _server_info | grep addresses | cut -d '|' -f 3)
    floating_ip=${addresses#*,}
    floating_ip=$(echo $floating_ip | sed -e 's/^[ \t]//g')
    echo "floating_ip:$floating_ip"

    #network
    network=${addresses%=*}
    network=$(echo $network | sed -e 's/^[ \t]//g')
    echo "network:$network"

    #volume_id
    volume_id=$(cat _server_info | grep volumes_attached | cut -d '|' -f 3)
    volume_id=${volume_id#*\'}
    volume_id=${volume_id%\'*}
    echo "volume_id:$volume_id"

    
    properties=$(cat _server_info | grep properties | cut -d '|' -f 3)
    #cmp_user
    cmp_user=$(echo $properties | cut -d ',' -f 1)
    cmp_user=$(echo $cmp_user | sed -e "s/^[ \t]//g" | sed -e "s/'//g")
    echo "cmp_user:$cmp_user"
    #description
    description=$(echo $properties | cut -d ',' -f 2)
    description=$(echo $description | sed -e "s/^[ \t]//g" | sed -e "s/'//g")
    echo "description:$description"  

    echo ""
}

del_server() {
    info "deleting server $server_name"
    openstack server delete "$server_id"
    sleep 2
}

create_server() {
    #create server
    info "rebuild server $server_name"
    openstack server create \
    --volume "$volume_id" \
    --flavor "$flavor_id" \
    --network "$network" \
    --availability-zone "$availability_zone" \
    --property "$cmp_user" \
    --property "$description" \
    "$server_name" > _new_server_info

    new_server_id=$(cat _new_server_info | grep -w 'id '| cut -d '|' -f '3')
    new_server_id=$(echo $new_server_id | sed -e 's/^[ \t]//g' )
    info "new_server_id:$new_server_id"

    #binding floating ip
    sleep 8
    info "binding floating ip $floating_ip"
    openstack server add floating ip "$new_server_id" "$floating_ip"
}




main() {
    if [[ $1 = "-h" ]] || [[ $1 = "--help" ]] || [[ $1 = "" ]]
    then
        echo "Usage: ./rebuild.sh <floating ip>"
        echo "this script is for rebuilding server"
        exit 0
    fi
    get_server_info
    del_server
    create_server
}

main "$1"

相关文章

  • openstack重建虚机脚本

    写了个重建虚机脚本 目前只支持虚机挂载一个卷 该脚本可以加入cmp_user,description

  • Openstack 虚机实例操作总结

    1. 概述 Openstack作为常见的IAAS服务,其最核心的对象就是虚机实例,而Openstack下支持的虚机...

  • openstack虚机状态

    OpenStack创建一个虚拟机,涉及到三种状态,vm_state,task_state和power_state。...

  • 交换机access vlan和trunk vlan

    背景:以openstack云上虚机通信为例,两个计算节点上虚机间如何通信,以及虚机网络如何访问互联网。 组网图来取...

  • OpenStack "VolumeLimitExceeded"故

    创建虚机遇到VolumeLimitExceeded报错,下面是排错过程 今天在用OpenStack创建虚机的时候,...

  • openstack

    openstack基础架构 为什么要用 Openstack 使用脚本自动化部署openstack M版 需要的脚本...

  • openstack使用记录

    virtual ip 有限制,需要openstack虚机在管理层(192.168.1.2)加入port支持; 虚拟...

  • 虚拟分区

    在 OpenStack 中, 一个虚机涉及到的存储空间, 主要是 根分区 (/ ,root分区), swap分区,...

  • Openstack平台列表缓存概要设计

    问题描述 当OpenStack集群部署规模大,资源数量多时(虚机资源,镜像资源,云硬盘资源,网络,子网,Port资...

  • 云计算day03-Openstack

    openstack基础架构 1.为什么要用 Openstack 2. 使用脚本自动化部署openstack M版 ...

网友评论

      本文标题:openstack重建虚机脚本

      本文链接:https://www.haomeiwen.com/subject/vncxartx.html