From 91d78a10c33d9e1c33120634fb3dad5e0d247d28 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 26 Mar 2013 03:05:10 -0700 Subject: [PATCH 1/2] #175 Add autodownload on run command --- commands.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 1f2c3650d7..7cc46dfcdd 100644 --- a/commands.go +++ b/commands.go @@ -826,10 +826,19 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) fmt.Fprintln(stdout, "Error: Command not specified") return fmt.Errorf("Command not specified") } + // Create new container container, err := srv.runtime.Create(config) if err != nil { - return errors.New("Error creating container: " + err.Error()) + // If container not found, try to pull it + // FIXME: not found != error + fmt.Fprintf(stdout, "Image %s not found, trying to pull it from registry.\n", config.Image) + if err = srv.CmdPull(stdin, stdout, config.Image); err != nil { + return err + } + if container, err = srv.runtime.Create(config); err != nil { + return fmt.Errorf("Error creating container: %s", err) + } } if config.OpenStdin { cmd_stdin, err := container.StdinPipe() From 004a5310d93c5373d6db7683058fb392c8a3bd37 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 26 Mar 2013 05:28:17 -0700 Subject: [PATCH 2/2] Try to fetch missing base only on "not found" error --- commands.go | 15 +++++++++------ graph.go | 7 +++++++ tags.go | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index 7cc46dfcdd..e7def9afec 100644 --- a/commands.go +++ b/commands.go @@ -831,14 +831,17 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) container, err := srv.runtime.Create(config) if err != nil { // If container not found, try to pull it - // FIXME: not found != error - fmt.Fprintf(stdout, "Image %s not found, trying to pull it from registry.\n", config.Image) - if err = srv.CmdPull(stdin, stdout, config.Image); err != nil { + if srv.runtime.graph.IsNotExist(err) { + fmt.Fprintf(stdout, "Image %s not found, trying to pull it from registry.\n", config.Image) + if err = srv.CmdPull(stdin, stdout, config.Image); err != nil { + return err + } + if container, err = srv.runtime.Create(config); err != nil { + return err + } + } else { return err } - if container, err = srv.runtime.Create(config); err != nil { - return fmt.Errorf("Error creating container: %s", err) - } } if config.OpenStdin { cmd_stdin, err := container.StdinPipe() diff --git a/graph.go b/graph.go index 35f092703f..29d8b2bb6f 100644 --- a/graph.go +++ b/graph.go @@ -6,6 +6,7 @@ import ( "os" "path" "path/filepath" + "strings" "time" ) @@ -27,6 +28,12 @@ func NewGraph(root string) (*Graph, error) { }, nil } +// FIXME: Implement error subclass instead of looking at the error text +// Note: This is the way golang implements os.IsNotExists on Plan9 +func (graph *Graph) IsNotExist(err error) bool { + return err != nil && strings.Contains(err.Error(), "does not exist") +} + func (graph *Graph) Exists(id string) bool { if _, err := graph.Get(id); err != nil { return false diff --git a/tags.go b/tags.go index 1df7c6e463..e259c2e7ea 100644 --- a/tags.go +++ b/tags.go @@ -75,7 +75,7 @@ func (store *TagStore) LookupImage(name string) (*Image, error) { if i, err := store.GetImage(repoAndTag[0], repoAndTag[1]); err != nil { return nil, err } else if i == nil { - return nil, fmt.Errorf("No such image: %s", name) + return nil, fmt.Errorf("Image does not exist: %s", name) } else { img = i }