Sometimes, especially during work in lab environments we need to stop all our instances ie. to avoid additional charges from cloud operator. Openstack offers a convenient way to do it – shelving. When you shelve an instance all machine’s volumes are saved but VM instance does not utilizing compute host resources. This state persists until you unshelve a specified machine. Openstack does not offer option to shelve/unshelve of all your instances at once, you need to do it on every machine separately. This simple bash script gives you an ability to shelve or unshelve all machines at once.
#!/bin/bash # parse command line options if [[ $# -ne 1 ]] || ([[ ! "$1" == "shelve" ]] && [[ ! "$1" == "unshelve" ]]); then echo -e "\nUSAGE: $0 <shelve|unshelve>" exit 1 fi mode=$1 echo "Current machines states:" openstack server list echo -e "\n\n" echo "Changing machines states to ${mode}d..." while read line; do machine_id=$(echo $line | awk -F"," '{print $1}' | sed 's/\"//g') machine_name=$(echo $line | awk -F"," '{print $2}' | sed 's/\"//g') echo -e "${mode}: $machine_id\t$machine_name..." openstack server $mode $machine_id done <<< "$(openstack server list --format csv | grep -vE 'ID|NAME|STATUS')" echo "Waiting 120s for machines states update..." sleep 120 echo "Current machines states:" openstack server list
This script uses openstack command and it needs a proper OS_* environmental variables to be set. If your openstack command works fine there is no need to do any additional configuration to use a script.