Created
December 7, 2010 16:20
-
-
Save chmouel/731985 to your computer and use it in GitHub Desktop.
Upload images to swift and insert it in glance DB
This file contains hidden or 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
__author__ = "Chmouel Boudjnah <[email protected]>" | |
import os | |
import cloudfiles | |
from glance.parallax import db | |
USERNAME = "c" # fill these out for testing | |
API_KEY = "" | |
AUTH_URL = "https://auth.api.rackspacecloud.com/v1.0" | |
CONTAINER = "openstack_images" | |
FILES = [("/var/lib/nova/images/aki-lucid/image", "aki", "kernel"), | |
("/var/lib/nova/images/ari-lucid/image", "ari", "ramdisk"), | |
("/var/lib/nova/images/ami-tiny/image", "ami", "machine"), | |
] | |
cf_auth = cloudfiles.authentication.Authentication(USERNAME, | |
API_KEY, | |
AUTH_URL) | |
cf_cnx = cloudfiles.get_connection(USERNAME, API_KEY, auth=cf_auth) | |
cnt_ptr = cf_cnx.create_container(CONTAINER) | |
for (filename, obj_name, image_type) in FILES: | |
if not os.path.exists(filename): | |
continue | |
size = os.path.getsize(filename) | |
try: | |
obj = cnt_ptr.get_object(obj_name) | |
except(cloudfiles.errors.NoSuchObject): | |
print "Uploading %s to swift." % (obj_name) | |
w_obj = cnt_ptr.create_object(obj_name) | |
w_obj.write(open(filename)) | |
location = "swift://%s:%s@%s/v1.0/%s/%s" % (USERNAME, API_KEY, AUTH_URL, CONTAINER, obj_name) | |
image = db.image_create( | |
None, | |
dict(name=obj_name, | |
state="available", | |
is_public=True, | |
image_type=image_type) | |
) | |
db.image_file_create(None, | |
dict(image_id=image.id, | |
location=location, | |
size=size)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment