From 6c168a8986261c95ae5e036f008375f77afbe78e Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 1 May 2013 14:36:45 -0700 Subject: [PATCH] Rebase master (autorun) --- builder.go | 53 +++++++++++++++++++++-- commands.go | 2 +- container_test.go | 7 +-- runtime.go | 107 ---------------------------------------------- 4 files changed, 54 insertions(+), 115 deletions(-) diff --git a/builder.go b/builder.go index 62a7731b4b..9ecf5f4e8e 100644 --- a/builder.go +++ b/builder.go @@ -24,12 +24,57 @@ func NewBuilder(runtime *Runtime) *Builder { } } +func (builder *Builder) mergeConfig(userConf, imageConf *Config) { + if userConf.Hostname != "" { + userConf.Hostname = imageConf.Hostname + } + if userConf.User != "" { + userConf.User = imageConf.User + } + if userConf.Memory == 0 { + userConf.Memory = imageConf.Memory + } + if userConf.MemorySwap == 0 { + userConf.MemorySwap = imageConf.MemorySwap + } + if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 { + userConf.PortSpecs = imageConf.PortSpecs + } + if !userConf.Tty { + userConf.Tty = userConf.Tty + } + if !userConf.OpenStdin { + userConf.OpenStdin = imageConf.OpenStdin + } + if !userConf.StdinOnce { + userConf.StdinOnce = imageConf.StdinOnce + } + if userConf.Env == nil || len(userConf.Env) == 0 { + userConf.Env = imageConf.Env + } + if userConf.Cmd == nil || len(userConf.Cmd) == 0 { + userConf.Cmd = imageConf.Cmd + } + if userConf.Dns == nil || len(userConf.Dns) == 0 { + userConf.Dns = imageConf.Dns + } +} + func (builder *Builder) Create(config *Config) (*Container, error) { // Lookup image img, err := builder.repositories.LookupImage(config.Image) if err != nil { return nil, err } + + if img.Config != nil { + builder.mergeConfig(config, img.Config) + } + + if config.Cmd == nil { + return nil, fmt.Errorf("No command specified") + } + // Generate id id := GenerateId() // Generate default hostname @@ -87,7 +132,7 @@ func (builder *Builder) Create(config *Config) (*Container, error) { // Commit creates a new filesystem image from the current state of a container. // The image can optionally be tagged into a repository -func (builder *Builder) Commit(container *Container, repository, tag, comment, author string) (*Image, error) { +func (builder *Builder) Commit(container *Container, repository, tag, comment, author string, config *Config) (*Image, error) { // FIXME: freeze the container before copying it to avoid data corruption? // FIXME: this shouldn't be in commands. rwTar, err := container.ExportRw() @@ -95,7 +140,7 @@ func (builder *Builder) Commit(container *Container, repository, tag, comment, a return nil, err } // Create a new image from the container's base layers + a new layer from container changes - img, err := builder.graph.Create(rwTar, container, comment, author) + img, err := builder.graph.Create(rwTar, container, comment, author, config) if err != nil { return nil, err } @@ -182,7 +227,7 @@ func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) (*Image, e } // Commit the container - base, err = builder.Commit(c, "", "", "", "") + base, err = builder.Commit(c, "", "", "", "", nil) if err != nil { return nil, err } @@ -234,7 +279,7 @@ func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) (*Image, e return nil, err } - base, err = builder.Commit(c, "", "", "", "") + base, err = builder.Commit(c, "", "", "", "", nil) if err != nil { return nil, err } diff --git a/commands.go b/commands.go index 08f9f7e18e..c72e24aa61 100644 --- a/commands.go +++ b/commands.go @@ -106,7 +106,7 @@ func (srv *Server) CmdInsert(stdin io.ReadCloser, stdout rcli.DockerConn, args . return err } // FIXME: Handle custom repo, tag comment, author - img, err = b.Commit(c, "", "", img.Comment, img.Author) + img, err = b.Commit(c, "", "", img.Comment, img.Author, nil) if err != nil { return err } diff --git a/container_test.go b/container_test.go index c06b8de876..5b63b2a0e7 100644 --- a/container_test.go +++ b/container_test.go @@ -225,7 +225,9 @@ func TestCommitAutoRun(t *testing.T) { t.Fatal(err) } defer nuke(runtime) - container1, err := runtime.Create( + + builder := NewBuilder(runtime) + container1, err := builder.Create( &Config{ Image: GetTestImage(runtime).Id, Cmd: []string{"/bin/sh", "-c", "echo hello > /world"}, @@ -256,8 +258,7 @@ func TestCommitAutoRun(t *testing.T) { } // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world - - container2, err := runtime.Create( + container2, err := builder.Create( &Config{ Image: img.Id, }, diff --git a/runtime.go b/runtime.go index ca27b7a7bf..5958aa1811 100644 --- a/runtime.go +++ b/runtime.go @@ -78,113 +78,6 @@ func (runtime *Runtime) containerRoot(id string) string { return path.Join(runtime.repository, id) } -func (runtime *Runtime) mergeConfig(userConf, imageConf *Config) { - if userConf.Hostname == "" { - userConf.Hostname = imageConf.Hostname - } - if userConf.User == "" { - userConf.User = imageConf.User - } - if userConf.Memory == 0 { - userConf.Memory = imageConf.Memory - } - if userConf.MemorySwap == 0 { - userConf.MemorySwap = imageConf.MemorySwap - } - if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 { - userConf.PortSpecs = imageConf.PortSpecs - } - if !userConf.Tty { - userConf.Tty = userConf.Tty - } - if !userConf.OpenStdin { - userConf.OpenStdin = imageConf.OpenStdin - } - if !userConf.StdinOnce { - userConf.StdinOnce = imageConf.StdinOnce - } - if userConf.Env == nil || len(userConf.Env) == 0 { - userConf.Env = imageConf.Env - } - if userConf.Cmd == nil || len(userConf.Cmd) == 0 { - userConf.Cmd = imageConf.Cmd - } - if userConf.Dns == nil || len(userConf.Dns) == 0 { - userConf.Dns = imageConf.Dns - } -} - -func (runtime *Runtime) Create(config *Config) (*Container, error) { - // Lookup image - img, err := runtime.repositories.LookupImage(config.Image) - if err != nil { - return nil, err - } - - if img.Config != nil { - runtime.mergeConfig(config, img.Config) - } - - if config.Cmd == nil || len(config.Cmd) == 0 { - return nil, fmt.Errorf("No command specified") - } - - // Generate id - id := GenerateId() - // Generate default hostname - // FIXME: the lxc template no longer needs to set a default hostname - if config.Hostname == "" { - config.Hostname = id[:12] - } - - container := &Container{ - // FIXME: we should generate the ID here instead of receiving it as an argument - Id: id, - Created: time.Now(), - Path: config.Cmd[0], - Args: config.Cmd[1:], //FIXME: de-duplicate from config - Config: config, - Image: img.Id, // Always use the resolved image id - NetworkSettings: &NetworkSettings{}, - // FIXME: do we need to store this in the container? - SysInitPath: sysInitPath, - } - container.root = runtime.containerRoot(container.Id) - // Step 1: create the container directory. - // This doubles as a barrier to avoid race conditions. - if err := os.Mkdir(container.root, 0700); err != nil { - return nil, err - } - - // If custom dns exists, then create a resolv.conf for the container - if len(config.Dns) > 0 { - container.ResolvConfPath = path.Join(container.root, "resolv.conf") - f, err := os.Create(container.ResolvConfPath) - if err != nil { - return nil, err - } - defer f.Close() - for _, dns := range config.Dns { - if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil { - return nil, err - } - } - } else { - container.ResolvConfPath = "/etc/resolv.conf" - } - - // Step 2: save the container json - if err := container.ToDisk(); err != nil { - return nil, err - } - // Step 3: register the container - if err := runtime.Register(container); err != nil { - return nil, err - } - return container, nil -} - -======= end func (runtime *Runtime) Load(id string) (*Container, error) { container := &Container{root: runtime.containerRoot(id)} if err := container.FromDisk(); err != nil {