aboutsummaryrefslogtreecommitdiffstats
path: root/aria/multivim-plugin/glance_plugin/image.py
blob: a8d5b203f4e36c26c6a43a26c8b1ae9182a0a061 (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
#########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. 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 httplib
from urlparse import urlparse

from cloudify import ctx
from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError

from openstack_plugin_common import (
    with_glance_client,
    get_resource_id,
    use_external_resource,
    get_openstack_ids_of_connected_nodes_by_openstack_type,
    delete_resource_and_runtime_properties,
    validate_resource,
    COMMON_RUNTIME_PROPERTIES_KEYS,
    OPENSTACK_ID_PROPERTY,
    OPENSTACK_TYPE_PROPERTY,
    OPENSTACK_NAME_PROPERTY)


IMAGE_OPENSTACK_TYPE = 'image'
IMAGE_STATUS_ACTIVE = 'active'

RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS
REQUIRED_PROPERTIES = ['container_format', 'disk_format']


@operation
@with_glance_client
def create(glance_client, **kwargs):
    if use_external_resource(ctx, glance_client, IMAGE_OPENSTACK_TYPE):
        return

    img_dict = {
        'name': get_resource_id(ctx, IMAGE_OPENSTACK_TYPE)
    }
    _validate_image_dictionary()
    img_properties = ctx.node.properties['image']
    img_dict.update({key: value for key, value in img_properties.iteritems()
                    if key != 'data'})
    img = glance_client.images.create(**img_dict)
    img_path = img_properties.get('data', '')
    img_url = ctx.node.properties.get('image_url')
    try:
        _validate_image()
        if img_path:
            with open(img_path, 'rb') as image_file:
                glance_client.images.upload(
                    image_id=img.id,
                    image_data=image_file)
        elif img_url:
            img = glance_client.images.add_location(img.id, img_url, {})

    except:
        _remove_protected(glance_client)
        glance_client.images.delete(image_id=img.id)
        raise

    ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = img.id
    ctx.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] = \
        IMAGE_OPENSTACK_TYPE
    ctx.instance.runtime_properties[OPENSTACK_NAME_PROPERTY] = img.name


def _get_image_by_ctx(glance_client, ctx):
    return glance_client.images.get(
        image_id=ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY])


@operation
@with_glance_client
def start(glance_client, start_retry_interval, **kwargs):
    img = _get_image_by_ctx(glance_client, ctx)
    if img.status != IMAGE_STATUS_ACTIVE:
        return ctx.operation.retry(
            message='Waiting for image to get uploaded',
            retry_after=start_retry_interval)


@operation
@with_glance_client
def delete(glance_client, **kwargs):
    _remove_protected(glance_client)
    delete_resource_and_runtime_properties(ctx, glance_client,
                                           RUNTIME_PROPERTIES_KEYS)


@operation
@with_glance_client
def creation_validation(glance_client, **kwargs):
    validate_resource(ctx, glance_client, IMAGE_OPENSTACK_TYPE)
    _validate_image_dictionary()
    _validate_image()


def _validate_image_dictionary():
    img = ctx.node.properties['image']
    missing = ''
    try:
        for prop in REQUIRED_PROPERTIES:
            if prop not in img:
                missing += '{0} '.format(prop)
    except TypeError:
        missing = ' '.join(REQUIRED_PROPERTIES)
    if missing:
        raise NonRecoverableError('Required properties are missing: {'
                                  '0}. Please update your image '
                                  'dictionary.'.format(missing))


def _validate_image():
    img = ctx.node.properties['image']
    img_path = img.get('data')
    img_url = ctx.node.properties.get('image_url')
    if not img_url and not img_path:
        raise NonRecoverableError('Neither image url nor image path was '
                                  'provided')
    if img_url and img_path:
        raise NonRecoverableError('Multiple image sources provided')
    if img_url:
        _check_url(img_url)
    if img_path:
        _check_path()


def _check_url(url):
    p = urlparse(url)
    conn = httplib.HTTPConnection(p.netloc)
    conn.request('HEAD', p.path)
    resp = conn.getresponse()
    if resp.status >= 400:
        raise NonRecoverableError('Invalid image URL')


def _check_path():
    img = ctx.node.properties['image']
    img_path = img.get('data')
    try:
        with open(img_path, 'rb'):
            pass
    except TypeError:
        if not img.get('url'):
            raise NonRecoverableError('No path or url provided')
    except IOError:
        raise NonRecoverableError(
            'Unable to open image file with path: "{}"'.format(img_path))


def _remove_protected(glance_client):
    if use_external_resource(ctx, glance_client, IMAGE_OPENSTACK_TYPE):
        return

    is_protected = ctx.node.properties['image'].get('protected', False)
    if is_protected:
        img_id = ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
        glance_client.images.update(img_id, protected=False)


def handle_image_from_relationship(obj_dict, property_name_to_put, ctx):
    images = get_openstack_ids_of_connected_nodes_by_openstack_type(
        ctx, IMAGE_OPENSTACK_TYPE)
    if images:
        obj_dict.update({property_name_to_put: images[0]})