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