#!/bin/bash # # A tool to wipe the by-products of a cluster bringup from a bootstrap # node but leave the bootstrap node and related roles, nodes, clients # etc undamaged # # Usage cluster_nuke.sh # if [[ -z "$1" ]] { echo "Usage : $0 'environment'" echo "did nothing..." exit } setglobal ENVIRONMENT = $1 if [[ ! -f "cluster.txt" ]] { echo "I am a bear of very little brain so I couldn't find the cluster.txt file. No clusters were harmed in the making of this error message" exit } if [[ ! -f "environments/${ENVIRONMENT}.json" ]] { echo "You are scaring me now, I don't see the environment file you asked for. Not doing nothing" exit } while read HOSTNAME MACADDR IPADDR ILOIPADDR DOMAIN ROLE { if [[ "$ROLE" = "bootstrap" ]] { setglobal BOOTSTRAP_NODE = $HOSTNAME setglobal BCPC_HYPERVISOR_DOMAIN = $DOMAIN echo "Ignoring $HOSTNAME - bootstrap node to be retained" } elif [[ "$ROLE" = "work" || "$ROLE" = "head" ]] { # # Build a list of entries to remove from the databag. # # Extend the above test if there are other roles that cluster # members can have setglobal CLUSTER_MEMBERS = ""$(CLUSTER_MEMBERS) $(HOSTNAME)"" } else { echo "Ignoring $HOSTNAME - unknown role" } } < cluster.txt if [[ -z "$BOOTSTRAP_NODE" ]] { echo "No bootstrap node found, unsafe to continue. Are you nuts?" exit } # cross check that what I think is the bootstrap node for this domain # based on the cluster definition file cluster appears correctly as a # node in the databag setglobal BOOT_FQDN = ""$(BOOTSTRAP_NODE).$(BCPC_HYPERVISOR_DOMAIN)"" echo "BOOT FQDN = $(BOOT_FQDN)" setglobal MATCH = $[knife node list | grep $BOOT_FQDN] if [[ ! -z "$MATCH" ]] { echo "Plausible level of consistency present : it looks to me like I should delete all nodes and clients except $MATCH..." } else { echo "$BOOT_FQDN not found in data bag, unsafe to continue. Can't fool me that way!" exit } echo "Making safety copy of $(ENVIRONMENT) configs..." setglobal DATABAG_BACKUP = ""~/$ENVIRONMENT.databag.$Pid"" knife data bag show configs $(ENVIRONMENT) >> $(DATABAG_BACKUP) echo "Databag configs for $(ENVIRONMENT) dumped to $(DATABAG_BACKUP)" echo "Deleting data bag configs..." knife data bag delete --yes configs echo "Removing clients and nodes..." for CLIENT in [$CLUSTER_MEMBERS] { knife client delete --yes "$(CLIENT).$(BCPC_HYPERVISOR_DOMAIN)" knife node delete --yes "$(CLIENT).$(BCPC_HYPERVISOR_DOMAIN)" } echo "reload knife data..." knife role from file roles/*.json knife cookbook upload -a -o cookbooks knife environment from file "environments/$(ENVIRONMENT).json" echo "Removing cobbler systems..." for CLIENT in [$(CLUSTER_MEMBERS)] { setglobal SOMETHING = $(CLIENT) sudo cobbler system remove --name=$(CLIENT) } if [[ ! -z "$SOMETHING" ]] { echo "Sync cobbler..." sudo cobbler sync } echo "Removing cobbler profile..." sudo cobbler profile remove --name=bcpc_host sudo cobbler sync # Prevent stale kickstart being installed echo "Hiding stale kickstart in case Chef goes all wibbly..." setglobal KICKSTART_FILE = '"/var/lib/cobbler/kickstarts/bcpc_ubuntu_host.preseed'" sudo mv $(KICKSTART_FILE) "$(KICKSTART_FILE).old.$Pid" echo "Rerunning Chef client ..." sudo chef-client sudo chef-client sudo chef-client echo "Checking results ..." setglobal CHECK = $[knife data bag show configs $(ENVIRONMENT) | grep -i cobbler-root] if [[ -z "$CHECK" ]] { echo "Warning, no cobbler-root found. I'm so sorry." exit } else { echo "Cobbler-root found : $(CHECK)" } echo "Verifying kickstarts..." setglobal CHECK = $[sudo cobbler profile dumpvars --name=bcpc_host | grep kickstart | grep bcpc] if [[ -z "$CHECK" ]] { echo "Warning, no BCPC kickstart data found. Duh!" exit } else { echo "Found BCPC kickstart : $(CHECK)" } echo "Succesfully took off and nuked the site from orbit. It's the only way to be sure."