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.

daemon.py 5.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import os
  2. from datetime import datetime
  3. from .. import auth, types, utils
  4. class DaemonApiMixin:
  5. @utils.minimum_version('1.25')
  6. def df(self):
  7. """
  8. Get data usage information.
  9. Returns:
  10. (dict): A dictionary representing different resource categories
  11. and their respective data usage.
  12. Raises:
  13. :py:class:`docker.errors.APIError`
  14. If the server returns an error.
  15. """
  16. url = self._url('/system/df')
  17. return self._result(self._get(url), True)
  18. def events(self, since=None, until=None, filters=None, decode=None):
  19. """
  20. Get real-time events from the server. Similar to the ``docker events``
  21. command.
  22. Args:
  23. since (UTC datetime or int): Get events from this point
  24. until (UTC datetime or int): Get events until this point
  25. filters (dict): Filter the events by event time, container or image
  26. decode (bool): If set to true, stream will be decoded into dicts on
  27. the fly. False by default.
  28. Returns:
  29. A :py:class:`docker.types.daemon.CancellableStream` generator
  30. Raises:
  31. :py:class:`docker.errors.APIError`
  32. If the server returns an error.
  33. Example:
  34. >>> for event in client.events(decode=True)
  35. ... print(event)
  36. {u'from': u'image/with:tag',
  37. u'id': u'container-id',
  38. u'status': u'start',
  39. u'time': 1423339459}
  40. ...
  41. or
  42. >>> events = client.events()
  43. >>> for event in events:
  44. ... print(event)
  45. >>> # and cancel from another thread
  46. >>> events.close()
  47. """
  48. if isinstance(since, datetime):
  49. since = utils.datetime_to_timestamp(since)
  50. if isinstance(until, datetime):
  51. until = utils.datetime_to_timestamp(until)
  52. if filters:
  53. filters = utils.convert_filters(filters)
  54. params = {
  55. 'since': since,
  56. 'until': until,
  57. 'filters': filters
  58. }
  59. url = self._url('/events')
  60. response = self._get(url, params=params, stream=True, timeout=None)
  61. stream = self._stream_helper(response, decode=decode)
  62. return types.CancellableStream(stream, response)
  63. def info(self):
  64. """
  65. Display system-wide information. Identical to the ``docker info``
  66. command.
  67. Returns:
  68. (dict): The info as a dict
  69. Raises:
  70. :py:class:`docker.errors.APIError`
  71. If the server returns an error.
  72. """
  73. return self._result(self._get(self._url("/info")), True)
  74. def login(self, username, password=None, email=None, registry=None,
  75. reauth=False, dockercfg_path=None):
  76. """
  77. Authenticate with a registry. Similar to the ``docker login`` command.
  78. Args:
  79. username (str): The registry username
  80. password (str): The plaintext password
  81. email (str): The email for the registry account
  82. registry (str): URL to the registry. E.g.
  83. ``https://index.docker.io/v1/``
  84. reauth (bool): Whether or not to refresh existing authentication on
  85. the Docker server.
  86. dockercfg_path (str): Use a custom path for the Docker config file
  87. (default ``$HOME/.docker/config.json`` if present,
  88. otherwise ``$HOME/.dockercfg``)
  89. Returns:
  90. (dict): The response from the login request
  91. Raises:
  92. :py:class:`docker.errors.APIError`
  93. If the server returns an error.
  94. """
  95. # If we don't have any auth data so far, try reloading the config file
  96. # one more time in case anything showed up in there.
  97. # If dockercfg_path is passed check to see if the config file exists,
  98. # if so load that config.
  99. if dockercfg_path and os.path.exists(dockercfg_path):
  100. self._auth_configs = auth.load_config(
  101. dockercfg_path, credstore_env=self.credstore_env
  102. )
  103. elif not self._auth_configs or self._auth_configs.is_empty:
  104. self._auth_configs = auth.load_config(
  105. credstore_env=self.credstore_env
  106. )
  107. authcfg = self._auth_configs.resolve_authconfig(registry)
  108. # If we found an existing auth config for this registry and username
  109. # combination, we can return it immediately unless reauth is requested.
  110. if authcfg and authcfg.get('username', None) == username \
  111. and not reauth:
  112. return authcfg
  113. req_data = {
  114. 'username': username,
  115. 'password': password,
  116. 'email': email,
  117. 'serveraddress': registry,
  118. }
  119. response = self._post_json(self._url('/auth'), data=req_data)
  120. if response.status_code == 200:
  121. self._auth_configs.add_auth(registry or auth.INDEX_NAME, req_data)
  122. return self._result(response, json=True)
  123. def ping(self):
  124. """
  125. Checks the server is responsive. An exception will be raised if it
  126. isn't responding.
  127. Returns:
  128. (bool) The response from the server.
  129. Raises:
  130. :py:class:`docker.errors.APIError`
  131. If the server returns an error.
  132. """
  133. return self._result(self._get(self._url('/_ping'))) == 'OK'
  134. def version(self, api_version=True):
  135. """
  136. Returns version information from the server. Similar to the ``docker
  137. version`` command.
  138. Returns:
  139. (dict): The server version information
  140. Raises:
  141. :py:class:`docker.errors.APIError`
  142. If the server returns an error.
  143. """
  144. url = self._url("/version", versioned_api=api_version)
  145. return self._result(self._get(url), json=True)