Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created December 3, 2022 01:10
Show Gist options
  • Save ThinGuy/f368a3c60e68af62b7e867eadb62a2b0 to your computer and use it in GitHub Desktop.
Save ThinGuy/f368a3c60e68af62b7e867eadb62a2b0 to your computer and use it in GitHub Desktop.
Create fake nodes in MAAS
#!/bin/bash
# Side loading example
# The following will create N fake nodes whose
# hostnames will be "fnode-${POWER_DRIVER}${LAST_IP_OCTET}
# And MAC address will be made up on the fly.
# For this example, ipmi and manual power drivers are supported.
export MAAS_LOCAL="http://127.0.0.1:5240/MAAS"
export MAAS_PROFILE="admin"
export FAKE_START="10"
export FAKE_COUNT="20"
export FAKE_IP_PREFIX="192.168.150"
export POWER_DRIVER=ipmi
#export POWER_DRIVER=manual
export FAKE_HOST_PREFIX="fnode-${POWER_DRIVER}"
export DEPLOYED=true
# This requires that you are logged into MAAS from the CLI
# Assuming you have ssh'd into the MAAS controller, run this command
# to login via the CLI
maas login ${MAAS_PROFILE} ${MAAS_LOCAL} $(sudo maas apikey --username ${MAAS_PROFILE})
# This just creates an array
declare -ag FAKE_IP_LIST=($(eval printf '${FAKE_IP_PREFIX}.%s\\n' {${FAKE_START}..$((${${FAKE_START}+${FAKE_COUNT}-1))}))
if [[ ${POWER_DRIVER,,} =~ man ]];then
for ((i=0; i<=$((${#FAKE_IP_LIST[@]}-1)); i++));do
printf '%s-%03d %s %s %s\n' ${FAKE_HOST_PREFIX} "${FAKE_IP_LIST[${i}]##*.}" "${FAKE_IP_LIST[${i}]}" "$(printf '52:54:%02x:%02x:%02x:%02x\n' $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})))";
done| xargs -rn3 -P0 bash -c 'maas ${MAAS_PROFILE} machines create commission=false hostname=${0} deployed=${DEPLOYED} architecture=amd64 mac_addresses=${2} power_type=manual description="Fake Node: Manual Power=${1}"';
elif [[ ${POWER_DRIVER,,} =~ ipmi ]];then
for ((i=0; i<=$((${#FAKE_IP_LIST[@]}-1)); i++));do
printf '%s-%03d %s %s %s\n' ${FAKE_HOST_PREFIX} "${FAKE_IP_LIST[${i}]##*.}" "${FAKE_IP_LIST[${i}]}" "$(printf '52:54:%02x:%02x:%02x:%02x\n' $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})) $((RANDOM%${FAKE_IP_LIST[${i}]%%.*})))";
done|xargs -rn3 -P0 bash -c 'maas ${MAAS_PROFILE} machines create commission=false hostname=${0} deployed=${DEPLOYED} architecture=amd64 mac_addresses=${2} power_parameters_power_address=${1} power_type=ipmi power_paramters_power_boot_type=efi power_parameters_power_user=maas power_parameters_power_pass="$(env LANG=C LC_ALL=C tr 2>/dev/null -dc "[:alnum:]" < /dev/urandom|fold -w12|head -n1)" description="Fake node for testing"';
fi
# Delete all the fake nodes
# maas ${MAAS_PROFILE} machines read|jq -r '.[]|select(.hostname|startswith("fnode")).system_id'|xargs -rn1 -P0 maas ${MAAS_PROFILE} machine delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment