Delete only the demo hostonly networks

Change-Id: I81b1c73d782e75e89ce76c51e0b6e9246409d80e
Closes-Bug: #1384976
This commit is contained in:
Serhiy Ovsianikov 2015-04-07 13:04:12 +03:00
parent 8841a977e3
commit 9737a945a6
8 changed files with 126 additions and 58 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash
# Copyright 2014 Mirantis, Inc.
#
@ -15,7 +15,7 @@
# under the License.
#
# This script check availble memory on host PC for quality provision VMs via VirtualBox
# This script check available memory on host PC for quality provision VMs via VirtualBox
#
source ./config.sh

View File

@ -25,11 +25,14 @@ source ./config.sh
source ./functions/vm.sh
source ./functions/network.sh
# Delete all host-only interfaces
delete_all_hostonly_interfaces
# Delete host-only interfaces
if [[ "$rm_network" == "0" ]]; then
delete_fuel_ifaces
else
delete_all_hostonly_interfaces
fi
# Create the required host-only interfaces
# Change {0..2} to {0..4} below if you are going to create 5 interfaces instead of 3
for idx in $(eval echo {0..2}); do
create_hostonly_interface "${host_nic_name[$idx]}" ${host_nic_ip[$idx]} ${host_nic_mask[$idx]}
for ip in $fuel_master_ips; do
create_hostonly_interfaces $ip
done

View File

@ -22,10 +22,15 @@
# Include the handy functions to operate VMs and track ISO installation progress
source ./config.sh
source ./functions/vm.sh
source ./functions/network.sh
source ./functions/product.sh
# Create master node for the product
# Get variables "host_nic_name" for the master node
get_fuel_name_ifaces
name="${vm_name_prefix}master"
create_vm $name "${host_nic_name[0]}" $vm_master_cpu_cores $vm_master_memory_mb $vm_master_disk_mb
echo

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash
# Copyright 2013 Mirantis, Inc.
#
@ -22,6 +22,10 @@
# Include the handy functions to operate VMs
source ./config.sh
source ./functions/vm.sh
source ./functions/network.sh
# Get variables "host_nic_name" for the slave nodes
get_fuel_name_ifaces
# Create and start slave nodes
for idx in $(eval echo {1..$cluster_size}); do

View File

@ -22,22 +22,28 @@ iso_path=`ls -1t iso/*.iso 2>/dev/null | head -1`
# Every Mirantis OpenStack machine name will start from this prefix
vm_name_prefix=fuel-
# Host interfaces to bridge VMs interfaces with
# VirtualBox has different virtual NIC naming convention and index base
# between Windows and Linux/MacOS
idx=0
# By default, all available network interfaces vboxnet won't be removed,
# if their IP addresses don't match with fuel_master_ips (10.20.0.1 172.16.0.1
# 172.16.1.1)
# If you want to remove all existing vbox interfaces, then use rm_network=1
# 0 - don't remove all vbox networks. Remove only fuel networks if they exist
# 1 - remove all vbox networks
rm_network=0
# Please add the IPs accordingly if you going to create non-default NICs number
# 10.20.0.1/24 - Mirantis OpenStack Admin network
# 172.16.0.1/24 - OpenStack Public/External/Floating network
# 172.16.1.1/24 - OpenStack Fixed/Internal/Private network
# 192.168.0.1/24 - OpenStack Management network
# 192.168.1.1/24 - OpenStack Storage network (for Ceph, Swift etc)
for ip in 10.20.0.1 172.16.0.1 172.16.1.1 ; do
# VirtualBox for Windows has different virtual NICs naming and indexing.
# Define the type of operating system and the number of processor cores for the fuel master node.
fuel_master_ips="10.20.0.1 172.16.0.1 172.16.1.1"
# Network mask for fuel interfaces
mask="255.255.255.0"
# Determining the type of operating system and adding CPU core to the master node
case "$(uname)" in
Linux)
host_nic_name[$idx]=vboxnet$idx
os_type="linux"
if [ "$(nproc)" -gt "1" ]; then
vm_master_cpu_cores=2
@ -46,7 +52,6 @@ for ip in 10.20.0.1 172.16.0.1 172.16.1.1 ; do
fi
;;
Darwin)
host_nic_name[$idx]=vboxnet$idx
os_type="darwin"
mac_nproc=`sysctl -a | grep machdep.cpu.thread_count | sed 's/^machdep.cpu.thread_count\:[ \t]*//'`
if [ "$mac_nproc" -gt "1" ]; then
@ -56,11 +61,6 @@ for ip in 10.20.0.1 172.16.0.1 172.16.1.1 ; do
fi
;;
CYGWIN*)
if [ $idx -eq 0 ]; then
host_nic_name[$idx]='VirtualBox Host-Only Ethernet Adapter'
else
host_nic_name[$idx]='VirtualBox Host-Only Ethernet Adapter #'$((idx+1))
fi
os_type="cygwin"
if [ "$(nproc)" -gt "1" ]; then
vm_master_cpu_cores=2
@ -73,10 +73,6 @@ for ip in 10.20.0.1 172.16.0.1 172.16.1.1 ; do
exit 1
;;
esac
host_nic_ip[$idx]=$ip
host_nic_mask[$idx]=255.255.255.0
idx=$((idx+1))
done
# Master node settings
vm_master_memory_mb=1536

View File

@ -20,11 +20,33 @@ get_hostonly_interfaces() {
echo -e `VBoxManage list hostonlyifs | grep '^Name' | sed 's/^Name\:[ \t]*//' | uniq | tr "\\n" ","`
}
get_fuel_ifaces() {
local fuel_iface
local fuel_ifaces=""
for ip in $fuel_master_ips; do
fuel_iface=`VBoxManage list hostonlyifs | grep -B5 $ip | grep '^Name' | sed 's/^Name\:[ \t]*//' | uniq | tr "\\n" ","`
fuel_ifaces+="$fuel_iface"
done
echo $fuel_ifaces
}
get_fuel_name_ifaces() {
local fuel_ifaces=$(get_fuel_ifaces)
IFS=","
set -- $fuel_ifaces
j=0
for i in $fuel_ifaces; do
host_nic_name[$j]=$i
j=$((j+1));
done
unset IFS
}
is_hostonly_interface_present() {
name=$1
# String comparison with IF works different in Cygwin, probably due to encoding.
# So, reduced Case is used. since it works the same way.
# Default divider character change is mandatory for Cygwin.
# String comparison with IF works different in Cygwin, probably due to encoding.
# So, reduced Case is used. since it works the same way.
# Default divider character change is mandatory for Cygwin.
case "$(uname)" in
CYGWIN*)
OIFS=$IFS
@ -34,7 +56,7 @@ is_hostonly_interface_present() {
;;
esac
# Call VBoxManage directly instead of function, due to changed IFS
list=(`VBoxManage list hostonlyifs | grep '^Name' | sed 's/^Name\:[ \t]*//' | uniq | tr "\\n" ","`)
local found_iface=(`VBoxManage list hostonlyifs | grep -E "Name: + $name\$" | awk '/Name/ { $1 = ""; print substr($0, 2) }'`)
# Change default divider back
case "$(uname)" in
CYGWIN*)
@ -44,7 +66,7 @@ is_hostonly_interface_present() {
;;
esac
# Check that the list of interfaces contains the given interface
if [[ $list = *$name* ]]; then
if [[ "$found_iface" == "$name" ]]; then
return 0
else
return 1
@ -97,26 +119,18 @@ check_if_iface_settings_applied() {
return 0
}
create_hostonly_interface() {
name=$1
ip=$2
mask=$3
echo "Creating host-only interface (name ip netmask): $name $ip $mask"
# Exit if the interface already exists (deleting it here is not safe, as VirtualBox creates hostonly adapters sequentially)
if is_hostonly_interface_present "$name"; then
echo "Fatal error. Interface $name cannot be created because it already exists. Exiting"
exit 1
fi
VBoxManage hostonlyif create
create_hostonly_interfaces() {
# Creating host-only interface
local ip=$1
echo "Creating host-only interface"
local id=`VBoxManage hostonlyif create | sed "s/'/_/g" | cut -d "_" -f2 | sed "s/^_//" | sed "s/_$//"`
# If it does not exist after creation, let's abort
if ! is_hostonly_interface_present "$name"; then
echo "Fatal error. Interface $name does not exist after creation. Exiting"
if ! is_hostonly_interface_present "$id"; then
echo "Fatal error. Interface $id does not exist after creation. Exiting"
exit 1
else
echo "Interface" $id "was successfully created"
fi
# Disable DHCP
echo "Disabling DHCP server on interface: $name..."
# These magic 1 second sleeps around DHCP config are required under Windows/Cygwin
@ -124,14 +138,14 @@ create_hostonly_interface() {
sleep 1s
VBoxManage dhcpserver remove --ifname "$name" 2>/dev/null
sleep 1s
set -x
# Set up IP address and network mask
echo "Configuring IP address $ip and network mask $mask on interface: $name..."
VBoxManage hostonlyif ipconfig "$name" --ip $ip --netmask $mask
set -x
VBoxManage hostonlyif ipconfig "$id" --ip $ip --netmask $mask
set +x
# Check what we have created actually.
# Sometimes VBox occasionally fails to apply settings to the last IFace under Windows
if !(check_if_iface_settings_applied "$name" $ip $mask); then
if !(check_if_iface_settings_applied "$id" $ip $mask); then
echo "Looks like VirtualBox failed to apply settings for interface $name"
echo "Sometimes such error happens under Windows."
echo "Please run launch.sh one more time."
@ -141,12 +155,42 @@ create_hostonly_interface() {
fi
}
# Checking that the interface has been removed
check_removed_iface() {
local iface=$1
if is_hostonly_interface_present "$iface"; then
echo "Host-only interface \"$iface\" was not removed. Aborting..."
exit 1
fi
}
delete_fuel_ifaces() {
# Only the interfaces that have IP addresses from the 'fuel_master_ips'
# variable in the config.sh scripts will be removed
local fuel_ifaces=$(get_fuel_ifaces)
if [ ! -z "$fuel_ifaces" ]; then
check_running_vms "$fuel_ifaces"
OIFS=$IFS
IFS=","
for interface in $fuel_ifaces; do
IFS=$OIFS
echo "Deleting host-only interface: $interface..."
VBoxManage hostonlyif remove "$interface"
check_removed_iface "$interface"
done
fi
}
delete_all_hostonly_interfaces() {
# All the hostonly interfaces will be removed
local all_hostonly_interfaces=$(get_hostonly_interfaces)
# Checking that the running virtual machines don't use removable host-only interfaces
check_running_vms "$all_hostonly_interfaces"
OIFS=$IFS;IFS=",";list=(`VBoxManage list hostonlyifs | grep '^Name' | sed 's/^Name\:[ \t]*//' | uniq | tr "\\n" ","`);IFS=$OIFS
# Delete every single hostonly interface in the system
for interface in "${list[@]}"; do
echo "Deleting host-only interface: $interface..."
VBoxManage hostonlyif remove "$interface"
check_removed_iface "$interface"
done
}

View File

@ -53,6 +53,24 @@ is_vm_present() {
return 1
}
check_running_vms() {
OIFS=$IFS
IFS=","
local hostonly_interfaces=$1
local list_running_vms=`VBoxManage list runningvms | awk '{print $1}' | sed 's/"//g' | uniq | tr "\\n" ","`
for i in $list_running_vms; do
for j in $hostonly_interfaces; do
running_vm=`VBoxManage showvminfo $i | grep "$j"`
if [[ $? -eq 0 ]]; then
echo "The \"$i\" VM uses host-only interface \"$j\" and it cannot be removed...."
echo "You should turn off the \"$i\" virtual machine, run the script again and then the host-only interface will be deleted. Aborting..."
exit 1
fi
done
done
IFS=$OIFS
}
create_vm() {
name=$1
nic=$2

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash
# Copyright 2013 Mirantis, Inc.
#
@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
# add VirtualBox directory to PATH
# Add VirtualBox directory to PATH
case "$(uname)" in
CYGWIN*)
vbox_path_registry=`cat /proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/Oracle/VirtualBox/InstallDir`
@ -25,17 +25,16 @@ case "$(uname)" in
;;
esac
# Prepare the host system
./actions/prepare-environment.sh || exit 1
# Check available memory on the host system
./actions/check-available-memory.sh || exit 1
# clean previous installation if exists
# Сlean previous installation if exists
./actions/clean-previous-installation.sh || exit 1
# create host-only interfaces
# Сreate host-only interfaces
./actions/create-interfaces.sh || exit 1
# Create and launch master node
@ -43,4 +42,3 @@ esac
# Create and launch slave nodes
./actions/slave-nodes-create-and-boot.sh || exit 1