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.

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import json
  2. import os
  3. from docker import errors
  4. from docker.context.config import get_meta_dir
  5. from docker.context.config import METAFILE
  6. from docker.context.config import get_current_context_name
  7. from docker.context.config import write_context_name_to_docker_config
  8. from docker.context import Context
  9. class ContextAPI:
  10. """Context API.
  11. Contains methods for context management:
  12. create, list, remove, get, inspect.
  13. """
  14. DEFAULT_CONTEXT = Context("default", "swarm")
  15. @classmethod
  16. def create_context(
  17. cls, name, orchestrator=None, host=None, tls_cfg=None,
  18. default_namespace=None, skip_tls_verify=False):
  19. """Creates a new context.
  20. Returns:
  21. (Context): a Context object.
  22. Raises:
  23. :py:class:`docker.errors.MissingContextParameter`
  24. If a context name is not provided.
  25. :py:class:`docker.errors.ContextAlreadyExists`
  26. If a context with the name already exists.
  27. :py:class:`docker.errors.ContextException`
  28. If name is default.
  29. Example:
  30. >>> from docker.context import ContextAPI
  31. >>> ctx = ContextAPI.create_context(name='test')
  32. >>> print(ctx.Metadata)
  33. {
  34. "Name": "test",
  35. "Metadata": {},
  36. "Endpoints": {
  37. "docker": {
  38. "Host": "unix:///var/run/docker.sock",
  39. "SkipTLSVerify": false
  40. }
  41. }
  42. }
  43. """
  44. if not name:
  45. raise errors.MissingContextParameter("name")
  46. if name == "default":
  47. raise errors.ContextException(
  48. '"default" is a reserved context name')
  49. ctx = Context.load_context(name)
  50. if ctx:
  51. raise errors.ContextAlreadyExists(name)
  52. endpoint = "docker"
  53. if orchestrator and orchestrator != "swarm":
  54. endpoint = orchestrator
  55. ctx = Context(name, orchestrator)
  56. ctx.set_endpoint(
  57. endpoint, host, tls_cfg,
  58. skip_tls_verify=skip_tls_verify,
  59. def_namespace=default_namespace)
  60. ctx.save()
  61. return ctx
  62. @classmethod
  63. def get_context(cls, name=None):
  64. """Retrieves a context object.
  65. Args:
  66. name (str): The name of the context
  67. Example:
  68. >>> from docker.context import ContextAPI
  69. >>> ctx = ContextAPI.get_context(name='test')
  70. >>> print(ctx.Metadata)
  71. {
  72. "Name": "test",
  73. "Metadata": {},
  74. "Endpoints": {
  75. "docker": {
  76. "Host": "unix:///var/run/docker.sock",
  77. "SkipTLSVerify": false
  78. }
  79. }
  80. }
  81. """
  82. if not name:
  83. name = get_current_context_name()
  84. if name == "default":
  85. return cls.DEFAULT_CONTEXT
  86. return Context.load_context(name)
  87. @classmethod
  88. def contexts(cls):
  89. """Context list.
  90. Returns:
  91. (Context): List of context objects.
  92. Raises:
  93. :py:class:`docker.errors.APIError`
  94. If the server returns an error.
  95. """
  96. names = []
  97. for dirname, dirnames, fnames in os.walk(get_meta_dir()):
  98. for filename in fnames + dirnames:
  99. if filename == METAFILE:
  100. try:
  101. data = json.load(
  102. open(os.path.join(dirname, filename)))
  103. names.append(data["Name"])
  104. except Exception as e:
  105. raise errors.ContextException(
  106. "Failed to load metafile {}: {}".format(
  107. filename, e))
  108. contexts = [cls.DEFAULT_CONTEXT]
  109. for name in names:
  110. contexts.append(Context.load_context(name))
  111. return contexts
  112. @classmethod
  113. def get_current_context(cls):
  114. """Get current context.
  115. Returns:
  116. (Context): current context object.
  117. """
  118. return cls.get_context()
  119. @classmethod
  120. def set_current_context(cls, name="default"):
  121. ctx = cls.get_context(name)
  122. if not ctx:
  123. raise errors.ContextNotFound(name)
  124. err = write_context_name_to_docker_config(name)
  125. if err:
  126. raise errors.ContextException(
  127. f'Failed to set current context: {err}')
  128. @classmethod
  129. def remove_context(cls, name):
  130. """Remove a context. Similar to the ``docker context rm`` command.
  131. Args:
  132. name (str): The name of the context
  133. Raises:
  134. :py:class:`docker.errors.MissingContextParameter`
  135. If a context name is not provided.
  136. :py:class:`docker.errors.ContextNotFound`
  137. If a context with the name does not exist.
  138. :py:class:`docker.errors.ContextException`
  139. If name is default.
  140. Example:
  141. >>> from docker.context import ContextAPI
  142. >>> ContextAPI.remove_context(name='test')
  143. >>>
  144. """
  145. if not name:
  146. raise errors.MissingContextParameter("name")
  147. if name == "default":
  148. raise errors.ContextException(
  149. 'context "default" cannot be removed')
  150. ctx = Context.load_context(name)
  151. if not ctx:
  152. raise errors.ContextNotFound(name)
  153. if name == get_current_context_name():
  154. write_context_name_to_docker_config(None)
  155. ctx.remove()
  156. @classmethod
  157. def inspect_context(cls, name="default"):
  158. """Remove a context. Similar to the ``docker context inspect`` command.
  159. Args:
  160. name (str): The name of the context
  161. Raises:
  162. :py:class:`docker.errors.MissingContextParameter`
  163. If a context name is not provided.
  164. :py:class:`docker.errors.ContextNotFound`
  165. If a context with the name does not exist.
  166. Example:
  167. >>> from docker.context import ContextAPI
  168. >>> ContextAPI.remove_context(name='test')
  169. >>>
  170. """
  171. if not name:
  172. raise errors.MissingContextParameter("name")
  173. if name == "default":
  174. return cls.DEFAULT_CONTEXT()
  175. ctx = Context.load_context(name)
  176. if not ctx:
  177. raise errors.ContextNotFound(name)
  178. return ctx()