2014-01-06 03:26:32 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import absolute_import
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2014-08-08 18:41:52 +02:00
|
|
|
from fig.packages import six
|
|
|
|
|
2014-07-15 22:22:16 +02:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
class Container(object):
|
|
|
|
"""
|
2014-01-18 15:11:25 +01:00
|
|
|
Represents a Docker container, constructed from the output of
|
2013-12-18 19:37:48 +01:00
|
|
|
GET /containers/:id:/json.
|
|
|
|
"""
|
|
|
|
def __init__(self, client, dictionary, has_been_inspected=False):
|
|
|
|
self.client = client
|
|
|
|
self.dictionary = dictionary
|
|
|
|
self.has_been_inspected = has_been_inspected
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_ps(cls, client, dictionary, **kwargs):
|
|
|
|
"""
|
|
|
|
Construct a container object from the output of GET /containers/json.
|
|
|
|
"""
|
|
|
|
new_dictionary = {
|
2014-06-25 12:47:29 +02:00
|
|
|
'Id': dictionary['Id'],
|
2013-12-18 19:37:48 +01:00
|
|
|
'Image': dictionary['Image'],
|
|
|
|
}
|
|
|
|
for name in dictionary.get('Names', []):
|
|
|
|
if len(name.split('/')) == 2:
|
|
|
|
new_dictionary['Name'] = name
|
|
|
|
return cls(client, new_dictionary, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_id(cls, client, id):
|
|
|
|
return cls(client, client.inspect_container(id))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, client, **options):
|
|
|
|
response = client.create_container(**options)
|
|
|
|
return cls.from_id(client, response['Id'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2014-06-25 12:47:29 +02:00
|
|
|
return self.dictionary['Id']
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2014-01-18 15:11:25 +01:00
|
|
|
@property
|
|
|
|
def image(self):
|
|
|
|
return self.dictionary['Image']
|
|
|
|
|
2013-12-19 14:02:04 +01:00
|
|
|
@property
|
|
|
|
def short_id(self):
|
|
|
|
return self.id[:10]
|
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
@property
|
|
|
|
def name(self):
|
2013-12-20 20:33:41 +01:00
|
|
|
return self.dictionary['Name'][1:]
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2014-01-27 16:29:58 +01:00
|
|
|
@property
|
|
|
|
def name_without_project(self):
|
|
|
|
return '_'.join(self.dictionary['Name'].split('_')[1:])
|
|
|
|
|
2014-01-16 19:05:45 +01:00
|
|
|
@property
|
|
|
|
def number(self):
|
|
|
|
try:
|
|
|
|
return int(self.name.split('_')[-1])
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2013-12-19 14:02:04 +01:00
|
|
|
@property
|
2014-08-08 18:41:52 +02:00
|
|
|
def ports(self):
|
2013-12-19 14:02:04 +01:00
|
|
|
self.inspect_if_not_inspected()
|
2014-08-08 18:41:52 +02:00
|
|
|
return self.dictionary['NetworkSettings']['Ports'] or {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def human_readable_ports(self):
|
|
|
|
def format_port(private, public):
|
|
|
|
if not public:
|
|
|
|
return private
|
|
|
|
return '{HostIp}:{HostPort}->{private}'.format(
|
|
|
|
private=private, **public[0])
|
|
|
|
|
|
|
|
return ', '.join(format_port(*item)
|
|
|
|
for item in sorted(six.iteritems(self.ports)))
|
2013-12-19 14:02:04 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def human_readable_state(self):
|
|
|
|
self.inspect_if_not_inspected()
|
|
|
|
if self.dictionary['State']['Running']:
|
2014-05-08 06:39:15 +02:00
|
|
|
if self.dictionary['State'].get('Ghost'):
|
2013-12-19 14:02:04 +01:00
|
|
|
return 'Ghost'
|
|
|
|
else:
|
|
|
|
return 'Up'
|
|
|
|
else:
|
|
|
|
return 'Exit %s' % self.dictionary['State']['ExitCode']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def human_readable_command(self):
|
|
|
|
self.inspect_if_not_inspected()
|
2014-03-04 12:26:56 +01:00
|
|
|
if self.dictionary['Config']['Cmd']:
|
|
|
|
return ' '.join(self.dictionary['Config']['Cmd'])
|
|
|
|
else:
|
|
|
|
return ''
|
2013-12-19 14:02:04 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
@property
|
|
|
|
def environment(self):
|
|
|
|
self.inspect_if_not_inspected()
|
2014-08-02 22:31:08 +02:00
|
|
|
return dict(var.split("=", 1)
|
|
|
|
for var in self.dictionary.get('Config', {}).get('Env', []))
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
@property
|
|
|
|
def is_running(self):
|
|
|
|
self.inspect_if_not_inspected()
|
|
|
|
return self.dictionary['State']['Running']
|
|
|
|
|
2014-08-08 18:41:52 +02:00
|
|
|
def get_local_port(self, port, protocol='tcp'):
|
|
|
|
port = self.ports.get("%s/%s" % (port, protocol))
|
|
|
|
return "{HostIp}:{HostPort}".format(**port[0]) if port else None
|
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
def start(self, **options):
|
|
|
|
return self.client.start(self.id, **options)
|
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
def stop(self, **options):
|
|
|
|
return self.client.stop(self.id, **options)
|
2013-12-18 19:37:48 +01:00
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
return self.client.kill(self.id)
|
|
|
|
|
2014-03-03 10:55:00 +01:00
|
|
|
def remove(self, **options):
|
2014-03-04 11:50:09 +01:00
|
|
|
return self.client.remove_container(self.id, **options)
|
2013-12-18 19:37:48 +01:00
|
|
|
|
|
|
|
def inspect_if_not_inspected(self):
|
|
|
|
if not self.has_been_inspected:
|
|
|
|
self.inspect()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
return self.client.wait(self.id)
|
|
|
|
|
|
|
|
def logs(self, *args, **kwargs):
|
|
|
|
return self.client.logs(self.id, *args, **kwargs)
|
|
|
|
|
|
|
|
def inspect(self):
|
|
|
|
self.dictionary = self.client.inspect_container(self.id)
|
2014-08-02 22:31:08 +02:00
|
|
|
self.has_been_inspected = True
|
2013-12-18 19:37:48 +01:00
|
|
|
return self.dictionary
|
|
|
|
|
|
|
|
def links(self):
|
|
|
|
links = []
|
|
|
|
for container in self.client.containers():
|
|
|
|
for name in container['Names']:
|
|
|
|
bits = name.split('/')
|
2013-12-20 20:33:41 +01:00
|
|
|
if len(bits) > 2 and bits[1] == self.name:
|
2013-12-18 19:37:48 +01:00
|
|
|
links.append(bits[2])
|
|
|
|
return links
|
2013-12-18 20:01:53 +01:00
|
|
|
|
2014-01-13 17:23:10 +01:00
|
|
|
def attach(self, *args, **kwargs):
|
|
|
|
return self.client.attach(self.id, *args, **kwargs)
|
|
|
|
|
2013-12-18 20:01:53 +01:00
|
|
|
def attach_socket(self, **kwargs):
|
|
|
|
return self.client.attach_socket(self.id, **kwargs)
|
2013-12-20 11:46:55 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Container: %s>' % self.name
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if type(self) != type(other):
|
|
|
|
return False
|
|
|
|
return self.id == other.id
|