Parcourir la source

Merge pull request #33999 from Microsoft/jjh/opengcs0.0.9

Revendor jhowardmsft/opengcs v0.0.9
Sebastiaan van Stijn il y a 8 ans
Parent
commit
20eb2caa3b

+ 1 - 1
vendor.conf

@@ -8,7 +8,7 @@ github.com/docker/libtrust 9cbd2a1374f46905c68a4eb3694a130610adc62a
 github.com/go-check/check 4ed411733c5785b40214c70bce814c3a3a689609 https://github.com/cpuguy83/check.git
 github.com/gorilla/context v1.1
 github.com/gorilla/mux v1.1
-github.com/jhowardmsft/opengcs v0.0.7
+github.com/jhowardmsft/opengcs v0.0.9
 github.com/kr/pty 5cf931ef8f
 github.com/mattn/go-shellwords v1.0.3
 github.com/tchap/go-patricia v2.2.6

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/config.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"encoding/json"
 	"fmt"

+ 4 - 15
vendor/github.com/jhowardmsft/opengcs/gogcs/client/createsandbox.go

@@ -2,19 +2,15 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"os"
-	"sync"
 
 	"github.com/Sirupsen/logrus"
 )
 
-var sandboxCacheLock sync.Mutex
-
-// CreateSandbox does what it says on the tin. This is done by copying a prebuilt-sandbox from the ServiceVM
+// CreateSandbox does what it says on the tin. This is done by copying a prebuilt-sandbox from the ServiceVM.
+// It is the responsibility of the caller to synchronise simultaneous attempts to create the cache file.
 // TODO: @jhowardmsft maxSizeInMB isn't hooked up in GCS. Needs a platform change which is in flight.
 func (config *Config) CreateSandbox(destFile string, maxSizeInMB uint32, cacheFile string) error {
 	// Smallest we can accept is the default sandbox size as we can't size down, only expand.
@@ -26,17 +22,13 @@ func (config *Config) CreateSandbox(destFile string, maxSizeInMB uint32, cacheFi
 
 	// Retrieve from cache if the default size and already on disk
 	if cacheFile != "" && maxSizeInMB == DefaultSandboxSizeMB {
-		sandboxCacheLock.Lock()
 		if _, err := os.Stat(cacheFile); err == nil {
-			if err := copyFile(cacheFile, destFile); err != nil {
-				sandboxCacheLock.Unlock()
+			if err := CopyFile(cacheFile, destFile, false); err != nil {
 				return fmt.Errorf("opengcs: CreateSandbox: Failed to copy cached sandbox '%s' to '%s': %s", cacheFile, destFile, err)
 			}
-			sandboxCacheLock.Unlock()
 			logrus.Debugf("opengcs: CreateSandbox: %s fulfilled from cache", destFile)
 			return nil
 		}
-		sandboxCacheLock.Unlock()
 	}
 
 	if config.Uvm == nil {
@@ -62,15 +54,12 @@ func (config *Config) CreateSandbox(destFile string, maxSizeInMB uint32, cacheFi
 
 	// Populate the cache
 	if cacheFile != "" && maxSizeInMB == DefaultSandboxSizeMB {
-		sandboxCacheLock.Lock()
 		// It may already exist due to being created on another thread, in which case no copy back needed.
 		if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
-			if err := copyFile(destFile, cacheFile); err != nil {
-				sandboxCacheLock.Unlock()
+			if err := CopyFile(destFile, cacheFile, false); err != nil {
 				return fmt.Errorf("opengcs: CreateSandbox: Failed to seed sandbox cache '%s' from '%s': %s", destFile, cacheFile, err)
 			}
 		}
-		sandboxCacheLock.Unlock()
 	}
 
 	logrus.Debugf("opengcs: CreateSandbox: %s created (non-cache)", destFile)

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/hotaddvhd.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/hotremovevhd.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/layervhddetails.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"os"

+ 41 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/process.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"io"
@@ -59,3 +57,44 @@ func (config *Config) createUtilsProcess(commandLine string) (process, error) {
 	logrus.Debugf("opengcs: createUtilsProcess success: pid %d", proc.Process.Pid())
 	return proc, nil
 }
+
+// RunProcess runs the given command line program in the utilityVM. It takes in
+// an input to the reader to feed into stdin and returns stdout to output.
+func (config *Config) RunProcess(commandLine string, input io.Reader, output io.Writer) error {
+	logrus.Debugf("opengcs: RunProcess: %s", commandLine)
+	process, err := config.createUtilsProcess(commandLine)
+	if err != nil {
+		return err
+	}
+	defer process.Process.Close()
+
+	// Send the data into the process's stdin
+	if input != nil {
+		if _, err = copyWithTimeout(process.Stdin,
+			input,
+			0,
+			config.UvmTimeoutSeconds,
+			fmt.Sprintf("send to stdin of %s", commandLine)); err != nil {
+			return err
+		}
+
+		// Don't need stdin now we've sent everything. This signals GCS that we are finished sending data.
+		if err := process.Process.CloseStdin(); err != nil {
+			return err
+		}
+	}
+
+	if output != nil {
+		// Copy the data over to the writer.
+		if _, err := copyWithTimeout(output,
+			process.Stdout,
+			0,
+			config.UvmTimeoutSeconds,
+			fmt.Sprintf("RunProcess: copy back from %s", commandLine)); err != nil {
+			return err
+		}
+	}
+
+	logrus.Debugf("opengcs: runProcess success: %s", commandLine)
+	return nil
+}

+ 2 - 4
vendor/github.com/jhowardmsft/opengcs/gogcs/client/tartovhd.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"io"
@@ -26,7 +24,7 @@ func (config *Config) TarToVhd(targetVHDFile string, reader io.Reader) (int64, e
 	defer process.Process.Close()
 
 	// Send the tarstream into the `tar2vhd`s stdin
-	if _, err = copyWithTimeout(process.Stdin, reader, 0, config.UvmTimeoutSeconds, fmt.Sprintf("send %s, to stdin of tar2vhd", targetVHDFile)); err != nil {
+	if _, err = copyWithTimeout(process.Stdin, reader, 0, config.UvmTimeoutSeconds, fmt.Sprintf("stdin of tar2vhd for generating %s", targetVHDFile)); err != nil {
 		return 0, fmt.Errorf("opengcs: TarToVhd: %s: failed to send to tar2vhd in uvm: %s", targetVHDFile, err)
 	}
 
@@ -36,7 +34,7 @@ func (config *Config) TarToVhd(targetVHDFile string, reader io.Reader) (int64, e
 	}
 
 	// Write stdout contents of `tar2vhd` to the VHD file
-	payloadSize, err := writeFileFromReader(targetVHDFile, process.Stdout, config.UvmTimeoutSeconds, fmt.Sprintf("output of tar2vhd to %s", targetVHDFile))
+	payloadSize, err := writeFileFromReader(targetVHDFile, process.Stdout, config.UvmTimeoutSeconds, fmt.Sprintf("stdout of tar2vhd to %s", targetVHDFile))
 	if err != nil {
 		return 0, fmt.Errorf("opengcs: TarToVhd: %s: failed writing VHD file: %s", targetVHDFile, err)
 	}

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/unsupported.go

@@ -1,5 +1,3 @@
 // +build !windows
 
 package client
-
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon

+ 6 - 6
vendor/github.com/jhowardmsft/opengcs/gogcs/client/utilities.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"io"
@@ -72,10 +70,13 @@ func copyWithTimeout(dst io.Writer, src io.Reader, size int64, timeoutSeconds in
 	return result.bytes, nil
 }
 
-// copyFile is a utility for copying a file - used for the sandbox cache.
+// CopyFile is a utility for copying a file - used for the sandbox cache.
 // Uses CopyFileW win32 API for performance
-func copyFile(srcFile, destFile string) error {
+func CopyFile(srcFile, destFile string, overwrite bool) error {
 	var bFailIfExists uint32 = 1
+	if overwrite {
+		bFailIfExists = 0
+	}
 
 	lpExistingFileName, err := syscall.UTF16PtrFromString(srcFile)
 	if err != nil {
@@ -92,8 +93,7 @@ func copyFile(srcFile, destFile string) error {
 		uintptr(unsafe.Pointer(lpNewFileName)),
 		uintptr(bFailIfExists))
 	if r1 == 0 {
-		return fmt.Errorf("failed CopyFileW Win32 call from '%s' to %s: %s", srcFile, destFile, err)
+		return fmt.Errorf("failed CopyFileW Win32 call from '%s' to '%s': %s", srcFile, destFile, err)
 	}
 	return nil
-
 }

+ 0 - 2
vendor/github.com/jhowardmsft/opengcs/gogcs/client/vhdtotar.go

@@ -2,8 +2,6 @@
 
 package client
 
-// TODO @jhowardmsft - This will move to Microsoft/opengcs soon
-
 import (
 	"fmt"
 	"io"