container.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. class Container(object):
  2. """
  3. Represents a Docker container, constructed from the output of
  4. GET /containers/:id:/json.
  5. """
  6. def __init__(self, client, dictionary, has_been_inspected=False):
  7. self.client = client
  8. self.dictionary = dictionary
  9. self.has_been_inspected = has_been_inspected
  10. @classmethod
  11. def from_ps(cls, client, dictionary, **kwargs):
  12. """
  13. Construct a container object from the output of GET /containers/json.
  14. """
  15. new_dictionary = {
  16. 'ID': dictionary['Id'],
  17. 'Image': dictionary['Image'],
  18. }
  19. for name in dictionary.get('Names', []):
  20. if len(name.split('/')) == 2:
  21. new_dictionary['Name'] = name
  22. return cls(client, new_dictionary, **kwargs)
  23. @classmethod
  24. def from_id(cls, client, id):
  25. return cls(client, client.inspect_container(id))
  26. @classmethod
  27. def create(cls, client, **options):
  28. response = client.create_container(**options)
  29. return cls.from_id(client, response['Id'])
  30. @property
  31. def id(self):
  32. return self.dictionary['ID']
  33. @property
  34. def short_id(self):
  35. return self.id[:10]
  36. @property
  37. def name(self):
  38. return self.dictionary['Name']
  39. @property
  40. def human_readable_ports(self):
  41. self.inspect_if_not_inspected()
  42. if not self.dictionary['NetworkSettings']['Ports']:
  43. return ''
  44. ports = []
  45. for private, public in self.dictionary['NetworkSettings']['Ports'].items():
  46. if public:
  47. ports.append('%s->%s' % (public[0]['HostPort'], private))
  48. return ', '.join(ports)
  49. @property
  50. def human_readable_state(self):
  51. self.inspect_if_not_inspected()
  52. if self.dictionary['State']['Running']:
  53. if self.dictionary['State']['Ghost']:
  54. return 'Ghost'
  55. else:
  56. return 'Up'
  57. else:
  58. return 'Exit %s' % self.dictionary['State']['ExitCode']
  59. @property
  60. def human_readable_command(self):
  61. self.inspect_if_not_inspected()
  62. return ' '.join(self.dictionary['Config']['Cmd'])
  63. @property
  64. def environment(self):
  65. self.inspect_if_not_inspected()
  66. out = {}
  67. for var in self.dictionary.get('Config', {}).get('Env', []):
  68. k, v = var.split('=', 1)
  69. out[k] = v
  70. return out
  71. def start(self, **options):
  72. return self.client.start(self.id, **options)
  73. def stop(self):
  74. return self.client.stop(self.id)
  75. def kill(self):
  76. return self.client.kill(self.id)
  77. def remove(self):
  78. return self.client.remove_container(self.id)
  79. def inspect_if_not_inspected(self):
  80. if not self.has_been_inspected:
  81. self.inspect()
  82. def wait(self):
  83. return self.client.wait(self.id)
  84. def logs(self, *args, **kwargs):
  85. return self.client.logs(self.id, *args, **kwargs)
  86. def inspect(self):
  87. self.dictionary = self.client.inspect_container(self.id)
  88. return self.dictionary
  89. def links(self):
  90. links = []
  91. for container in self.client.containers():
  92. for name in container['Names']:
  93. bits = name.split('/')
  94. if len(bits) > 2 and bits[1] == self.name[1:]:
  95. links.append(bits[2])
  96. return links
  97. def attach_socket(self, **kwargs):
  98. return self.client.attach_socket(self.id, **kwargs)