2018-02-05 16:05:59 -05:00
|
|
|
package plugin // import "github.com/docker/docker/plugin"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
import (
|
2016-12-12 18:05:53 -05:00
|
|
|
"encoding/json"
|
2017-02-24 18:35:10 -05:00
|
|
|
"net"
|
2016-12-12 18:05:53 -05:00
|
|
|
"os"
|
2016-05-16 11:50:55 -04:00
|
|
|
"path/filepath"
|
2016-07-01 14:36:11 -04:00
|
|
|
"time"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/daemon/initlayer"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-05-19 18:06:46 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2016-11-22 14:21:34 -05:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-08-26 13:02:38 -04:00
|
|
|
"github.com/docker/docker/plugin/v2"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-16 11:50:55 -04:00
|
|
|
)
|
|
|
|
|
2017-12-14 09:29:11 -05:00
|
|
|
func (pm *Manager) enable(p *v2.Plugin, c *controller, force bool) error {
|
2016-12-12 18:05:53 -05:00
|
|
|
p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs")
|
2016-08-26 13:02:38 -04:00
|
|
|
if p.IsEnabled() && !force {
|
2017-07-19 10:20:13 -04:00
|
|
|
return errors.Wrap(enabledError(p.Name()), "plugin already enabled")
|
2016-07-25 17:06:27 -04:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
spec, err := p.InitSpec(pm.config.ExecRoot)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 14:36:56 -05:00
|
|
|
|
|
|
|
c.restart = true
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
|
2017-02-02 23:08:35 -05:00
|
|
|
var propRoot string
|
2017-12-14 09:29:11 -05:00
|
|
|
if p.PluginObj.Config.PropagatedMount != "" {
|
2017-02-02 23:08:35 -05:00
|
|
|
propRoot = filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
|
2017-12-14 09:29:11 -05:00
|
|
|
if err := os.MkdirAll(propRoot, 0755); err != nil {
|
2017-02-02 23:08:35 -05:00
|
|
|
logrus.Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err)
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:29:11 -05:00
|
|
|
if err := mount.MakeRShared(propRoot); err != nil {
|
2017-02-02 23:08:35 -05:00
|
|
|
return errors.Wrap(err, "error setting up propagated mount dir")
|
|
|
|
}
|
2016-11-22 14:21:34 -05:00
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
rootFS := containerfs.NewLocalContainerFS(filepath.Join(pm.config.Root, p.PluginObj.ID, rootFSFileName))
|
2017-11-16 01:20:33 -05:00
|
|
|
if err := initlayer.Setup(rootFS, idtools.Identity{UID: 0, GID: 0}); err != nil {
|
2017-01-17 13:27:01 -05:00
|
|
|
return errors.WithStack(err)
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2017-07-14 16:45:32 -04:00
|
|
|
stdout, stderr := makeLoggerStreams(p.GetID())
|
|
|
|
if err := pm.executor.Create(p.GetID(), *spec, stdout, stderr); err != nil {
|
2017-12-14 09:29:11 -05:00
|
|
|
if p.PluginObj.Config.PropagatedMount != "" {
|
2017-02-02 23:08:35 -05:00
|
|
|
if err := mount.Unmount(propRoot); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", propRoot, err)
|
|
|
|
}
|
2016-12-12 15:56:44 -05:00
|
|
|
}
|
2018-03-20 16:49:42 -04:00
|
|
|
return errors.WithStack(err)
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
2016-12-09 12:53:10 -05:00
|
|
|
return pm.pluginPostStart(p, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) pluginPostStart(p *v2.Plugin, c *controller) error {
|
2017-02-24 18:35:10 -05:00
|
|
|
sockAddr := filepath.Join(pm.config.ExecRoot, p.GetID(), p.GetSocket())
|
2018-04-24 21:45:00 -04:00
|
|
|
p.SetTimeout(time.Duration(c.timeoutInSecs) * time.Second)
|
|
|
|
addr := &net.UnixAddr{Net: "unix", Name: sockAddr}
|
|
|
|
p.SetAddr(addr)
|
|
|
|
|
|
|
|
if p.Protocol() == plugins.ProtocolSchemeHTTPV1 {
|
|
|
|
client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, p.Timeout())
|
|
|
|
if err != nil {
|
|
|
|
c.restart = false
|
2018-04-20 10:48:54 -04:00
|
|
|
shutdownPlugin(p, c.exitChan, pm.executor)
|
2018-04-24 21:45:00 -04:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2018-04-24 21:45:00 -04:00
|
|
|
p.SetPClient(client)
|
|
|
|
}
|
2017-02-24 18:35:10 -05:00
|
|
|
|
2017-04-06 14:16:35 -04:00
|
|
|
// Initial sleep before net Dial to allow plugin to listen on socket.
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2017-02-24 18:35:10 -05:00
|
|
|
maxRetries := 3
|
|
|
|
var retries int
|
|
|
|
for {
|
2017-04-06 14:16:35 -04:00
|
|
|
// net dial into the unix socket to see if someone's listening.
|
|
|
|
conn, err := net.Dial("unix", sockAddr)
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-02-24 18:35:10 -05:00
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
retries++
|
|
|
|
|
|
|
|
if retries > maxRetries {
|
|
|
|
logrus.Debugf("error net dialing plugin: %v", err)
|
|
|
|
c.restart = false
|
2017-03-24 15:05:12 -04:00
|
|
|
// While restoring plugins, we need to explicitly set the state to disabled
|
|
|
|
pm.config.Store.SetState(p, false)
|
2018-04-20 10:48:54 -04:00
|
|
|
shutdownPlugin(p, c.exitChan, pm.executor)
|
2017-02-24 18:35:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
pm.config.Store.SetState(p, true)
|
|
|
|
pm.config.Store.CallHandler(p)
|
|
|
|
|
|
|
|
return pm.save(p)
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
|
|
|
|
2018-04-20 10:48:54 -04:00
|
|
|
func (pm *Manager) restore(p *v2.Plugin, c *controller) error {
|
2017-07-14 16:45:32 -04:00
|
|
|
stdout, stderr := makeLoggerStreams(p.GetID())
|
2018-04-20 10:48:54 -04:00
|
|
|
alive, err := pm.executor.Restore(p.GetID(), stdout, stderr)
|
|
|
|
if err != nil {
|
2016-12-09 12:53:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
if pm.config.LiveRestoreEnabled {
|
2018-04-20 10:48:54 -04:00
|
|
|
if !alive {
|
2016-12-09 12:53:10 -05:00
|
|
|
return pm.enable(p, c, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
c.restart = true
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
return pm.pluginPostStart(p, c)
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:48:54 -04:00
|
|
|
if alive {
|
|
|
|
// TODO(@cpuguy83): Should we always just re-attach to the running plugin instead of doing this?
|
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c.exitChan, pm.executor)
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:53:10 -05:00
|
|
|
return nil
|
2016-06-15 13:39:33 -04:00
|
|
|
}
|
|
|
|
|
2018-04-20 10:48:54 -04:00
|
|
|
func shutdownPlugin(p *v2.Plugin, ec chan bool, executor Executor) {
|
2016-11-14 18:07:21 -05:00
|
|
|
pluginID := p.GetID()
|
|
|
|
|
2017-07-14 16:45:32 -04:00
|
|
|
err := executor.Signal(pluginID, int(unix.SIGTERM))
|
2016-11-14 18:07:21 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Sending SIGTERM to plugin failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
select {
|
2018-04-20 10:48:54 -04:00
|
|
|
case <-ec:
|
2016-11-14 18:07:21 -05:00
|
|
|
logrus.Debug("Clean shutdown of plugin")
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
logrus.Debug("Force shutdown plugin")
|
2017-07-14 16:45:32 -04:00
|
|
|
if err := executor.Signal(pluginID, int(unix.SIGKILL)); err != nil {
|
2016-11-14 18:07:21 -05:00
|
|
|
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
|
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
select {
|
2018-04-20 10:48:54 -04:00
|
|
|
case <-ec:
|
2017-09-22 09:52:41 -04:00
|
|
|
logrus.Debug("SIGKILL plugin shutdown")
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
logrus.Debug("Force shutdown plugin FAILED")
|
|
|
|
}
|
2016-11-14 18:07:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
func (pm *Manager) disable(p *v2.Plugin, c *controller) error {
|
2016-08-26 13:02:38 -04:00
|
|
|
if !p.IsEnabled() {
|
2017-07-19 10:20:13 -04:00
|
|
|
return errors.Wrap(errDisabled(p.Name()), "plugin is already disabled")
|
2016-07-25 17:06:27 -04:00
|
|
|
}
|
2016-11-14 18:07:21 -05:00
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
2018-04-20 10:48:54 -04:00
|
|
|
shutdownPlugin(p, c.exitChan, pm.executor)
|
2016-12-12 18:05:53 -05:00
|
|
|
pm.config.Store.SetState(p, false)
|
|
|
|
return pm.save(p)
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
2016-07-01 14:36:11 -04:00
|
|
|
|
|
|
|
// Shutdown stops all plugins and called during daemon shutdown.
|
|
|
|
func (pm *Manager) Shutdown() {
|
2016-12-12 18:05:53 -05:00
|
|
|
plugins := pm.config.Store.GetAll()
|
2016-08-26 13:02:38 -04:00
|
|
|
for _, p := range plugins {
|
2016-12-01 14:36:56 -05:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
if pm.config.LiveRestoreEnabled && p.IsEnabled() {
|
2016-08-26 13:02:38 -04:00
|
|
|
logrus.Debug("Plugin active when liveRestore is set, skipping shutdown")
|
2016-07-22 11:53:26 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-07-14 16:45:32 -04:00
|
|
|
if pm.executor != nil && p.IsEnabled() {
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
2018-04-20 10:48:54 -04:00
|
|
|
shutdownPlugin(p, c.exitChan, pm.executor)
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 21:28:16 -05:00
|
|
|
if err := mount.RecursiveUnmount(pm.config.Root); err != nil {
|
|
|
|
logrus.WithError(err).Warn("error cleaning up plugin mounts")
|
|
|
|
}
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
|
2017-01-28 19:54:32 -05:00
|
|
|
func (pm *Manager) upgradePlugin(p *v2.Plugin, configDigest digest.Digest, blobsums []digest.Digest, tmpRootFSDir string, privileges *types.PluginPrivileges) (err error) {
|
|
|
|
config, err := pm.setupNewPlugin(configDigest, blobsums, privileges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pdir := filepath.Join(pm.config.Root, p.PluginObj.ID)
|
|
|
|
orig := filepath.Join(pdir, "rootfs")
|
2017-04-11 09:56:36 -04:00
|
|
|
|
|
|
|
// Make sure nothing is mounted
|
|
|
|
// This could happen if the plugin was disabled with `-f` with active mounts.
|
|
|
|
// If there is anything in `orig` is still mounted, this should error out.
|
2017-06-26 14:54:14 -04:00
|
|
|
if err := mount.RecursiveUnmount(orig); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.System(err)
|
2017-04-11 09:56:36 -04:00
|
|
|
}
|
|
|
|
|
2017-01-28 19:54:32 -05:00
|
|
|
backup := orig + "-old"
|
|
|
|
if err := os.Rename(orig, backup); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errors.Wrap(errdefs.System(err), "error backing up plugin data before upgrade")
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2017-01-28 19:54:32 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if rmErr := os.RemoveAll(orig); rmErr != nil && !os.IsNotExist(rmErr) {
|
|
|
|
logrus.WithError(rmErr).WithField("dir", backup).Error("error cleaning up after failed upgrade")
|
|
|
|
return
|
|
|
|
}
|
2017-02-20 08:13:50 -05:00
|
|
|
if mvErr := os.Rename(backup, orig); mvErr != nil {
|
|
|
|
err = errors.Wrap(mvErr, "error restoring old plugin root on upgrade failure")
|
2017-01-28 19:54:32 -05:00
|
|
|
}
|
|
|
|
if rmErr := os.RemoveAll(tmpRootFSDir); rmErr != nil && !os.IsNotExist(rmErr) {
|
|
|
|
logrus.WithError(rmErr).WithField("plugin", p.Name()).Errorf("error cleaning up plugin upgrade dir: %s", tmpRootFSDir)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if rmErr := os.RemoveAll(backup); rmErr != nil && !os.IsNotExist(rmErr) {
|
|
|
|
logrus.WithError(rmErr).WithField("dir", backup).Error("error cleaning up old plugin root after successful upgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Config = configDigest
|
|
|
|
p.Blobsums = blobsums
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := os.Rename(tmpRootFSDir, orig); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errors.Wrap(errdefs.System(err), "error upgrading")
|
2017-01-28 19:54:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
p.PluginObj.Config = config
|
|
|
|
err = pm.save(p)
|
|
|
|
return errors.Wrap(err, "error saving upgraded plugin config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) setupNewPlugin(configDigest digest.Digest, blobsums []digest.Digest, privileges *types.PluginPrivileges) (types.PluginConfig, error) {
|
2016-12-12 18:05:53 -05:00
|
|
|
configRC, err := pm.blobStore.Get(configDigest)
|
|
|
|
if err != nil {
|
2017-01-28 19:54:32 -05:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
defer configRC.Close()
|
|
|
|
|
|
|
|
var config types.PluginConfig
|
|
|
|
dec := json.NewDecoder(configRC)
|
|
|
|
if err := dec.Decode(&config); err != nil {
|
2017-01-28 19:54:32 -05:00
|
|
|
return types.PluginConfig{}, errors.Wrapf(err, "failed to parse config")
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
if dec.More() {
|
2017-01-28 19:54:32 -05:00
|
|
|
return types.PluginConfig{}, errors.New("invalid config json")
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
requiredPrivileges := computePrivileges(config)
|
2016-12-12 18:05:53 -05:00
|
|
|
if err != nil {
|
2017-01-28 19:54:32 -05:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
if privileges != nil {
|
|
|
|
if err := validatePrivileges(requiredPrivileges, *privileges); err != nil {
|
2017-01-28 19:54:32 -05:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:54:32 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createPlugin creates a new plugin. take lock before calling.
|
2017-06-07 13:07:01 -04:00
|
|
|
func (pm *Manager) createPlugin(name string, configDigest digest.Digest, blobsums []digest.Digest, rootFSDir string, privileges *types.PluginPrivileges, opts ...CreateOpt) (p *v2.Plugin, err error) {
|
2017-01-28 19:54:32 -05:00
|
|
|
if err := pm.config.Store.validateName(name); err != nil { // todo: this check is wrong. remove store
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2017-01-28 19:54:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
config, err := pm.setupNewPlugin(configDigest, blobsums, privileges)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
p = &v2.Plugin{
|
|
|
|
PluginObj: types.Plugin{
|
|
|
|
Name: name,
|
|
|
|
ID: stringid.GenerateRandomID(),
|
|
|
|
Config: config,
|
|
|
|
},
|
|
|
|
Config: configDigest,
|
|
|
|
Blobsums: blobsums,
|
|
|
|
}
|
|
|
|
p.InitEmptySettings()
|
2017-06-07 13:07:01 -04:00
|
|
|
for _, o := range opts {
|
|
|
|
o(p)
|
|
|
|
}
|
2016-12-12 18:05:53 -05:00
|
|
|
|
|
|
|
pdir := filepath.Join(pm.config.Root, p.PluginObj.ID)
|
|
|
|
if err := os.MkdirAll(pdir, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", pdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(pdir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := os.Rename(rootFSDir, filepath.Join(pdir, rootFSFileName)); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to rename rootfs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pm.save(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pm.config.Store.Add(p) // todo: remove
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|