summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/zun/container.py
blob: 56a312c24806555f7a5eb24edc382fbc1f37476e (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#
#    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 copy

from heat.common import exception
from heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine import resource
from heat.engine import support


class Container(resource.Resource):
    """A resource that creates a Zun Container.

    This resource creates a Zun container.
    """

    support_status = support.SupportStatus(version='9.0.0')

    PROPERTIES = (
        NAME, IMAGE, COMMAND, CPU, MEMORY,
        ENVIRONMENT, WORKDIR, LABELS, IMAGE_PULL_POLICY,
        RESTART_POLICY, INTERACTIVE, IMAGE_DRIVER, HINTS,
        HOSTNAME, SECURITY_GROUPS, MOUNTS,
    ) = (
        'name', 'image', 'command', 'cpu', 'memory',
        'environment', 'workdir', 'labels', 'image_pull_policy',
        'restart_policy', 'interactive', 'image_driver', 'hints',
        'hostname', 'security_groups', 'mounts',
    )

    _MOUNT_KEYS = (
        VOLUME_ID, MOUNT_PATH, VOLUME_SIZE
    ) = (
        'volume_id', 'mount_path', 'volume_size',
    )

    ATTRIBUTES = (
        NAME, ADDRESSES
    ) = (
        'name', 'addresses'
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('Name of the container.'),
            update_allowed=True
        ),
        IMAGE: properties.Schema(
            properties.Schema.STRING,
            _('Name or ID of the image.'),
            required=True
        ),
        COMMAND: properties.Schema(
            properties.Schema.STRING,
            _('Send command to the container.'),
        ),
        CPU: properties.Schema(
            properties.Schema.NUMBER,
            _('The number of virtual cpus.'),
            update_allowed=True
        ),
        MEMORY: properties.Schema(
            properties.Schema.INTEGER,
            _('The container memory size in MiB.'),
            update_allowed=True
        ),
        ENVIRONMENT: properties.Schema(
            properties.Schema.MAP,
            _('The environment variables.'),
        ),
        WORKDIR: properties.Schema(
            properties.Schema.STRING,
            _('The working directory for commands to run in.'),
        ),
        LABELS: properties.Schema(
            properties.Schema.MAP,
            _('Adds a map of labels to a container. '
              'May be used multiple times.'),
        ),
        IMAGE_PULL_POLICY: properties.Schema(
            properties.Schema.STRING,
            _('The policy which determines if the image should '
              'be pulled prior to starting the container.'),
            constraints=[
                constraints.AllowedValues(['ifnotpresent', 'always',
                                           'never']),
            ]
        ),
        RESTART_POLICY: properties.Schema(
            properties.Schema.STRING,
            _('Restart policy to apply when a container exits. Possible '
              'values are "no", "on-failure[:max-retry]", "always", and '
              '"unless-stopped".'),
        ),
        INTERACTIVE: properties.Schema(
            properties.Schema.BOOLEAN,
            _('Keep STDIN open even if not attached.'),
        ),
        IMAGE_DRIVER: properties.Schema(
            properties.Schema.STRING,
            _('The image driver to use to pull container image.'),
            constraints=[
                constraints.AllowedValues(['docker', 'glance']),
            ]
        ),
        HINTS: properties.Schema(
            properties.Schema.MAP,
            _('Arbitrary key-value pairs for scheduler to select host.'),
            support_status=support.SupportStatus(version='10.0.0'),
        ),
        HOSTNAME: properties.Schema(
            properties.Schema.STRING,
            _('The hostname of the container.'),
            support_status=support.SupportStatus(version='10.0.0'),
        ),
        SECURITY_GROUPS: properties.Schema(
            properties.Schema.LIST,
            _('List of security group names or IDs.'),
            support_status=support.SupportStatus(version='10.0.0'),
            default=[]
        ),
        MOUNTS: properties.Schema(
            properties.Schema.LIST,
            _('A list of volumes mounted inside the container.'),
            schema=properties.Schema(
                properties.Schema.MAP,
                schema={
                    VOLUME_ID: properties.Schema(
                        properties.Schema.STRING,
                        _('The ID or name of the cinder volume mount to '
                          'the container.'),
                        constraints=[
                            constraints.CustomConstraint('cinder.volume')
                        ]
                    ),
                    VOLUME_SIZE: properties.Schema(
                        properties.Schema.INTEGER,
                        _('The size of the cinder volume to create.'),
                    ),
                    MOUNT_PATH: properties.Schema(
                        properties.Schema.STRING,
                        _('The filesystem path inside the container.'),
                        required=True,
                    ),
                },
            )
        ),
    }

    attributes_schema = {
        NAME: attributes.Schema(
            _('Name of the container.'),
            type=attributes.Schema.STRING
        ),
        ADDRESSES: attributes.Schema(
            _('A dict of all network addresses with corresponding port_id. '
              'Each network will have two keys in dict, they are network '
              'name and network id. '
              'The port ID may be obtained through the following expression: '
              '"{get_attr: [<container>, addresses, <network name_or_id>, 0, '
              'port]}".'),
            type=attributes.Schema.MAP
        ),
    }

    default_client_name = 'zun'

    entity = 'containers'

    def validate(self):
        super(Container, self).validate()

        policy = self.properties[self.RESTART_POLICY]
        if policy and not self._parse_restart_policy(policy):
            msg = _('restart_policy "%s" is invalid. Valid values are '
                    '"no", "on-failure[:max-retry]", "always", and '
                    '"unless-stopped".') % policy
            raise exception.StackValidationFailed(message=msg)

        mounts = self.properties[self.MOUNTS] or []
        for mount in mounts:
            self._validate_mount(mount)

    def _validate_mount(self, mount):
        volume_id = mount.get(self.VOLUME_ID)
        volume_size = mount.get(self.VOLUME_SIZE)

        if volume_id is None and volume_size is None:
            msg = _('One of the properties "%(id)s" or "%(size)s" '
                    'should be set for the specified mount of '
                    'container "%(container)s".'
                    '') % dict(id=self.VOLUME_ID,
                               size=self.VOLUME_SIZE,
                               container=self.name)
            raise exception.StackValidationFailed(message=msg)

        # Don't allow specify volume_id and volume_size at the same time
        if volume_id and volume_size:
            raise exception.ResourcePropertyConflict(
                "/".join([self.NETWORKS, self.VOLUME_ID]),
                "/".join([self.NETWORKS, self.VOLUME_SIZE]))

    def handle_create(self):
        args = dict((k, v) for k, v in self.properties.items()
                    if v is not None)
        policy = args.pop(self.RESTART_POLICY, None)
        if policy:
            args[self.RESTART_POLICY] = self._parse_restart_policy(policy)
        mounts = args.pop(self.MOUNTS, None)
        if mounts:
            args[self.MOUNTS] = self._build_mounts(mounts)
        container = self.client().containers.run(**args)
        self.resource_id_set(container.uuid)
        return container.uuid

    def _parse_restart_policy(self, policy):
        restart_policy = None
        if ":" in policy:
            policy, count = policy.split(":")
            if policy in ['on-failure']:
                restart_policy = {"Name": policy,
                                  "MaximumRetryCount": count or '0'}
        else:
            if policy in ['always', 'unless-stopped', 'on-failure', 'no']:
                restart_policy = {"Name": policy, "MaximumRetryCount": '0'}

        return restart_policy

    def _build_mounts(self, mounts):
        mnts = []
        for mount in mounts:
            mnt_info = {'destination': mount[self.MOUNT_PATH]}
            if mount.get(self.VOLUME_ID):
                mnt_info['source'] = mount[self.VOLUME_ID]
            if mount.get(self.VOLUME_SIZE):
                mnt_info['size'] = mount[self.VOLUME_SIZE]
            mnts.append(mnt_info)
        return mnts

    def check_create_complete(self, id):
        container = self.client().containers.get(id)
        if container.status in ('Creating', 'Created'):
            return False
        elif container.status == 'Running':
            return True
        elif container.status == 'Stopped':
            if container.interactive:
                msg = (_("Error in creating container '%(name)s' - "
                         "interactive mode was enabled but the container "
                         "has stopped running") % {'name': self.name})
                raise exception.ResourceInError(
                    status_reason=msg, resource_status=container.status)
            return True
        elif container.status == 'Error':
            msg = (_("Error in creating container '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': container.status_reason})
            raise exception.ResourceInError(status_reason=msg,
                                            resource_status=container.status)
        else:
            msg = (_("Unknown status Container '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': container.status_reason})
            raise exception.ResourceUnknownStatus(status_reason=msg,
                                                  resource_status=container
                                                  .status)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if self.NAME in prop_diff:
            name = prop_diff.pop(self.NAME)
            self.client().containers.rename(self.resource_id, name=name)
        if prop_diff:
            self.client().containers.update(self.resource_id, **prop_diff)

    def handle_delete(self):
        if not self.resource_id:
            return
        try:
            self.client().containers.delete(self.resource_id, stop=True)
            return self.resource_id
        except Exception as exc:
            self.client_plugin().ignore_not_found(exc)

    def check_delete_complete(self, id):
        if not id:
            return True
        try:
            self.client().containers.get(id)
        except Exception as exc:
            self.client_plugin().ignore_not_found(exc)
            return True
        return False

    def _resolve_attribute(self, name):
        if self.resource_id is None:
            return
        try:
            container = self.client().containers.get(self.resource_id)
        except Exception as exc:
            self.client_plugin().ignore_not_found(exc)
            return ''

        if name == self.ADDRESSES:
            return self._extend_addresses(container)

        return getattr(container, name, '')

    def _extend_addresses(self, container):
        """Method adds network name to list of addresses.

        This method is used only for resolving attributes.
        """
        nets = self.neutron().list_networks()['networks']
        id_name_mapping_on_network = {net['id']: net['name']
                                      for net in nets}
        addresses = copy.deepcopy(container.addresses)
        for net_uuid in container.addresses or {}:
            addr_list = addresses[net_uuid]
            net_name = id_name_mapping_on_network.get(net_uuid)
            if not net_name:
                continue

            addresses.setdefault(net_name, [])
            addresses[net_name] += addr_list

        return addresses


def resource_mapping():
    return {
        'OS::Zun::Container': Container
    }