2014-07-31 16:57:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2014-10-01 05:35:44 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-04-10 16:41:43 -04:00
|
|
|
"io"
|
2014-10-01 05:35:44 -04:00
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-31 16:57:21 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
type ContainerCommitConfig struct {
|
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
|
|
|
Changes []string
|
|
|
|
Config io.ReadCloser
|
|
|
|
}
|
2014-07-31 16:57:21 -04:00
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
func (daemon *Daemon) ContainerCommit(name string, c *ContainerCommitConfig) (string, error) {
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
2015-04-10 16:41:43 -04:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-04-10 16:41:43 -04:00
|
|
|
subenv engine.Env
|
2014-10-01 05:35:44 -04:00
|
|
|
config = container.Config
|
|
|
|
stdoutBuffer = bytes.NewBuffer(nil)
|
|
|
|
newConfig runconfig.Config
|
2014-07-31 16:57:21 -04:00
|
|
|
)
|
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
if err := subenv.Decode(c.Config); err != nil {
|
|
|
|
logrus.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:35:44 -04:00
|
|
|
buildConfigJob := daemon.eng.Job("build_config")
|
|
|
|
buildConfigJob.Stdout.Add(stdoutBuffer)
|
2015-04-10 16:41:43 -04:00
|
|
|
buildConfigJob.SetenvList("changes", c.Changes)
|
2014-10-01 05:35:44 -04:00
|
|
|
// FIXME this should be remove when we remove deprecated config param
|
2015-04-10 16:41:43 -04:00
|
|
|
buildConfigJob.SetenvSubEnv("config", &subenv)
|
2014-10-01 05:35:44 -04:00
|
|
|
|
|
|
|
if err := buildConfigJob.Run(); err != nil {
|
2015-04-10 16:41:43 -04:00
|
|
|
return "", err
|
2014-10-01 05:35:44 -04:00
|
|
|
}
|
|
|
|
if err := json.NewDecoder(stdoutBuffer).Decode(&newConfig); err != nil {
|
2015-04-10 16:41:43 -04:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := runconfig.Merge(&newConfig, config); err != nil {
|
2015-04-10 16:41:43 -04:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:41:43 -04:00
|
|
|
img, err := daemon.Commit(container, c.Repo, c.Tag, c.Comment, c.Author, c.Pause, &newConfig)
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
2015-04-10 16:41:43 -04:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
2015-04-10 16:41:43 -04:00
|
|
|
|
|
|
|
return img.ID, nil
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
|
|
|
// The image can optionally be tagged into a repository
|
|
|
|
func (daemon *Daemon) Commit(container *Container, repository, tag, comment, author string, pause bool, config *runconfig.Config) (*image.Image, error) {
|
2015-02-24 06:28:40 -05:00
|
|
|
if pause && !container.IsPaused() {
|
2014-07-31 16:57:21 -04:00
|
|
|
container.Pause()
|
|
|
|
defer container.Unpause()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Mount(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
|
|
|
rwTar, err := container.ExportRw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rwTar.Close()
|
|
|
|
|
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
|
|
|
var (
|
2014-10-28 17:06:23 -04:00
|
|
|
containerID, parentImageID string
|
|
|
|
containerConfig *runconfig.Config
|
2014-07-31 16:57:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if container != nil {
|
|
|
|
containerID = container.ID
|
2014-10-28 17:06:23 -04:00
|
|
|
parentImageID = container.ImageID
|
2014-07-31 16:57:21 -04:00
|
|
|
containerConfig = container.Config
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
img, err := daemon.graph.Create(rwTar, containerID, parentImageID, comment, author, containerConfig, config)
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the image if needed
|
|
|
|
if repository != "" {
|
2015-04-13 22:46:29 -04:00
|
|
|
if err := daemon.repositories.Tag(repository, tag, img.ID, true); err != nil {
|
2014-07-31 16:57:21 -04:00
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|