2016-10-06 10:09:54 -04:00
|
|
|
// +build linux
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-12-12 18:05:53 -05:00
|
|
|
"encoding/json"
|
2016-07-25 17:06:27 -04:00
|
|
|
"fmt"
|
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"
|
|
|
|
"syscall"
|
2016-07-01 14:36:11 -04:00
|
|
|
"time"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/daemon/initlayer"
|
2016-11-14 18:07:21 -05:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
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-10-06 10:09:54 -04:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-05-16 11:50:55 -04:00
|
|
|
)
|
|
|
|
|
2016-12-01 14:36:56 -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 {
|
2016-07-25 17:06:27 -04:00
|
|
|
return fmt.Errorf("plugin %s is already enabled", p.Name())
|
|
|
|
}
|
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
|
2016-11-22 14:21:34 -05:00
|
|
|
if p.PropagatedMount != "" {
|
2017-02-02 23:08:35 -05:00
|
|
|
propRoot = filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(propRoot, 0755); err != nil {
|
|
|
|
logrus.Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.MakeRShared(propRoot); err != nil {
|
|
|
|
return errors.Wrap(err, "error setting up propagated mount dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(propRoot, p.PropagatedMount, "none", "rbind"); err != nil {
|
|
|
|
return errors.Wrap(err, "error creating mount for propagated mount")
|
2016-11-22 14:21:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := initlayer.Setup(filepath.Join(pm.config.Root, p.PluginObj.ID, rootFSFileName), idtools.IDPair{0, 0}); err != nil {
|
2017-01-17 13:27:01 -05:00
|
|
|
return errors.WithStack(err)
|
2016-12-12 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := pm.containerdClient.Create(p.GetID(), "", "", specs.Spec(*spec), attachToLog(p.GetID())); err != nil {
|
2016-12-12 15:56:44 -05:00
|
|
|
if p.PropagatedMount != "" {
|
|
|
|
if err := mount.Unmount(p.PropagatedMount); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", p.PropagatedMount, err)
|
|
|
|
}
|
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
|
|
|
}
|
2017-01-17 13:27:01 -05: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())
|
2017-05-22 05:38:23 -04:00
|
|
|
client, err := plugins.NewClientWithTimeout("unix://"+sockAddr, nil, time.Duration(c.timeoutInSecs)*time.Second)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2017-01-17 13:27:01 -05:00
|
|
|
return errors.WithStack(err)
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05: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)
|
2017-02-24 18:35:10 -05:00
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
func (pm *Manager) restore(p *v2.Plugin) error {
|
2016-12-09 12:53:10 -05:00
|
|
|
if err := pm.containerdClient.Restore(p.GetID(), attachToLog(p.GetID())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
if pm.config.LiveRestoreEnabled {
|
2016-12-09 12:53:10 -05:00
|
|
|
c := &controller{}
|
|
|
|
if pids, _ := pm.containerdClient.GetPidsForContainer(p.GetID()); len(pids) == 0 {
|
|
|
|
// 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 13:39:33 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
func shutdownPlugin(p *v2.Plugin, c *controller, containerdClient libcontainerd.Client) {
|
2016-11-14 18:07:21 -05:00
|
|
|
pluginID := p.GetID()
|
|
|
|
|
|
|
|
err := containerdClient.Signal(pluginID, int(syscall.SIGTERM))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Sending SIGTERM to plugin failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
select {
|
2016-12-01 14:36:56 -05:00
|
|
|
case <-c.exitChan:
|
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")
|
|
|
|
if err := containerdClient.Signal(pluginID, int(syscall.SIGKILL)); err != nil {
|
|
|
|
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-07-25 17:06:27 -04:00
|
|
|
return fmt.Errorf("plugin %s is already disabled", p.Name())
|
|
|
|
}
|
2016-11-14 18:07:21 -05:00
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
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
|
|
|
|
}
|
2016-08-26 13:02:38 -04:00
|
|
|
if pm.containerdClient != nil && p.IsEnabled() {
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
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.
|
|
|
|
if err := recursiveUnmount(orig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:54:32 -05:00
|
|
|
backup := orig + "-old"
|
|
|
|
if err := os.Rename(orig, backup); err != nil {
|
2017-04-11 09:56:36 -04:00
|
|
|
return errors.Wrap(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 {
|
|
|
|
return errors.Wrap(err, "error upgrading")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
requiredPrivileges, err := computePrivileges(config)
|
|
|
|
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.
|
|
|
|
func (pm *Manager) createPlugin(name string, configDigest digest.Digest, blobsums []digest.Digest, rootFSDir string, privileges *types.PluginPrivileges) (p *v2.Plugin, err error) {
|
|
|
|
if err := pm.config.Store.validateName(name); err != nil { // todo: this check is wrong. remove store
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|