2020-12-25 15:48:50 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# set -x
|
2020-12-26 20:03:28 +00:00
|
|
|
. functions.sh
|
2020-12-25 15:48:50 +00:00
|
|
|
|
|
|
|
# Time between building VMs
|
|
|
|
build_fanout=60
|
|
|
|
|
|
|
|
# Attempts to auto assign all the networks for a host
|
|
|
|
maas_assign_networks()
|
|
|
|
{
|
|
|
|
system_id=$1
|
|
|
|
|
|
|
|
# Get the details of the physical interface
|
|
|
|
phsy_int=$(maas ${maas_profile} interfaces read ${system_id} | jq ".[] | {id:.id, name:.name,parent:.parents}" --compact-output | grep "parent.*\[\]")
|
|
|
|
phys_int_name=$(echo $phsy_int | jq .name | sed s/\"//g)
|
|
|
|
phys_int_id=$(echo $phsy_int | jq .id | sed s/\"//g)
|
|
|
|
|
|
|
|
i=0
|
|
|
|
for vlan in ${vlans[*]}
|
|
|
|
do
|
|
|
|
subnet_line=$(maas admin subnets read | jq ".[] | {subnet_id:.id, vlan:.vlan.vid, vlan_id:.vlan.id}" --compact-output | grep "vlan\":$vlan,")
|
|
|
|
maas_vlan_id=$(echo $subnet_line | jq .vlan_id | sed s/\"//g)
|
|
|
|
maas_subnet_id=$(echo $subnet_line | jq .subnet_id | sed s/\"//g)
|
2020-12-26 20:03:28 +00:00
|
|
|
ip_addr=""
|
2020-12-25 15:48:50 +00:00
|
|
|
if [[ $i -eq 0 ]] ; then
|
|
|
|
vlan_int_id=${phys_int_id}
|
|
|
|
mode="STATIC"
|
|
|
|
ip_addr="ip_address=$hypervisor_ip"
|
|
|
|
else
|
2020-12-26 20:03:28 +00:00
|
|
|
vlan_int=$(maas ${maas_profile} interfaces create-vlan ${system_id} vlan=${maas_vlan_id} parent=$phys_int_id)
|
2020-12-25 15:48:50 +00:00
|
|
|
vlan_int_id=$(echo $vlan_int | jq .id | sed s/\"//g)
|
|
|
|
if [[ $vlan -eq $external_vlan ]] ; then
|
2020-12-26 20:03:28 +00:00
|
|
|
mode="STATIC"
|
|
|
|
ip_addr="ip_address=$external_ip"
|
2020-12-25 15:48:50 +00:00
|
|
|
else
|
|
|
|
mode="AUTO"
|
|
|
|
fi
|
|
|
|
fi
|
2020-12-26 20:03:28 +00:00
|
|
|
bridge_int=$(maas ${maas_profile} interfaces create-bridge ${system_id} name=${bridges[$i]} vlan=$maas_vlan_id mac_address=${hypervisor_mac} parent=$vlan_int_id)
|
2020-12-25 15:48:50 +00:00
|
|
|
bridge_int_id=$(echo $bridge_int | jq .id | sed s/\"//g)
|
|
|
|
bridge_link=$(maas ${maas_profile} interface link-subnet $system_id $bridge_int_id mode=${mode} subnet=${maas_subnet_id} ${ip_addr})
|
|
|
|
(( i++ ))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Calls the functions that destroys and cleans up all the VMs
|
|
|
|
wipe_node() {
|
2020-12-26 20:03:28 +00:00
|
|
|
install_deps
|
2020-12-25 15:48:50 +00:00
|
|
|
maas_login
|
|
|
|
destroy_node
|
|
|
|
}
|
|
|
|
|
|
|
|
create_node() {
|
2020-12-26 20:03:28 +00:00
|
|
|
install_deps
|
2020-12-25 15:48:50 +00:00
|
|
|
maas_login
|
|
|
|
maas_add_node ${hypervisor_name} ${hypervisor_mac} physical
|
|
|
|
}
|
|
|
|
|
|
|
|
install_node() {
|
2020-12-26 20:03:28 +00:00
|
|
|
install_deps
|
2020-12-25 15:48:50 +00:00
|
|
|
maas_login
|
|
|
|
deploy_node
|
|
|
|
}
|
|
|
|
|
|
|
|
# The purpose of this function is to stop, release the nodes and wipe the disks
|
|
|
|
destroy_node() {
|
|
|
|
pod_id=$(maas_pod_id ${hypervisor_name})
|
|
|
|
maas ${maas_profile} pod delete ${pod_id}
|
|
|
|
|
|
|
|
system_id=$(maas_system_id ${hypervisor_name})
|
|
|
|
maas ${maas_profile} machine delete ${system_id}
|
|
|
|
}
|
|
|
|
|
|
|
|
deploy_node() {
|
|
|
|
system_id=$(maas_system_id ${hypervisor_name})
|
|
|
|
maas ${maas_profile} machine deploy ${system_id} user_data="$(base64 user-data.yaml)"
|
|
|
|
|
2020-12-26 20:03:28 +00:00
|
|
|
# Only return when the node has finised deploying
|
|
|
|
ensure_machine_in_state ${system_id} "Deployed"
|
2020-12-25 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
show_help() {
|
|
|
|
echo "
|
|
|
|
|
|
|
|
-c Creates Hypervisor
|
|
|
|
-w Removes Hypervisor
|
2020-12-26 20:03:28 +00:00
|
|
|
-d Deploy Hypervisor
|
2020-12-25 15:48:50 +00:00
|
|
|
-a Create and Deploy
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
2020-12-26 20:03:28 +00:00
|
|
|
read_config
|
|
|
|
|
|
|
|
while getopts ":cwia" opt; do
|
2020-12-25 15:48:50 +00:00
|
|
|
case $opt in
|
|
|
|
c)
|
|
|
|
create_node
|
|
|
|
;;
|
|
|
|
w)
|
|
|
|
wipe_node
|
|
|
|
;;
|
2020-12-26 20:03:28 +00:00
|
|
|
d)
|
2020-12-25 15:48:50 +00:00
|
|
|
install_node
|
|
|
|
;;
|
|
|
|
a)
|
|
|
|
create_node
|
|
|
|
install_node
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|