2
# Copyright (c) 2013 Docker, Inc.
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
9
# http://www.apache.org/licenses/LICENSE-2.0
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
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
25
LOG = logging.getLogger(__name__)
27
DOCKER_INSTALLED = False
28
# conditionally import so tests can work without having the dependency
32
DOCKER_INSTALLED = True
37
class DockerContainer(resource.Resource):
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,
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'
50
INFO, NETWORK_INFO, NETWORK_IP, NETWORK_GATEWAY,
51
NETWORK_TCP_PORTS, NETWORK_UDP_PORTS, LOGS, LOGS_HEAD,
54
'info', 'network_info', 'network_ip', 'network_gateway',
55
'network_tcp_ports', 'network_udp_ports', 'logs', 'logs_head',
60
DOCKER_ENDPOINT: properties.Schema(
61
properties.Schema.STRING,
62
_('Docker daemon endpoint (by default the local docker daemon '
66
HOSTNAME: properties.Schema(
67
properties.Schema.STRING,
68
_('Hostname of the container.'),
71
USER: properties.Schema(
72
properties.Schema.STRING,
73
_('Username or UID.'),
76
MEMORY: properties.Schema(
77
properties.Schema.INTEGER,
78
_('Memory limit (Bytes).'),
81
PORT_SPECS: properties.Schema(
82
properties.Schema.LIST,
83
_('TCP/UDP ports mapping.'),
86
PORT_BINDINGS: properties.Schema(
87
properties.Schema.MAP,
88
_('TCP/UDP ports bindings.'),
90
LINKS: properties.Schema(
91
properties.Schema.MAP,
92
_('Links to other containers.'),
94
NAME: properties.Schema(
95
properties.Schema.STRING,
96
_('Name of the container.'),
98
PRIVILEGED: properties.Schema(
99
properties.Schema.BOOLEAN,
100
_('Enable extended privileges.'),
103
TTY: properties.Schema(
104
properties.Schema.BOOLEAN,
105
_('Allocate a pseudo-tty.'),
108
OPEN_STDIN: properties.Schema(
109
properties.Schema.BOOLEAN,
113
STDIN_ONCE: properties.Schema(
114
properties.Schema.BOOLEAN,
115
_('If true, close stdin after the 1 attached client disconnects.'),
118
ENV: properties.Schema(
119
properties.Schema.LIST,
120
_('Set environment variables.'),
122
CMD: properties.Schema(
123
properties.Schema.LIST,
124
_('Command to run after spawning the container.'),
127
DNS: properties.Schema(
128
properties.Schema.LIST,
129
_('Set custom dns servers.'),
131
IMAGE: properties.Schema(
132
properties.Schema.STRING,
135
VOLUMES: properties.Schema(
136
properties.Schema.MAP,
137
_('Create a bind mount.'),
140
VOLUMES_FROM: properties.Schema(
141
properties.Schema.LIST,
142
_('Mount all specified volumes.'),
147
attributes_schema = {
148
INFO: attributes.Schema(
151
NETWORK_INFO: attributes.Schema(
152
_('Container network info.')
154
NETWORK_IP: attributes.Schema(
155
_('Container ip address.')
157
NETWORK_GATEWAY: attributes.Schema(
158
_('Container ip gateway.')
160
NETWORK_TCP_PORTS: attributes.Schema(
161
_('Container TCP ports.')
163
NETWORK_UDP_PORTS: attributes.Schema(
164
_('Container UDP ports.')
166
LOGS: attributes.Schema(
169
LOGS_HEAD: attributes.Schema(
170
_('Container first logs line.')
172
LOGS_TAIL: attributes.Schema(
173
_('Container last logs line.')
177
def get_client(self):
180
endpoint = self.properties.get(self.DOCKER_ENDPOINT)
182
client = docker.Client(endpoint)
184
client = docker.Client()
187
def _parse_networkinfo_ports(self, networkinfo):
190
for port, info in six.iteritems(networkinfo['Ports']):
192
if not info or len(p) != 2 or 'HostPort' not in info[0]:
194
port = info[0]['HostPort']
199
return (','.join(tcp), ','.join(udp))
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]
209
def _resolve_attribute(self, name):
210
if not self.resource_id:
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)
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']
236
client = self.get_client()
237
logs = client.logs(self.resource_id)
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()
248
def handle_create(self):
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]
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)
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]
282
client.start(container_id, **start_args)
285
def _get_container_status(self, container_id):
286
client = self.get_client()
287
info = client.inspect_container(container_id)
290
def check_create_complete(self, container_id):
291
status = self._get_container_status(container_id)
292
return status['Running']
294
def handle_delete(self):
295
if self.resource_id is None:
297
client = self.get_client()
299
client.kill(self.resource_id)
300
except docker.errors.APIError as ex:
301
if ex.response.status_code != 404:
303
return self.resource_id
305
def check_delete_complete(self, container_id):
306
if container_id is None:
309
status = self._get_container_status(container_id)
310
except docker.errors.APIError as ex:
311
if ex.response.status_code == 404:
314
return (not status['Running'])
316
def handle_suspend(self):
317
if not self.resource_id:
319
client = self.get_client()
320
client.stop(self.resource_id)
321
return self.resource_id
323
def check_suspend_complete(self, container_id):
324
status = self._get_container_status(container_id)
325
return (not status['Running'])
327
def handle_resume(self):
328
if not self.resource_id:
330
client = self.get_client()
331
client.start(self.resource_id)
332
return self.resource_id
334
def check_resume_complete(self, container_id):
335
status = self._get_container_status(container_id)
336
return status['Running']
339
def resource_mapping():
341
'DockerInc::Docker::Container': DockerContainer,
345
def available_resource_mapping():
347
return resource_mapping()
349
LOG.warn(_("Docker plug-in loaded, but docker lib not installed."))