github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/daemon/execdriver/driver.go (about)

     1  package execdriver
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os/exec"
     7  	"time"
     8  
     9  	// TODO Windows: Factor out ulimit
    10  	"github.com/docker/docker/pkg/ulimit"
    11  	"github.com/docker/libcontainer"
    12  	"github.com/docker/libcontainer/configs"
    13  )
    14  
    15  // Context is a generic key value pair that allows
    16  // arbatrary data to be sent
    17  type Context map[string]string
    18  
    19  var (
    20  	ErrNotRunning              = errors.New("Container is not running")
    21  	ErrWaitTimeoutReached      = errors.New("Wait timeout reached")
    22  	ErrDriverAlreadyRegistered = errors.New("A driver already registered this docker init function")
    23  	ErrDriverNotFound          = errors.New("The requested docker init has not been found")
    24  )
    25  
    26  type StartCallback func(*ProcessConfig, int)
    27  
    28  // Driver specific information based on
    29  // processes registered with the driver
    30  type Info interface {
    31  	IsRunning() bool
    32  }
    33  
    34  // Terminal in an interface for drivers to implement
    35  // if they want to support Close and Resize calls from
    36  // the core
    37  type Terminal interface {
    38  	io.Closer
    39  	Resize(height, width int) error
    40  }
    41  
    42  type TtyTerminal interface {
    43  	Master() libcontainer.Console
    44  }
    45  
    46  // ExitStatus provides exit reasons for a container.
    47  type ExitStatus struct {
    48  	// The exit code with which the container exited.
    49  	ExitCode int
    50  
    51  	// Whether the container encountered an OOM.
    52  	OOMKilled bool
    53  }
    54  
    55  type Driver interface {
    56  	Run(c *Command, pipes *Pipes, startCallback StartCallback) (ExitStatus, error) // Run executes the process and blocks until the process exits and returns the exit code
    57  	// Exec executes the process in an existing container, blocks until the process exits and returns the exit code
    58  	Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, startCallback StartCallback) (int, error)
    59  	Kill(c *Command, sig int) error
    60  	Pause(c *Command) error
    61  	Unpause(c *Command) error
    62  	Name() string                                 // Driver name
    63  	Info(id string) Info                          // "temporary" hack (until we move state from core to plugins)
    64  	GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
    65  	Terminate(c *Command) error                   // kill it with fire
    66  	Clean(id string) error                        // clean all traces of container exec
    67  	Stats(id string) (*ResourceStats, error)      // Get resource stats for a running container
    68  }
    69  
    70  // Network settings of the container
    71  type Network struct {
    72  	Interface      *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
    73  	Mtu            int               `json:"mtu"`
    74  	ContainerID    string            `json:"container_id"` // id of the container to join network.
    75  	HostNetworking bool              `json:"host_networking"`
    76  }
    77  
    78  // IPC settings of the container
    79  type Ipc struct {
    80  	ContainerID string `json:"container_id"` // id of the container to join ipc.
    81  	HostIpc     bool   `json:"host_ipc"`
    82  }
    83  
    84  // PID settings of the container
    85  type Pid struct {
    86  	HostPid bool `json:"host_pid"`
    87  }
    88  
    89  // UTS settings of the container
    90  type UTS struct {
    91  	HostUTS bool `json:"host_uts"`
    92  }
    93  
    94  type NetworkInterface struct {
    95  	Gateway              string `json:"gateway"`
    96  	IPAddress            string `json:"ip"`
    97  	IPPrefixLen          int    `json:"ip_prefix_len"`
    98  	MacAddress           string `json:"mac"`
    99  	Bridge               string `json:"bridge"`
   100  	GlobalIPv6Address    string `json:"global_ipv6"`
   101  	LinkLocalIPv6Address string `json:"link_local_ipv6"`
   102  	GlobalIPv6PrefixLen  int    `json:"global_ipv6_prefix_len"`
   103  	IPv6Gateway          string `json:"ipv6_gateway"`
   104  	HairpinMode          bool   `json:"hairpin_mode"`
   105  }
   106  
   107  // TODO Windows: Factor out ulimit.Rlimit
   108  type Resources struct {
   109  	Memory         int64            `json:"memory"`
   110  	MemorySwap     int64            `json:"memory_swap"`
   111  	CpuShares      int64            `json:"cpu_shares"`
   112  	CpusetCpus     string           `json:"cpuset_cpus"`
   113  	CpusetMems     string           `json:"cpuset_mems"`
   114  	CpuPeriod      int64            `json:"cpu_period"`
   115  	CpuQuota       int64            `json:"cpu_quota"`
   116  	BlkioWeight    int64            `json:"blkio_weight"`
   117  	Rlimits        []*ulimit.Rlimit `json:"rlimits"`
   118  	OomKillDisable bool             `json:"oom_kill_disable"`
   119  }
   120  
   121  type ResourceStats struct {
   122  	*libcontainer.Stats
   123  	Read        time.Time `json:"read"`
   124  	MemoryLimit int64     `json:"memory_limit"`
   125  	SystemUsage uint64    `json:"system_usage"`
   126  }
   127  
   128  type Mount struct {
   129  	Source      string `json:"source"`
   130  	Destination string `json:"destination"`
   131  	Writable    bool   `json:"writable"`
   132  	Private     bool   `json:"private"`
   133  	Slave       bool   `json:"slave"`
   134  }
   135  
   136  // Describes a process that will be run inside a container.
   137  type ProcessConfig struct {
   138  	exec.Cmd `json:"-"`
   139  
   140  	Privileged bool     `json:"privileged"`
   141  	User       string   `json:"user"`
   142  	Tty        bool     `json:"tty"`
   143  	Entrypoint string   `json:"entrypoint"`
   144  	Arguments  []string `json:"arguments"`
   145  	Terminal   Terminal `json:"-"` // standard or tty terminal
   146  	Console    string   `json:"-"` // dev/console path
   147  }
   148  
   149  // TODO Windows: Factor out unused fields such as LxcConfig, AppArmorProfile,
   150  // and CgroupParent.
   151  //
   152  // Process wrapps an os/exec.Cmd to add more metadata
   153  type Command struct {
   154  	ID                 string            `json:"id"`
   155  	Rootfs             string            `json:"rootfs"` // root fs of the container
   156  	ReadonlyRootfs     bool              `json:"readonly_rootfs"`
   157  	InitPath           string            `json:"initpath"` // dockerinit
   158  	WorkingDir         string            `json:"working_dir"`
   159  	ConfigPath         string            `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
   160  	Network            *Network          `json:"network"`
   161  	Ipc                *Ipc              `json:"ipc"`
   162  	Pid                *Pid              `json:"pid"`
   163  	UTS                *UTS              `json:"uts"`
   164  	Resources          *Resources        `json:"resources"`
   165  	Mounts             []Mount           `json:"mounts"`
   166  	AllowedDevices     []*configs.Device `json:"allowed_devices"`
   167  	AutoCreatedDevices []*configs.Device `json:"autocreated_devices"`
   168  	CapAdd             []string          `json:"cap_add"`
   169  	CapDrop            []string          `json:"cap_drop"`
   170  	ContainerPid       int               `json:"container_pid"`  // the pid for the process inside a container
   171  	ProcessConfig      ProcessConfig     `json:"process_config"` // Describes the init process of the container.
   172  	ProcessLabel       string            `json:"process_label"`
   173  	MountLabel         string            `json:"mount_label"`
   174  	LxcConfig          []string          `json:"lxc_config"`
   175  	AppArmorProfile    string            `json:"apparmor_profile"`
   176  	CgroupParent       string            `json:"cgroup_parent"` // The parent cgroup for this command.
   177  }