Ver código fonte

Merge pull request #3971 from creack/fix_add_ownership

Change ownership to root for ADD file/directory
Michael Crosby 11 anos atrás
pai
commit
9cc7eb4ab3
1 arquivos alterados com 26 adições e 2 exclusões
  1. 26 2
      server/buildfile.go

+ 26 - 2
server/buildfile.go

@@ -419,10 +419,22 @@ func (b *buildFile) addContext(container *runtime.Container, orig, dest string,
 		return err
 	}
 
+	chownR := func(destPath string, uid, gid int) error {
+		return filepath.Walk(destPath, func(path string, info os.FileInfo, err error) error {
+			if err := os.Lchown(path, uid, gid); err != nil {
+				return err
+			}
+			return nil
+		})
+	}
+
 	if fi.IsDir() {
 		if err := archive.CopyWithTar(origPath, destPath); err != nil {
 			return err
 		}
+		if err := chownR(destPath, 0, 0); err != nil {
+			return err
+		}
 		return nil
 	}
 
@@ -452,6 +464,10 @@ func (b *buildFile) addContext(container *runtime.Container, orig, dest string,
 	if err := archive.CopyWithTar(origPath, destPath); err != nil {
 		return err
 	}
+
+	if err := chownR(destPath, 0, 0); err != nil {
+		return err
+	}
 	return nil
 }
 
@@ -486,28 +502,36 @@ func (b *buildFile) CmdAdd(args string) error {
 	)
 
 	if utils.IsURL(orig) {
+		// Initiate the download
 		isRemote = true
 		resp, err := utils.Download(orig)
 		if err != nil {
 			return err
 		}
+
+		// Create a tmp dir
 		tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
 		if err != nil {
 			return err
 		}
+
+		// Create a tmp file within our tmp dir
 		tmpFileName := path.Join(tmpDirName, "tmp")
 		tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
 		if err != nil {
 			return err
 		}
 		defer os.RemoveAll(tmpDirName)
-		if _, err = io.Copy(tmpFile, resp.Body); err != nil {
+
+		// Download and dump result to tmp file
+		if _, err := io.Copy(tmpFile, resp.Body); err != nil {
 			tmpFile.Close()
 			return err
 		}
-		origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
 		tmpFile.Close()
 
+		origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
+
 		// Process the checksum
 		r, err := archive.Tar(tmpFileName, archive.Uncompressed)
 		if err != nil {