2016-05-16 15:50:55 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-12-12 23:05:53 +00:00
|
|
|
"encoding/json"
|
2017-02-24 23:35:10 +00:00
|
|
|
"net"
|
2016-12-12 23:05:53 +00:00
|
|
|
"os"
|
2016-05-16 15:50:55 +00:00
|
|
|
"path/filepath"
|
2016-07-01 18:36:11 +00:00
|
|
|
"time"
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/daemon/initlayer"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-08-04 00:22:00 +00:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-05-19 22:06:46 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2016-11-22 19:21:34 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-08-26 17:02:38 +00:00
|
|
|
"github.com/docker/docker/plugin/v2"
|
2017-01-07 01:23:18 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 14:22:32 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-16 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
2017-09-22 13:52:41 +00:00
|
|
|
func (pm *Manager) enable(p *v2.Plugin, c *controller, force bool) (err error) {
|
2016-12-12 23:05:53 +00:00
|
|
|
p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs")
|
2016-08-26 17:02:38 +00:00
|
|
|
if p.IsEnabled() && !force {
|
2017-07-19 14:20:13 +00:00
|
|
|
return errors.Wrap(enabledError(p.Name()), "plugin already enabled")
|
2016-07-25 21:06:27 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
spec, err := p.InitSpec(pm.config.ExecRoot)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
|
|
|
|
c.restart = true
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
|
2017-02-03 04:08:35 +00:00
|
|
|
var propRoot string
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.PropagatedMount != "" {
|
2017-02-03 04:08:35 +00:00
|
|
|
propRoot = filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
|
2017-09-22 13:52:41 +00:00
|
|
|
if err = os.MkdirAll(propRoot, 0755); err != nil {
|
2017-02-03 04:08:35 +00:00
|
|
|
logrus.Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err)
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:52:41 +00:00
|
|
|
if err = mount.MakeRShared(propRoot); err != nil {
|
2017-02-03 04:08:35 +00:00
|
|
|
return errors.Wrap(err, "error setting up propagated mount dir")
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:52:41 +00:00
|
|
|
if err = mount.Mount(propRoot, p.PropagatedMount, "none", "rbind"); err != nil {
|
2017-02-03 04:08:35 +00:00
|
|
|
return errors.Wrap(err, "error creating mount for propagated mount")
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:22:00 +00:00
|
|
|
rootFS := containerfs.NewLocalContainerFS(filepath.Join(pm.config.Root, p.PluginObj.ID, rootFSFileName))
|
|
|
|
if err := initlayer.Setup(rootFS, idtools.IDPair{0, 0}); err != nil {
|
2017-01-17 18:27:01 +00:00
|
|
|
return errors.WithStack(err)
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
stdout, stderr := makeLoggerStreams(p.GetID())
|
|
|
|
if err := pm.executor.Create(p.GetID(), *spec, stdout, stderr); err != nil {
|
2016-12-12 20:56:44 +00:00
|
|
|
if p.PropagatedMount != "" {
|
|
|
|
if err := mount.Unmount(p.PropagatedMount); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", p.PropagatedMount, err)
|
|
|
|
}
|
2017-02-03 04:08:35 +00:00
|
|
|
if err := mount.Unmount(propRoot); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", propRoot, err)
|
|
|
|
}
|
2016-12-12 20:56:44 +00:00
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 17:53:10 +00:00
|
|
|
return pm.pluginPostStart(p, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) pluginPostStart(p *v2.Plugin, c *controller) error {
|
2017-02-24 23:35:10 +00:00
|
|
|
sockAddr := filepath.Join(pm.config.ExecRoot, p.GetID(), p.GetSocket())
|
2017-05-22 09:38:23 +00:00
|
|
|
client, err := plugins.NewClientWithTimeout("unix://"+sockAddr, nil, time.Duration(c.timeoutInSecs)*time.Second)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
2017-07-14 20:45:32 +00:00
|
|
|
shutdownPlugin(p, c, pm.executor)
|
2017-01-17 18:27:01 +00:00
|
|
|
return errors.WithStack(err)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
p.SetPClient(client)
|
2017-02-24 23:35:10 +00:00
|
|
|
|
2017-04-06 18:16:35 +00:00
|
|
|
// Initial sleep before net Dial to allow plugin to listen on socket.
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2017-02-24 23:35:10 +00:00
|
|
|
maxRetries := 3
|
|
|
|
var retries int
|
|
|
|
for {
|
2017-04-06 18:16:35 +00: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 23:35:10 +00:00
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
retries++
|
|
|
|
|
|
|
|
if retries > maxRetries {
|
|
|
|
logrus.Debugf("error net dialing plugin: %v", err)
|
|
|
|
c.restart = false
|
2017-03-24 19:05:12 +00:00
|
|
|
// While restoring plugins, we need to explicitly set the state to disabled
|
|
|
|
pm.config.Store.SetState(p, false)
|
2017-07-14 20:45:32 +00:00
|
|
|
shutdownPlugin(p, c, pm.executor)
|
2017-02-24 23:35:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetState(p, true)
|
|
|
|
pm.config.Store.CallHandler(p)
|
|
|
|
|
|
|
|
return pm.save(p)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
func (pm *Manager) restore(p *v2.Plugin) error {
|
2017-07-14 20:45:32 +00:00
|
|
|
stdout, stderr := makeLoggerStreams(p.GetID())
|
|
|
|
if err := pm.executor.Restore(p.GetID(), stdout, stderr); err != nil {
|
2016-12-09 17:53:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
if pm.config.LiveRestoreEnabled {
|
2016-12-09 17:53:10 +00:00
|
|
|
c := &controller{}
|
2017-07-14 20:45:32 +00:00
|
|
|
if isRunning, _ := pm.executor.IsRunning(p.GetID()); !isRunning {
|
2016-12-09 17:53:10 +00:00
|
|
|
// plugin is not running, so follow normal startup procedure
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-06-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
func shutdownPlugin(p *v2.Plugin, c *controller, executor Executor) {
|
2016-11-14 23:07:21 +00:00
|
|
|
pluginID := p.GetID()
|
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
err := executor.Signal(pluginID, int(unix.SIGTERM))
|
2016-11-14 23:07:21 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Sending SIGTERM to plugin failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
select {
|
2016-12-01 19:36:56 +00:00
|
|
|
case <-c.exitChan:
|
2016-11-14 23:07:21 +00:00
|
|
|
logrus.Debug("Clean shutdown of plugin")
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
logrus.Debug("Force shutdown plugin")
|
2017-07-14 20:45:32 +00:00
|
|
|
if err := executor.Signal(pluginID, int(unix.SIGKILL)); err != nil {
|
2016-11-14 23:07:21 +00:00
|
|
|
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
|
|
|
|
}
|
2017-09-22 13:52:41 +00:00
|
|
|
select {
|
|
|
|
case <-c.exitChan:
|
|
|
|
logrus.Debug("SIGKILL plugin shutdown")
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
logrus.Debug("Force shutdown plugin FAILED")
|
|
|
|
}
|
2016-11-14 23:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:45:12 +00:00
|
|
|
func setupRoot(root string) error {
|
|
|
|
if err := mount.MakePrivate(root); err != nil {
|
|
|
|
return errors.Wrap(err, "error setting plugin manager root to private")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
func (pm *Manager) disable(p *v2.Plugin, c *controller) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
if !p.IsEnabled() {
|
2017-07-19 14:20:13 +00:00
|
|
|
return errors.Wrap(errDisabled(p.Name()), "plugin is already disabled")
|
2016-07-25 21:06:27 +00:00
|
|
|
}
|
2016-11-14 23:07:21 +00:00
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
2017-07-14 20:45:32 +00:00
|
|
|
shutdownPlugin(p, c, pm.executor)
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetState(p, false)
|
|
|
|
return pm.save(p)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-07-01 18:36:11 +00:00
|
|
|
|
|
|
|
// Shutdown stops all plugins and called during daemon shutdown.
|
|
|
|
func (pm *Manager) Shutdown() {
|
2016-12-12 23:05:53 +00:00
|
|
|
plugins := pm.config.Store.GetAll()
|
2016-08-26 17:02:38 +00:00
|
|
|
for _, p := range plugins {
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
if pm.config.LiveRestoreEnabled && p.IsEnabled() {
|
2016-08-26 17:02:38 +00:00
|
|
|
logrus.Debug("Plugin active when liveRestore is set, skipping shutdown")
|
2016-07-22 15:53:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-14 20:45:32 +00:00
|
|
|
if pm.executor != nil && p.IsEnabled() {
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
2017-07-14 20:45:32 +00:00
|
|
|
shutdownPlugin(p, c, pm.executor)
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-02 20:45:12 +00:00
|
|
|
mount.Unmount(pm.config.Root)
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
2017-01-29 00:54:32 +00: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 13:56:36 +00: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 18:54:14 +00:00
|
|
|
if err := mount.RecursiveUnmount(orig); err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.System(err)
|
2017-04-11 13:56:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 00:54:32 +00:00
|
|
|
backup := orig + "-old"
|
|
|
|
if err := os.Rename(orig, backup); err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errors.Wrap(errdefs.System(err), "error backing up plugin data before upgrade")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 00:54:32 +00: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 13:13:50 +00:00
|
|
|
if mvErr := os.Rename(backup, orig); mvErr != nil {
|
|
|
|
err = errors.Wrap(mvErr, "error restoring old plugin root on upgrade failure")
|
2017-01-29 00:54:32 +00: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-29 04:09:37 +00:00
|
|
|
return errors.Wrap(errdefs.System(err), "error upgrading")
|
2017-01-29 00:54:32 +00: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 23:05:53 +00:00
|
|
|
configRC, err := pm.blobStore.Get(configDigest)
|
|
|
|
if err != nil {
|
2017-01-29 00:54:32 +00:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
defer configRC.Close()
|
|
|
|
|
|
|
|
var config types.PluginConfig
|
|
|
|
dec := json.NewDecoder(configRC)
|
|
|
|
if err := dec.Decode(&config); err != nil {
|
2017-01-29 00:54:32 +00:00
|
|
|
return types.PluginConfig{}, errors.Wrapf(err, "failed to parse config")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
if dec.More() {
|
2017-01-29 00:54:32 +00:00
|
|
|
return types.PluginConfig{}, errors.New("invalid config json")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
requiredPrivileges := computePrivileges(config)
|
2016-12-12 23:05:53 +00:00
|
|
|
if err != nil {
|
2017-01-29 00:54:32 +00:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
if privileges != nil {
|
|
|
|
if err := validatePrivileges(requiredPrivileges, *privileges); err != nil {
|
2017-01-29 00:54:32 +00:00
|
|
|
return types.PluginConfig{}, err
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:54:32 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createPlugin creates a new plugin. take lock before calling.
|
2017-06-07 17:07:01 +00: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-29 00:54:32 +00:00
|
|
|
if err := pm.config.Store.validateName(name); err != nil { // todo: this check is wrong. remove store
|
2017-11-29 04:09:37 +00:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2017-01-29 00:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config, err := pm.setupNewPlugin(configDigest, blobsums, privileges)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
p = &v2.Plugin{
|
|
|
|
PluginObj: types.Plugin{
|
|
|
|
Name: name,
|
|
|
|
ID: stringid.GenerateRandomID(),
|
|
|
|
Config: config,
|
|
|
|
},
|
|
|
|
Config: configDigest,
|
|
|
|
Blobsums: blobsums,
|
|
|
|
}
|
|
|
|
p.InitEmptySettings()
|
2017-06-07 17:07:01 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(p)
|
|
|
|
}
|
2016-12-12 23:05:53 +00: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
|
|
|
|
}
|