Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.py 7.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from .api.client import APIClient
  2. from .constants import (DEFAULT_TIMEOUT_SECONDS, DEFAULT_MAX_POOL_SIZE)
  3. from .models.configs import ConfigCollection
  4. from .models.containers import ContainerCollection
  5. from .models.images import ImageCollection
  6. from .models.networks import NetworkCollection
  7. from .models.nodes import NodeCollection
  8. from .models.plugins import PluginCollection
  9. from .models.secrets import SecretCollection
  10. from .models.services import ServiceCollection
  11. from .models.swarm import Swarm
  12. from .models.volumes import VolumeCollection
  13. from .utils import kwargs_from_env
  14. class DockerClient:
  15. """
  16. A client for communicating with a Docker server.
  17. Example:
  18. >>> import docker
  19. >>> client = docker.DockerClient(base_url='unix://var/run/docker.sock')
  20. Args:
  21. base_url (str): URL to the Docker server. For example,
  22. ``unix:///var/run/docker.sock`` or ``tcp://127.0.0.1:1234``.
  23. version (str): The version of the API to use. Set to ``auto`` to
  24. automatically detect the server's version. Default: ``1.35``
  25. timeout (int): Default timeout for API calls, in seconds.
  26. tls (bool or :py:class:`~docker.tls.TLSConfig`): Enable TLS. Pass
  27. ``True`` to enable it with default options, or pass a
  28. :py:class:`~docker.tls.TLSConfig` object to use custom
  29. configuration.
  30. user_agent (str): Set a custom user agent for requests to the server.
  31. credstore_env (dict): Override environment variables when calling the
  32. credential store process.
  33. use_ssh_client (bool): If set to `True`, an ssh connection is made
  34. via shelling out to the ssh client. Ensure the ssh client is
  35. installed and configured on the host.
  36. max_pool_size (int): The maximum number of connections
  37. to save in the pool.
  38. """
  39. def __init__(self, *args, **kwargs):
  40. self.api = APIClient(*args, **kwargs)
  41. @classmethod
  42. def from_env(cls, **kwargs):
  43. """
  44. Return a client configured from environment variables.
  45. The environment variables used are the same as those used by the
  46. Docker command-line client. They are:
  47. .. envvar:: DOCKER_HOST
  48. The URL to the Docker host.
  49. .. envvar:: DOCKER_TLS_VERIFY
  50. Verify the host against a CA certificate.
  51. .. envvar:: DOCKER_CERT_PATH
  52. A path to a directory containing TLS certificates to use when
  53. connecting to the Docker host.
  54. Args:
  55. version (str): The version of the API to use. Set to ``auto`` to
  56. automatically detect the server's version. Default: ``auto``
  57. timeout (int): Default timeout for API calls, in seconds.
  58. max_pool_size (int): The maximum number of connections
  59. to save in the pool.
  60. ssl_version (int): A valid `SSL version`_.
  61. assert_hostname (bool): Verify the hostname of the server.
  62. environment (dict): The environment to read environment variables
  63. from. Default: the value of ``os.environ``
  64. credstore_env (dict): Override environment variables when calling
  65. the credential store process.
  66. use_ssh_client (bool): If set to `True`, an ssh connection is
  67. made via shelling out to the ssh client. Ensure the ssh
  68. client is installed and configured on the host.
  69. Example:
  70. >>> import docker
  71. >>> client = docker.from_env()
  72. .. _`SSL version`:
  73. https://docs.python.org/3.5/library/ssl.html#ssl.PROTOCOL_TLSv1
  74. """
  75. timeout = kwargs.pop('timeout', DEFAULT_TIMEOUT_SECONDS)
  76. max_pool_size = kwargs.pop('max_pool_size', DEFAULT_MAX_POOL_SIZE)
  77. version = kwargs.pop('version', None)
  78. use_ssh_client = kwargs.pop('use_ssh_client', False)
  79. return cls(
  80. timeout=timeout,
  81. max_pool_size=max_pool_size,
  82. version=version,
  83. use_ssh_client=use_ssh_client,
  84. **kwargs_from_env(**kwargs)
  85. )
  86. # Resources
  87. @property
  88. def configs(self):
  89. """
  90. An object for managing configs on the server. See the
  91. :doc:`configs documentation <configs>` for full details.
  92. """
  93. return ConfigCollection(client=self)
  94. @property
  95. def containers(self):
  96. """
  97. An object for managing containers on the server. See the
  98. :doc:`containers documentation <containers>` for full details.
  99. """
  100. return ContainerCollection(client=self)
  101. @property
  102. def images(self):
  103. """
  104. An object for managing images on the server. See the
  105. :doc:`images documentation <images>` for full details.
  106. """
  107. return ImageCollection(client=self)
  108. @property
  109. def networks(self):
  110. """
  111. An object for managing networks on the server. See the
  112. :doc:`networks documentation <networks>` for full details.
  113. """
  114. return NetworkCollection(client=self)
  115. @property
  116. def nodes(self):
  117. """
  118. An object for managing nodes on the server. See the
  119. :doc:`nodes documentation <nodes>` for full details.
  120. """
  121. return NodeCollection(client=self)
  122. @property
  123. def plugins(self):
  124. """
  125. An object for managing plugins on the server. See the
  126. :doc:`plugins documentation <plugins>` for full details.
  127. """
  128. return PluginCollection(client=self)
  129. @property
  130. def secrets(self):
  131. """
  132. An object for managing secrets on the server. See the
  133. :doc:`secrets documentation <secrets>` for full details.
  134. """
  135. return SecretCollection(client=self)
  136. @property
  137. def services(self):
  138. """
  139. An object for managing services on the server. See the
  140. :doc:`services documentation <services>` for full details.
  141. """
  142. return ServiceCollection(client=self)
  143. @property
  144. def swarm(self):
  145. """
  146. An object for managing a swarm on the server. See the
  147. :doc:`swarm documentation <swarm>` for full details.
  148. """
  149. return Swarm(client=self)
  150. @property
  151. def volumes(self):
  152. """
  153. An object for managing volumes on the server. See the
  154. :doc:`volumes documentation <volumes>` for full details.
  155. """
  156. return VolumeCollection(client=self)
  157. # Top-level methods
  158. def events(self, *args, **kwargs):
  159. return self.api.events(*args, **kwargs)
  160. events.__doc__ = APIClient.events.__doc__
  161. def df(self):
  162. return self.api.df()
  163. df.__doc__ = APIClient.df.__doc__
  164. def info(self, *args, **kwargs):
  165. return self.api.info(*args, **kwargs)
  166. info.__doc__ = APIClient.info.__doc__
  167. def login(self, *args, **kwargs):
  168. return self.api.login(*args, **kwargs)
  169. login.__doc__ = APIClient.login.__doc__
  170. def ping(self, *args, **kwargs):
  171. return self.api.ping(*args, **kwargs)
  172. ping.__doc__ = APIClient.ping.__doc__
  173. def version(self, *args, **kwargs):
  174. return self.api.version(*args, **kwargs)
  175. version.__doc__ = APIClient.version.__doc__
  176. def close(self):
  177. return self.api.close()
  178. close.__doc__ = APIClient.close.__doc__
  179. def __getattr__(self, name):
  180. s = [f"'DockerClient' object has no attribute '{name}'"]
  181. # If a user calls a method on APIClient, they
  182. if hasattr(APIClient, name):
  183. s.append("In Docker SDK for Python 2.0, this method is now on the "
  184. "object APIClient. See the low-level API section of the "
  185. "documentation for more details.")
  186. raise AttributeError(' '.join(s))
  187. from_env = DockerClient.from_env