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.

plugins.py 5.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from .. import errors
  2. from .resource import Collection, Model
  3. class Plugin(Model):
  4. """
  5. A plugin on the server.
  6. """
  7. def __repr__(self):
  8. return f"<{self.__class__.__name__}: '{self.name}'>"
  9. @property
  10. def name(self):
  11. """
  12. The plugin's name.
  13. """
  14. return self.attrs.get('Name')
  15. @property
  16. def enabled(self):
  17. """
  18. Whether the plugin is enabled.
  19. """
  20. return self.attrs.get('Enabled')
  21. @property
  22. def settings(self):
  23. """
  24. A dictionary representing the plugin's configuration.
  25. """
  26. return self.attrs.get('Settings')
  27. def configure(self, options):
  28. """
  29. Update the plugin's settings.
  30. Args:
  31. options (dict): A key-value mapping of options.
  32. Raises:
  33. :py:class:`docker.errors.APIError`
  34. If the server returns an error.
  35. """
  36. self.client.api.configure_plugin(self.name, options)
  37. self.reload()
  38. def disable(self, force=False):
  39. """
  40. Disable the plugin.
  41. Args:
  42. force (bool): Force disable. Default: False
  43. Raises:
  44. :py:class:`docker.errors.APIError`
  45. If the server returns an error.
  46. """
  47. self.client.api.disable_plugin(self.name, force)
  48. self.reload()
  49. def enable(self, timeout=0):
  50. """
  51. Enable the plugin.
  52. Args:
  53. timeout (int): Timeout in seconds. Default: 0
  54. Raises:
  55. :py:class:`docker.errors.APIError`
  56. If the server returns an error.
  57. """
  58. self.client.api.enable_plugin(self.name, timeout)
  59. self.reload()
  60. def push(self):
  61. """
  62. Push the plugin to a remote registry.
  63. Returns:
  64. A dict iterator streaming the status of the upload.
  65. Raises:
  66. :py:class:`docker.errors.APIError`
  67. If the server returns an error.
  68. """
  69. return self.client.api.push_plugin(self.name)
  70. def remove(self, force=False):
  71. """
  72. Remove the plugin from the server.
  73. Args:
  74. force (bool): Remove even if the plugin is enabled.
  75. Default: False
  76. Raises:
  77. :py:class:`docker.errors.APIError`
  78. If the server returns an error.
  79. """
  80. return self.client.api.remove_plugin(self.name, force=force)
  81. def upgrade(self, remote=None):
  82. """
  83. Upgrade the plugin.
  84. Args:
  85. remote (string): Remote reference to upgrade to. The
  86. ``:latest`` tag is optional and is the default if omitted.
  87. Default: this plugin's name.
  88. Returns:
  89. A generator streaming the decoded API logs
  90. """
  91. if self.enabled:
  92. raise errors.DockerError(
  93. 'Plugin must be disabled before upgrading.'
  94. )
  95. if remote is None:
  96. remote = self.name
  97. privileges = self.client.api.plugin_privileges(remote)
  98. yield from self.client.api.upgrade_plugin(
  99. self.name,
  100. remote,
  101. privileges,
  102. )
  103. self.reload()
  104. class PluginCollection(Collection):
  105. model = Plugin
  106. def create(self, name, plugin_data_dir, gzip=False):
  107. """
  108. Create a new plugin.
  109. Args:
  110. name (string): The name of the plugin. The ``:latest`` tag is
  111. optional, and is the default if omitted.
  112. plugin_data_dir (string): Path to the plugin data directory.
  113. Plugin data directory must contain the ``config.json``
  114. manifest file and the ``rootfs`` directory.
  115. gzip (bool): Compress the context using gzip. Default: False
  116. Returns:
  117. (:py:class:`Plugin`): The newly created plugin.
  118. """
  119. self.client.api.create_plugin(name, plugin_data_dir, gzip)
  120. return self.get(name)
  121. def get(self, name):
  122. """
  123. Gets a plugin.
  124. Args:
  125. name (str): The name of the plugin.
  126. Returns:
  127. (:py:class:`Plugin`): The plugin.
  128. Raises:
  129. :py:class:`docker.errors.NotFound` If the plugin does not
  130. exist.
  131. :py:class:`docker.errors.APIError`
  132. If the server returns an error.
  133. """
  134. return self.prepare_model(self.client.api.inspect_plugin(name))
  135. def install(self, remote_name, local_name=None):
  136. """
  137. Pull and install a plugin.
  138. Args:
  139. remote_name (string): Remote reference for the plugin to
  140. install. The ``:latest`` tag is optional, and is the
  141. default if omitted.
  142. local_name (string): Local name for the pulled plugin.
  143. The ``:latest`` tag is optional, and is the default if
  144. omitted. Optional.
  145. Returns:
  146. (:py:class:`Plugin`): The installed plugin
  147. Raises:
  148. :py:class:`docker.errors.APIError`
  149. If the server returns an error.
  150. """
  151. privileges = self.client.api.plugin_privileges(remote_name)
  152. it = self.client.api.pull_plugin(remote_name, privileges, local_name)
  153. for data in it:
  154. pass
  155. return self.get(local_name or remote_name)
  156. def list(self):
  157. """
  158. List plugins installed on the server.
  159. Returns:
  160. (list of :py:class:`Plugin`): The plugins.
  161. Raises:
  162. :py:class:`docker.errors.APIError`
  163. If the server returns an error.
  164. """
  165. resp = self.client.api.plugins()
  166. return [self.prepare_model(r) for r in resp]