github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/plugin/manager.go (about) 1 package plugin // import "github.com/docker/docker/plugin" 2 3 import ( 4 "context" 5 "encoding/json" 6 "io" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "reflect" 11 "regexp" 12 "sort" 13 "strings" 14 "sync" 15 16 "github.com/containerd/containerd/content" 17 "github.com/containerd/containerd/content/local" 18 "github.com/docker/distribution/reference" 19 "github.com/docker/docker/api/types" 20 "github.com/docker/docker/pkg/authorization" 21 "github.com/docker/docker/pkg/ioutils" 22 "github.com/docker/docker/pkg/pubsub" 23 "github.com/docker/docker/pkg/system" 24 v2 "github.com/docker/docker/plugin/v2" 25 "github.com/docker/docker/registry" 26 "github.com/moby/sys/mount" 27 digest "github.com/opencontainers/go-digest" 28 specs "github.com/opencontainers/runtime-spec/specs-go" 29 "github.com/pkg/errors" 30 "github.com/sirupsen/logrus" 31 ) 32 33 const configFileName = "config.json" 34 const rootFSFileName = "rootfs" 35 36 var validFullID = regexp.MustCompile(`^([a-f0-9]{64})$`) 37 38 // Executor is the interface that the plugin manager uses to interact with for starting/stopping plugins 39 type Executor interface { 40 Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error 41 IsRunning(id string) (bool, error) 42 Restore(id string, stdout, stderr io.WriteCloser) (alive bool, err error) 43 Signal(id string, signal int) error 44 } 45 46 func (pm *Manager) restorePlugin(p *v2.Plugin, c *controller) error { 47 if p.IsEnabled() { 48 return pm.restore(p, c) 49 } 50 return nil 51 } 52 53 type eventLogger func(id, name, action string) 54 55 // ManagerConfig defines configuration needed to start new manager. 56 type ManagerConfig struct { 57 Store *Store // remove 58 RegistryService registry.Service 59 LiveRestoreEnabled bool // TODO: remove 60 LogPluginEvent eventLogger 61 Root string 62 ExecRoot string 63 CreateExecutor ExecutorCreator 64 AuthzMiddleware *authorization.Middleware 65 } 66 67 // ExecutorCreator is used in the manager config to pass in an `Executor` 68 type ExecutorCreator func(*Manager) (Executor, error) 69 70 // Manager controls the plugin subsystem. 71 type Manager struct { 72 config ManagerConfig 73 mu sync.RWMutex // protects cMap 74 muGC sync.RWMutex // protects blobstore deletions 75 cMap map[*v2.Plugin]*controller 76 blobStore content.Store 77 publisher *pubsub.Publisher 78 executor Executor 79 } 80 81 // controller represents the manager's control on a plugin. 82 type controller struct { 83 restart bool 84 exitChan chan bool 85 timeoutInSecs int 86 } 87 88 // pluginRegistryService ensures that all resolved repositories 89 // are of the plugin class. 90 type pluginRegistryService struct { 91 registry.Service 92 } 93 94 func (s pluginRegistryService) ResolveRepository(name reference.Named) (repoInfo *registry.RepositoryInfo, err error) { 95 repoInfo, err = s.Service.ResolveRepository(name) 96 if repoInfo != nil { 97 repoInfo.Class = "plugin" 98 } 99 return 100 } 101 102 // NewManager returns a new plugin manager. 103 func NewManager(config ManagerConfig) (*Manager, error) { 104 if config.RegistryService != nil { 105 config.RegistryService = pluginRegistryService{config.RegistryService} 106 } 107 manager := &Manager{ 108 config: config, 109 } 110 for _, dirName := range []string{manager.config.Root, manager.config.ExecRoot, manager.tmpDir()} { 111 if err := os.MkdirAll(dirName, 0700); err != nil { 112 return nil, errors.Wrapf(err, "failed to mkdir %v", dirName) 113 } 114 } 115 var err error 116 manager.executor, err = config.CreateExecutor(manager) 117 if err != nil { 118 return nil, err 119 } 120 121 manager.blobStore, err = local.NewStore(filepath.Join(manager.config.Root, "storage")) 122 if err != nil { 123 return nil, errors.Wrap(err, "error creating plugin blob store") 124 } 125 126 manager.cMap = make(map[*v2.Plugin]*controller) 127 if err := manager.reload(); err != nil { 128 return nil, errors.Wrap(err, "failed to restore plugins") 129 } 130 131 manager.publisher = pubsub.NewPublisher(0, 0) 132 return manager, nil 133 } 134 135 func (pm *Manager) tmpDir() string { 136 return filepath.Join(pm.config.Root, "tmp") 137 } 138 139 // HandleExitEvent is called when the executor receives the exit event 140 // In the future we may change this, but for now all we care about is the exit event. 141 func (pm *Manager) HandleExitEvent(id string) error { 142 p, err := pm.config.Store.GetV2Plugin(id) 143 if err != nil { 144 return err 145 } 146 147 if err := os.RemoveAll(filepath.Join(pm.config.ExecRoot, id)); err != nil && !os.IsNotExist(err) { 148 logrus.WithError(err).WithField("id", id).Error("Could not remove plugin bundle dir") 149 } 150 151 pm.mu.RLock() 152 c := pm.cMap[p] 153 if c.exitChan != nil { 154 close(c.exitChan) 155 c.exitChan = nil // ignore duplicate events (containerd issue #2299) 156 } 157 restart := c.restart 158 pm.mu.RUnlock() 159 160 if restart { 161 pm.enable(p, c, true) 162 } else { 163 if err := mount.RecursiveUnmount(filepath.Join(pm.config.Root, id)); err != nil { 164 return errors.Wrap(err, "error cleaning up plugin mounts") 165 } 166 } 167 return nil 168 } 169 170 func handleLoadError(err error, id string) { 171 if err == nil { 172 return 173 } 174 logger := logrus.WithError(err).WithField("id", id) 175 if errors.Is(err, os.ErrNotExist) { 176 // Likely some error while removing on an older version of docker 177 logger.Warn("missing plugin config, skipping: this may be caused due to a failed remove and requires manual cleanup.") 178 return 179 } 180 logger.Error("error loading plugin, skipping") 181 } 182 183 func (pm *Manager) reload() error { // todo: restore 184 dir, err := ioutil.ReadDir(pm.config.Root) 185 if err != nil { 186 return errors.Wrapf(err, "failed to read %v", pm.config.Root) 187 } 188 plugins := make(map[string]*v2.Plugin) 189 for _, v := range dir { 190 if validFullID.MatchString(v.Name()) { 191 p, err := pm.loadPlugin(v.Name()) 192 if err != nil { 193 handleLoadError(err, v.Name()) 194 continue 195 } 196 plugins[p.GetID()] = p 197 } else { 198 if validFullID.MatchString(strings.TrimSuffix(v.Name(), "-removing")) { 199 // There was likely some error while removing this plugin, let's try to remove again here 200 if err := system.EnsureRemoveAll(v.Name()); err != nil { 201 logrus.WithError(err).WithField("id", v.Name()).Warn("error while attempting to clean up previously removed plugin") 202 } 203 } 204 } 205 } 206 207 pm.config.Store.SetAll(plugins) 208 209 var wg sync.WaitGroup 210 wg.Add(len(plugins)) 211 for _, p := range plugins { 212 c := &controller{exitChan: make(chan bool)} 213 pm.mu.Lock() 214 pm.cMap[p] = c 215 pm.mu.Unlock() 216 217 go func(p *v2.Plugin) { 218 defer wg.Done() 219 if err := pm.restorePlugin(p, c); err != nil { 220 logrus.WithError(err).WithField("id", p.GetID()).Error("Failed to restore plugin") 221 return 222 } 223 224 if p.Rootfs != "" { 225 p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs") 226 } 227 228 // We should only enable rootfs propagation for certain plugin types that need it. 229 for _, typ := range p.PluginObj.Config.Interface.Types { 230 if (typ.Capability == "volumedriver" || typ.Capability == "graphdriver") && typ.Prefix == "docker" && strings.HasPrefix(typ.Version, "1.") { 231 if p.PluginObj.Config.PropagatedMount != "" { 232 propRoot := filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount") 233 234 // check if we need to migrate an older propagated mount from before 235 // these mounts were stored outside the plugin rootfs 236 if _, err := os.Stat(propRoot); os.IsNotExist(err) { 237 rootfsProp := filepath.Join(p.Rootfs, p.PluginObj.Config.PropagatedMount) 238 if _, err := os.Stat(rootfsProp); err == nil { 239 if err := os.Rename(rootfsProp, propRoot); err != nil { 240 logrus.WithError(err).WithField("dir", propRoot).Error("error migrating propagated mount storage") 241 } 242 } 243 } 244 245 if err := os.MkdirAll(propRoot, 0755); err != nil { 246 logrus.Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err) 247 } 248 } 249 } 250 } 251 252 pm.save(p) 253 requiresManualRestore := !pm.config.LiveRestoreEnabled && p.IsEnabled() 254 255 if requiresManualRestore { 256 // if liveRestore is not enabled, the plugin will be stopped now so we should enable it 257 if err := pm.enable(p, c, true); err != nil { 258 logrus.WithError(err).WithField("id", p.GetID()).Error("failed to enable plugin") 259 } 260 } 261 }(p) 262 } 263 wg.Wait() 264 return nil 265 } 266 267 // Get looks up the requested plugin in the store. 268 func (pm *Manager) Get(idOrName string) (*v2.Plugin, error) { 269 return pm.config.Store.GetV2Plugin(idOrName) 270 } 271 272 func (pm *Manager) loadPlugin(id string) (*v2.Plugin, error) { 273 p := filepath.Join(pm.config.Root, id, configFileName) 274 dt, err := ioutil.ReadFile(p) 275 if err != nil { 276 return nil, errors.Wrapf(err, "error reading %v", p) 277 } 278 var plugin v2.Plugin 279 if err := json.Unmarshal(dt, &plugin); err != nil { 280 return nil, errors.Wrapf(err, "error decoding %v", p) 281 } 282 return &plugin, nil 283 } 284 285 func (pm *Manager) save(p *v2.Plugin) error { 286 pluginJSON, err := json.Marshal(p) 287 if err != nil { 288 return errors.Wrap(err, "failed to marshal plugin json") 289 } 290 if err := ioutils.AtomicWriteFile(filepath.Join(pm.config.Root, p.GetID(), configFileName), pluginJSON, 0600); err != nil { 291 return errors.Wrap(err, "failed to write atomically plugin json") 292 } 293 return nil 294 } 295 296 // GC cleans up unreferenced blobs. This is recommended to run in a goroutine 297 func (pm *Manager) GC() { 298 pm.muGC.Lock() 299 defer pm.muGC.Unlock() 300 301 whitelist := make(map[digest.Digest]struct{}) 302 for _, p := range pm.config.Store.GetAll() { 303 whitelist[p.Config] = struct{}{} 304 for _, b := range p.Blobsums { 305 whitelist[b] = struct{}{} 306 } 307 } 308 309 ctx := context.TODO() 310 pm.blobStore.Walk(ctx, func(info content.Info) error { 311 _, ok := whitelist[info.Digest] 312 if ok { 313 return nil 314 } 315 316 return pm.blobStore.Delete(ctx, info.Digest) 317 }) 318 } 319 320 type logHook struct{ id string } 321 322 func (logHook) Levels() []logrus.Level { 323 return logrus.AllLevels 324 } 325 326 func (l logHook) Fire(entry *logrus.Entry) error { 327 entry.Data = logrus.Fields{"plugin": l.id} 328 return nil 329 } 330 331 func makeLoggerStreams(id string) (stdout, stderr io.WriteCloser) { 332 logger := logrus.New() 333 logger.Hooks.Add(logHook{id}) 334 return logger.WriterLevel(logrus.InfoLevel), logger.WriterLevel(logrus.ErrorLevel) 335 } 336 337 func validatePrivileges(requiredPrivileges, privileges types.PluginPrivileges) error { 338 if !isEqual(requiredPrivileges, privileges, isEqualPrivilege) { 339 return errors.New("incorrect privileges") 340 } 341 342 return nil 343 } 344 345 func isEqual(arrOne, arrOther types.PluginPrivileges, compare func(x, y types.PluginPrivilege) bool) bool { 346 if len(arrOne) != len(arrOther) { 347 return false 348 } 349 350 sort.Sort(arrOne) 351 sort.Sort(arrOther) 352 353 for i := 1; i < arrOne.Len(); i++ { 354 if !compare(arrOne[i], arrOther[i]) { 355 return false 356 } 357 } 358 359 return true 360 } 361 362 func isEqualPrivilege(a, b types.PluginPrivilege) bool { 363 if a.Name != b.Name { 364 return false 365 } 366 367 return reflect.DeepEqual(a.Value, b.Value) 368 }