Skip to content

Instantly share code, notes, and snippets.

@dmccue
Created March 18, 2014 09:51
Show Gist options
  • Save dmccue/9616912 to your computer and use it in GitHub Desktop.
Save dmccue/9616912 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Author: David McCue
import keystoneclient.v2_0.client as ksclient
import glanceclient.v1.client as glclient
import os, sys
def get_keystone_creds():
d = {}
d['username'] = os.environ['OS_USERNAME']
d['password'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['tenant_name'] = os.environ['OS_TENANT_NAME']
return d
image_filename = sys.argv[1]
image_name = 'auto_' + image_filename.split('/')[-1:][0].split('.')[0]
if not os.path.isfile(image_filename):
print 'Usage: Please specify existing filename as argument 1'
sys.exit(1)
creds = get_keystone_creds()
keystone = ksclient.Client(**creds)
glance_endpoint = keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
glance_endpoint = '/'.join(glance_endpoint.split('/')[:-1])
glance = glclient.Client(glance_endpoint, token=keystone.auth_token)
image_cache = glance.images.list()
image_match = None
for image in image_cache:
if image.name == image_name:
image_match = image
image.delete()
if image_match:
print 'Updating: ' + image_name
else:
print 'Creating: ' + image_name
try:
with open(image_filename) as fimage:
image = glance.images.create(name=image_name, is_public="True", disk_format="qcow2", container_format="bare", data=fimage)
except:
print 'Unknown error!'
sys.exit(1)
if image.status == 'active':
print 'Success!'
else:
print 'Unknown error!'
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment