github.com/feiyang21687/docker@v1.5.0/daemon/execdriver/driver.go (about)

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