|
@@ -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)
|