From 9e4234261c3e4d13f98218a97d7cc6644723e310 Mon Sep 17 00:00:00 2001 From: Anusha Ragunathan Date: Mon, 28 Nov 2016 11:08:39 -0800 Subject: [PATCH] On plugin pull errors, delete created dirs. Signed-off-by: Anusha Ragunathan --- plugin/backend_linux.go | 57 +++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/plugin/backend_linux.go b/plugin/backend_linux.go index 59ca225a9c..46e43903b8 100644 --- a/plugin/backend_linux.go +++ b/plugin/backend_linux.go @@ -19,6 +19,7 @@ import ( "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/plugin/distribution" "github.com/docker/docker/plugin/v2" + "github.com/docker/docker/reference" "golang.org/x/net/context" ) @@ -60,27 +61,7 @@ func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) { return p.PluginObj, nil } -// Pull pulls a plugin and computes the privileges required to install it. -func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) { - ref, err := distribution.GetRef(name) - if err != nil { - logrus.Debugf("error in distribution.GetRef: %v", err) - return nil, err - } - name = ref.String() - - if p, _ := pm.pluginStore.GetByName(name); p != nil { - logrus.Debug("plugin already exists") - return nil, fmt.Errorf("%s exists", name) - } - - pluginID := stringid.GenerateNonCryptoID() - - if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil { - logrus.Debugf("error in MkdirAll: %v", err) - return nil, err - } - +func (pm *Manager) pull(ref reference.Named, metaHeader http.Header, authConfig *types.AuthConfig, pluginID string) (types.PluginPrivileges, error) { pd, err := distribution.Pull(ref, pm.registryService, metaHeader, authConfig) if err != nil { logrus.Debugf("error in distribution.Pull(): %v", err) @@ -99,10 +80,42 @@ func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.A } pm.pluginStore.Add(p) - pm.pluginEventLogger(pluginID, name, "pull") + pm.pluginEventLogger(pluginID, ref.String(), "pull") return p.ComputePrivileges(), nil } +// Pull pulls a plugin and computes the privileges required to install it. +func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) { + ref, err := distribution.GetRef(name) + if err != nil { + logrus.Debugf("error in distribution.GetRef: %v", err) + return nil, err + } + name = ref.String() + + if p, _ := pm.pluginStore.GetByName(name); p != nil { + logrus.Debug("plugin already exists") + return nil, fmt.Errorf("%s exists", name) + } + + pluginID := stringid.GenerateNonCryptoID() + pluginDir := filepath.Join(pm.libRoot, pluginID) + if err := os.MkdirAll(pluginDir, 0755); err != nil { + logrus.Debugf("error in MkdirAll: %v", err) + return nil, err + } + + priv, err := pm.pull(ref, metaHeader, authConfig, pluginID) + if err != nil { + if err := os.RemoveAll(pluginDir); err != nil { + logrus.Warnf("unable to remove %q from failed plugin pull: %v", pluginDir, err) + } + return nil, err + } + + return priv, nil +} + // List displays the list of plugins and associated metadata. func (pm *Manager) List() ([]types.Plugin, error) { plugins := pm.pluginStore.GetAll()