maas-autobuilder/manage-maas-nodes.sh

298 lines
8.7 KiB
Bash
Raw Normal View History

2018-04-20 14:12:01 -04:00
#!/bin/bash
# set -x
. functions.sh
# Storage type
storage_format="raw"
# Models for nic and storage
2018-04-20 14:12:01 -04:00
nic_model="virtio"
stg_bus="scsi"
# Time between building VMs
build_fanout=60
maas_assign_networks()
{
maas_auto_assign_networks $1
}
# Attempts to auto assign all the networks for a host
maas_auto_assign_networks()
{
2020-12-23 16:00:28 +00:00
system_id=$1
2020-12-23 16:37:26 +00:00
# Grabs all the interfaces that are attached to the system
node_interfaces=$(maas ${maas_profile} interfaces read ${system_id} \
| jq ".[] | {id:.id, name:.name, mode:.links[].mode, subnet:.links[].subnet.id }" --compact-output)
# This for loop will go through all the interfaces and enable Auto-Assign
# on all ports
2020-12-23 16:00:28 +00:00
for interface in ${node_interfaces}
do
int_id=$(echo $interface | jq ".id" | sed s/\"//g)
subnet_id=$(echo $interface | jq ".subnet" | sed s/\"//g)
mode=$(echo $interface | jq ".mode" | sed s/\"//g)
if [[ $mode != "auto" ]] ; then
maas ${maas_profile} interface link-subnet ${system_id} ${int_id} mode="AUTO" subnet=${subnet_id}
fi
done
}
2018-04-20 14:12:01 -04:00
2020-12-23 16:37:26 +00:00
# Calls the 3 functions that creates the VMs
2018-04-20 14:12:01 -04:00
create_vms() {
install_deps
2020-12-23 16:00:28 +00:00
maas_login
create_storage
build_vms
2018-04-20 14:12:01 -04:00
}
2020-12-23 16:37:26 +00:00
# Calls the functions that destroys and cleans up all the VMs
2018-04-20 14:12:01 -04:00
wipe_vms() {
install_deps
2020-12-23 16:00:28 +00:00
maas_login
destroy_vms
2018-04-20 14:12:01 -04:00
}
2020-12-27 10:32:11 +00:00
# Fixes all the networks on all the VMs
network_auto()
2020-12-27 10:12:42 +00:00
{
install_deps
maas_login
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
system_id=$(maas_system_id ${virt_node})
maas_auto_assign_networks ${system_id} &
done
wait
}
2020-12-27 10:32:11 +00:00
commision_vm()
{
system_id=$1
maas ${maas_profile} machine commission ${system_id}
# Ensure that the machine is in ready state before the next step
ensure_machine_in_state ${system_id} "Ready"
maas_auto_assign_networks ${system_id}
}
recommission_vms()
{
install_deps
maas_login
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
system_id=$(maas_system_id ${virt_node})
commission_vm ${system_id} &
sleep ${build_fanout}
done
wait
}
2020-12-23 16:37:26 +00:00
# Creates the disks for all the nodes
2018-04-20 14:12:01 -04:00
create_storage() {
2020-12-23 16:00:28 +00:00
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
2020-12-23 16:37:26 +00:00
# Create th directory where the storage files will be located
2020-12-23 16:00:28 +00:00
mkdir -p "$storage_path/$virt_node"
2020-12-23 16:37:26 +00:00
# For all the disks that are defined in the array, create a disk
2020-12-23 16:00:28 +00:00
for ((disk=0;disk<${#disks[@]};disk++)); do
2020-12-23 16:37:26 +00:00
/usr/bin/qemu-img create -f "$storage_format" \
"$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img" "${disks[$disk]}"G &
2020-12-23 16:00:28 +00:00
done
done
wait
2018-04-20 14:12:01 -04:00
}
2020-12-23 16:37:26 +00:00
# The purpose of this function is to stop, release the nodes and wipe the disks
# to save space, and then so that the machines in MAAS can be re-used
wipe_disks() {
2020-12-23 16:00:28 +00:00
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
system_id=$(maas_system_id ${virt_node})
2020-12-23 16:37:26 +00:00
# Release the machine in MAAS
2020-12-23 16:00:28 +00:00
maas ${maas_profile} machine release ${system_id}
2020-12-23 16:37:26 +00:00
# Ensure that the machine is in ready state before the next step
2020-12-26 21:05:23 +00:00
ensure_machine_in_state ${system_id} "Ready"
2020-12-23 16:37:26 +00:00
# Stop the machine if it is running
# It's probably stopped anyway as per the release above
2020-12-23 16:00:28 +00:00
virsh --connect qemu:///system shutdown "$virt_node"
2020-12-23 16:37:26 +00:00
# Remove the disks
2020-12-23 16:00:28 +00:00
for ((disk=0;disk<${#disks[@]};disk++)); do
rm -rf "$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img" &
done
done
2020-12-23 16:37:26 +00:00
# Re-create the storage again from scratch
2020-12-23 16:00:28 +00:00
create_storage
wait
}
2018-04-20 14:12:01 -04:00
# Builds the VMs from scratch, and then adds them to MAAS
2018-04-20 14:12:01 -04:00
build_vms() {
2020-12-23 16:00:28 +00:00
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
2020-12-23 16:37:26 +00:00
# Based on the variables in hypervisor.config, we define the variables
# for ram and cpus. This also allows a number of control nodes that
# can be defined as part of full set of nodes.
2020-12-23 16:00:28 +00:00
ram="$node_ram"
vcpus="$node_cpus"
node_type="compute"
if [[ $virt -le $control_count ]] ; then
ram="$control_ram"
vcpus="$control_cpus"
node_type="control"
fi
bus=$stg_bus
2020-12-23 16:37:26 +00:00
# Based on the bridges array, it will generate these amount of MAC
# addresses and then create the network definitions to add to
# virt-install
2020-12-23 16:00:28 +00:00
macaddr=()
network_spec=""
# Based on the type of network we are using we will assign variables
# such that this can be either bridge or network type
if [[ $network_type == "bridge" ]] ; then
net_prefix="bridge"
2020-12-23 19:32:35 +00:00
net_type=(${bridges[@]})
elif [[ $network_type == "network" ]] ; then
net_prefix="network"
2020-12-23 16:59:40 +00:00
net_type=(${networks[@]})
fi
# Now define the network definition
for ((mac=0;mac<${#net_type[@]};mac++)); do
2020-12-23 16:00:28 +00:00
macaddr+=($(printf '52:54:00:%02x:%02x:%02x\n' "$((RANDOM%256))" "$((RANDOM%256))" "$((RANDOM%256))"))
network_spec+=" --network=$net_prefix="${net_type[$mac]}",mac="${macaddr[$mac]}",model=$nic_model"
2020-12-23 16:00:28 +00:00
done
2020-12-23 16:37:26 +00:00
# Based on the disks array, it will create a definition to add these
# disks to the VM
2020-12-23 16:00:28 +00:00
disk_spec=""
for ((disk=0;disk<${#disks[@]};disk++)); do
disk_spec+=" --disk path=$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img"
disk_spec+=",format=$storage_format,size=${disks[$disk]},bus=$bus,io=native,cache=directsync"
done
2020-12-23 16:37:26 +00:00
# Creates the VM with all the attributes given
2020-12-23 16:00:28 +00:00
virt-install -v --noautoconsole \
2020-12-23 16:37:26 +00:00
--print-xml \
--autostart \
--boot network,hd,menu=on \
--video qxl,vram=256 \
--channel spicevmc \
--name "$virt_node" \
--ram "$ram" \
--vcpus "$vcpus" \
--os-variant "ubuntu18.04" \
2020-12-23 16:00:28 +00:00
--console pty,target_type=serial \
--graphics spice,clipboard_copypaste=no,mouse_mode=client,filetransfer_enable=off \
--cpu host-passthrough,cache.mode=passthrough \
--controller "$bus",model=virtio-scsi,index=0 \
$disk_spec \
$network_spec > "$virt_node.xml" &&
2020-12-23 16:37:26 +00:00
# Create the Vm based on the XML file defined in the above command
2020-12-23 16:00:28 +00:00
virsh define "$virt_node.xml"
2020-12-23 16:37:26 +00:00
# Start the VM
2020-12-23 16:00:28 +00:00
virsh start "$virt_node" &
2020-12-23 16:37:26 +00:00
# Call the maas_add_node function, this will add the node to MAAS
2020-12-23 16:00:28 +00:00
maas_add_node ${virt_node} ${macaddr[0]} ${node_type} &
# Wait some time before building the next, this helps with a lot of DHCP requests
# and ensures that all VMs are commissioned and deployed.
sleep ${build_fanout}
done
wait
2018-04-20 14:12:01 -04:00
}
destroy_vms() {
2020-12-23 16:00:28 +00:00
for ((node="$node_start"; node<=node_count; node++)); do
printf -v virt_node %s-%02d "$compute" "$node"
2020-12-23 16:37:26 +00:00
# If the domain is running, this will complete, else throw a warning
virsh --connect qemu:///system destroy "$virt_node"
2020-12-23 16:00:28 +00:00
2020-12-23 16:37:26 +00:00
# Actually remove the VM
virsh --connect qemu:///system undefine "$virt_node"
2020-12-23 16:00:28 +00:00
2020-12-23 16:37:26 +00:00
# Remove the three storage volumes from disk
for ((disk=0;disk<${#disks[@]};disk++)); do
virsh vol-delete --pool "$virt_node" "$virt_node-d$((${disk} + 1)).img"
done
2020-12-23 16:00:28 +00:00
2020-12-23 16:37:26 +00:00
# Remove the folder storage is located
rm -rf "$storage_path/$virt_node/"
sync
2020-12-23 16:00:28 +00:00
2020-12-23 16:37:26 +00:00
# Remove the XML definitions for the VM
rm -f "$virt_node.xml" \
"/etc/libvirt/qemu/$virt_node.xml" \
"/etc/libvirt/storage/$virt_node.xml" \
"/etc/libvirt/storage/autostart/$virt_node.xml"
2020-12-23 16:00:28 +00:00
2020-12-23 16:37:26 +00:00
# Now remove the VM from MAAS
system_id=$(maas_system_id ${virt_node})
maas ${maas_profile} machine delete ${system_id}
2020-12-23 16:00:28 +00:00
done
2018-04-20 14:12:01 -04:00
}
2020-12-23 20:03:00 +00:00
show_help() {
echo "
-c Creates everything
-w Removes everything
-d Releases VMs, Clears Disk
2020-12-27 10:32:11 +00:00
-n Updates all the networks on all VMs
-r Recommission all VMs
2020-12-23 20:03:00 +00:00
"
}
# Initialise the configs
read_config
2020-12-27 10:32:11 +00:00
while getopts ":cwdnr" opt; do
2018-04-20 14:12:01 -04:00
case $opt in
2020-12-23 16:00:28 +00:00
c)
create_vms
;;
w)
wipe_vms
;;
d)
wipe_disks
;;
2020-12-27 10:12:42 +00:00
n)
2020-12-27 10:32:11 +00:00
network_auto
;;
r)
recommission_vms
2020-12-27 10:12:42 +00:00
;;
2020-12-23 16:00:28 +00:00
\?)
2020-12-23 20:03:00 +00:00
printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2
show_help
exit 1
2020-12-23 16:00:28 +00:00
;;
2018-04-20 14:12:01 -04:00
esac
done