2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2015-05-05 16:00:20 -04:00
|
|
|
"strings"
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-06-30 12:41:01 -04:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
// Entrypoint encapsulates the container entrypoint.
|
|
|
|
// It might be represented as a string or an array of strings.
|
|
|
|
// We need to override the json decoder to accept both options.
|
|
|
|
// The JSON decoder will fail if the api sends an string and
|
|
|
|
// we try to decode it into an array of string.
|
|
|
|
type Entrypoint struct {
|
|
|
|
parts []string
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// MarshalJSON Marshals (or serializes) the Entrypoint into the json format.
|
|
|
|
// This method is needed to implement json.Marshaller.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Entrypoint) MarshalJSON() ([]byte, error) {
|
|
|
|
if e == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return json.Marshal(e.Slice())
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// UnmarshalJSON decodes the entrypoint whether it's a string or an array of strings.
|
|
|
|
// This method is needed to implement json.Unmarshaler.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Entrypoint) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := make([]string, 0, 1)
|
|
|
|
if err := json.Unmarshal(b, &p); err != nil {
|
2015-06-26 01:15:57 -04:00
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p = append(p, s)
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
|
|
|
e.parts = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Len returns the number of parts of the Entrypoint.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Entrypoint) Len() int {
|
|
|
|
if e == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(e.parts)
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Slice gets the parts of the Entrypoint as a Slice of string.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Entrypoint) Slice() []string {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.parts
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// NewEntrypoint creates an Entrypoint based on the specified parts (as strings).
|
2015-04-10 20:05:21 -04:00
|
|
|
func NewEntrypoint(parts ...string) *Entrypoint {
|
|
|
|
return &Entrypoint{parts}
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Command encapsulates the container command.
|
|
|
|
// It might be represented as a string or an array of strings.
|
|
|
|
// We need to override the json decoder to accept both options.
|
|
|
|
// The JSON decoder will fail if the api sends an string and
|
|
|
|
// we try to decode it into an array of string.
|
2015-04-10 20:05:21 -04:00
|
|
|
type Command struct {
|
|
|
|
parts []string
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// ToString gets a string representing a Command.
|
2015-05-05 16:00:20 -04:00
|
|
|
func (e *Command) ToString() string {
|
|
|
|
return strings.Join(e.parts, " ")
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// MarshalJSON Marshals (or serializes) the Command into the json format.
|
|
|
|
// This method is needed to implement json.Marshaller.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Command) MarshalJSON() ([]byte, error) {
|
|
|
|
if e == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return json.Marshal(e.Slice())
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// UnmarshalJSON decodes the entrypoint whether it's a string or an array of strings.
|
|
|
|
// This method is needed to implement json.Unmarshaler.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Command) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := make([]string, 0, 1)
|
|
|
|
if err := json.Unmarshal(b, &p); err != nil {
|
2015-06-26 01:15:57 -04:00
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p = append(p, s)
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
|
|
|
e.parts = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Len returns the number of parts of the Entrypoint.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Command) Len() int {
|
|
|
|
if e == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(e.parts)
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Slice gets the parts of the Entrypoint as a Slice of string.
|
2015-04-10 20:05:21 -04:00
|
|
|
func (e *Command) Slice() []string {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.parts
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// NewCommand creates a Command based on the specified parts (as strings).
|
2015-04-10 20:05:21 -04:00
|
|
|
func NewCommand(parts ...string) *Command {
|
|
|
|
return &Command{parts}
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Config contains the configuration data about a container.
|
|
|
|
// It should hold only portable information about the container.
|
2014-02-11 23:04:39 -05:00
|
|
|
// Here, "portable" means "independent from the host we are running on".
|
|
|
|
// Non-portable information *should* appear in HostConfig.
|
|
|
|
type Config struct {
|
2015-07-25 05:11:45 -04:00
|
|
|
Hostname string // Hostname
|
|
|
|
Domainname string // Domainname
|
|
|
|
User string // User that will run the command(s) inside the container
|
|
|
|
AttachStdin bool // Attach the standard input, makes possible user interaction
|
|
|
|
AttachStdout bool // Attach the standard output
|
|
|
|
AttachStderr bool // Attach the standard error
|
|
|
|
ExposedPorts map[nat.Port]struct{} // List of exposed ports
|
|
|
|
PublishService string // Name of the network service exposed by the container
|
|
|
|
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
|
|
|
OpenStdin bool // Open stdin
|
|
|
|
StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
|
|
|
|
Env []string // List of environment variable to set in the container
|
|
|
|
Cmd *Command // Command to run when starting the container
|
|
|
|
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
|
|
|
|
Volumes map[string]struct{} // List of volumes (mounts) used for the container
|
|
|
|
VolumeDriver string // Name of the volume driver used to mount volumes
|
|
|
|
WorkingDir string // Current directory (PWD) in the command will be launched
|
|
|
|
Entrypoint *Entrypoint // Entrypoint to run when starting the container
|
|
|
|
NetworkDisabled bool // Is network disabled
|
|
|
|
MacAddress string // Mac Address of the container
|
|
|
|
OnBuild []string // ONBUILD metadata that were defined on the image Dockerfile
|
|
|
|
Labels map[string]string // List of labels set to this container
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerConfigWrapper is a Config wrapper that hold the container Config (portable)
|
|
|
|
// and the corresponding HostConfig (non-portable).
|
2015-04-10 20:05:21 -04:00
|
|
|
type ContainerConfigWrapper struct {
|
|
|
|
*Config
|
2015-05-14 10:39:44 -04:00
|
|
|
InnerHostConfig *HostConfig `json:"HostConfig,omitempty"`
|
|
|
|
Cpuset string `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
|
|
|
|
*HostConfig // Deprecated. Exported to read attrubutes from json that are not in the inner host config structure.
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// GetHostConfig gets the HostConfig of the Config.
|
|
|
|
// It's mostly there to handle Deprecated fields of the ContainerConfigWrapper
|
2015-05-14 10:39:44 -04:00
|
|
|
func (w *ContainerConfigWrapper) GetHostConfig() *HostConfig {
|
|
|
|
hc := w.HostConfig
|
|
|
|
|
|
|
|
if hc == nil && w.InnerHostConfig != nil {
|
|
|
|
hc = w.InnerHostConfig
|
|
|
|
} else if w.InnerHostConfig != nil {
|
|
|
|
if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
|
|
|
|
w.InnerHostConfig.Memory = hc.Memory
|
|
|
|
}
|
|
|
|
if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
|
|
|
|
w.InnerHostConfig.MemorySwap = hc.MemorySwap
|
|
|
|
}
|
2015-07-25 05:11:45 -04:00
|
|
|
if hc.CPUShares != 0 && w.InnerHostConfig.CPUShares == 0 {
|
|
|
|
w.InnerHostConfig.CPUShares = hc.CPUShares
|
2015-05-14 10:39:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hc = w.InnerHostConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" {
|
|
|
|
hc.CpusetCpus = w.Cpuset
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-02-17 10:20:06 -05:00
|
|
|
|
2015-05-14 10:39:44 -04:00
|
|
|
return hc
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
2015-02-17 10:20:06 -05:00
|
|
|
|
2015-06-06 12:41:42 -04:00
|
|
|
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
|
|
|
|
// struct and returns both a Config and an HostConfig struct
|
|
|
|
// Be aware this function is not checking whether the resulted structs are nil,
|
|
|
|
// it's your business to do so
|
2015-04-10 20:05:21 -04:00
|
|
|
func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) {
|
|
|
|
decoder := json.NewDecoder(src)
|
|
|
|
|
|
|
|
var w ContainerConfigWrapper
|
|
|
|
if err := decoder.Decode(&w); err != nil {
|
|
|
|
return nil, nil, err
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-05-14 10:39:44 -04:00
|
|
|
return w.Config, w.GetHostConfig(), nil
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|