~ubuntu-branches/ubuntu/utopic/heat/utopic-proposed

« back to all changes in this revision

Viewing changes to contrib/heat_docker/heat_docker/resources/docker_container.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-09-08 09:40:59 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20140908094059-pzysrm0uy4senjez
Tags: 2014.2~b3-0ubuntu1
New upstream version. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2013 Docker, Inc.
 
3
# All Rights Reserved.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    a copy of the License at
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#    Unless required by applicable law or agreed to in writing, software
 
12
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
 
 
17
import six
 
18
 
 
19
from heat.engine import attributes
 
20
from heat.engine import properties
 
21
from heat.engine import resource
 
22
from heat.openstack.common.gettextutils import _
 
23
from heat.openstack.common import log as logging
 
24
 
 
25
LOG = logging.getLogger(__name__)
 
26
 
 
27
DOCKER_INSTALLED = False
 
28
# conditionally import so tests can work without having the dependency
 
29
# satisfied
 
30
try:
 
31
    import docker
 
32
    DOCKER_INSTALLED = True
 
33
except ImportError:
 
34
    docker = None
 
35
 
 
36
 
 
37
class DockerContainer(resource.Resource):
 
38
 
 
39
    PROPERTIES = (
 
40
        DOCKER_ENDPOINT, HOSTNAME, USER, MEMORY, PORT_SPECS,
 
41
        PRIVILEGED, TTY, OPEN_STDIN, STDIN_ONCE, ENV, CMD, DNS,
 
42
        IMAGE, VOLUMES, VOLUMES_FROM, PORT_BINDINGS, LINKS, NAME,
 
43
    ) = (
 
44
        'docker_endpoint', 'hostname', 'user', 'memory', 'port_specs',
 
45
        'privileged', 'tty', 'open_stdin', 'stdin_once', 'env', 'cmd', 'dns',
 
46
        'image', 'volumes', 'volumes_from', 'port_bindings', 'links', 'name'
 
47
    )
 
48
 
 
49
    ATTRIBUTES = (
 
50
        INFO, NETWORK_INFO, NETWORK_IP, NETWORK_GATEWAY,
 
51
        NETWORK_TCP_PORTS, NETWORK_UDP_PORTS, LOGS, LOGS_HEAD,
 
52
        LOGS_TAIL,
 
53
    ) = (
 
54
        'info', 'network_info', 'network_ip', 'network_gateway',
 
55
        'network_tcp_ports', 'network_udp_ports', 'logs', 'logs_head',
 
56
        'logs_tail',
 
57
    )
 
58
 
 
59
    properties_schema = {
 
60
        DOCKER_ENDPOINT: properties.Schema(
 
61
            properties.Schema.STRING,
 
62
            _('Docker daemon endpoint (by default the local docker daemon '
 
63
              'will be used).'),
 
64
            default=None
 
65
        ),
 
66
        HOSTNAME: properties.Schema(
 
67
            properties.Schema.STRING,
 
68
            _('Hostname of the container.'),
 
69
            default=''
 
70
        ),
 
71
        USER: properties.Schema(
 
72
            properties.Schema.STRING,
 
73
            _('Username or UID.'),
 
74
            default=''
 
75
        ),
 
76
        MEMORY: properties.Schema(
 
77
            properties.Schema.INTEGER,
 
78
            _('Memory limit (Bytes).'),
 
79
            default=0
 
80
        ),
 
81
        PORT_SPECS: properties.Schema(
 
82
            properties.Schema.LIST,
 
83
            _('TCP/UDP ports mapping.'),
 
84
            default=None
 
85
        ),
 
86
        PORT_BINDINGS: properties.Schema(
 
87
            properties.Schema.MAP,
 
88
            _('TCP/UDP ports bindings.'),
 
89
        ),
 
90
        LINKS: properties.Schema(
 
91
            properties.Schema.MAP,
 
92
            _('Links to other containers.'),
 
93
        ),
 
94
        NAME: properties.Schema(
 
95
            properties.Schema.STRING,
 
96
            _('Name of the container.'),
 
97
        ),
 
98
        PRIVILEGED: properties.Schema(
 
99
            properties.Schema.BOOLEAN,
 
100
            _('Enable extended privileges.'),
 
101
            default=False
 
102
        ),
 
103
        TTY: properties.Schema(
 
104
            properties.Schema.BOOLEAN,
 
105
            _('Allocate a pseudo-tty.'),
 
106
            default=False
 
107
        ),
 
108
        OPEN_STDIN: properties.Schema(
 
109
            properties.Schema.BOOLEAN,
 
110
            _('Open stdin.'),
 
111
            default=False
 
112
        ),
 
113
        STDIN_ONCE: properties.Schema(
 
114
            properties.Schema.BOOLEAN,
 
115
            _('If true, close stdin after the 1 attached client disconnects.'),
 
116
            default=False
 
117
        ),
 
118
        ENV: properties.Schema(
 
119
            properties.Schema.LIST,
 
120
            _('Set environment variables.'),
 
121
        ),
 
122
        CMD: properties.Schema(
 
123
            properties.Schema.LIST,
 
124
            _('Command to run after spawning the container.'),
 
125
            default=[]
 
126
        ),
 
127
        DNS: properties.Schema(
 
128
            properties.Schema.LIST,
 
129
            _('Set custom dns servers.'),
 
130
        ),
 
131
        IMAGE: properties.Schema(
 
132
            properties.Schema.STRING,
 
133
            _('Image name.')
 
134
        ),
 
135
        VOLUMES: properties.Schema(
 
136
            properties.Schema.MAP,
 
137
            _('Create a bind mount.'),
 
138
            default={}
 
139
        ),
 
140
        VOLUMES_FROM: properties.Schema(
 
141
            properties.Schema.LIST,
 
142
            _('Mount all specified volumes.'),
 
143
            default=''
 
144
        ),
 
145
    }
 
146
 
 
147
    attributes_schema = {
 
148
        INFO: attributes.Schema(
 
149
            _('Container info.')
 
150
        ),
 
151
        NETWORK_INFO: attributes.Schema(
 
152
            _('Container network info.')
 
153
        ),
 
154
        NETWORK_IP: attributes.Schema(
 
155
            _('Container ip address.')
 
156
        ),
 
157
        NETWORK_GATEWAY: attributes.Schema(
 
158
            _('Container ip gateway.')
 
159
        ),
 
160
        NETWORK_TCP_PORTS: attributes.Schema(
 
161
            _('Container TCP ports.')
 
162
        ),
 
163
        NETWORK_UDP_PORTS: attributes.Schema(
 
164
            _('Container UDP ports.')
 
165
        ),
 
166
        LOGS: attributes.Schema(
 
167
            _('Container logs.')
 
168
        ),
 
169
        LOGS_HEAD: attributes.Schema(
 
170
            _('Container first logs line.')
 
171
        ),
 
172
        LOGS_TAIL: attributes.Schema(
 
173
            _('Container last logs line.')
 
174
        ),
 
175
    }
 
176
 
 
177
    def get_client(self):
 
178
        client = None
 
179
        if DOCKER_INSTALLED:
 
180
            endpoint = self.properties.get(self.DOCKER_ENDPOINT)
 
181
            if endpoint:
 
182
                client = docker.Client(endpoint)
 
183
            else:
 
184
                client = docker.Client()
 
185
        return client
 
186
 
 
187
    def _parse_networkinfo_ports(self, networkinfo):
 
188
        tcp = []
 
189
        udp = []
 
190
        for port, info in six.iteritems(networkinfo['Ports']):
 
191
            p = port.split('/')
 
192
            if not info or len(p) != 2 or 'HostPort' not in info[0]:
 
193
                continue
 
194
            port = info[0]['HostPort']
 
195
            if p[1] == 'tcp':
 
196
                tcp.append(port)
 
197
            elif p[1] == 'udp':
 
198
                udp.append(port)
 
199
        return (','.join(tcp), ','.join(udp))
 
200
 
 
201
    def _container_networkinfo(self, client, resource_id):
 
202
        info = client.inspect_container(self.resource_id)
 
203
        networkinfo = info['NetworkSettings']
 
204
        ports = self._parse_networkinfo_ports(networkinfo)
 
205
        networkinfo['TcpPorts'] = ports[0]
 
206
        networkinfo['UdpPorts'] = ports[1]
 
207
        return networkinfo
 
208
 
 
209
    def _resolve_attribute(self, name):
 
210
        if not self.resource_id:
 
211
            return
 
212
        if name == 'info':
 
213
            client = self.get_client()
 
214
            return client.inspect_container(self.resource_id)
 
215
        if name == 'network_info':
 
216
            client = self.get_client()
 
217
            networkinfo = self._container_networkinfo(client, self.resource_id)
 
218
            return networkinfo
 
219
        if name == 'network_ip':
 
220
            client = self.get_client()
 
221
            networkinfo = self._container_networkinfo(client, self.resource_id)
 
222
            return networkinfo['IPAddress']
 
223
        if name == 'network_gateway':
 
224
            client = self.get_client()
 
225
            networkinfo = self._container_networkinfo(client, self.resource_id)
 
226
            return networkinfo['Gateway']
 
227
        if name == 'network_tcp_ports':
 
228
            client = self.get_client()
 
229
            networkinfo = self._container_networkinfo(client, self.resource_id)
 
230
            return networkinfo['TcpPorts']
 
231
        if name == 'network_udp_ports':
 
232
            client = self.get_client()
 
233
            networkinfo = self._container_networkinfo(client, self.resource_id)
 
234
            return networkinfo['UdpPorts']
 
235
        if name == 'logs':
 
236
            client = self.get_client()
 
237
            logs = client.logs(self.resource_id)
 
238
            return logs
 
239
        if name == 'logs_head':
 
240
            client = self.get_client()
 
241
            logs = client.logs(self.resource_id)
 
242
            return logs.split('\n')[0]
 
243
        if name == 'logs_tail':
 
244
            client = self.get_client()
 
245
            logs = client.logs(self.resource_id)
 
246
            return logs.split('\n').pop()
 
247
 
 
248
    def handle_create(self):
 
249
        create_args = {
 
250
            'image': self.properties[self.IMAGE],
 
251
            'command': self.properties[self.CMD],
 
252
            'hostname': self.properties[self.HOSTNAME],
 
253
            'user': self.properties[self.USER],
 
254
            'stdin_open': self.properties[self.OPEN_STDIN],
 
255
            'tty': self.properties[self.TTY],
 
256
            'mem_limit': self.properties[self.MEMORY],
 
257
            'ports': self.properties[self.PORT_SPECS],
 
258
            'environment': self.properties[self.ENV],
 
259
            'dns': self.properties[self.DNS],
 
260
            'volumes': self.properties[self.VOLUMES],
 
261
            'name': self.properties[self.NAME]
 
262
        }
 
263
        client = self.get_client()
 
264
        client.pull(self.properties[self.IMAGE])
 
265
        result = client.create_container(**create_args)
 
266
        container_id = result['Id']
 
267
        self.resource_id_set(container_id)
 
268
 
 
269
        start_args = {}
 
270
 
 
271
        if self.properties[self.PRIVILEGED]:
 
272
            start_args[self.PRIVILEGED] = True
 
273
        if self.properties[self.VOLUMES]:
 
274
            start_args['binds'] = self.properties[self.VOLUMES]
 
275
        if self.properties[self.VOLUMES_FROM]:
 
276
            start_args['volumes_from'] = self.properties[self.VOLUMES_FROM]
 
277
        if self.properties[self.PORT_BINDINGS]:
 
278
            start_args['port_bindings'] = self.properties[self.PORT_BINDINGS]
 
279
        if self.properties[self.LINKS]:
 
280
            start_args['links'] = self.properties[self.LINKS]
 
281
 
 
282
        client.start(container_id, **start_args)
 
283
        return container_id
 
284
 
 
285
    def _get_container_status(self, container_id):
 
286
        client = self.get_client()
 
287
        info = client.inspect_container(container_id)
 
288
        return info['State']
 
289
 
 
290
    def check_create_complete(self, container_id):
 
291
        status = self._get_container_status(container_id)
 
292
        return status['Running']
 
293
 
 
294
    def handle_delete(self):
 
295
        if self.resource_id is None:
 
296
            return
 
297
        client = self.get_client()
 
298
        try:
 
299
            client.kill(self.resource_id)
 
300
        except docker.errors.APIError as ex:
 
301
            if ex.response.status_code != 404:
 
302
                raise
 
303
        return self.resource_id
 
304
 
 
305
    def check_delete_complete(self, container_id):
 
306
        if container_id is None:
 
307
            return True
 
308
        try:
 
309
            status = self._get_container_status(container_id)
 
310
        except docker.errors.APIError as ex:
 
311
            if ex.response.status_code == 404:
 
312
                return True
 
313
            raise
 
314
        return (not status['Running'])
 
315
 
 
316
    def handle_suspend(self):
 
317
        if not self.resource_id:
 
318
            return
 
319
        client = self.get_client()
 
320
        client.stop(self.resource_id)
 
321
        return self.resource_id
 
322
 
 
323
    def check_suspend_complete(self, container_id):
 
324
        status = self._get_container_status(container_id)
 
325
        return (not status['Running'])
 
326
 
 
327
    def handle_resume(self):
 
328
        if not self.resource_id:
 
329
            return
 
330
        client = self.get_client()
 
331
        client.start(self.resource_id)
 
332
        return self.resource_id
 
333
 
 
334
    def check_resume_complete(self, container_id):
 
335
        status = self._get_container_status(container_id)
 
336
        return status['Running']
 
337
 
 
338
 
 
339
def resource_mapping():
 
340
    return {
 
341
        'DockerInc::Docker::Container': DockerContainer,
 
342
    }
 
343
 
 
344
 
 
345
def available_resource_mapping():
 
346
    if DOCKER_INSTALLED:
 
347
        return resource_mapping()
 
348
    else:
 
349
        LOG.warn(_("Docker plug-in loaded, but docker lib not installed."))
 
350
        return {}