2018-02-05 21:05:59 +00:00
|
|
|
package plugin // import "github.com/docker/docker/plugin"
|
2016-05-16 15:50:55 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-11 00:31:04 +00:00
|
|
|
"context"
|
2016-05-16 15:50:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-12-12 23:05:53 +00:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2016-12-28 03:38:13 +00:00
|
|
|
"sort"
|
2016-11-22 19:21:34 +00:00
|
|
|
"strings"
|
2016-05-16 15:50:55 +00:00
|
|
|
"sync"
|
2022-05-01 22:52:16 +00:00
|
|
|
"syscall"
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2020-02-11 00:31:04 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/content/local"
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2017-03-17 21:57:23 +00:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
pkg/system: move EnsureRemoveAll() to pkg/containerfs
pkg/system historically has been a bit of a kitchen-sink of things that were
somewhat "system" related, but didn't have a good place for. EnsureRemoveAll()
is one of those utilities. EnsureRemoveAll() is used to both unmount and remove
a path, for which it depends on both github.com/moby/sys/mount, which in turn
depends on github.com/moby/sys/mountinfo.
pkg/system is imported in the CLI, but neither EnsureRemoveAll(), nor any of its
moby/sys dependencies are used on the client side, so let's move this function
somewhere else, to remove those dependencies from the CLI.
I looked for plausible locations that were related; it's used in:
- daemon
- daemon/graphdriver/XXX/
- plugin
I considered moving it into a (e.g.) "utils" package within graphdriver (but not
a huge fan of "utils" packages), and given that it felt (mostly) related to
cleaning up container filesystems, I decided to move it there.
Some things to follow-up on after this:
- Verify if this function is still needed (it feels a bit like a big hammer in
a "YOLO, let's try some things just in case it fails")
- Perhaps it should be integrated in `containerfs.Remove()` (so that it's used
automatically)
- Look if there's other implementations (and if they should be consolidated),
although (e.g.) the one in containerd is a copy of ours:
https://github.com/containerd/containerd/blob/v1.5.9/pkg/cri/server/helpers_linux.go#L200
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-02 21:43:07 +00:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2019-08-05 14:37:47 +00:00
|
|
|
v2 "github.com/docker/docker/plugin/v2"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2022-09-30 13:35:16 +00:00
|
|
|
"github.com/moby/pubsub"
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2019-08-05 14:37:47 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-05-16 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
2022-01-20 13:12:23 +00:00
|
|
|
const (
|
|
|
|
configFileName = "config.json"
|
|
|
|
rootFSFileName = "rootfs"
|
|
|
|
)
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
var validFullID = regexp.MustCompile(`^([a-f0-9]{64})$`)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
// Executor is the interface that the plugin manager uses to interact with for starting/stopping plugins
|
|
|
|
type Executor interface {
|
|
|
|
Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error
|
|
|
|
IsRunning(id string) (bool, error)
|
2018-04-20 14:48:54 +00:00
|
|
|
Restore(id string, stdout, stderr io.WriteCloser) (alive bool, err error)
|
2022-05-01 22:52:16 +00:00
|
|
|
Signal(id string, signal syscall.Signal) error
|
2017-07-14 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:29:23 +00:00
|
|
|
// EndpointResolver provides looking up registry endpoints for pulling.
|
|
|
|
type EndpointResolver interface {
|
|
|
|
LookupPullEndpoints(hostname string) (endpoints []registry.APIEndpoint, err error)
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:48:54 +00:00
|
|
|
func (pm *Manager) restorePlugin(p *v2.Plugin, c *controller) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
if p.IsEnabled() {
|
2018-04-20 14:48:54 +00:00
|
|
|
return pm.restore(p, c)
|
2016-06-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-26 13:24:46 +00:00
|
|
|
type eventLogger func(id, name string, action events.Action)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// ManagerConfig defines configuration needed to start new manager.
|
|
|
|
type ManagerConfig struct {
|
|
|
|
Store *Store // remove
|
2022-03-11 14:29:23 +00:00
|
|
|
RegistryService EndpointResolver
|
2016-12-12 23:05:53 +00:00
|
|
|
LiveRestoreEnabled bool // TODO: remove
|
|
|
|
LogPluginEvent eventLogger
|
|
|
|
Root string
|
|
|
|
ExecRoot string
|
2017-07-14 20:45:32 +00:00
|
|
|
CreateExecutor ExecutorCreator
|
2017-03-17 21:57:23 +00:00
|
|
|
AuthzMiddleware *authorization.Middleware
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
// ExecutorCreator is used in the manager config to pass in an `Executor`
|
|
|
|
type ExecutorCreator func(*Manager) (Executor, error)
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
// Manager controls the plugin subsystem.
|
|
|
|
type Manager struct {
|
2017-07-14 20:45:32 +00:00
|
|
|
config ManagerConfig
|
|
|
|
mu sync.RWMutex // protects cMap
|
|
|
|
muGC sync.RWMutex // protects blobstore deletions
|
|
|
|
cMap map[*v2.Plugin]*controller
|
2020-02-11 00:31:04 +00:00
|
|
|
blobStore content.Store
|
2017-07-14 20:45:32 +00:00
|
|
|
publisher *pubsub.Publisher
|
|
|
|
executor Executor
|
2016-12-01 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// controller represents the manager's control on a plugin.
|
|
|
|
type controller struct {
|
|
|
|
restart bool
|
|
|
|
exitChan chan bool
|
|
|
|
timeoutInSecs int
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// NewManager returns a new plugin manager.
|
|
|
|
func NewManager(config ManagerConfig) (*Manager, error) {
|
|
|
|
manager := &Manager{
|
|
|
|
config: config,
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2017-11-01 09:29:37 +00:00
|
|
|
for _, dirName := range []string{manager.config.Root, manager.config.ExecRoot, manager.tmpDir()} {
|
2022-01-20 13:12:23 +00:00
|
|
|
if err := os.MkdirAll(dirName, 0o700); err != nil {
|
2017-11-01 09:29:37 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", dirName)
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
var err error
|
2017-07-14 20:45:32 +00:00
|
|
|
manager.executor, err = config.CreateExecutor(manager)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2017-07-14 20:45:32 +00:00
|
|
|
return nil, err
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2017-07-14 20:45:32 +00:00
|
|
|
|
2020-02-11 00:31:04 +00:00
|
|
|
manager.blobStore, err = local.NewStore(filepath.Join(manager.config.Root, "storage"))
|
2016-12-12 23:05:53 +00:00
|
|
|
if err != nil {
|
2020-02-11 00:31:04 +00:00
|
|
|
return nil, errors.Wrap(err, "error creating plugin blob store")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
manager.cMap = make(map[*v2.Plugin]*controller)
|
2016-12-12 23:05:53 +00:00
|
|
|
if err := manager.reload(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to restore plugins")
|
|
|
|
}
|
2017-06-07 17:07:01 +00:00
|
|
|
|
|
|
|
manager.publisher = pubsub.NewPublisher(0, 0)
|
2016-12-12 23:05:53 +00:00
|
|
|
return manager, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) tmpDir() string {
|
|
|
|
return filepath.Join(pm.config.Root, "tmp")
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
// HandleExitEvent is called when the executor receives the exit event
|
|
|
|
// In the future we may change this, but for now all we care about is the exit event.
|
|
|
|
func (pm *Manager) HandleExitEvent(id string) error {
|
|
|
|
p, err := pm.config.Store.GetV2Plugin(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
|
2020-09-23 08:30:53 +00:00
|
|
|
if err := os.RemoveAll(filepath.Join(pm.config.ExecRoot, id)); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).WithField("id", id).Error("Could not remove plugin bundle dir")
|
2018-04-20 14:48:54 +00:00
|
|
|
}
|
2016-11-22 19:21:34 +00:00
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
if c.exitChan != nil {
|
|
|
|
close(c.exitChan)
|
2018-04-20 14:48:54 +00:00
|
|
|
c.exitChan = nil // ignore duplicate events (containerd issue #2299)
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
2017-07-14 20:45:32 +00:00
|
|
|
restart := c.restart
|
|
|
|
pm.mu.RUnlock()
|
2016-07-01 18:36:11 +00:00
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
if restart {
|
|
|
|
pm.enable(p, c, true)
|
2020-09-19 16:45:41 +00:00
|
|
|
} else if err := recursiveUnmount(filepath.Join(pm.config.Root, id)); err != nil {
|
|
|
|
return errors.Wrap(err, "error cleaning up plugin mounts")
|
2017-07-14 20:45:32 +00:00
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:54:14 +00:00
|
|
|
func handleLoadError(err error, id string) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
logger := log.G(context.TODO()).WithError(err).WithField("id", id)
|
2020-04-17 10:01:01 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2017-06-26 18:54:14 +00:00
|
|
|
// Likely some error while removing on an older version of docker
|
|
|
|
logger.Warn("missing plugin config, skipping: this may be caused due to a failed remove and requires manual cleanup.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Error("error loading plugin, skipping")
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
func (pm *Manager) reload() error { // todo: restore
|
2021-08-24 10:10:50 +00:00
|
|
|
dir, err := os.ReadDir(pm.config.Root)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2016-12-12 23:05:53 +00:00
|
|
|
return errors.Wrapf(err, "failed to read %v", pm.config.Root)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
plugins := make(map[string]*v2.Plugin)
|
2016-12-12 23:05:53 +00:00
|
|
|
for _, v := range dir {
|
|
|
|
if validFullID.MatchString(v.Name()) {
|
|
|
|
p, err := pm.loadPlugin(v.Name())
|
|
|
|
if err != nil {
|
2017-06-26 18:54:14 +00:00
|
|
|
handleLoadError(err, v.Name())
|
|
|
|
continue
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
plugins[p.GetID()] = p
|
2017-06-26 18:54:14 +00:00
|
|
|
} else {
|
|
|
|
if validFullID.MatchString(strings.TrimSuffix(v.Name(), "-removing")) {
|
|
|
|
// There was likely some error while removing this plugin, let's try to remove again here
|
pkg/system: move EnsureRemoveAll() to pkg/containerfs
pkg/system historically has been a bit of a kitchen-sink of things that were
somewhat "system" related, but didn't have a good place for. EnsureRemoveAll()
is one of those utilities. EnsureRemoveAll() is used to both unmount and remove
a path, for which it depends on both github.com/moby/sys/mount, which in turn
depends on github.com/moby/sys/mountinfo.
pkg/system is imported in the CLI, but neither EnsureRemoveAll(), nor any of its
moby/sys dependencies are used on the client side, so let's move this function
somewhere else, to remove those dependencies from the CLI.
I looked for plausible locations that were related; it's used in:
- daemon
- daemon/graphdriver/XXX/
- plugin
I considered moving it into a (e.g.) "utils" package within graphdriver (but not
a huge fan of "utils" packages), and given that it felt (mostly) related to
cleaning up container filesystems, I decided to move it there.
Some things to follow-up on after this:
- Verify if this function is still needed (it feels a bit like a big hammer in
a "YOLO, let's try some things just in case it fails")
- Perhaps it should be integrated in `containerfs.Remove()` (so that it's used
automatically)
- Look if there's other implementations (and if they should be consolidated),
although (e.g.) the one in containerd is a copy of ours:
https://github.com/containerd/containerd/blob/v1.5.9/pkg/cri/server/helpers_linux.go#L200
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-02 21:43:07 +00:00
|
|
|
if err := containerfs.EnsureRemoveAll(v.Name()); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).WithField("id", v.Name()).Warn("error while attempting to clean up previously removed plugin")
|
2017-06-26 18:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetAll(plugins)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(plugins))
|
2016-08-26 17:02:38 +00:00
|
|
|
for _, p := range plugins {
|
2018-04-20 14:48:54 +00:00
|
|
|
c := &controller{exitChan: make(chan bool)}
|
|
|
|
pm.mu.Lock()
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.cMap[p] = c
|
2018-04-20 14:48:54 +00:00
|
|
|
pm.mu.Unlock()
|
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
go func(p *v2.Plugin) {
|
2016-12-12 23:05:53 +00:00
|
|
|
defer wg.Done()
|
2018-04-20 14:48:54 +00:00
|
|
|
if err := pm.restorePlugin(p, c); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).WithField("id", p.GetID()).Error("Failed to restore plugin")
|
2016-06-15 17:39:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.Rootfs != "" {
|
2016-12-12 23:05:53 +00:00
|
|
|
p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs")
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should only enable rootfs propagation for certain plugin types that need it.
|
|
|
|
for _, typ := range p.PluginObj.Config.Interface.Types {
|
2021-05-14 17:38:50 +00:00
|
|
|
if (typ.Capability == "volumedriver" || typ.Capability == "graphdriver" || typ.Capability == "csinode" || typ.Capability == "csicontroller") && typ.Prefix == "docker" && strings.HasPrefix(typ.Version, "1.") {
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.PluginObj.Config.PropagatedMount != "" {
|
2017-02-03 04:08:35 +00:00
|
|
|
propRoot := filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
|
|
|
|
// check if we need to migrate an older propagated mount from before
|
|
|
|
// these mounts were stored outside the plugin rootfs
|
|
|
|
if _, err := os.Stat(propRoot); os.IsNotExist(err) {
|
2017-12-14 14:29:11 +00:00
|
|
|
rootfsProp := filepath.Join(p.Rootfs, p.PluginObj.Config.PropagatedMount)
|
|
|
|
if _, err := os.Stat(rootfsProp); err == nil {
|
|
|
|
if err := os.Rename(rootfsProp, propRoot); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).WithField("dir", propRoot).Error("error migrating propagated mount storage")
|
2017-02-03 04:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 13:12:23 +00:00
|
|
|
if err := os.MkdirAll(propRoot, 0o755); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err)
|
2017-02-03 04:08:35 +00:00
|
|
|
}
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.save(p)
|
|
|
|
requiresManualRestore := !pm.config.LiveRestoreEnabled && p.IsEnabled()
|
2016-06-15 17:39:33 +00:00
|
|
|
|
|
|
|
if requiresManualRestore {
|
|
|
|
// if liveRestore is not enabled, the plugin will be stopped now so we should enable it
|
2016-12-01 19:36:56 +00:00
|
|
|
if err := pm.enable(p, c, true); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).WithField("id", p.GetID()).Error("failed to enable plugin")
|
2016-06-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(p)
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
wg.Wait()
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
// Get looks up the requested plugin in the store.
|
|
|
|
func (pm *Manager) Get(idOrName string) (*v2.Plugin, error) {
|
|
|
|
return pm.config.Store.GetV2Plugin(idOrName)
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
func (pm *Manager) loadPlugin(id string) (*v2.Plugin, error) {
|
|
|
|
p := filepath.Join(pm.config.Root, id, configFileName)
|
2021-08-24 10:10:50 +00:00
|
|
|
dt, err := os.ReadFile(p)
|
2016-12-12 23:05:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %v", p)
|
|
|
|
}
|
|
|
|
var plugin v2.Plugin
|
|
|
|
if err := json.Unmarshal(dt, &plugin); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error decoding %v", p)
|
|
|
|
}
|
|
|
|
return &plugin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) save(p *v2.Plugin) error {
|
|
|
|
pluginJSON, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal plugin json")
|
|
|
|
}
|
2022-01-20 13:12:23 +00:00
|
|
|
if err := ioutils.AtomicWriteFile(filepath.Join(pm.config.Root, p.GetID(), configFileName), pluginJSON, 0o600); err != nil {
|
2017-01-17 18:27:01 +00:00
|
|
|
return errors.Wrap(err, "failed to write atomically plugin json")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-21 23:24:07 +00:00
|
|
|
// GC cleans up unreferenced blobs. This is recommended to run in a goroutine
|
2016-12-12 23:05:53 +00:00
|
|
|
func (pm *Manager) GC() {
|
|
|
|
pm.muGC.Lock()
|
|
|
|
defer pm.muGC.Unlock()
|
|
|
|
|
2020-07-14 08:41:34 +00:00
|
|
|
used := make(map[digest.Digest]struct{})
|
2016-12-12 23:05:53 +00:00
|
|
|
for _, p := range pm.config.Store.GetAll() {
|
2020-07-14 08:41:34 +00:00
|
|
|
used[p.Config] = struct{}{}
|
2016-12-12 23:05:53 +00:00
|
|
|
for _, b := range p.Blobsums {
|
2020-07-14 08:41:34 +00:00
|
|
|
used[b] = struct{}{}
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 00:31:04 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
pm.blobStore.Walk(ctx, func(info content.Info) error {
|
2020-07-14 08:41:34 +00:00
|
|
|
_, ok := used[info.Digest]
|
2020-02-11 00:31:04 +00:00
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm.blobStore.Delete(ctx, info.Digest)
|
|
|
|
})
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
type logHook struct{ id string }
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
func (logHook) Levels() []log.Level {
|
|
|
|
return []log.Level{
|
|
|
|
log.PanicLevel,
|
|
|
|
log.FatalLevel,
|
|
|
|
log.ErrorLevel,
|
|
|
|
log.WarnLevel,
|
|
|
|
log.InfoLevel,
|
|
|
|
log.DebugLevel,
|
|
|
|
log.TraceLevel,
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
func (l logHook) Fire(entry *log.Entry) error {
|
2023-07-30 15:18:56 +00:00
|
|
|
entry.Data = log.Fields{"plugin": l.id}
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-17 21:39:52 +00:00
|
|
|
|
2017-07-14 20:45:32 +00:00
|
|
|
func makeLoggerStreams(id string) (stdout, stderr io.WriteCloser) {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.Hooks.Add(logHook{id})
|
2023-09-15 11:51:51 +00:00
|
|
|
return logger.WriterLevel(log.InfoLevel), logger.WriterLevel(log.ErrorLevel)
|
2016-10-17 21:39:52 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
func validatePrivileges(requiredPrivileges, privileges types.PluginPrivileges) error {
|
2016-12-28 03:38:13 +00:00
|
|
|
if !isEqual(requiredPrivileges, privileges, isEqualPrivilege) {
|
2016-12-12 23:05:53 +00:00
|
|
|
return errors.New("incorrect privileges")
|
|
|
|
}
|
2016-12-28 03:38:13 +00:00
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:38:13 +00:00
|
|
|
func isEqual(arrOne, arrOther types.PluginPrivileges, compare func(x, y types.PluginPrivilege) bool) bool {
|
|
|
|
if len(arrOne) != len(arrOther) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(arrOne)
|
|
|
|
sort.Sort(arrOther)
|
|
|
|
|
|
|
|
for i := 1; i < arrOne.Len(); i++ {
|
|
|
|
if !compare(arrOne[i], arrOther[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEqualPrivilege(a, b types.PluginPrivilege) bool {
|
|
|
|
if a.Name != b.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.DeepEqual(a.Value, b.Value)
|
|
|
|
}
|