github.com/jandre/docker@v1.7.0/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  	NamespacePath  string            `json:"namespace_path"`
    76  	HostNetworking bool              `json:"host_networking"`
    77  }
    78  
    79  // IPC settings of the container
    80  type Ipc struct {
    81  	ContainerID string `json:"container_id"` // id of the container to join ipc.
    82  	HostIpc     bool   `json:"host_ipc"`
    83  }
    84  
    85  // PID settings of the container
    86  type Pid struct {
    87  	HostPid bool `json:"host_pid"`
    88  }
    89  
    90  // UTS settings of the container
    91  type UTS struct {
    92  	HostUTS bool `json:"host_uts"`
    93  }
    94  
    95  type NetworkInterface struct {
    96  	Gateway              string `json:"gateway"`
    97  	IPAddress            string `json:"ip"`
    98  	IPPrefixLen          int    `json:"ip_prefix_len"`
    99  	MacAddress           string `json:"mac"`
   100  	Bridge               string `json:"bridge"`
   101  	GlobalIPv6Address    string `json:"global_ipv6"`
   102  	LinkLocalIPv6Address string `json:"link_local_ipv6"`
   103  	GlobalIPv6PrefixLen  int    `json:"global_ipv6_prefix_len"`
   104  	IPv6Gateway          string `json:"ipv6_gateway"`
   105  	HairpinMode          bool   `json:"hairpin_mode"`
   106  }
   107  
   108  // TODO Windows: Factor out ulimit.Rlimit
   109  type Resources struct {
   110  	Memory         int64            `json:"memory"`
   111  	MemorySwap     int64            `json:"memory_swap"`
   112  	CpuShares      int64            `json:"cpu_shares"`
   113  	CpusetCpus     string           `json:"cpuset_cpus"`
   114  	CpusetMems     string           `json:"cpuset_mems"`
   115  	CpuPeriod      int64            `json:"cpu_period"`
   116  	CpuQuota       int64            `json:"cpu_quota"`
   117  	BlkioWeight    int64            `json:"blkio_weight"`
   118  	Rlimits        []*ulimit.Rlimit `json:"rlimits"`
   119  	OomKillDisable bool             `json:"oom_kill_disable"`
   120  }
   121  
   122  type ResourceStats struct {
   123  	*libcontainer.Stats
   124  	Read        time.Time `json:"read"`
   125  	MemoryLimit int64     `json:"memory_limit"`
   126  	SystemUsage uint64    `json:"system_usage"`
   127  }
   128  
   129  type Mount struct {
   130  	Source      string `json:"source"`
   131  	Destination string `json:"destination"`
   132  	Writable    bool   `json:"writable"`
   133  	Private     bool   `json:"private"`
   134  	Slave       bool   `json:"slave"`
   135  }
   136  
   137  // Describes a process that will be run inside a container.
   138  type ProcessConfig struct {
   139  	exec.Cmd `json:"-"`
   140  
   141  	Privileged bool     `json:"privileged"`
   142  	User       string   `json:"user"`
   143  	Tty        bool     `json:"tty"`
   144  	Entrypoint string   `json:"entrypoint"`
   145  	Arguments  []string `json:"arguments"`
   146  	Terminal   Terminal `json:"-"` // standard or tty terminal
   147  	Console    string   `json:"-"` // dev/console path
   148  }
   149  
   150  // TODO Windows: Factor out unused fields such as LxcConfig, AppArmorProfile,
   151  // and CgroupParent.
   152  //
   153  // Process wrapps an os/exec.Cmd to add more metadata
   154  type Command struct {
   155  	ID                 string            `json:"id"`
   156  	Rootfs             string            `json:"rootfs"` // root fs of the container
   157  	ReadonlyRootfs     bool              `json:"readonly_rootfs"`
   158  	InitPath           string            `json:"initpath"` // dockerinit
   159  	WorkingDir         string            `json:"working_dir"`
   160  	ConfigPath         string            `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
   161  	Network            *Network          `json:"network"`
   162  	Ipc                *Ipc              `json:"ipc"`
   163  	Pid                *Pid              `json:"pid"`
   164  	UTS                *UTS              `json:"uts"`
   165  	Resources          *Resources        `json:"resources"`
   166  	Mounts             []Mount           `json:"mounts"`
   167  	AllowedDevices     []*configs.Device `json:"allowed_devices"`
   168  	AutoCreatedDevices []*configs.Device `json:"autocreated_devices"`
   169  	CapAdd             []string          `json:"cap_add"`
   170  	CapDrop            []string          `json:"cap_drop"`
   171  	ContainerPid       int               `json:"container_pid"`  // the pid for the process inside a container
   172  	ProcessConfig      ProcessConfig     `json:"process_config"` // Describes the init process of the container.
   173  	ProcessLabel       string            `json:"process_label"`
   174  	MountLabel         string            `json:"mount_label"`
   175  	LxcConfig          []string          `json:"lxc_config"`
   176  	AppArmorProfile    string            `json:"apparmor_profile"`
   177  	CgroupParent       string            `json:"cgroup_parent"` // The parent cgroup for this command.
   178  }