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.

plugin.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from .. import auth, utils
  2. class PluginApiMixin:
  3. @utils.minimum_version('1.25')
  4. @utils.check_resource('name')
  5. def configure_plugin(self, name, options):
  6. """
  7. Configure a plugin.
  8. Args:
  9. name (string): The name of the plugin. The ``:latest`` tag is
  10. optional, and is the default if omitted.
  11. options (dict): A key-value mapping of options
  12. Returns:
  13. ``True`` if successful
  14. """
  15. url = self._url('/plugins/{0}/set', name)
  16. data = options
  17. if isinstance(data, dict):
  18. data = [f'{k}={v}' for k, v in data.items()]
  19. res = self._post_json(url, data=data)
  20. self._raise_for_status(res)
  21. return True
  22. @utils.minimum_version('1.25')
  23. def create_plugin(self, name, plugin_data_dir, gzip=False):
  24. """
  25. Create a new plugin.
  26. Args:
  27. name (string): The name of the plugin. The ``:latest`` tag is
  28. optional, and is the default if omitted.
  29. plugin_data_dir (string): Path to the plugin data directory.
  30. Plugin data directory must contain the ``config.json``
  31. manifest file and the ``rootfs`` directory.
  32. gzip (bool): Compress the context using gzip. Default: False
  33. Returns:
  34. ``True`` if successful
  35. """
  36. url = self._url('/plugins/create')
  37. with utils.create_archive(
  38. root=plugin_data_dir, gzip=gzip,
  39. files=set(utils.build.walk(plugin_data_dir, []))
  40. ) as archv:
  41. res = self._post(url, params={'name': name}, data=archv)
  42. self._raise_for_status(res)
  43. return True
  44. @utils.minimum_version('1.25')
  45. def disable_plugin(self, name, force=False):
  46. """
  47. Disable an installed plugin.
  48. Args:
  49. name (string): The name of the plugin. The ``:latest`` tag is
  50. optional, and is the default if omitted.
  51. force (bool): To enable the force query parameter.
  52. Returns:
  53. ``True`` if successful
  54. """
  55. url = self._url('/plugins/{0}/disable', name)
  56. res = self._post(url, params={'force': force})
  57. self._raise_for_status(res)
  58. return True
  59. @utils.minimum_version('1.25')
  60. def enable_plugin(self, name, timeout=0):
  61. """
  62. Enable an installed plugin.
  63. Args:
  64. name (string): The name of the plugin. The ``:latest`` tag is
  65. optional, and is the default if omitted.
  66. timeout (int): Operation timeout (in seconds). Default: 0
  67. Returns:
  68. ``True`` if successful
  69. """
  70. url = self._url('/plugins/{0}/enable', name)
  71. params = {'timeout': timeout}
  72. res = self._post(url, params=params)
  73. self._raise_for_status(res)
  74. return True
  75. @utils.minimum_version('1.25')
  76. def inspect_plugin(self, name):
  77. """
  78. Retrieve plugin metadata.
  79. Args:
  80. name (string): The name of the plugin. The ``:latest`` tag is
  81. optional, and is the default if omitted.
  82. Returns:
  83. A dict containing plugin info
  84. """
  85. url = self._url('/plugins/{0}/json', name)
  86. return self._result(self._get(url), True)
  87. @utils.minimum_version('1.25')
  88. def pull_plugin(self, remote, privileges, name=None):
  89. """
  90. Pull and install a plugin. After the plugin is installed, it can be
  91. enabled using :py:meth:`~enable_plugin`.
  92. Args:
  93. remote (string): Remote reference for the plugin to install.
  94. The ``:latest`` tag is optional, and is the default if
  95. omitted.
  96. privileges (:py:class:`list`): A list of privileges the user
  97. consents to grant to the plugin. Can be retrieved using
  98. :py:meth:`~plugin_privileges`.
  99. name (string): Local name for the pulled plugin. The
  100. ``:latest`` tag is optional, and is the default if omitted.
  101. Returns:
  102. An iterable object streaming the decoded API logs
  103. """
  104. url = self._url('/plugins/pull')
  105. params = {
  106. 'remote': remote,
  107. }
  108. if name:
  109. params['name'] = name
  110. headers = {}
  111. registry, repo_name = auth.resolve_repository_name(remote)
  112. header = auth.get_config_header(self, registry)
  113. if header:
  114. headers['X-Registry-Auth'] = header
  115. response = self._post_json(
  116. url, params=params, headers=headers, data=privileges,
  117. stream=True
  118. )
  119. self._raise_for_status(response)
  120. return self._stream_helper(response, decode=True)
  121. @utils.minimum_version('1.25')
  122. def plugins(self):
  123. """
  124. Retrieve a list of installed plugins.
  125. Returns:
  126. A list of dicts, one per plugin
  127. """
  128. url = self._url('/plugins')
  129. return self._result(self._get(url), True)
  130. @utils.minimum_version('1.25')
  131. def plugin_privileges(self, name):
  132. """
  133. Retrieve list of privileges to be granted to a plugin.
  134. Args:
  135. name (string): Name of the remote plugin to examine. The
  136. ``:latest`` tag is optional, and is the default if omitted.
  137. Returns:
  138. A list of dictionaries representing the plugin's
  139. permissions
  140. """
  141. params = {
  142. 'remote': name,
  143. }
  144. headers = {}
  145. registry, repo_name = auth.resolve_repository_name(name)
  146. header = auth.get_config_header(self, registry)
  147. if header:
  148. headers['X-Registry-Auth'] = header
  149. url = self._url('/plugins/privileges')
  150. return self._result(
  151. self._get(url, params=params, headers=headers), True
  152. )
  153. @utils.minimum_version('1.25')
  154. @utils.check_resource('name')
  155. def push_plugin(self, name):
  156. """
  157. Push a plugin to the registry.
  158. Args:
  159. name (string): Name of the plugin to upload. The ``:latest``
  160. tag is optional, and is the default if omitted.
  161. Returns:
  162. ``True`` if successful
  163. """
  164. url = self._url('/plugins/{0}/pull', name)
  165. headers = {}
  166. registry, repo_name = auth.resolve_repository_name(name)
  167. header = auth.get_config_header(self, registry)
  168. if header:
  169. headers['X-Registry-Auth'] = header
  170. res = self._post(url, headers=headers)
  171. self._raise_for_status(res)
  172. return self._stream_helper(res, decode=True)
  173. @utils.minimum_version('1.25')
  174. @utils.check_resource('name')
  175. def remove_plugin(self, name, force=False):
  176. """
  177. Remove an installed plugin.
  178. Args:
  179. name (string): Name of the plugin to remove. The ``:latest``
  180. tag is optional, and is the default if omitted.
  181. force (bool): Disable the plugin before removing. This may
  182. result in issues if the plugin is in use by a container.
  183. Returns:
  184. ``True`` if successful
  185. """
  186. url = self._url('/plugins/{0}', name)
  187. res = self._delete(url, params={'force': force})
  188. self._raise_for_status(res)
  189. return True
  190. @utils.minimum_version('1.26')
  191. @utils.check_resource('name')
  192. def upgrade_plugin(self, name, remote, privileges):
  193. """
  194. Upgrade an installed plugin.
  195. Args:
  196. name (string): Name of the plugin to upgrade. The ``:latest``
  197. tag is optional and is the default if omitted.
  198. remote (string): Remote reference to upgrade to. The
  199. ``:latest`` tag is optional and is the default if omitted.
  200. privileges (:py:class:`list`): A list of privileges the user
  201. consents to grant to the plugin. Can be retrieved using
  202. :py:meth:`~plugin_privileges`.
  203. Returns:
  204. An iterable object streaming the decoded API logs
  205. """
  206. url = self._url('/plugins/{0}/upgrade', name)
  207. params = {
  208. 'remote': remote,
  209. }
  210. headers = {}
  211. registry, repo_name = auth.resolve_repository_name(remote)
  212. header = auth.get_config_header(self, registry)
  213. if header:
  214. headers['X-Registry-Auth'] = header
  215. response = self._post_json(
  216. url, params=params, headers=headers, data=privileges,
  217. stream=True
  218. )
  219. self._raise_for_status(response)
  220. return self._stream_helper(response, decode=True)