1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/plugin/backend.go
Tibor Vass f37117045c plugins: experimental support for new plugin management
This patch introduces a new experimental engine-level plugin management
with a new API and command line. Plugins can be distributed via a Docker
registry, and their lifecycle is managed by the engine.
This makes plugins a first-class construct.

For more background, have a look at issue #20363.

Documentation is in a separate commit. If you want to understand how the
new plugin system works, you can start by reading the documentation.

Note: backwards compatibility with existing plugins is maintained,
albeit they won't benefit from the advantages of the new system.

Signed-off-by: Tibor Vass <tibor@docker.com>
Signed-off-by: Anusha Ragunathan <anusha@docker.com>
2016-06-14 14:20:27 -07:00

139 lines
3.4 KiB
Go

// +build experimental
package plugin
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/plugin/distribution"
"github.com/docker/docker/reference"
"github.com/docker/engine-api/types"
)
// Disable deactivates a plugin, which implies that they cannot be used by containers.
func (pm *Manager) Disable(name string) error {
p, err := pm.get(name)
if err != nil {
return err
}
return pm.disable(p)
}
// Enable activates a plugin, which implies that they are ready to be used by containers.
func (pm *Manager) Enable(name string) error {
p, err := pm.get(name)
if err != nil {
return err
}
return pm.enable(p)
}
// Inspect examines a plugin manifest
func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
p, err := pm.get(name)
if err != nil {
return tp, err
}
return p.p, nil
}
// Pull pulls a plugin and enables it.
func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
ref, err := reference.ParseNamed(name)
if err != nil {
logrus.Debugf("error in reference.ParseNamed: %v", err)
return nil, err
}
name = ref.String()
if p, _ := pm.get(name); p != nil {
logrus.Debugf("plugin already exists")
return nil, fmt.Errorf("%s exists", name)
}
pluginID := stringid.GenerateNonCryptoID()
if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
logrus.Debugf("error in MkdirAll: %v", err)
return nil, err
}
pd, err := distribution.Pull(name, pm.registryService, metaHeader, authConfig)
if err != nil {
logrus.Debugf("error in distribution.Pull(): %v", err)
return nil, err
}
if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
logrus.Debugf("error in distribution.WritePullData(): %v", err)
return nil, err
}
p := pm.newPlugin(ref, pluginID)
if ref, ok := ref.(reference.NamedTagged); ok {
p.p.Tag = ref.Tag()
}
if err := pm.initPlugin(p); err != nil {
return nil, err
}
pm.Lock()
pm.plugins[pluginID] = p
pm.nameToID[name] = pluginID
pm.save()
pm.Unlock()
return computePrivileges(&p.p.Manifest), nil
}
// List displays the list of plugins and associated metadata.
func (pm *Manager) List() ([]types.Plugin, error) {
out := make([]types.Plugin, 0, len(pm.plugins))
for _, p := range pm.plugins {
out = append(out, p.p)
}
return out, nil
}
// Push pushes a plugin to the store.
func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
p, err := pm.get(name)
dest := filepath.Join(pm.libRoot, p.p.ID)
config, err := os.Open(filepath.Join(dest, "manifest.json"))
if err != nil {
return err
}
rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
if err != nil {
return err
}
_, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, config, rootfs)
// XXX: Ignore returning digest for now.
// Since digest needs to be written to the ProgressWriter.
return nil
}
// Remove deletes plugin's root directory.
func (pm *Manager) Remove(name string) error {
p, err := pm.get(name)
if err != nil {
return err
}
return pm.remove(p)
}
// Set sets plugin args
func (pm *Manager) Set(name string, args []string) error {
p, err := pm.get(name)
if err != nil {
return err
}
return pm.set(p, args)
}