Skip to content

Commit b2aa220

Browse files
committed
Add plugin API implementation to DockerClient
Signed-off-by: Joffrey F <[email protected]>
1 parent c262dfe commit b2aa220

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

docker/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .models.images import ImageCollection
44
from .models.networks import NetworkCollection
55
from .models.nodes import NodeCollection
6+
from .models.plugins import PluginCollection
67
from .models.services import ServiceCollection
78
from .models.swarm import Swarm
89
from .models.volumes import VolumeCollection
@@ -109,6 +110,14 @@ def nodes(self):
109110
"""
110111
return NodeCollection(client=self)
111112

113+
@property
114+
def plugins(self):
115+
"""
116+
An object for managing plugins on the server. See the
117+
:doc:`plugins documentation <plugins>` for full details.
118+
"""
119+
return PluginCollection(client=self)
120+
112121
@property
113122
def services(self):
114123
"""

docker/models/plugins.py

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

0 commit comments

Comments
 (0)