Compare commits

...

4 Commits

8 changed files with 367 additions and 70 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
maas.config
maas.debconf
*.xml

View File

@ -1,5 +1,12 @@
qemu_connection="qemu+ssh://virsh@10.0.1.253/system"
qemu_password="seBGtkWFKZuFUFgig8NYU5uh"
hypervisor_name=asrock01
hypervisor_ip=10.0.1.253
hypervisor_mac="a8:a1:59:44:70:ac"
external_vlan=1
qemu_connection="qemu+ssh://virsh@${hypervisor_ip}/system"
qemu_password="SgUoBByKjG9Lj78SwfWAHXD8DvgE67Cu"
storage_path="/var/lib/libvirt/maas-images"
compute="as1-maas-node"
@ -20,9 +27,20 @@ disks+=(20)
network_type=bridge
vlans=()
vlans+=(300)
vlans+=(301)
vlans+=(302)
vlans+=(303)
vlans+=(304)
vlans+=(305)
vlans+=(1)
bridges=()
bridges+=("br-enp1s0")
bridges+=("br-enp1s0.301")
bridges+=("br-enp1s0.302")
bridges+=("br-enp1s0.303")
bridges+=("br-enp1s0.304")
bridges+=("br0")
bridges+=("br1")
bridges+=("br2")
bridges+=("br3")
bridges+=("br4")
bridges+=("br5")
bridges+=("br6")

View File

@ -1,4 +1,7 @@
qemu_connection="qemu+ssh://virsh@10.0.1.251/system"
hypervisor_ip=10.0.1.251
hypervisor_mac="a8:a1:59:44:76:79"
qemu_connection="qemu+ssh://virsh@${hypervisor_ip}/system"
qemu_password="FPEW2H2hc2ot.HNinxpgGIGM88DI2be7"
storage_path="/var/lib/libvirt/maas-images"

View File

@ -39,6 +39,8 @@ init_variables() {
core_packages=( jq moreutils uuid )
maas_packages=( maas maas-cli maas-proxy maas-dhcp maas-dns maas-rack-controller maas-region-api maas-common )
pg_packages=( postgresql-10 postgresql-client postgresql-client-common postgresql-common )
maas_snaps=( maas maas-test-db )
}
remove_maas() {
@ -56,12 +58,21 @@ remove_maas() {
done
}
remove_maas_snap() {
sudo snap remove ${maas_snaps[@]}
}
install_maas() {
# This is separate from the removal, so we can handle them atomically
sudo apt-get -fuy --reinstall install "${core_packages}" "${maas_packages[@]}" "${pg_packages[@]}"
sudo sed -i 's/DISPLAY_LIMIT=5/DISPLAY_LIMIT=100/' /usr/share/maas/web/static/js/bundle/maas-min.js
}
install_maas_snap() {
sudo apt-get -fuy --reinstall install "${core_packages}"
sudo snap install ${maas_snaps[@]}
}
purge_admin_user() {
read -r -d '' purgeadmin <<EOF
@ -72,7 +83,10 @@ with deleted_user as (delete from auth_user where username = '$maas_profile' ret
delete from piston3_consumer where user_id = (select id from deleted_user);
EOF
sudo -u postgres psql -c "$purgeadmin" maasdb
psql_cmd="psql"
[[ $maas_pkg_type == "snap" ]] && psql_cmd="maas-test-db.psql"
sudo -u postgres $psql_cmd -c "$purgeadmin" maasdb
}
build_maas() {
@ -85,7 +99,9 @@ build_maas() {
if [ -f ~/.maas-api.key ]; then
rm ~/.maas-api.key
maas_api_key="$(sudo maas-region apikey --username=$maas_profile | tee ~/.maas-api.key)"
[[ $maas_pkg_type == "deb" ]] && maas_api_key="$(sudo maas-region apikey --username=$maas_profile | tee ~/.maas-api.key)"
[[ $maas_pkg_type == "snap" ]] && maas_api_key="$(sudo maas apikey --username $maas_profile | head -n 1 | tee ~/.maas-api.key)"
fi;
# Fetch the MAAS API key, store to a file for later reuse, also set this var to that value
@ -125,19 +141,21 @@ build_maas() {
sleep 3
maas $maas_profile vlan update fabric-1 0 dhcp_on=True primary_rack="$maas_system_id"
# This is needed, because it points to localhost by default and will fail to
# commission/deploy in this state
echo "DEBUG: http://$maas_bridge_ip:5240/MAAS/"
if [[ $maas_pkg_type == "deb" ]]; then
# This is needed, because it points to localhost by default and will fail to
# commission/deploy in this state
echo "DEBUG: http://$maas_bridge_ip:5240/MAAS/"
sudo debconf-set-selections maas.debconf
sleep 2
# sudo maas-rack config --region-url "http://$maas_bridge_ip:5240/MAAS/" && sudo service maas-rackd restart
sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure maas-rack-controller
sleep 2
sudo debconf-set-selections maas.debconf
sleep 2
# sudo maas-rack config --region-url "http://$maas_bridge_ip:5240/MAAS/" && sudo service maas-rackd restart
sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure maas-rack-controller
sleep 2
sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure maas-region-controller
sudo service maas-rackd restart
sleep 5
sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure maas-region-controller
sudo service maas-rackd restart
sleep 5
fi
}
bootstrap_maas() {
@ -171,12 +189,12 @@ bootstrap_maas() {
# These are for juju, adding a cloud matching the customer/reproducer we need
add_cloud() {
if ! [ -x "$(command -v juju)" ]; then
sudo snap install juju --channel "$juju_version"
fi
rand_uuid=$(uuid -F siv)
cloud_name="$1"
maas_api_key=$(<~/.maas-api.key)
if ! [ -x "$(command -v juju)" ]; then
sudo snap install juju --channel "$juju_version"
fi
rand_uuid=$(uuid -F siv)
cloud_name="$1"
maas_api_key=$(<~/.maas-api.key)
cat > clouds-"$rand_uuid".yaml <<EOF
clouds:
@ -234,7 +252,7 @@ EOF
# Since we created ephemeral files, let's wipe them out. Comment if you want to keep them around
if [[ $? = 0 ]]; then
rm -f clouds-"$rand_uuid".yaml credentials-"$rand_uuid".yaml config-"$rand_uuid".yaml
rm -f clouds-"$rand_uuid".yaml credentials-"$rand_uuid".yaml config-"$rand_uuid".yaml
fi
juju enable-ha
@ -281,54 +299,54 @@ no_proxy="localhost,127.0.0.1,$maas_system_ip,$(echo $maas_ip_range.{100..200} |
while getopts ":a:bc:ij:nt:r" opt; do
case $opt in
a )
check_bins
remove_maas
install_maas
build_maas
bootstrap_maas
add_cloud "$OPTARG"
;;
check_bins
remove_maas
install_maas
build_maas
bootstrap_maas
add_cloud "$OPTARG"
;;
b )
echo "Building out a new MAAS server"
check_bins
install_maas
build_maas
bootstrap_maas
exit 0
;;
echo "Building out a new MAAS server"
check_bins
install_maas
build_maas
bootstrap_maas
exit 0
;;
c )
check_bins maas
init_variables
add_cloud "$OPTARG"
;;
check_bins maas
init_variables
add_cloud "$OPTARG"
;;
i )
echo "Installing MAAS and PostgreSQL dependencies"
install_maas
exit 0
;;
echo "Installing MAAS and PostgreSQL dependencies"
install_maas
exit 0
;;
j )
echo "Bootstrapping Juju controller $OPTARG"
add_cloud "$OPTARG"
exit 0
;;
echo "Bootstrapping Juju controller $OPTARG"
add_cloud "$OPTARG"
exit 0
;;
r )
remove_maas
exit 0
;;
remove_maas
exit 0
;;
t )
destroy_cloud "$OPTARG"
exit 0
;;
\? )
printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2
show_help
exit 1
;;
destroy_cloud "$OPTARG"
exit 0
;;
\? )
printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2
show_help
exit 1
;;
: )
printf "Option -%s needs an argument.\n" "$OPTARG" >&2
show_help
echo ""
exit 1
printf "Option -%s needs an argument.\n" "$OPTARG" >&2
show_help
echo ""
exit 1
;;
esac
done

View File

@ -1,5 +1,9 @@
qemu_connection="qemu+ssh://virt@10.0.1.252/system"
hypervisor_ip=10.0.1.252
hypervisor_mac="e0:07:1b:ff:27:48"
qemu_connection="qemu+ssh://virsh@${hypervisor_ip}/system"
qemu_password="5b5Bnev4kh3QeDGQ4KK8bVtX"
storage_path="/var/lib/vz/maas"
compute="hp1-maas-node"

205
manage-hypervisor-nodes.sh Executable file
View File

@ -0,0 +1,205 @@
#!/bin/bash
# set -x
. default.config
. maas.config
. hypervisor.config
# how long you want to wait for commissioning
# default is 1200, i.e. 20 mins
commission_timeout=1200
# Time between building VMs
build_fanout=60
# 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 virtinst
# 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
maas_system_id()
{
node_name=$1
maas ${maas_profile} machines read hostname=${node_name} | jq ".[].system_id" | sed s/\"//g
}
maas_pod_id()
{
node_name=$1
maas ${maas_profile} pods read | jq ".[] | {pod_id:.id, hyp_name:.name}" --compact-output | \
grep ${node_name} | jq ".pod_id" | sed s/\"//g
}
# Adds the VM into MAAS
maas_add_node()
{
node_name=$1
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=manual
# 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
if [[ $(maas ${maas_profile} tag read ${node_type}) == "Not Found" ]] ; then
maas ${maas_profile} tags create name=${node_type}
fi
# 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_assign_networks ${system_id}
}
# 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)
if [[ $i -eq 0 ]] ; then
vlan_int_id=${phys_int_id}
mode="STATIC"
ip_addr="ip_address=$hypervisor_ip"
else
vlan_int=$(maas ${maas_profile} interfaces create-vlan ${system_id} vlan=${maas_vlan_id} parent=$phys_int_id)
vlan_int_id=$(echo $vlan_int | jq .id | sed s/\"//g)
if [[ $vlan -eq $external_vlan ]] ; then
mode="DHCP"
else
mode="AUTO"
fi
ip_addr=""
fi
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)
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
}
# 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
time_start=$(date +%s)
time_end=${time_start}
status_name=$(maas ${maas_profile} machine read ${system_id} | jq ".status_name" | sed s/\"//g)
while [[ ${status_name} != "Ready" ]] && [[ $( echo ${time_end} - ${time_start} | bc ) -le ${commission_timeout} ]]
do
sleep 20
status_name=$(maas ${maas_profile} machine read ${system_id} | jq ".status_name" | sed s/\"//g)
time_end=$(date +%s)
done
}
# Calls the functions that destroys and cleans up all the VMs
wipe_node() {
maas_login
destroy_node
}
create_node() {
maas_login
maas_add_node ${hypervisor_name} ${hypervisor_mac} physical
}
install_node() {
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} install_kvm=true user_data="$(base64 user-data.yaml)"
maas ${maas_profile} machine deploy ${system_id} user_data="$(base64 user-data.yaml)"
# TODO: keep trying, until it gives a valid output
#until $(maas ${maas_profile} machine deploy ${system_id} install_kvm=true) ; do
# machine ${maas_profile} machine release ${system_id}
}
show_help() {
echo "
-c Creates Hypervisor
-w Removes Hypervisor
-i Install/Deploy Hypervisor
-a Create and Deploy
"
}
while getopts ":cwdi" opt; do
case $opt in
c)
create_node
;;
w)
wipe_node
;;
i)
install_node
;;
a)
create_node
install_node
;;
\?)
printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2
show_help
exit 1
;;
esac
done

View File

@ -12,6 +12,7 @@ maas_upstream_dns="1.1.1.1 4.4.4.4 8.8.8.8"
maas_user="maas"
maas_version="2.4"
maas_api_key="z9cT7jE3BhmxcXkWWN:ew5WW9QdDMg8TXVnjt:NtKgJdfgA5FVw2YT9CnaKU87wJ5fTxKa"
maas_pkg_type="snap"
no_proxy="localhost,127.0.0.1,$maas_system_ip"
squid_proxy="http://192.168.100.10:3128"
virsh_user="ubuntu"

47
user-data.yaml Normal file
View File

@ -0,0 +1,47 @@
#cloud-config
ssh_import_id:
- lp:arif-ali
## Update apt database and upgrade packages on first boot
package_update: true
package_upgrade: true
users:
- default
- name: virsh
primary_group: virsh
groups: [ libvirt, libvirt-qemu ]
passwd: $6$SVOxUrhz9mNyscUJ$hKF0RMY1nkGC3BpiozpaznE3AWerd8Ac8AlV9YEpLx50bLw5zweFCuTEEdS04McJNlaIqA.E4HiPuaIYGMzlH/
## Install additional packages on first boot
packages:
- virtinst
- bc
- jq
- qemu-kvm
- libvirt-bin
- qemu-efi
## Write arbitrary files to the file-system (including binaries!)
write_files:
- path: /root/initial_setup.sh
content: |
#!/bin/bash
sed "s/^\(PasswordAuthentication\).*/\1 yes/g" /etc/ssh/sshd_config
systemctl restart sshd
git clone https://github.com/arif-ali/maas-autobuilder.git /root/maas-autobuilder
cd /root/maas-autobuilder
ln -sf ${HOSTNAME}.config hypervisor.config
chmod +x manage-maas-nodes.sh
./manage-maas-nodes.sh -c
permissions: '0755'
owner: root:root
# Runs any command that we need to run post install
runcmd:
- [ "/root/initial_setup.sh" ]
- [ "efibootmgr", "-n", "0001"]