Browse code

explicitly show plugins as unsupported on !linux

Signed-off-by: Victor Vieux <victorvieux@gmail.com>

Victor Vieux authored on 2016/11/16 07:59:08
Showing 3 changed files
1 1
deleted file mode 100644
... ...
@@ -1,214 +0,0 @@
1
-package plugin
2
-
3
-import (
4
-	"bytes"
5
-	"encoding/json"
6
-	"fmt"
7
-	"io"
8
-	"io/ioutil"
9
-	"net/http"
10
-	"os"
11
-	"path/filepath"
12
-
13
-	"github.com/Sirupsen/logrus"
14
-	"github.com/docker/docker/api/types"
15
-	"github.com/docker/docker/pkg/archive"
16
-	"github.com/docker/docker/pkg/chrootarchive"
17
-	"github.com/docker/docker/pkg/stringid"
18
-	"github.com/docker/docker/plugin/distribution"
19
-	"github.com/docker/docker/plugin/v2"
20
-	"golang.org/x/net/context"
21
-)
22
-
23
-// Disable deactivates a plugin, which implies that they cannot be used by containers.
24
-func (pm *Manager) Disable(name string) error {
25
-	p, err := pm.pluginStore.GetByName(name)
26
-	if err != nil {
27
-		return err
28
-	}
29
-	if err := pm.disable(p); err != nil {
30
-		return err
31
-	}
32
-	pm.pluginEventLogger(p.GetID(), name, "disable")
33
-	return nil
34
-}
35
-
36
-// Enable activates a plugin, which implies that they are ready to be used by containers.
37
-func (pm *Manager) Enable(name string) error {
38
-	p, err := pm.pluginStore.GetByName(name)
39
-	if err != nil {
40
-		return err
41
-	}
42
-	if err := pm.enable(p, false); err != nil {
43
-		return err
44
-	}
45
-	pm.pluginEventLogger(p.GetID(), name, "enable")
46
-	return nil
47
-}
48
-
49
-// Inspect examines a plugin config
50
-func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
51
-	p, err := pm.pluginStore.GetByName(name)
52
-	if err != nil {
53
-		return tp, err
54
-	}
55
-	return p.PluginObj, nil
56
-}
57
-
58
-// Pull pulls a plugin and computes the privileges required to install it.
59
-func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
60
-	ref, err := distribution.GetRef(name)
61
-	if err != nil {
62
-		logrus.Debugf("error in distribution.GetRef: %v", err)
63
-		return nil, err
64
-	}
65
-	name = ref.String()
66
-
67
-	if p, _ := pm.pluginStore.GetByName(name); p != nil {
68
-		logrus.Debug("plugin already exists")
69
-		return nil, fmt.Errorf("%s exists", name)
70
-	}
71
-
72
-	pluginID := stringid.GenerateNonCryptoID()
73
-
74
-	if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
75
-		logrus.Debugf("error in MkdirAll: %v", err)
76
-		return nil, err
77
-	}
78
-
79
-	pd, err := distribution.Pull(ref, pm.registryService, metaHeader, authConfig)
80
-	if err != nil {
81
-		logrus.Debugf("error in distribution.Pull(): %v", err)
82
-		return nil, err
83
-	}
84
-
85
-	if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
86
-		logrus.Debugf("error in distribution.WritePullData(): %v", err)
87
-		return nil, err
88
-	}
89
-
90
-	tag := distribution.GetTag(ref)
91
-	p := v2.NewPlugin(ref.Name(), pluginID, pm.runRoot, pm.libRoot, tag)
92
-	if err := p.InitPlugin(); err != nil {
93
-		return nil, err
94
-	}
95
-	pm.pluginStore.Add(p)
96
-
97
-	pm.pluginEventLogger(pluginID, name, "pull")
98
-	return p.ComputePrivileges(), nil
99
-}
100
-
101
-// List displays the list of plugins and associated metadata.
102
-func (pm *Manager) List() ([]types.Plugin, error) {
103
-	plugins := pm.pluginStore.GetAll()
104
-	out := make([]types.Plugin, 0, len(plugins))
105
-	for _, p := range plugins {
106
-		out = append(out, p.PluginObj)
107
-	}
108
-	return out, nil
109
-}
110
-
111
-// Push pushes a plugin to the store.
112
-func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
113
-	p, err := pm.pluginStore.GetByName(name)
114
-	if err != nil {
115
-		return err
116
-	}
117
-	dest := filepath.Join(pm.libRoot, p.GetID())
118
-	config, err := ioutil.ReadFile(filepath.Join(dest, "config.json"))
119
-	if err != nil {
120
-		return err
121
-	}
122
-
123
-	var dummy types.Plugin
124
-	err = json.Unmarshal(config, &dummy)
125
-	if err != nil {
126
-		return err
127
-	}
128
-
129
-	rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
130
-	if err != nil {
131
-		return err
132
-	}
133
-	defer rootfs.Close()
134
-
135
-	_, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, ioutil.NopCloser(bytes.NewReader(config)), rootfs)
136
-	// XXX: Ignore returning digest for now.
137
-	// Since digest needs to be written to the ProgressWriter.
138
-	return err
139
-}
140
-
141
-// Remove deletes plugin's root directory.
142
-func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
143
-	p, err := pm.pluginStore.GetByName(name)
144
-	if err != nil {
145
-		return err
146
-	}
147
-
148
-	if !config.ForceRemove {
149
-		p.RLock()
150
-		if p.RefCount > 0 {
151
-			p.RUnlock()
152
-			return fmt.Errorf("plugin %s is in use", p.Name())
153
-		}
154
-		p.RUnlock()
155
-
156
-		if p.IsEnabled() {
157
-			return fmt.Errorf("plugin %s is enabled", p.Name())
158
-		}
159
-	}
160
-
161
-	if p.IsEnabled() {
162
-		if err := pm.disable(p); err != nil {
163
-			logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
164
-		}
165
-	}
166
-
167
-	pm.pluginStore.Remove(p)
168
-	os.RemoveAll(filepath.Join(pm.libRoot, p.GetID()))
169
-	pm.pluginEventLogger(p.GetID(), name, "remove")
170
-	return nil
171
-}
172
-
173
-// Set sets plugin args
174
-func (pm *Manager) Set(name string, args []string) error {
175
-	p, err := pm.pluginStore.GetByName(name)
176
-	if err != nil {
177
-		return err
178
-	}
179
-	return p.Set(args)
180
-}
181
-
182
-// CreateFromContext creates a plugin from the given pluginDir which contains
183
-// both the rootfs and the config.json and a repoName with optional tag.
184
-func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.Reader, options *types.PluginCreateOptions) error {
185
-	pluginID := stringid.GenerateNonCryptoID()
186
-
187
-	pluginDir := filepath.Join(pm.libRoot, pluginID)
188
-	if err := os.MkdirAll(pluginDir, 0755); err != nil {
189
-		return err
190
-	}
191
-
192
-	if err := chrootarchive.Untar(tarCtx, pluginDir, nil); err != nil {
193
-		return err
194
-	}
195
-
196
-	repoName := options.RepoName
197
-	ref, err := distribution.GetRef(repoName)
198
-	if err != nil {
199
-		return err
200
-	}
201
-	name := ref.Name()
202
-	tag := distribution.GetTag(ref)
203
-
204
-	p := v2.NewPlugin(name, pluginID, pm.runRoot, pm.libRoot, tag)
205
-	if err := p.InitPlugin(); err != nil {
206
-		return err
207
-	}
208
-
209
-	pm.pluginStore.Add(p)
210
-
211
-	pm.pluginEventLogger(p.GetID(), repoName, "create")
212
-
213
-	return nil
214
-}
215 1
new file mode 100644
... ...
@@ -0,0 +1,216 @@
0
+// +build linux
1
+
2
+package plugin
3
+
4
+import (
5
+	"bytes"
6
+	"encoding/json"
7
+	"fmt"
8
+	"io"
9
+	"io/ioutil"
10
+	"net/http"
11
+	"os"
12
+	"path/filepath"
13
+
14
+	"github.com/Sirupsen/logrus"
15
+	"github.com/docker/docker/api/types"
16
+	"github.com/docker/docker/pkg/archive"
17
+	"github.com/docker/docker/pkg/chrootarchive"
18
+	"github.com/docker/docker/pkg/stringid"
19
+	"github.com/docker/docker/plugin/distribution"
20
+	"github.com/docker/docker/plugin/v2"
21
+	"golang.org/x/net/context"
22
+)
23
+
24
+// Disable deactivates a plugin, which implies that they cannot be used by containers.
25
+func (pm *Manager) Disable(name string) error {
26
+	p, err := pm.pluginStore.GetByName(name)
27
+	if err != nil {
28
+		return err
29
+	}
30
+	if err := pm.disable(p); err != nil {
31
+		return err
32
+	}
33
+	pm.pluginEventLogger(p.GetID(), name, "disable")
34
+	return nil
35
+}
36
+
37
+// Enable activates a plugin, which implies that they are ready to be used by containers.
38
+func (pm *Manager) Enable(name string) error {
39
+	p, err := pm.pluginStore.GetByName(name)
40
+	if err != nil {
41
+		return err
42
+	}
43
+	if err := pm.enable(p, false); err != nil {
44
+		return err
45
+	}
46
+	pm.pluginEventLogger(p.GetID(), name, "enable")
47
+	return nil
48
+}
49
+
50
+// Inspect examines a plugin config
51
+func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
52
+	p, err := pm.pluginStore.GetByName(name)
53
+	if err != nil {
54
+		return tp, err
55
+	}
56
+	return p.PluginObj, nil
57
+}
58
+
59
+// Pull pulls a plugin and computes the privileges required to install it.
60
+func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
61
+	ref, err := distribution.GetRef(name)
62
+	if err != nil {
63
+		logrus.Debugf("error in distribution.GetRef: %v", err)
64
+		return nil, err
65
+	}
66
+	name = ref.String()
67
+
68
+	if p, _ := pm.pluginStore.GetByName(name); p != nil {
69
+		logrus.Debug("plugin already exists")
70
+		return nil, fmt.Errorf("%s exists", name)
71
+	}
72
+
73
+	pluginID := stringid.GenerateNonCryptoID()
74
+
75
+	if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
76
+		logrus.Debugf("error in MkdirAll: %v", err)
77
+		return nil, err
78
+	}
79
+
80
+	pd, err := distribution.Pull(ref, pm.registryService, metaHeader, authConfig)
81
+	if err != nil {
82
+		logrus.Debugf("error in distribution.Pull(): %v", err)
83
+		return nil, err
84
+	}
85
+
86
+	if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
87
+		logrus.Debugf("error in distribution.WritePullData(): %v", err)
88
+		return nil, err
89
+	}
90
+
91
+	tag := distribution.GetTag(ref)
92
+	p := v2.NewPlugin(ref.Name(), pluginID, pm.runRoot, pm.libRoot, tag)
93
+	if err := p.InitPlugin(); err != nil {
94
+		return nil, err
95
+	}
96
+	pm.pluginStore.Add(p)
97
+
98
+	pm.pluginEventLogger(pluginID, name, "pull")
99
+	return p.ComputePrivileges(), nil
100
+}
101
+
102
+// List displays the list of plugins and associated metadata.
103
+func (pm *Manager) List() ([]types.Plugin, error) {
104
+	plugins := pm.pluginStore.GetAll()
105
+	out := make([]types.Plugin, 0, len(plugins))
106
+	for _, p := range plugins {
107
+		out = append(out, p.PluginObj)
108
+	}
109
+	return out, nil
110
+}
111
+
112
+// Push pushes a plugin to the store.
113
+func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
114
+	p, err := pm.pluginStore.GetByName(name)
115
+	if err != nil {
116
+		return err
117
+	}
118
+	dest := filepath.Join(pm.libRoot, p.GetID())
119
+	config, err := ioutil.ReadFile(filepath.Join(dest, "config.json"))
120
+	if err != nil {
121
+		return err
122
+	}
123
+
124
+	var dummy types.Plugin
125
+	err = json.Unmarshal(config, &dummy)
126
+	if err != nil {
127
+		return err
128
+	}
129
+
130
+	rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
131
+	if err != nil {
132
+		return err
133
+	}
134
+	defer rootfs.Close()
135
+
136
+	_, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, ioutil.NopCloser(bytes.NewReader(config)), rootfs)
137
+	// XXX: Ignore returning digest for now.
138
+	// Since digest needs to be written to the ProgressWriter.
139
+	return err
140
+}
141
+
142
+// Remove deletes plugin's root directory.
143
+func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
144
+	p, err := pm.pluginStore.GetByName(name)
145
+	if err != nil {
146
+		return err
147
+	}
148
+
149
+	if !config.ForceRemove {
150
+		p.RLock()
151
+		if p.RefCount > 0 {
152
+			p.RUnlock()
153
+			return fmt.Errorf("plugin %s is in use", p.Name())
154
+		}
155
+		p.RUnlock()
156
+
157
+		if p.IsEnabled() {
158
+			return fmt.Errorf("plugin %s is enabled", p.Name())
159
+		}
160
+	}
161
+
162
+	if p.IsEnabled() {
163
+		if err := pm.disable(p); err != nil {
164
+			logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
165
+		}
166
+	}
167
+
168
+	pm.pluginStore.Remove(p)
169
+	os.RemoveAll(filepath.Join(pm.libRoot, p.GetID()))
170
+	pm.pluginEventLogger(p.GetID(), name, "remove")
171
+	return nil
172
+}
173
+
174
+// Set sets plugin args
175
+func (pm *Manager) Set(name string, args []string) error {
176
+	p, err := pm.pluginStore.GetByName(name)
177
+	if err != nil {
178
+		return err
179
+	}
180
+	return p.Set(args)
181
+}
182
+
183
+// CreateFromContext creates a plugin from the given pluginDir which contains
184
+// both the rootfs and the config.json and a repoName with optional tag.
185
+func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.Reader, options *types.PluginCreateOptions) error {
186
+	pluginID := stringid.GenerateNonCryptoID()
187
+
188
+	pluginDir := filepath.Join(pm.libRoot, pluginID)
189
+	if err := os.MkdirAll(pluginDir, 0755); err != nil {
190
+		return err
191
+	}
192
+
193
+	if err := chrootarchive.Untar(tarCtx, pluginDir, nil); err != nil {
194
+		return err
195
+	}
196
+
197
+	repoName := options.RepoName
198
+	ref, err := distribution.GetRef(repoName)
199
+	if err != nil {
200
+		return err
201
+	}
202
+	name := ref.Name()
203
+	tag := distribution.GetTag(ref)
204
+
205
+	p := v2.NewPlugin(name, pluginID, pm.runRoot, pm.libRoot, tag)
206
+	if err := p.InitPlugin(); err != nil {
207
+		return err
208
+	}
209
+
210
+	pm.pluginStore.Add(p)
211
+
212
+	pm.pluginEventLogger(p.GetID(), repoName, "create")
213
+
214
+	return nil
215
+}
0 216
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+// +build !linux
1
+
2
+package plugin
3
+
4
+import (
5
+	"errors"
6
+	"io"
7
+	"net/http"
8
+
9
+	"github.com/docker/docker/api/types"
10
+	"golang.org/x/net/context"
11
+)
12
+
13
+var ErrNotSupported = errors.New("plugins are not supported on this platform")
14
+
15
+// Disable deactivates a plugin, which implies that they cannot be used by containers.
16
+func (pm *Manager) Disable(name string) error {
17
+	return ErrNotSupported
18
+}
19
+
20
+// Enable activates a plugin, which implies that they are ready to be used by containers.
21
+func (pm *Manager) Enable(name string) error {
22
+	return ErrNotSupported
23
+}
24
+
25
+// Inspect examines a plugin config
26
+func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
27
+	return tp, ErrNotSupported
28
+}
29
+
30
+// Pull pulls a plugin and computes the privileges required to install it.
31
+func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
32
+	return nil, ErrNotSupported
33
+}
34
+
35
+// List displays the list of plugins and associated metadata.
36
+func (pm *Manager) List() ([]types.Plugin, error) {
37
+	return nil, ErrNotSupported
38
+}
39
+
40
+// Push pushes a plugin to the store.
41
+func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
42
+	return ErrNotSupported
43
+}
44
+
45
+// Remove deletes plugin's root directory.
46
+func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
47
+	return ErrNotSupported
48
+}
49
+
50
+// Set sets plugin args
51
+func (pm *Manager) Set(name string, args []string) error {
52
+	return ErrNotSupported
53
+}
54
+
55
+// CreateFromContext creates a plugin from the given pluginDir which contains
56
+// both the rootfs and the config.json and a repoName with optional tag.
57
+func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.Reader, options *types.PluginCreateOptions) error {
58
+	return ErrNotSupported
59
+}