github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/daemon/execdriver/windows/windows.go (about)

     1  // +build windows
     2  
     3  package windows
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"sync"
     9  
    10  	"github.com/Microsoft/hcsshim"
    11  	"github.com/Sirupsen/logrus"
    12  	"github.com/docker/docker/daemon/execdriver"
    13  	"github.com/docker/docker/dockerversion"
    14  	"github.com/docker/docker/pkg/parsers"
    15  	"github.com/docker/engine-api/types/container"
    16  )
    17  
    18  // TP4RetryHack is a hack to retry CreateComputeSystem if it fails with
    19  // known return codes from Windows due to bugs in TP4.
    20  var TP4RetryHack bool
    21  
    22  // This is a daemon development variable only and should not be
    23  // used for running production containers on Windows.
    24  var dummyMode bool
    25  
    26  // This allows the daemon to terminate containers rather than shutdown
    27  // This allows the daemon to force kill (HCS terminate) rather than shutdown
    28  var forceKill bool
    29  
    30  // DefaultIsolation allows users to specify a default isolation technology for
    31  // when running a container on Windows. For example docker daemon -D
    32  // --exec-opt isolation=hyperv will cause Windows to always run containers
    33  // as Hyper-V containers unless otherwise specified.
    34  var DefaultIsolation container.Isolation = "process"
    35  
    36  // Define name and version for windows
    37  var (
    38  	DriverName = "Windows 1854"
    39  	Version    = dockerversion.Version + " " + dockerversion.GitCommit
    40  )
    41  
    42  type activeContainer struct {
    43  	command *execdriver.Command
    44  }
    45  
    46  // Driver contains all information for windows driver,
    47  // it implements execdriver.Driver
    48  type Driver struct {
    49  	root             string
    50  	activeContainers map[string]*activeContainer
    51  	sync.Mutex
    52  }
    53  
    54  // Name implements the exec driver Driver interface.
    55  func (d *Driver) Name() string {
    56  	return fmt.Sprintf("\n Name: %s\n Build: %s \n Default Isolation: %s", DriverName, Version, DefaultIsolation)
    57  }
    58  
    59  // NewDriver returns a new windows driver, called from NewDriver of execdriver.
    60  func NewDriver(root string, options []string) (*Driver, error) {
    61  
    62  	for _, option := range options {
    63  		key, val, err := parsers.ParseKeyValueOpt(option)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		key = strings.ToLower(key)
    68  		switch key {
    69  
    70  		case "dummy":
    71  			switch val {
    72  			case "1":
    73  				dummyMode = true
    74  				logrus.Warn("Using dummy mode in Windows exec driver. This is for development use only!")
    75  			}
    76  
    77  		case "forcekill":
    78  			switch val {
    79  			case "1":
    80  				forceKill = true
    81  				logrus.Warn("Using force kill mode in Windows exec driver. This is for testing purposes only.")
    82  			}
    83  
    84  		case "isolation":
    85  			if !container.Isolation(val).IsValid() {
    86  				return nil, fmt.Errorf("Unrecognised exec driver option 'isolation':'%s'", val)
    87  			}
    88  			if container.Isolation(val).IsHyperV() {
    89  				DefaultIsolation = "hyperv"
    90  			}
    91  			logrus.Infof("Windows default isolation: '%s'", val)
    92  		default:
    93  			return nil, fmt.Errorf("Unrecognised exec driver option %s\n", key)
    94  		}
    95  	}
    96  
    97  	// TODO Windows TP5 timeframe. Remove this next block of code once TP4
    98  	// is no longer supported. Also remove the workaround in run.go.
    99  	//
   100  	// Hack for TP4.
   101  	// This overcomes an issue on TP4 which causes CreateComputeSystem to
   102  	// intermittently fail. It's predominantly here to make Windows to Windows
   103  	// CI more reliable.
   104  	TP4RetryHack = hcsshim.IsTP4()
   105  
   106  	return &Driver{
   107  		root:             root,
   108  		activeContainers: make(map[string]*activeContainer),
   109  	}, nil
   110  }
   111  
   112  // setupEnvironmentVariables convert a string array of environment variables
   113  // into a map as required by the HCS. Source array is in format [v1=k1] [v2=k2] etc.
   114  func setupEnvironmentVariables(a []string) map[string]string {
   115  	r := make(map[string]string)
   116  	for _, s := range a {
   117  		arr := strings.Split(s, "=")
   118  		if len(arr) == 2 {
   119  			r[arr[0]] = arr[1]
   120  		}
   121  	}
   122  	return r
   123  }