2012-02-04 17:40:31 -06:00
|
|
|
# Copyright 2012 United States Government as represented by the
|
2011-10-31 11:31:05 -07:00
|
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
2012-02-04 17:40:31 -06:00
|
|
|
# Copyright 2012 Nebula, Inc.
|
2011-10-31 11:31:05 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-02-19 18:38:45 +11:00
|
|
|
import itertools
|
2011-10-31 11:31:05 -07:00
|
|
|
import logging
|
|
|
|
|
2014-01-03 17:31:49 +01:00
|
|
|
from django.conf import settings
|
2012-07-31 13:28:48 -07:00
|
|
|
import glanceclient as glance_client
|
2014-07-29 16:57:39 +02:00
|
|
|
from six.moves import _thread as thread
|
2012-03-11 18:37:18 -07:00
|
|
|
|
2013-11-14 15:01:24 -07:00
|
|
|
from horizon.utils import functions as utils
|
2013-08-02 13:23:46 +04:00
|
|
|
from openstack_dashboard.api import base
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-01-29 08:30:19 -06:00
|
|
|
class ImageCustomProperty(object):
|
|
|
|
def __init__(self, image_id, key, val):
|
|
|
|
self.image_id = image_id
|
|
|
|
self.id = key
|
|
|
|
self.key = key
|
|
|
|
self.value = val
|
|
|
|
|
|
|
|
|
|
|
|
def glanceclient(request, version='1'):
|
2014-03-14 08:20:19 -06:00
|
|
|
url = base.url_for(request, 'image')
|
2012-07-17 12:35:49 +02:00
|
|
|
insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
|
2013-08-28 13:21:19 +02:00
|
|
|
cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
|
2012-04-13 21:46:04 -07:00
|
|
|
LOG.debug('glanceclient connection created using token "%s" and url "%s"'
|
|
|
|
% (request.user.token.id, url))
|
2014-01-29 08:30:19 -06:00
|
|
|
return glance_client.Client(version, url, token=request.user.token.id,
|
2013-08-28 13:21:19 +02:00
|
|
|
insecure=insecure, cacert=cacert)
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
def image_delete(request, image_id):
|
2012-04-12 17:13:56 -07:00
|
|
|
return glanceclient(request).images.delete(image_id)
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
def image_get(request, image_id):
|
2013-11-01 14:02:41 -04:00
|
|
|
"""Returns an Image object populated with metadata for image
|
Fixes LP Bug#862664 - Improper calls to get_image
The glance.client.Client.get_image() call returns a
tuple of (metadata, image_iterator). Unfortunately,
Horizon's glance API calls get_image() when it means
to call get_image_meta(). Because the call to get_image()
simply ignores the image iterator returned from get_image(),
when the image iterator is garbage-collected, this causes
the connection to Glance to be closed, however by that time
the socket bound to the iterator has been switched out by
eventlet. The result is lots of these in the Glance API log:
2011-09-28 17:46:12 DEBUG [glance.store.filesystem] Found image at /opt/stack/glance/images/3. Returning in ChunkedFile.
2011-09-28 17:46:12 DEBUG [eventlet.wsgi.server] Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/eventlet/wsgi.py", line 351, in handle_one_response
write(''.join(towrite))
File "/usr/lib/pymodules/python2.7/eventlet/wsgi.py", line 301, in write
_writelines(towrite)
File "/usr/lib/python2.7/socket.py", line 334, in writelines
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
File "/usr/lib/pymodules/python2.7/eventlet/greenio.py", line 283, in sendall
tail = self.send(data, flags)
File "/usr/lib/pymodules/python2.7/eventlet/greenio.py", line 269, in send
total_sent += fd.send(data[total_sent:], flags)
error: [Errno 104] Connection reset by peer
This patch fixes the improper calls to get_image() by replacing them
with appropriate calls to get_image_meta().
Change-Id: I741a207ba0e222820492aeb48bab9464d17539ab
2011-12-06 12:27:19 -05:00
|
|
|
with supplied identifier.
|
|
|
|
"""
|
2013-12-16 13:05:37 -05:00
|
|
|
image = glanceclient(request).images.get(image_id)
|
|
|
|
if not hasattr(image, 'name'):
|
|
|
|
image.name = None
|
|
|
|
return image
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
2014-01-29 08:30:19 -06:00
|
|
|
def image_get_properties(request, image_id, reserved=True):
|
|
|
|
"""List all custom properties of an image."""
|
|
|
|
image = glanceclient(request, '2').images.get(image_id)
|
|
|
|
reserved_props = getattr(settings, 'IMAGE_RESERVED_CUSTOM_PROPERTIES', [])
|
|
|
|
properties_list = []
|
|
|
|
for key in image.keys():
|
|
|
|
if reserved or key not in reserved_props:
|
|
|
|
prop = ImageCustomProperty(image_id, key, image.get(key))
|
|
|
|
properties_list.append(prop)
|
|
|
|
return properties_list
|
|
|
|
|
|
|
|
|
|
|
|
def image_get_property(request, image_id, key, reserved=True):
|
|
|
|
"""Get a custom property of an image."""
|
|
|
|
for prop in image_get_properties(request, image_id, reserved):
|
|
|
|
if prop.key == key:
|
|
|
|
return prop
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-04-28 16:42:43 -07:00
|
|
|
def image_list_detailed(request, marker=None, sort_dir='desc',
|
|
|
|
sort_key='created_at', filters=None, paginate=False):
|
2012-05-22 20:00:43 -07:00
|
|
|
limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
|
2013-11-14 15:01:24 -07:00
|
|
|
page_size = utils.get_page_size(request)
|
2013-02-19 18:38:45 +11:00
|
|
|
|
|
|
|
if paginate:
|
|
|
|
request_size = page_size + 1
|
|
|
|
else:
|
|
|
|
request_size = limit
|
|
|
|
|
2012-07-23 13:24:55 +03:00
|
|
|
kwargs = {'filters': filters or {}}
|
|
|
|
if marker:
|
|
|
|
kwargs['marker'] = marker
|
2014-04-28 16:42:43 -07:00
|
|
|
kwargs['sort_dir'] = sort_dir
|
|
|
|
kwargs['sort_key'] = sort_key
|
2013-02-19 18:38:45 +11:00
|
|
|
|
|
|
|
images_iter = glanceclient(request).images.list(page_size=request_size,
|
2012-07-23 13:24:55 +03:00
|
|
|
limit=limit,
|
2013-02-19 18:38:45 +11:00
|
|
|
**kwargs)
|
2014-04-28 16:42:43 -07:00
|
|
|
has_prev_data = False
|
2013-02-19 18:38:45 +11:00
|
|
|
has_more_data = False
|
|
|
|
if paginate:
|
|
|
|
images = list(itertools.islice(images_iter, request_size))
|
2014-04-28 16:42:43 -07:00
|
|
|
# first and middle page condition
|
2013-02-19 18:38:45 +11:00
|
|
|
if len(images) > page_size:
|
|
|
|
images.pop(-1)
|
|
|
|
has_more_data = True
|
2014-04-28 16:42:43 -07:00
|
|
|
# middle page condition
|
|
|
|
if marker is not None:
|
|
|
|
has_prev_data = True
|
|
|
|
# first page condition when reached via prev back
|
|
|
|
elif sort_dir == 'asc' and marker is not None:
|
|
|
|
has_more_data = True
|
|
|
|
# last page condition
|
|
|
|
elif marker is not None:
|
|
|
|
has_prev_data = True
|
2012-05-22 20:00:43 -07:00
|
|
|
else:
|
2013-02-19 18:38:45 +11:00
|
|
|
images = list(images_iter)
|
2014-04-28 16:42:43 -07:00
|
|
|
return (images, has_more_data, has_prev_data)
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
2012-04-12 17:13:56 -07:00
|
|
|
def image_update(request, image_id, **kwargs):
|
|
|
|
return glanceclient(request).images.update(image_id, **kwargs)
|
2011-10-31 11:31:05 -07:00
|
|
|
|
|
|
|
|
2012-06-04 20:54:19 -07:00
|
|
|
def image_create(request, **kwargs):
|
2014-05-22 18:27:42 -07:00
|
|
|
copy_from = kwargs.pop('copy_from', None)
|
|
|
|
data = kwargs.pop('data', None)
|
2012-06-04 20:54:19 -07:00
|
|
|
|
|
|
|
image = glanceclient(request).images.create(**kwargs)
|
|
|
|
|
2014-05-22 18:27:42 -07:00
|
|
|
if data:
|
|
|
|
thread.start_new_thread(image_update,
|
|
|
|
(request, image.id),
|
|
|
|
{'data': data,
|
|
|
|
'purge_props': False})
|
|
|
|
elif copy_from:
|
2012-06-04 20:54:19 -07:00
|
|
|
thread.start_new_thread(image_update,
|
|
|
|
(request, image.id),
|
2014-06-04 14:13:11 +08:00
|
|
|
{'copy_from': copy_from,
|
|
|
|
'purge_props': False})
|
2012-06-04 20:54:19 -07:00
|
|
|
|
|
|
|
return image
|
2014-01-29 08:30:19 -06:00
|
|
|
|
|
|
|
|
|
|
|
def image_update_properties(request, image_id, **kwargs):
|
|
|
|
"""Add or update a custom property of an image."""
|
|
|
|
return glanceclient(request, '2').images.update(image_id, None, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def image_delete_properties(request, image_id, keys):
|
|
|
|
"""Delete custom properties for an image."""
|
|
|
|
return glanceclient(request, '2').images.update(image_id, keys)
|