Переглянути джерело

Implement the CmdAdd instruction

Guillaume J. Charmes 12 роки тому
батько
коміт
6ae3800151
4 змінених файлів з 54 додано та 41 видалено
  1. 45 3
      buildfile.go
  2. 0 29
      commands.go
  3. 2 9
      runtime_test.go
  4. 7 0
      utils/utils.go

+ 45 - 3
buildfile.go

@@ -27,6 +27,7 @@ type buildFile struct {
 	image      string
 	maintainer string
 	config     *Config
+	context    string
 
 	tmpContainers map[string]struct{}
 	tmpImages     map[string]struct{}
@@ -168,6 +169,7 @@ func (b *buildFile) CmdInsert(args string) error {
 	}
 	defer file.Body.Close()
 
+	b.config.Cmd = []string{"echo", "INSERT", sourceUrl, "in", destPath}
 	cid, err := b.run()
 	if err != nil {
 		return err
@@ -185,6 +187,36 @@ func (b *buildFile) CmdInsert(args string) error {
 	return b.commit(cid)
 }
 
+func (b *buildFile) CmdAdd(args string) error {
+	tmp := strings.SplitN(args, " ", 2)
+	if len(tmp) != 2 {
+		return fmt.Errorf("Invalid INSERT format")
+	}
+	orig := strings.Trim(tmp[0], " ")
+	dest := strings.Trim(tmp[1], " ")
+
+	b.config.Cmd = []string{"echo", "PUSH", orig, "in", dest}
+	cid, err := b.run()
+	if err != nil {
+		return err
+	}
+
+	container := b.runtime.Get(cid)
+	if container == nil {
+		return fmt.Errorf("Error while creating the container (CmdAdd)")
+	}
+
+	if err := os.MkdirAll(path.Join(container.rwPath(), dest), 0700); err != nil {
+		return err
+	}
+
+	if err := utils.CopyDirectory(path.Join(b.context, orig), path.Join(container.rwPath(), dest)); err != nil {
+		return err
+	}
+
+	return b.commit(cid)
+}
+
 func (b *buildFile) run() (string, error) {
 	if b.image == "" {
 		return "", fmt.Errorf("Please provide a source image with `from` prior to run")
@@ -216,7 +248,6 @@ func (b *buildFile) commit(id string) error {
 		return fmt.Errorf("Please provide a source image with `from` prior to commit")
 	}
 	b.config.Image = b.image
-
 	if id == "" {
 		cmd := b.config.Cmd
 		b.config.Cmd = []string{"true"}
@@ -245,9 +276,19 @@ func (b *buildFile) commit(id string) error {
 }
 
 func (b *buildFile) Build(dockerfile, context io.Reader) (string, error) {
-	b.out = os.Stdout
-
 	defer b.clearTmp(b.tmpContainers, b.tmpImages)
+
+	if context != nil {
+		name, err := ioutil.TempDir("/tmp", "docker-build")
+		if err != nil {
+			return "", err
+		}
+		if err := Untar(context, name); err != nil {
+			return "", err
+		}
+		defer os.RemoveAll(name)
+		b.context = name
+	}
 	file := bufio.NewReader(dockerfile)
 	for {
 		line, err := file.ReadString('\n')
@@ -307,6 +348,7 @@ func NewBuildFile(srv *Server, out io.Writer) BuildFile {
 		runtime:       srv.runtime,
 		srv:           srv,
 		config:        &Config{},
+		out:           out,
 		tmpContainers: make(map[string]struct{}),
 		tmpImages:     make(map[string]struct{}),
 	}

+ 0 - 29
commands.go

@@ -189,35 +189,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 	return nil
 }
 
-func (cli *DockerCli) CmdBuildClient(args ...string) error {
-	cmd := Subcmd("build", "-|Dockerfile", "Build an image from Dockerfile or via stdin")
-	if err := cmd.Parse(args); err != nil {
-		return nil
-	}
-	var (
-		file io.ReadCloser
-		err  error
-	)
-
-	if cmd.NArg() == 0 {
-		file, err = os.Open("Dockerfile")
-		if err != nil {
-			return err
-		}
-	} else if cmd.Arg(0) == "-" {
-		file = os.Stdin
-	} else {
-		file, err = os.Open(cmd.Arg(0))
-		if err != nil {
-			return err
-		}
-	}
-	if _, err := NewBuilderClient("0.0.0.0", 4243).Build(file, nil); err != nil {
-		return err
-	}
-	return nil
-}
-
 // 'docker login': login / register a user to registry service.
 func (cli *DockerCli) CmdLogin(args ...string) error {
 	var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {

+ 2 - 9
runtime_test.go

@@ -32,13 +32,6 @@ func nuke(runtime *Runtime) error {
 	return os.RemoveAll(runtime.root)
 }
 
-func CopyDirectory(source, dest string) error {
-	if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
-		return err
-	}
-	return nil
-}
-
 func layerArchive(tarfile string) (io.Reader, error) {
 	// FIXME: need to close f somewhere
 	f, err := os.Open(tarfile)
@@ -88,7 +81,7 @@ func newTestRuntime() (*Runtime, error) {
 	if err := os.Remove(root); err != nil {
 		return nil, err
 	}
-	if err := CopyDirectory(unitTestStoreBase, root); err != nil {
+	if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
 		return nil, err
 	}
 
@@ -345,7 +338,7 @@ func TestRestore(t *testing.T) {
 	if err := os.Remove(root); err != nil {
 		t.Fatal(err)
 	}
-	if err := CopyDirectory(unitTestStoreBase, root); err != nil {
+	if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
 		t.Fatal(err)
 	}
 

+ 7 - 0
utils/utils.go

@@ -531,6 +531,13 @@ func GetKernelVersion() (*KernelVersionInfo, error) {
 	}, nil
 }
 
+func CopyDirectory(source, dest string) error {
+	if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
+		return err
+	}
+	return nil
+}
+
 type NopFlusher struct{}
 
 func (f *NopFlusher) Flush() {}