Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created December 3, 2022 01:31
Show Gist options
  • Save ThinGuy/198daea9fd0253e97e71bbcb46092909 to your computer and use it in GitHub Desktop.
Save ThinGuy/198daea9fd0253e97e71bbcb46092909 to your computer and use it in GitHub Desktop.
Rename machines based on BMC Info
# The following are bash functions. Either source this file (i.e. source ~/.node-rename.sh) or copy and paste the stanza directly in your terminal
# If you're using normal postgresq
maas-rename-new-machines-prod-db() {
sudo -u postgres psql -F"|" --no-align -P pager=off -t maasdb -c "\
SELECT sip.ip,node.hostname,node.system_id FROM maasserver_bmc bmc \
LEFT OUTER JOIN maasserver_staticipaddress sip ON sip.id = bmc.ip_address_id \
LEFT OUTER JOIN maasserver_node node ON bmc.id = node.bmc_id \
WHERE bmc.ip_address_id IS NOT NULL and power_type LIKE 'ipmi%' ORDER BY sip.ip"|awk -F\| '{print $1"|"$3}'|\
xargs -n1 -P0 bash -c 'IP=${0%%|*};ID=${0##*|};NEWNAME=$(printf "faux-node-%02d\n" ${IP##*.});maas '${MAAS_PROFILE}' machine update $ID hostname=$NEWNAME'
};export -f maas-rename-new-machines-prod-db
# If you're using the maas-test-db snap
maas-rename-new-machines-test-db() {
sudo -iu root psql -F"|" --no-align -P pager=off -U postgres -h /var/snap/maas-test-db/common/postgres/sockets -d maasdb -c "\
SELECT sip.ip,node.hostname,node.system_id FROM maasserver_bmc bmc \
LEFT OUTER JOIN maasserver_staticipaddress sip ON sip.id = bmc.ip_address_id \
LEFT OUTER JOIN maasserver_node node ON bmc.id = node.bmc_id \
WHERE bmc.ip_address_id IS NOT NULL and power_type LIKE 'ipmi%' ORDER BY sip.ip"|awk -F\| '{print $1"|"$3}'|\
xargs -n1 -P0 bash -c 'IP=${0%%|*};ID=${0##*|};NEWNAME=$(printf "faux-node-%02d\n" ${IP##*.});maas '${MAAS_PROFILE}' machine update $ID hostname=$NEWNAME'
};export -f maas-rename-new-machines-test-db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment