summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/glance_utils.py
blob: ad9c5e519112c8b135745675ec017a9d28c402bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# 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.
import logging
import os
import uuid

from snaps import file_utils
from glanceclient.client import Client

from snaps.domain.image import Image
from snaps.openstack.utils import keystone_utils

__author__ = 'spisarski'

logger = logging.getLogger('glance_utils')

VERSION_1 = 1.0
VERSION_2 = 2.0

"""
Utilities for basic neutron API calls
"""


def glance_client(os_creds):
    """
    Creates and returns a glance client object
    :return: the glance client
    """
    return Client(version=os_creds.image_api_version,
                  session=keystone_utils.keystone_session(os_creds),
                  region_name=os_creds.region_name)


def get_image(glance, image_name=None):
    """
    Returns an OpenStack image object for a given name
    :param glance: the Glance client
    :param image_name: the image name to lookup
    :return: the image object or None
    """
    images = glance.images.list()
    for image in images:
        if glance.version == VERSION_1:
            if image.name == image_name:
                image = glance.images.get(image.id)
                return Image(name=image.name, image_id=image.id,
                             size=image.size, properties=image.properties)
        elif glance.version == VERSION_2:
            if image['name'] == image_name:
                return Image(
                    name=image['name'], image_id=image['id'],
                    size=image['size'], properties=image.get('properties'))
    return None


def get_image_by_id(glance, image_id):
    """
    Returns an OpenStack image object for a given name
    :param glance: the Glance client
    :param image_id: the image ID to lookup
    :return: the SNAPS-OO Domain Image object or None
    """
    image = glance.images.get(image_id)
    return Image(
        name=image['name'], image_id=image['id'],
        size=image['size'], properties=image.get('properties'))


def get_image_status(glance, image):
    """
    Returns a new OpenStack Image object for a given OpenStack image object
    :param glance: the Glance client
    :param image: the domain Image object
    :return: the OpenStack Image object
    """
    if glance.version == VERSION_1:
        os_image = glance.images.get(image.id)
        return os_image.status
    elif glance.version == VERSION_2:
        os_image = glance.images.get(image.id)
        return os_image['status']
    else:
        raise GlanceException('Unsupported glance client version')


def create_image(glance, image_settings):
    """
    Creates and returns OpenStack image object with an external URL
    :param glance: the glance client
    :param image_settings: the image settings object
    :return: the OpenStack image object
    :raise Exception if using a file and it cannot be found
    """
    if glance.version == VERSION_1:
        return __create_image_v1(glance, image_settings)
    elif glance.version == VERSION_2:
        return __create_image_v2(glance, image_settings)
    else:
        raise GlanceException('Unsupported glance client version')


def __create_image_v1(glance, image_settings):
    """
    Creates and returns OpenStack image object with an external URL
    :param glance: the glance client
    :param image_settings: the image settings object
    :return: the OpenStack image object
    :raise exceptions from the Glance client or IOError when opening a file
    """
    kwargs = {
        'name': image_settings.name, 'disk_format': image_settings.format,
        'container_format': 'bare', 'is_public': image_settings.public}

    image_file = None

    try:
        if image_settings.extra_properties:
            kwargs['properties'] = image_settings.extra_properties

        if image_settings.url:
            kwargs['location'] = image_settings.url
        elif image_settings.image_file:
            image_file = open(image_settings.image_file, 'rb')
            kwargs['data'] = image_file
        else:
            logger.warn(
                'Unable to create image with name - %s. No file or URL',
                image_settings.name)
            return None

        created_image = glance.images.create(**kwargs)
        return Image(name=image_settings.name, image_id=created_image.id,
                     size=created_image.size,
                     properties=created_image.properties)
    finally:
        if image_file:
            image_file.close()


def __create_image_v2(glance, image_settings):
    """
    Creates and returns OpenStack image object with an external URL
    :param glance: the glance client v2
    :param image_settings: the image settings object
    :return: the OpenStack image object
    :raise GlanceException or IOException or URLError
    """
    cleanup_temp_file = False
    image_file = None
    if image_settings.image_file:
        image_filename = image_settings.image_file
    elif image_settings.url:
        file_name = str(uuid.uuid4())
        try:
            image_file = file_utils.download(
                image_settings.url, './tmp', file_name)
            image_filename = image_file.name
        except:
            if image_file:
                os.remove(image_file.name)
            raise

        cleanup_temp_file = True
    else:
        raise GlanceException('Filename or URL of image not configured')

    os_image = None
    try:
        kwargs = dict()
        kwargs['name'] = image_settings.name
        kwargs['disk_format'] = image_settings.format
        kwargs['container_format'] = 'bare'

        if image_settings.public:
            kwargs['visibility'] = 'public'

        if image_settings.extra_properties:
            kwargs.update(image_settings.extra_properties)

        os_image = glance.images.create(**kwargs)
        image_file = open(image_filename, 'rb')
        glance.images.upload(os_image['id'], image_file)
    except:
        logger.error('Unexpected exception creating image. Rolling back')
        if os_image:
            delete_image(glance, Image(
                name=os_image['name'], image_id=os_image['id'],
                size=os_image['size'], properties=os_image.get('properties')))
        raise
    finally:
        if image_file:
            logger.debug('Closing file %s', image_file.name)
            image_file.close()
        if cleanup_temp_file:
            logger.info('Removing file %s', image_file.name)
            os.remove(image_filename)

    return get_image_by_id(glance, os_image['id'])


def delete_image(glance, image):
    """
    Deletes an image from OpenStack
    :param glance: the glance client
    :param image: the image to delete
    """
    logger.info('Deleting image named - %s', image.name)
    glance.images.delete(image.id)


class GlanceException(Exception):
    """
    Exception when calls to the Glance client cannot be served properly
    """