写了个重建虚机脚本
- 目前只支持虚机挂载一个卷
- 该脚本可以加入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"
网友评论