1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove engine.Job from Create action.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-04-09 14:49:22 -07:00
parent 610c436e07
commit 98996a432e
4 changed files with 53 additions and 68 deletions

View file

@ -809,30 +809,23 @@ func postContainersCreate(eng *engine.Engine, version version.Version, w http.Re
return err
}
var (
job = eng.Job("create", r.Form.Get("name"))
outWarnings []string
stdoutBuffer = bytes.NewBuffer(nil)
warnings = bytes.NewBuffer(nil)
warnings []string
name = r.Form.Get("name")
env = new(engine.Env)
)
if err := job.DecodeEnv(r.Body); err != nil {
if err := env.Decode(r.Body); err != nil {
return err
}
// Read container ID from the first line of stdout
job.Stdout.Add(stdoutBuffer)
// Read warnings from stderr
job.Stderr.Add(warnings)
if err := job.Run(); err != nil {
containerId, warnings, err := getDaemon(eng).ContainerCreate(name, env)
if err != nil {
return err
}
// Parse warnings from stderr
scanner := bufio.NewScanner(warnings)
for scanner.Scan() {
outWarnings = append(outWarnings, scanner.Text())
}
return writeJSON(w, http.StatusCreated, &types.ContainerCreateResponse{
ID: engine.Tail(stdoutBuffer, 1),
Warnings: outWarnings,
ID: containerId,
Warnings: warnings,
})
}

View file

@ -12,36 +12,31 @@ import (
"github.com/docker/libcontainer/label"
)
func (daemon *Daemon) ContainerCreate(job *engine.Job) error {
var name string
if len(job.Args) == 1 {
name = job.Args[0]
} else if len(job.Args) > 1 {
return fmt.Errorf("Usage: %s", job.Name)
}
func (daemon *Daemon) ContainerCreate(name string, env *engine.Env) (string, []string, error) {
var warnings []string
config := runconfig.ContainerConfigFromJob(job)
hostConfig := runconfig.ContainerHostConfigFromJob(job.env)
config := runconfig.ContainerConfigFromJob(env)
hostConfig := runconfig.ContainerHostConfigFromJob(env)
if len(hostConfig.LxcConf) > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
return fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
return "", warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
}
if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
return fmt.Errorf("Minimum memory limit allowed is 4MB")
return "", warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
}
if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
job.Errorf("Your kernel does not support memory limit capabilities. Limitation discarded.\n")
warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.\n")
hostConfig.Memory = 0
}
if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !daemon.SystemConfig().SwapLimit {
job.Errorf("Your kernel does not support swap limit capabilities. Limitation discarded.\n")
warnings = append(warnings, "Your kernel does not support swap limit capabilities. Limitation discarded.\n")
hostConfig.MemorySwap = -1
}
if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
return fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
return "", warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
}
if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
return fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
return "", warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
}
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
@ -51,22 +46,18 @@ func (daemon *Daemon) ContainerCreate(job *engine.Job) error {
if tag == "" {
tag = graph.DEFAULTTAG
}
return fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag)
return "", warnings, fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag)
}
return err
return "", warnings, err
}
if !container.Config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
job.Errorf("IPv4 forwarding is disabled.\n")
warnings = append(warnings, "IPv4 forwarding is disabled.\n")
}
container.LogEvent("create")
warnings = append(warnings, buildWarnings...)
job.Printf("%s\n", container.ID)
for _, warning := range buildWarnings {
job.Errorf("%s\n", warning)
}
return nil
return container.ID, warnings, nil
}
// Create creates a new container from the given configuration with a given name.

View file

@ -119,7 +119,8 @@ type Daemon struct {
func (daemon *Daemon) Install(eng *engine.Engine) error {
for name, method := range map[string]engine.Handler{
"container_inspect": daemon.ContainerInspect,
"create": daemon.ContainerCreate,
"container_stats": daemon.ContainerStats,
"export": daemon.ContainerExport,
"info": daemon.CmdInfo,
"restart": daemon.ContainerRestart,
"stop": daemon.ContainerStop,

View file

@ -36,41 +36,41 @@ type Config struct {
Labels map[string]string
}
func ContainerConfigFromJob(job *engine.Job) *Config {
func ContainerConfigFromJob(env *engine.Env) *Config {
config := &Config{
Hostname: job.Getenv("Hostname"),
Domainname: job.Getenv("Domainname"),
User: job.Getenv("User"),
Memory: job.GetenvInt64("Memory"),
MemorySwap: job.GetenvInt64("MemorySwap"),
CpuShares: job.GetenvInt64("CpuShares"),
Cpuset: job.Getenv("Cpuset"),
AttachStdin: job.GetenvBool("AttachStdin"),
AttachStdout: job.GetenvBool("AttachStdout"),
AttachStderr: job.GetenvBool("AttachStderr"),
Tty: job.GetenvBool("Tty"),
OpenStdin: job.GetenvBool("OpenStdin"),
StdinOnce: job.GetenvBool("StdinOnce"),
Image: job.Getenv("Image"),
WorkingDir: job.Getenv("WorkingDir"),
NetworkDisabled: job.GetenvBool("NetworkDisabled"),
MacAddress: job.Getenv("MacAddress"),
Hostname: env.Get("Hostname"),
Domainname: env.Get("Domainname"),
User: env.Get("User"),
Memory: env.GetInt64("Memory"),
MemorySwap: env.GetInt64("MemorySwap"),
CpuShares: env.GetInt64("CpuShares"),
Cpuset: env.Get("Cpuset"),
AttachStdin: env.GetBool("AttachStdin"),
AttachStdout: env.GetBool("AttachStdout"),
AttachStderr: env.GetBool("AttachStderr"),
Tty: env.GetBool("Tty"),
OpenStdin: env.GetBool("OpenStdin"),
StdinOnce: env.GetBool("StdinOnce"),
Image: env.Get("Image"),
WorkingDir: env.Get("WorkingDir"),
NetworkDisabled: env.GetBool("NetworkDisabled"),
MacAddress: env.Get("MacAddress"),
}
job.GetenvJson("ExposedPorts", &config.ExposedPorts)
job.GetenvJson("Volumes", &config.Volumes)
if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
env.GetJson("ExposedPorts", &config.ExposedPorts)
env.GetJson("Volumes", &config.Volumes)
if PortSpecs := env.GetList("PortSpecs"); PortSpecs != nil {
config.PortSpecs = PortSpecs
}
if Env := job.GetenvList("Env"); Env != nil {
if Env := env.GetList("Env"); Env != nil {
config.Env = Env
}
if Cmd := job.GetenvList("Cmd"); Cmd != nil {
if Cmd := env.GetList("Cmd"); Cmd != nil {
config.Cmd = Cmd
}
job.GetenvJson("Labels", &config.Labels)
env.GetJson("Labels", &config.Labels)
if Entrypoint := job.GetenvList("Entrypoint"); Entrypoint != nil {
if Entrypoint := env.GetList("Entrypoint"); Entrypoint != nil {
config.Entrypoint = Entrypoint
}
return config