Commit b29a3b75 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[dock] Refactor ports and volumes handling

parent bc6240bc
......@@ -379,17 +379,11 @@ class Host(object):
cmd.append('--cpu-quota=%d' % int(quota * period))
# Mount the volumes
for k, v in container.volumes.items():
cmd.append('--volume=%s:%s:%s' % (k, v['bind'], v['mode']))
cmd.extend(container.volumes)
# Expose the ports
for k, v in container.ports.items():
cmd.append('-p')
if isinstance(v, tuple):
cmd.append('%s:%d:%d' % (v[0], v[1], k))
else:
cmd.append('%d:%d' % (v[0], k))
cmd.extend(container.ports)
cmd.append(container.image)
cmd.extend(container.command)
......@@ -590,12 +584,13 @@ class Container:
:param str command: Command to execute in the container.
"""
def __init__(self, image, command):
self.image = image
self.command = command
self.volumes = {}
self.ports = {}
self.id = None
self._volumes = {}
self._ports = {}
self._stats = None
......@@ -610,7 +605,7 @@ class Container:
:param boolean read_only: Whether the volume will be read only
"""
self.volumes[path] = {
self._volumes[path] = {
'bind': mount_path,
'mode': 'ro' if read_only else 'rw',
}
......@@ -632,16 +627,52 @@ class Container:
else:
value = [host_port]
self.ports[container_port] = value
self._ports[container_port] = value
def reset_ports(self):
"""Empty the port bindings"""
self.ports = {}
self._ports = {}
@property
def volumes(self):
"""Returns the volumes of this container in a suitable form to build
a command to start the container.
"""
volumes = []
for k, v in self._volumes.items():
if k.startswith('nfs://'):
addr, src = k[6:].split(':')
volumes.append('--mount=type=volume,'
'dst={dst},'
'volume-driver=local,'
'volume-opt=type=nfs,'
'volume-opt=device=:{src},'
'volume-opt=o=addr={addr}'.format(dst=v['bind'], src=src, addr=addr))
else:
if k.startswith('file://'):
k = k[6:]
volumes.append('--volume=%s:%s:%s' % (k, v['bind'], v['mode']))
return volumes
@property
def ports(self):
"""Returns the ports of this container in a suitable form to build
a command to start the container.
"""
ports = []
for k, v in self._ports.items():
ports.append('-p')
if isinstance(v, tuple):
ports.append('%s:%d:%d' % (v[0], v[1], k))
else:
ports.append('%d:%d' % (v[0], k))
return ports
def command_line(self):
"""Returns the complete docker command to start the container and
execute the specified command.
......@@ -651,15 +682,8 @@ class Container:
"""
cmd = "docker run -ti --rm=true "
for k, v in self.volumes.items():
cmd += "--volume %s:%s:%s " % (k, v['bind'], v['mode'])
for k, v in self.ports.items():
if isinstance(v, tuple):
cmd += "-p %s:%d:%d " % (v[0], v[1], k)
else:
cmd += "-p %d:%d " % (v[0], k)
cmd += ' '.join(self.volumes)
cmd += ' '.join(self.ports)
cmd += "%s " % self.image
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment