Fix more tab to spaces, Add comments

This commit is contained in:
Arif Ali 2020-12-23 16:37:26 +00:00
parent 02d222c3a8
commit b12f28aeca
Signed by: arif
GPG Key ID: 369608FBA1353A70
5 changed files with 118 additions and 37 deletions

View File

@ -13,10 +13,12 @@ node_start=1
node_cpus=2
node_ram=4096
disks=()
disks+=(50)
disks+=(20)
disks+=(20)
bridges=()
bridges+=("br-enp1s0")
bridges+=("br-enp1s0.301")
bridges+=("br-enp1s0.302")

View File

@ -13,10 +13,12 @@ node_start=1
node_cpus=2
node_ram=4096
disks=()
disks+=(50)
disks+=(20)
disks+=(20)
bridges=()
bridges+=("br-enp1s0")
bridges+=("br-enp1s0.301")
bridges+=("br-enp1s0.302")

20
default.config Normal file
View File

@ -0,0 +1,20 @@
compute="maas-node"
control_count=0
node_count=20
node_start=1
node_cpus=4
node_ram=4096
disks=()
disks+=(50)
disks+=(100)
disks+=(100)
bridges=()
bridges+=("br-enp1s0")
bridges+=("br-enp1s0.301")
bridges+=("br-enp1s0.302")
bridges+=("br-enp1s0.303")
bridges+=("br-enp1s0.304")

View File

@ -13,10 +13,12 @@ node_start=1
node_cpus=2
node_ram=4096
disks=()
disks+=(50)
disks+=(20)
disks+=(20)
bridges=()
bridges+=("br0")
bridges+=("br1")
bridges+=("br2")

View File

@ -2,6 +2,7 @@
# set -x
. default.config
. maas.config
. hypervisor.config
@ -19,16 +20,22 @@ commission_timeout=1200
# Time between building VMs
build_fanout=60
# This logs in to maas, and sets up the admin profile
# Ensures that any dependent packages are installed for any MAAS CLI commands
# This also logs in to MAAS, and sets up the admin profile
maas_login()
{
# Install some of the dependent packages
sudo apt -y update && sudo apt -y install jq bc
# We install the snap, as maas-cli is not in distributions, this ensures
# that the package we invoke would be consistent
sudo snap install maas --channel=2.8/stable
# Login to MAAS using the API key and the endpoint
echo ${maas_api_key} | maas login ${maas_profile} ${maas_endpoint} -
}
# Grabs the unique system)id for the host human readable hostname
# Grabs the unique system_id for the host human readable hostname
maas_system_id()
{
node_name=$1
@ -43,17 +50,22 @@ maas_add_node()
mac_addr=$2
node_type=$3
# This command creates the machine in MAAS. This will then automatically
# turn the machines on, and start commissioning.
maas ${maas_profile} machines create \
hostname=${node_name} \
mac_addresses=${mac_addr} \
architecture=amd64/generic \
power_type=virsh \
power_parameters_power_id=${node_name} \
hostname=${node_name} \
mac_addresses=${mac_addr} \
architecture=amd64/generic \
power_type=virsh \
power_parameters_power_id=${node_name} \
power_parameters_power_address=${qemu_connection} \
power_parameters_power_pass=${qemu_password}
# Grabs the system_id for th node that we are adding
system_id=$(maas_system_id ${node_name})
# This will ensure that the node is ready before we start manipulating
# other attributes.
ensure_machine_ready ${system_id}
# If the tag doesn't exist, then create it
@ -64,6 +76,8 @@ maas_add_node()
# Assign the tag to the machine
maas ${maas_profile} tag update-nodes ${node_type} add=${system_id}
# Ensure that all the networks on the system have the Auto-Assign set
# so that the all the of the networks on the host have an IP automatically.
maas_auto_assign_networks ${system_id}
}
@ -71,7 +85,13 @@ maas_add_node()
maas_auto_assign_networks()
{
system_id=$1
node_interfaces=$(maas ${maas_profile} interfaces read ${system_id} | jq ".[] | {id:.id, name:.name, mode:.links[].mode, subnet:.links[].subnet.id }" --compact-output)
# 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
for interface in ${node_interfaces}
do
int_id=$(echo $interface | jq ".id" | sed s/\"//g)
@ -83,12 +103,16 @@ maas_auto_assign_networks()
done
}
# Calls the 3 functions that creates the VMs
create_vms() {
maas_login
create_storage
build_vms
}
# This takes the system_id, and ensures that the machine is uin Ready state
# You may want to tweak the commission_timeout above in somehow it's failing
# and needs to be done quicker
ensure_machine_ready()
{
system_id=$1
@ -104,36 +128,52 @@ ensure_machine_ready()
done
}
# Calls the functions that destroys and cleans up all the VMs
wipe_vms() {
maas_login
destroy_vms
}
# Creates the disks for all the nodes
create_storage() {
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
# Create th directory where the storage files will be located
mkdir -p "$storage_path/$virt_node"
# For all the disks that are defined in the array, create a disk
for ((disk=0;disk<${#disks[@]};disk++)); do
/usr/bin/qemu-img create -f "$storage_format" "$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img" "${disks[$disk]}"G &
/usr/bin/qemu-img create -f "$storage_format" \
"$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img" "${disks[$disk]}"G &
done
done
wait
}
# 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() {
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
system_id=$(maas_system_id ${virt_node})
# Release the machine in MAAS
maas ${maas_profile} machine release ${system_id}
# Ensure that the machine is in ready state before the next step
ensure_machine_ready ${system_id}
# Stop the machine if it is running
virsh --connect qemu:///system shutdown "$virt_node"
# Remove the disks
for ((disk=0;disk<${#disks[@]};disk++)); do
rm -rf "$storage_path/$virt_node/$virt_node-d$((${disk} + 1)).img" &
done
done
# Re-create the storage again from scratch
create_storage
wait
}
@ -142,6 +182,10 @@ wipe_disks() {
build_vms() {
for ((virt="$node_start"; virt<=node_count; virt++)); do
printf -v virt_node %s-%02d "$compute" "$virt"
# 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.
ram="$node_ram"
vcpus="$node_cpus"
node_type="compute"
@ -152,6 +196,9 @@ build_vms() {
fi
bus=$stg_bus
# Based on the bridges array, it will generate these amount of MAC
# addresses and then create the network definitions to add to
# virt-install
macaddr=()
network_spec=""
for ((mac=0;mac<${#bridges[@]};mac++)); do
@ -159,31 +206,39 @@ build_vms() {
network_spec+=" --network=bridge="${bridges[$mac]}",mac="${macaddr[$mac]}",model=$nic_model"
done
# Based on the disks array, it will create a definition to add these
# disks to the VM
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
# Creates the VM with all the attributes given
virt-install -v --noautoconsole \
--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" \
--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" \
--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" &&
# Create the Vm based on the XML file defined in the above command
virsh define "$virt_node.xml"
# Start the VM
virsh start "$virt_node" &
# Call the maas_add_node function, this will add the node to MAAS
maas_add_node ${virt_node} ${macaddr[0]} ${node_type} &
# Wait some time before building the next, this helps with a lot of DHCP requests
@ -198,30 +253,30 @@ destroy_vms() {
for ((node="$node_start"; node<=node_count; node++)); do
printf -v virt_node %s-%02d "$compute" "$node"
# If the domain is running, this will complete, else throw a warning
virsh --connect qemu:///system destroy "$virt_node"
# If the domain is running, this will complete, else throw a warning
virsh --connect qemu:///system destroy "$virt_node"
# Actually remove the VM
virsh --connect qemu:///system undefine "$virt_node"
# Actually remove the VM
virsh --connect qemu:///system undefine "$virt_node"
# 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
# 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
# Remove the folder storage is located
rm -rf "$storage_path/$virt_node/"
sync
# Remove the folder storage is located
rm -rf "$storage_path/$virt_node/"
sync
# 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"
# 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"
# Now remove the VM from MAAS
system_id=$(maas_system_id ${virt_node})
maas ${maas_profile} machine delete ${system_id}
# Now remove the VM from MAAS
system_id=$(maas_system_id ${virt_node})
maas ${maas_profile} machine delete ${system_id}
done
}