Last active
November 17, 2015 21:55
-
-
Save hassenius/47982907a8f61c3588e1 to your computer and use it in GitHub Desktop.
OpenStack Project Janitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################# | |
## janitor.sh | |
## ©Copyright IBM Corporation 2014. | |
## Brought to life by hans.moen@ie.ibm.com | |
## LICENSE: MIT (http://opensource.org/licenses/MIT) | |
################################################################################# | |
################################################################################ | |
## | |
## janitor.sh -- employed to clean up other peoples mess | |
## | |
## janitor.sh expects to be run at regular interval, for example by cron | |
## Each time it is run it will save a list of instances and volumes | |
## This list is compared with the last list, and instances and volumes that | |
## are present in both are deemed old enough to be forcefully retired | |
## | |
## Dependencies: | |
## python-openstackclient | |
## dos2unix | |
## comm | |
## bash | |
## | |
## cron example to run every second day: 0 0 */2 * * /path/to/janitor.sh | |
AUTH_FILE=/home/ibmcloud/demosjanitor.rc | |
LOG_FILE=/var/log/demos-janitor.log | |
CUR_VM_LIST=/var/local/icos-demo-janitor/janitor-vm-list.current | |
LAST_VM_LIST=/var/local/icos-demo-janitor/janitor-vm-list.last | |
CUR_VOLUME_LIST=/var/local/icos-demo-janitor/janitor-volume-list.current | |
LAST_VOLUME_LIST=/var/local/icos-demo-janitor/janitor-volume-list.last | |
. $AUTH_FILE | |
# Get current list of VMs | |
/usr/local/bin/openstack -q server list -f csv -c ID --quote none > $CUR_VM_LIST | |
# Clean up the file | |
dos2unix $CUR_VM_LIST | |
# Compare current list of VMs with previous pull | |
for instance in `comm -1 -2 --nocheck-order $CUR_VM_LIST $LAST_VM_LIST` ; do | |
# Delete the VMs that were there in previous pull | |
if [ ${#instance} -lt 4 ] ; then continue ; fi | |
echo ${#instance} | |
echo "$(date): Deleting instance ID: $instance" >> $LOG_FILE | |
/usr/local/bin/openstack -q server show $instance -f shell -c name -c id -c user_id -c created >> $LOG_FILE | |
/usr/local/bin/openstack server delete $instance >> $LOG_FILE | |
echo "---------------------------" >> $LOG_FILE | |
done | |
# We probably need to sleep for a few minutes to complete all detachments | |
sleep 5m | |
# Get current list of volumes | |
/usr/local/bin/openstack -q volume list -f csv -c ID --quote none > $CUR_VOLUME_LIST | |
# Clean up the file | |
dos2unix $CUR_VOLUME_LIST | |
# Compare current list of volumes with previous pull | |
for volume in `comm -1 -2 --nocheck-order $CUR_VOLUME_LIST $LAST_VOLUME_LIST` ; do | |
# Delete the volumes that were there in previous pull | |
if [ ${#volume} -lt 4 ] ; then continue ; fi | |
echo "$(date): Deleting volume ID: $volume" >> $LOG_FILE | |
/usr/local/bin/openstack -q volume show $volume -f shell -c name -c id -c user_id -c created >> $LOG_FILE | |
/usr/local/bin/openstack volume delete $volume >> $LOG_FILE | |
echo "---------------------------" >> $LOG_FILE | |
done | |
# Rotate the files to be ready for next run | |
mv $LAST_VM_LIST $LAST_VM_LIST.$(date -u -Ihours) | |
mv $CUR_VM_LIST $LAST_VM_LIST | |
mv $LAST_VOLUME_LIST $LAST_VOLUME_LIST.$(date -u -Ihours) | |
mv $CUR_VOLUME_LIST $LAST_VOLUME_LIST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment