Skip to content

Instantly share code, notes, and snippets.

@martinb3
Created January 30, 2014 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinb3/8714823 to your computer and use it in GitHub Desktop.
Save martinb3/8714823 to your computer and use it in GitHub Desktop.
Clean up orphaned cloudfiles backups, from servers that no longer exist
#!/usr/bin/python
import pyrax
import subprocess
import time
import sys
username = sys.argv[1] # username
ddi = sys.argv[2] # ddi
api_key = sys.argv[3] # api key
# workaround to skip needing a ~/.pyrax
cls = pyrax.utils.import_class('pyrax.identity.rax_identity.RaxIdentity')
pyrax.identity = cls()
# Using direct method
pyrax.set_credentials(username, api_key)
# regions, servers, and nova API versions we care about
lServers = []
lRegions = ['IAD','DFW','ORD','SYD','HKG']
lVersions = ['v1.0', 'v2.0']
# can't use pyrax for 1st gen, so shell out :-(
# this is a very ugly way to do this, but it supports 1st gen.
cmd = "nova --os-auth-url https://identity.api.rackspacecloud.com/%s/ --os-username %s --os-password %s --os-tenant-id %s --os-auth-system rackspace --os-region-name %s list | grep ACTIVE | awk '{print $2}' | xargs echo "
for v in lVersions:
for r in lRegions:
result = subprocess.Popen(['/bin/bash', '-c', cmd % (v, username, api_key, ddi, r)], stdout=subprocess.PIPE)
output = result.stdout.read()
serverIds = output.split()
for sid in serverIds:
lServers.append(sid)
# init cloudfiles object
cf = pyrax.cloudfiles
# find the container that keeps backups
cont = cf.get_container("cloudservers")
# search the container
lFilesToDelete = []
objects = cont.get_objects()
for o in objects:
if "cloudserver" in o.name:
veto = False
# if any of our server IDs are inside the candidate filename,
# veto the file - don't delete
for candidate_match in lServers:
if candidate_match in o.name:
veto = True
if not veto:
lFilesToDelete.append(o)
for f in lFilesToDelete:
fname = f.name
# skip files that don't get weekly or daily monikers, prob. not automatic RCBU
if ('weekly' in f.name) or ('daily' in f.name):
print fname # write the file, then try to delete it
start = time.time()
tries = 0
cont.delete_object(fname) # this can take a long time, even before the loop
while f:
try:
f = cont.get_object(fname)
if(tries > 4):
print "...giving up, after %4.2f seconds..." % (time.time() - start)
f = None
else:
print "...still there, tries = %s..." % (tries)
tries = tries + 1
time.sleep(0.5)
except Exception:
f = None
print "Object '%s' has been deleted, took %4.2f seconds" % (fname, time.time() - start)
else:
print "Not deleting %s" % f.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment