Skip to content

Commit

Permalink
Merge pull request #4 from Pansanel/master
Browse files Browse the repository at this point in the history
Sync with development version
  • Loading branch information
Pansanel authored Jun 13, 2017
2 parents eae86fa + 696c445 commit c990831
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
65 changes: 39 additions & 26 deletions cloudkeeper_os/imagemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

CONF = cfg.CONF
LOG = log.getLogger(__name__)

APPLIANCE_INT_VALUES = ['ram', 'core', 'expiration_date']
IMAGE_ID_TAG = 'APPLIANCE_ID'
IMAGE_LIST_ID_TAG = 'APPLIANCE_IMAGE_LIST_ID'

Expand All @@ -52,19 +54,22 @@ def add_appliance(self, appliance):
sess = keystone_client.get_session(project_name=project_name)
glance = glanceclient.Client(session=sess)
LOG.info('Adding appliance: ' + appliance.title)
image = appliance.image
appliance.ClearField('image')
if image.mode == 'REMOTE':
LOG.debug("Image access mode: "
"%s" % appliance.image.Mode.Name(appliance.image.mode))
if appliance.image.Mode.Name(appliance.image.mode) == 'REMOTE':
LOG.debug("Downloading image from Cloudkeeper")
filename = CONF.tempdir + '/' + str(uuid.uuid4())
kwargs = {}
kwargs['uri'] = image.location
kwargs['uri'] = appliance.image.location
kwargs['filename'] = filename
if image.HasField('username') and image.HasField('password'):
kwargs['username'] = image.username
kwargs['password'] = image.password
utils.retrieve_image(**kwargs)
kwargs['username'] = appliance.image.username
kwargs['password'] = appliance.image.password
if not utils.retrieve_image(**kwargs):
return None
else:
filename = image.location
filename = appliance.image.location
image_format = appliance.image.Format.Name(appliance.image.format)
appliance.ClearField('image')
image_data = open(filename, 'rb')
properties = {}
for (descriptor, value) in appliance.ListFields():
Expand All @@ -79,13 +84,15 @@ def add_appliance(self, appliance):
LOG.debug("attribute value: %s" % value)
key = 'APPLIANCE_' + str.upper(descriptor.name)
properties[key] = str(value)
image_format = str.lower(image.Format.Name(image.format))
# Add property for cloud-info-provider compatibility
properties['vmcatcher_event_ad_mpuri'] = appliance.mpuri
LOG.debug("Create image %s (format: %s, "
"properties %s)" % (appliance.title, image_format,
"properties %s)" % (appliance.title,
str.lower(image_format),
properties)
)
glance_image = glance.images.create(name=appliance.title,
disk_format=image_format,
disk_format=str.lower(image_format),
container_format="bare"
)
glance.images.upload(glance_image.id, image_data)
Expand Down Expand Up @@ -132,9 +139,11 @@ def update_appliance(self, appliance):
data = dict(value)
value = json.dumps(data)
key = 'APPLIANCE_' + str.upper(descriptor.name)
properties[key] = str(value)
LOG.debug("The appliance properties are updated with"
"these values: %s" % (properties))
properties[key] = str(value)
# Add property for cloud-info-provider compatibility
properties['vmcatcher_event_ad_mpuri'] = appliance.mpuri
LOG.debug("Appliance properties updated with new "
"values: %s" % (properties))
glance.images.update(image_list[0]['id'], **properties)

def remove_appliance(self, appliance):
Expand Down Expand Up @@ -183,16 +192,20 @@ def update_image_list_identifiers(self, project_name=None):
else:
project_list = self.mapping.get_projects()
for project in project_list:
sess = keystone_client.get_session(project)
glance = glanceclient.Client(session=sess)
img_generator = glance.images.list()
image_list = list(img_generator)
for image in image_list:
if IMAGE_LIST_ID_TAG in image:
if image[IMAGE_LIST_ID_TAG] not in appliances:
appliances[image[IMAGE_LIST_ID_TAG]] = []
appliances[image[IMAGE_LIST_ID_TAG]].append(image)
self.appliances = appliances
try:
sess = keystone_client.get_session(project)
glance = glanceclient.Client(session=sess)
img_generator = glance.images.list()
image_list = list(img_generator)
for image in image_list:
if IMAGE_LIST_ID_TAG in image:
if image[IMAGE_LIST_ID_TAG] not in appliances:
appliances[image[IMAGE_LIST_ID_TAG]] = []
appliances[image[IMAGE_LIST_ID_TAG]].append(image)
except Exception:
LOG.error("Not authorized to manage images from the "
"project: %s" % project)
self.appliances = appliances

def get_appliances(self, image_list_identifier):
"""Return all appliances with a given image_list_identifier
Expand All @@ -209,7 +222,7 @@ def get_appliances(self, image_list_identifier):
else:
key = 'APPLIANCE_'+str.upper(field)
if key in image:
if field == 'expiration_date':
if field in APPLIANCE_INT_VALUES:
properties[field] = long(image[key])
elif field == 'attributes':
properties[field] = json.loads(image[key])
Expand Down
22 changes: 15 additions & 7 deletions cloudkeeper_os/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@
import requests
from requests.auth import HTTPBasicAuth

def retrieve_image(uri, filename, username=None, password=None, capath=None):
from oslo_log import log

LOG = log.getLogger(__name__)

def retrieve_image(uri, filename, username='', password='', capath=None):
"""Retrieve an image
"""
# TODO manage SSL case
if username and password:
auth = HTTPBasicAuth(username, password)
response = requests.get(uri, auth=auth, stream=True)
else:
response = requests.get(uri, stream=True)
LOG.info("Download image from %s" % uri)
auth = HTTPBasicAuth(username, password)
response = requests.get(uri, auth=auth, stream=True)
LOG.debug("Response: %s when accessing the image." % str(response.status_code))
if response.status_code == 200:
output = open(filename, 'wb')
# response.raw.decode_content = True
shutil.copyfileobj(response.raw, output)
output.close()
LOG.debug("Image data successfully saved to %s" % filename)
return True
else:
# TODO raise an exception
LOG.error("Failed to download image data due to HTTP error")
return False

0 comments on commit c990831

Please sign in to comment.