2014-01-08 18:47:57 -08:00
package execdriver
import (
2014-01-13 16:18:46 -08:00
"errors"
2014-02-21 13:27:15 -08:00
"io"
"os"
2014-01-09 15:04:45 -08:00
"os/exec"
2014-02-17 15:14:30 -08:00
2014-06-10 19:58:15 -07:00
"github.com/docker/libcontainer/devices"
2014-01-08 18:47:57 -08:00
)
2014-03-18 16:49:16 -04:00
// Context is a generic key value pair that allows
// arbatrary data to be sent
type Context map [ string ] string
2014-01-13 16:18:46 -08:00
var (
2014-01-13 18:36:59 -08:00
ErrNotRunning = errors . New ( "Process could not be started" )
ErrWaitTimeoutReached = errors . New ( "Wait timeout reached" )
ErrDriverAlreadyRegistered = errors . New ( "A driver already registered this docker init function" )
ErrDriverNotFound = errors . New ( "The requested docker init has not been found" )
2014-01-13 16:18:46 -08:00
)
2014-01-15 13:57:07 -08:00
var dockerInitFcts map [ string ] InitFunc
2014-01-13 18:36:59 -08:00
type (
2014-01-20 16:05:07 -08:00
StartCallback func ( * Command )
2014-01-15 13:57:07 -08:00
InitFunc func ( i * InitArgs ) error
2014-01-13 18:36:59 -08:00
)
2014-01-15 13:57:07 -08:00
func RegisterInitFunc ( name string , fct InitFunc ) error {
2014-01-13 18:36:59 -08:00
if dockerInitFcts == nil {
2014-01-15 13:57:07 -08:00
dockerInitFcts = make ( map [ string ] InitFunc )
2014-01-13 18:36:59 -08:00
}
if _ , ok := dockerInitFcts [ name ] ; ok {
return ErrDriverAlreadyRegistered
}
dockerInitFcts [ name ] = fct
return nil
}
2014-01-15 13:57:07 -08:00
func GetInitFunc ( name string ) ( InitFunc , error ) {
2014-01-13 18:36:59 -08:00
fct , ok := dockerInitFcts [ name ]
if ! ok {
return nil , ErrDriverNotFound
}
return fct , nil
}
2014-01-15 13:57:07 -08:00
// Args provided to the init function for a driver
type InitArgs struct {
2014-01-13 18:36:59 -08:00
User string
Gateway string
Ip string
WorkDir string
Privileged bool
Env [ ] string
Args [ ] string
Mtu int
Driver string
2014-02-24 12:21:13 -08:00
Console string
Pipe int
2014-02-25 12:41:31 -08:00
Root string
2014-01-13 18:36:59 -08:00
}
2014-01-13 15:02:12 -08:00
2014-01-15 13:57:07 -08:00
// Driver specific information based on
// processes registered with the driver
2014-01-15 11:46:25 -08:00
type Info interface {
IsRunning ( ) bool
}
2014-02-21 13:27:15 -08:00
// Terminal in an interface for drivers to implement
// if they want to support Close and Resize calls from
// the core
type Terminal interface {
io . Closer
Resize ( height , width int ) error
}
type TtyTerminal interface {
Master ( ) * os . File
}
2014-01-09 15:04:45 -08:00
type Driver interface {
2014-02-21 12:32:14 -08:00
Run ( c * Command , pipes * Pipes , startCallback StartCallback ) ( int , error ) // Run executes the process and blocks until the process exits and returns the exit code
2014-01-20 16:05:07 -08:00
Kill ( c * Command , sig int ) error
2014-05-21 15:06:18 -06:00
Pause ( c * Command ) error
Unpause ( c * Command ) error
2014-01-28 16:17:51 +01:00
Name ( ) string // Driver name
Info ( id string ) Info // "temporary" hack (until we move state from core to plugins)
GetPidsForContainer ( id string ) ( [ ] int , error ) // Returns a list of pids for the given container.
2014-03-26 06:48:16 +00:00
Terminate ( c * Command ) error // kill it with fire
2014-01-09 15:04:45 -08:00
}
2014-01-08 18:47:57 -08:00
// Network settings of the container
type Network struct {
2014-05-02 14:17:31 -07:00
Interface * NetworkInterface ` json:"interface" ` // if interface is nil then networking is disabled
Mtu int ` json:"mtu" `
ContainerID string ` json:"container_id" ` // id of the container to join network.
HostNetworking bool ` json:"host_networking" `
2014-03-16 20:52:27 +01:00
}
type NetworkInterface struct {
2014-01-13 16:18:46 -08:00
Gateway string ` json:"gateway" `
IPAddress string ` json:"ip" `
2014-01-15 17:26:04 -08:00
Bridge string ` json:"bridge" `
2014-01-13 16:18:46 -08:00
IPPrefixLen int ` json:"ip_prefix_len" `
2014-01-08 18:47:57 -08:00
}
2014-01-20 16:23:02 -05:00
type Resources struct {
2014-05-12 17:44:57 -07:00
Memory int64 ` json:"memory" `
MemorySwap int64 ` json:"memory_swap" `
CpuShares int64 ` json:"cpu_shares" `
Cpuset string ` json:"cpuset" `
2014-01-20 16:23:02 -05:00
}
2014-03-03 16:15:29 +01:00
type Mount struct {
Source string ` json:"source" `
Destination string ` json:"destination" `
Writable bool ` json:"writable" `
Private bool ` json:"private" `
}
2014-01-10 14:26:29 -08:00
// Process wrapps an os/exec.Cmd to add more metadata
2014-01-20 16:05:07 -08:00
type Command struct {
exec . Cmd ` json:"-" `
2014-01-10 11:44:35 -08:00
2014-02-17 15:14:30 -08:00
ID string ` json:"id" `
Privileged bool ` json:"privileged" `
User string ` json:"user" `
Rootfs string ` json:"rootfs" ` // root fs of the container
InitPath string ` json:"initpath" ` // dockerinit
Entrypoint string ` json:"entrypoint" `
Arguments [ ] string ` json:"arguments" `
WorkingDir string ` json:"working_dir" `
ConfigPath string ` json:"config_path" ` // this should be able to be removed when the lxc template is moved into the driver
Tty bool ` json:"tty" `
Network * Network ` json:"network" `
Config map [ string ] [ ] string ` json:"config" ` // generic values that specific drivers can consume
Resources * Resources ` json:"resources" `
Mounts [ ] Mount ` json:"mounts" `
2014-05-30 18:30:27 -07:00
AllowedDevices [ ] * devices . Device ` json:"allowed_devices" `
AutoCreatedDevices [ ] * devices . Device ` json:"autocreated_devices" `
2014-02-13 17:23:09 -08:00
2014-03-05 17:57:27 -08:00
Terminal Terminal ` json:"-" ` // standard or tty terminal
Console string ` json:"-" ` // dev/console path
ContainerPid int ` json:"container_pid" ` // the pid for the process inside a container
2014-01-09 15:04:45 -08:00
}
2014-01-13 16:18:46 -08:00
// Return the pid of the process
// If the process is nil -1 will be returned
2014-01-20 16:05:07 -08:00
func ( c * Command ) Pid ( ) int {
2014-03-05 17:57:27 -08:00
return c . ContainerPid
2014-01-09 15:04:45 -08:00
}