maas-autobuilder/functions.sh
Arif Ali 098be2f389
Many update
* Move all configs into config directory
* Move all examples into example directoy
* Move all common functions into functions.sh
* Fine tune a few other functions
* Moved common hypervisor config away
* Added examples for some of the hypervisor configs
* Start work on snap enablement of boostrap-maas.sh script
2020-12-26 20:03:28 +00:00

109 lines
3.1 KiB
Bash

#!/bin/bash
# how long you want to wait for commissioning
# default is 1200, i.e. 20 mins
state_timeout=1200
install_deps()
{
# 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=${maas_version}/stable
}
# 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()
{
# 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
}
# Based on the nodename, finds the pod id, if it exists
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
}
machine_add_tag()
{
system_id=$1
tag=$2
# If the tag doesn't exist, then create it
if [[ $(maas ${maas_profile} tag read ${tag}) == "Not Found" ]] ; then
maas ${maas_profile} tags create name=${tag}
fi
# Assign the tag to the machine
maas ${maas_profile} tag update-nodes ${tag} add=${system_id}
}
# This takes the system_id, and ensures that the machine is in $state state
# You may want to tweak the commission_timeout above in somehow it's failing
# and needs to be done quicker
ensure_machine_in_state()
{
system_id=$1
state=$2
# TODO: add a $3 to be able to customise the timeout
# timout= if [[ $3 == "" ]] ; then state_timeout else $3 ; fi
timeout=${state_timeout}
# The epoch time when this part started
time_start=$(date +%s)
# variable that will be used to check against for the timeout
time_end=${time_start}
# The initial state of the system
status_name=$(maas ${maas_profile} machine read ${system_id} | jq ".status_name" | sed s/\"//g)
# We will continue to check the state of the machine to see if it is in
# $state or the timeout has occured, which defaults to 20 mins
while [[ ${status_name} != "${state}" ]] && [[ $( echo ${time_end} - ${time_start} | bc ) -le ${timeout} ]]
do
# Check every 20 seconds of the state
sleep 20
# Grab the current state
status_name=$(maas ${maas_profile} machine read ${system_id} | jq ".status_name" | sed s/\"//g)
# Grab the current time to compare against
time_end=$(date +%s)
done
}
read_config()
{
configs=""
configs+=" configs/default.config"
configs+=" configs/maas.config"
configs+=" configs/hypervisor.common.config"
configs+=" configs/hypervisor.config"
for config in $configs ; do
if [ ! -f $config ]; then
printf "Error: missing config file. Please create the file '$config'.\n"
exit 1
else
shopt -s extglob
source "$config"
fi
done
}