瀏覽代碼

Merge remote-tracking branch 'origin/175-auto_download_run-feature'

Solomon Hykes 12 年之前
父節點
當前提交
8da1810975
共有 3 個文件被更改,包括 21 次插入2 次删除
  1. 13 1
      commands.go
  2. 7 0
      graph.go
  3. 1 1
      tags.go

+ 13 - 1
commands.go

@@ -826,10 +826,22 @@ 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
+		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 config.OpenStdin {
 		cmd_stdin, err := container.StdinPipe()

+ 7 - 0
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

+ 1 - 1
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
 		}