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

fix convertion issues

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-01-30 00:56:42 +00:00
parent 5b82a1b726
commit 187646127f
5 changed files with 59 additions and 61 deletions

View file

@ -104,6 +104,44 @@ type Config struct {
NetworkDisabled bool NetworkDisabled bool
} }
func ContainerConfigFromJob(job *engine.Job) *Config {
var config Config
config.Hostname = job.Getenv("Hostname")
config.Domainname = job.Getenv("Domainname")
config.User = job.Getenv("User")
config.Memory = job.GetenvInt64("Memory")
config.MemorySwap = job.GetenvInt64("MemorySwap")
config.CpuShares = job.GetenvInt64("CpuShares")
config.AttachStdin = job.GetenvBool("AttachStdin")
config.AttachStdout = job.GetenvBool("AttachStdout")
config.AttachStderr = job.GetenvBool("AttachStderr")
if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
config.PortSpecs = PortSpecs
}
job.GetenvJson("ExposedPorts", &config.ExposedPorts)
config.Tty = job.GetenvBool("Tty")
config.OpenStdin = job.GetenvBool("OpenStdin")
config.StdinOnce = job.GetenvBool("StdinOnce")
if Env := job.GetenvList("Env"); Env != nil {
config.Env = Env
}
if Cmd := job.GetenvList("Cmd"); Cmd != nil {
config.Cmd = Cmd
}
if Dns := job.GetenvList("Dns"); Dns != nil {
config.Dns = Dns
}
config.Image = job.Getenv("Image")
job.GetenvJson("Volumes", &config.Volumes)
config.VolumesFrom = job.Getenv("VolumesFrom")
config.WorkingDir = job.Getenv("WorkingDir")
if Entrypoint := job.GetenvList("Entrypoint"); Entrypoint != nil {
config.Entrypoint = Entrypoint
}
config.NetworkDisabled = job.GetenvBool("NetworkDisabled")
return &config
}
type HostConfig struct { type HostConfig struct {
Binds []string Binds []string
ContainerIDFile string ContainerIDFile string
@ -114,6 +152,22 @@ type HostConfig struct {
PublishAllPorts bool PublishAllPorts bool
} }
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
var hostConfig HostConfig
if Binds := job.GetenvList("Binds"); Binds != nil {
hostConfig.Binds = Binds
}
hostConfig.ContainerIDFile = job.Getenv("ContainerIDFile")
job.GetenvJson("LxcConf", &hostConfig.LxcConf)
hostConfig.Privileged = job.GetenvBool("Privileged")
job.GetenvJson("PortBindings", &hostConfig.PortBindings)
if Links := job.GetenvList("Links"); Links != nil {
hostConfig.Links = Links
}
hostConfig.PublishAllPorts = job.GetenvBool("PublishAllPorts")
return &hostConfig
}
type BindMap struct { type BindMap struct {
SrcPath string SrcPath string
DstPath string DstPath string

View file

@ -191,24 +191,6 @@ func (env *Env) WriteTo(dst io.Writer) (n int64, err error) {
return 0, env.Encode(dst) return 0, env.Encode(dst)
} }
func (env *Env) Export(dst interface{}) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("ExportEnv %s", err)
}
}()
var buf bytes.Buffer
// step 1: encode/marshal the env to an intermediary json representation
if err := env.Encode(&buf); err != nil {
return err
}
// step 2: decode/unmarshal the intermediary json into the destination object
if err := json.NewDecoder(&buf).Decode(dst); err != nil {
return err
}
return nil
}
func (env *Env) Import(src interface{}) (err error) { func (env *Env) Import(src interface{}) (err error) {
defer func() { defer func() {
if err != nil { if err != nil {

View file

@ -84,32 +84,6 @@ func TestSetenvList(t *testing.T) {
} }
} }
func TestImportEnv(t *testing.T) {
type dummy struct {
DummyInt int
DummyStringArray []string
}
job := mkJob(t, "dummy")
if err := job.ImportEnv(&dummy{42, []string{"foo", "bar"}}); err != nil {
t.Fatal(err)
}
dmy := dummy{}
if err := job.ExportEnv(&dmy); err != nil {
t.Fatal(err)
}
if dmy.DummyInt != 42 {
t.Fatalf("Expected 42, got %d", dmy.DummyInt)
}
if len(dmy.DummyStringArray) != 2 || dmy.DummyStringArray[0] != "foo" || dmy.DummyStringArray[1] != "bar" {
t.Fatalf("Expected {foo, bar}, got %v", dmy.DummyStringArray)
}
}
func TestEnviron(t *testing.T) { func TestEnviron(t *testing.T) {
job := mkJob(t, "dummy") job := mkJob(t, "dummy")
job.Setenv("foo", "bar") job.Setenv("foo", "bar")

View file

@ -164,10 +164,6 @@ func (job *Job) EncodeEnv(dst io.Writer) error {
return job.env.Encode(dst) return job.env.Encode(dst)
} }
func (job *Job) ExportEnv(dst interface{}) (err error) {
return job.env.Export(dst)
}
func (job *Job) ImportEnv(src interface{}) (err error) { func (job *Job) ImportEnv(src interface{}) (err error) {
return job.env.Import(src) return job.env.Import(src)
} }

View file

@ -1742,11 +1742,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
job.Printf("Usage: %s", job.Name) job.Printf("Usage: %s", job.Name)
return engine.StatusErr return engine.StatusErr
} }
var config Config config := ContainerConfigFromJob(job)
if err := job.ExportEnv(&config); err != nil {
job.Error(err)
return engine.StatusErr
}
if config.Memory != 0 && config.Memory < 524288 { if config.Memory != 0 && config.Memory < 524288 {
job.Errorf("Minimum memory limit allowed is 512k") job.Errorf("Minimum memory limit allowed is 512k")
return engine.StatusErr return engine.StatusErr
@ -1769,7 +1765,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
config.Dns = defaultDns config.Dns = defaultDns
} }
container, buildWarnings, err := srv.runtime.Create(&config, name) container, buildWarnings, err := srv.runtime.Create(config, name)
if err != nil { if err != nil {
if srv.runtime.graph.IsNotExist(err) { if srv.runtime.graph.IsNotExist(err) {
_, tag := utils.ParseRepositoryTag(config.Image) _, tag := utils.ParseRepositoryTag(config.Image)
@ -2196,11 +2192,7 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
} }
// If no environment was set, then no hostconfig was passed. // If no environment was set, then no hostconfig was passed.
if len(job.Environ()) > 0 { if len(job.Environ()) > 0 {
var hostConfig HostConfig hostConfig := ContainerHostConfigFromJob(job)
if err := job.ExportEnv(&hostConfig); err != nil {
job.Error(err)
return engine.StatusErr
}
// Validate the HostConfig binds. Make sure that: // Validate the HostConfig binds. Make sure that:
// 1) the source of a bind mount isn't / // 1) the source of a bind mount isn't /
// The bind mount "/:/foo" isn't allowed. // The bind mount "/:/foo" isn't allowed.
@ -2227,11 +2219,11 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
} }
} }
// Register any links from the host config before starting the container // Register any links from the host config before starting the container
if err := srv.RegisterLinks(container, &hostConfig); err != nil { if err := srv.RegisterLinks(container, hostConfig); err != nil {
job.Error(err) job.Error(err)
return engine.StatusErr return engine.StatusErr
} }
container.hostConfig = &hostConfig container.hostConfig = hostConfig
container.ToDisk() container.ToDisk()
} }
if err := container.Start(); err != nil { if err := container.Start(); err != nil {