107 lines
3.2 KiB
Python
Raw Normal View History

# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
# 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
import itertools
import logging
import thread
from django.conf import settings
import glanceclient as glance_client
from horizon.utils import functions as utils
from openstack_dashboard.api import base
LOG = logging.getLogger(__name__)
def glanceclient(request):
url = base.url_for(request, 'image')
insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
LOG.debug('glanceclient connection created using token "%s" and url "%s"'
% (request.user.token.id, url))
return glance_client.Client('1', url, token=request.user.token.id,
insecure=insecure, cacert=cacert)
def image_delete(request, image_id):
return glanceclient(request).images.delete(image_id)
def image_get(request, image_id):
"""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.
"""
image = glanceclient(request).images.get(image_id)
if not hasattr(image, 'name'):
image.name = None
return image
def image_list_detailed(request, marker=None, filters=None, paginate=False):
limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
page_size = utils.get_page_size(request)
if paginate:
request_size = page_size + 1
else:
request_size = limit
kwargs = {'filters': filters or {}}
if marker:
kwargs['marker'] = marker
images_iter = glanceclient(request).images.list(page_size=request_size,
limit=limit,
**kwargs)
has_more_data = False
if paginate:
images = list(itertools.islice(images_iter, request_size))
if len(images) > page_size:
images.pop(-1)
has_more_data = True
else:
images = list(images_iter)
return (images, has_more_data)
def image_update(request, image_id, **kwargs):
return glanceclient(request).images.update(image_id, **kwargs)
def image_create(request, **kwargs):
copy_from = None
if kwargs.get('copy_from'):
copy_from = kwargs.pop('copy_from')
image = glanceclient(request).images.create(**kwargs)
if copy_from:
thread.start_new_thread(image_update,
(request, image.id),
{'copy_from': copy_from,
'purge_props': False})
return image