diff options
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/.gitignore | 1 | ||||
| -rw-r--r-- | libs/__init__.py | 1 | ||||
| -rw-r--r-- | libs/environment.py | 53 | ||||
| -rw-r--r-- | libs/glance.py | 38 | ||||
| -rw-r--r-- | libs/keystone.py | 39 |
5 files changed, 132 insertions, 0 deletions
diff --git a/libs/.gitignore b/libs/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/libs/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/libs/__init__.py b/libs/__init__.py new file mode 100644 index 0000000..609997c --- /dev/null +++ b/libs/__init__.py @@ -0,0 +1 @@ +__all__ = ["environment", "keystone", "glance"] diff --git a/libs/environment.py b/libs/environment.py new file mode 100644 index 0000000..f78f3d1 --- /dev/null +++ b/libs/environment.py @@ -0,0 +1,53 @@ +import sys +import os + + +class Environment(object): + def __init__(self, + os_auth_url, + os_project_name, + os_region, + os_username, + os_password, + os_project_domain_name, + os_user_domain_name, + os_identity_api_version, + os_image_api_version, + elements_path, + extra_envs={}): + if os_auth_url is None or \ + os_project_name is None or \ + os_region is None or \ + os_username is None or \ + os_password is None or \ + os_project_domain_name is None or \ + os_user_domain_name is None or \ + os_identity_api_version is None or \ + os_image_api_version is None or \ + elements_path is None: + print("All required environment arguments must have a value. You passed in:" + \ + "\nos_auth_url = " + str(os_auth_url) + \ + "\nos_project_name = " + str(os_project_name) + \ + "\nos_region = " + str(os_region) + \ + "\nos_username = " + str(os_username) + \ + "\nos_password = " + str(os_password) + \ + "\nos_project_domain_name = " + str(os_project_domain_name) + \ + "\nos_user_domain_name = " + str(os_user_domain_name) + \ + "\nos_identity_api_version = " + str(os_identity_api_version) + \ + "\nos_image_api_version = " + str(os_image_api_version) + \ + "\nelements_path = " + str(elements_path)) + sys.exit(1) + else: + self.os_auth_url = os_auth_url + self.os_project_name = os_project_name + self.os_region = os_region + self.os_username = os_username + self.os_password = os_password + self.os_project_domain_name = os_project_domain_name + self.os_user_domain_name = os_user_domain_name + self.os_identity_api_version = os_identity_api_version + self.os_image_api_version = os_image_api_version + self.elements_path = elements_path + self.extra_envs = {} + for key, value in [(k, v) for (k, v) in extra_envs.items() if v]: + self.extra_envs[key] = value 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 diff --git a/libs/keystone.py b/libs/keystone.py new file mode 100644 index 0000000..05c4d8c --- /dev/null +++ b/libs/keystone.py @@ -0,0 +1,39 @@ +from keystoneauth1.identity import v3 +from keystoneauth1 import session +from keystoneclient.v3 import client as keystoneclient + + +class Project(object): + def __init__(self, name, uuid): + self.name = name + self.uuid = uuid + + +class Keystone(object): + def __init__(self, env): + if env.os_identity_api_version != "3": + raise Exception("Keystone v3 is required. You are using version %s" % env.os_identity_api_version) + self.os_identity_api_version = env.os_identity_api_version + self.os_image_api_version = env.os_image_api_version + self.os_auth_url = env.os_auth_url.replace("v2.0", "v3") + + auth = v3.Password(username=env.os_username, + password=env.os_password, + project_domain_name=env.os_project_domain_name, + user_domain_name=env.os_user_domain_name, + project_name=env.os_project_name, + auth_url=self.os_auth_url) + self.session = session.Session(auth=auth) + self.client = keystoneclient.Client(session=self.session, + region_name=env.os_region) + + def get_projects(self): + return map(lambda x: Project(x.name, x.id), + [p for p in self.client.projects.list() if p.enabled]) + + def get_catalog_url(self, service_type=None, endpoint_type="publicURL"): + return self.session.get_endpoint(service_type=service_type, + endpoint_type=endpoint_type) + + def get_glance_url(self, endpoint_type="publicURL"): + return self.get_catalog_url(service_type="image", endpoint_type=endpoint_type) |
