From 4a1c4e8c3e6ca4ffbcd9ef38f07a1527226d7430 Mon Sep 17 00:00:00 2001 From: Carlos Konstanski Date: Thu, 26 Oct 2017 15:16:59 -0600 Subject: initial commit --- libs/glance.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 libs/glance.py (limited to 'libs/glance.py') diff --git a/libs/glance.py b/libs/glance.py new file mode 100644 index 0000000..b2a5c8d --- /dev/null +++ b/libs/glance.py @@ -0,0 +1,38 @@ +from __future__ import print_function +import datetime +import glanceclient +import json +import time +import traceback + + +class Glance(object): + def __init__(self, keystone, endpoint_type="publicURL"): + self.keystone = keystone + self.glance = glanceclient.Client(keystone.os_image_api_version, + endpoint=keystone.get_glance_url(endpoint_type=endpoint_type), + session=keystone.session) + + def find(self, image_name): + for image in self.glance.images.list(): + if image.name == image_name: + return image + return None + + def delete(self, image_name): + image = self.find(image_name) + if image: + self.glance.images.delete(image.id) + + def upload(self, image_name, image_type, file_path, tag): + self.delete(image_name) + image = self.glance.images.create(name=image_name) + if image.status != "queued": + raise Exception("Image did not enter queued state upon create.") + image = self.glance.images.update(image.id, disk_format=image_type) + image = self.glance.images.update(image.id, container_format="bare") + image = self.glance.images.update(image.id, visibility="private") + image = self.glance.images.update(image.id, tags=[str(tag)]) + with open(file_path, "rb") as myfile: + self.glance.images.upload(image.id, myfile) + return image.id -- cgit v1.3