Merge pull request #127 from hashicorp/b-docker-config

Correct implementation of config check for container and image cleanup
This commit is contained in:
Armon Dadgar 2015-09-27 14:50:16 -07:00
commit 9adea7ae1d
1 changed files with 47 additions and 25 deletions

View File

@ -25,6 +25,8 @@ type dockerPID struct {
type dockerHandle struct { type dockerHandle struct {
client *docker.Client client *docker.Client
logger *log.Logger logger *log.Logger
cleanupContainer bool
cleanupImage bool
imageID string imageID string
containerID string containerID string
waitCh chan error waitCh chan error
@ -44,11 +46,11 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool
return false, nil return false, nil
} }
cleanupContainer, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true")) _, err = strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true"))
if err != nil { if err != nil {
return false, fmt.Errorf("Unable to parse docker.cleanup.container: %s", err) return false, fmt.Errorf("Unable to parse docker.cleanup.container: %s", err)
} }
cleanupImage, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true")) _, err = strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true"))
if err != nil { if err != nil {
return false, fmt.Errorf("Unable to parse docker.cleanup.image: %s", err) return false, fmt.Errorf("Unable to parse docker.cleanup.image: %s", err)
} }
@ -179,6 +181,15 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
return nil, fmt.Errorf("CPU limit cannot be zero") return nil, fmt.Errorf("CPU limit cannot be zero")
} }
cleanupContainer, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.container: %s", err)
}
cleanupImage, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.image: %s", err)
}
// Initialize docker API client // Initialize docker API client
dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock") dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock")
client, err := docker.NewClient(dockerEndpoint) client, err := docker.NewClient(dockerEndpoint)
@ -245,6 +256,8 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
// Return a driver handle // Return a driver handle
h := &dockerHandle{ h := &dockerHandle{
client: client, client: client,
cleanupContainer: cleanupContainer,
cleanupImage: cleanupImage,
logger: d.logger, logger: d.logger,
imageID: dockerImage.ID, imageID: dockerImage.ID,
containerID: container.ID, containerID: container.ID,
@ -256,10 +269,19 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
} }
func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) { func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
cleanupContainer, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.container: %s", err)
}
cleanupImage, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.image: %s", err)
}
// Split the handle // Split the handle
pidBytes := []byte(strings.TrimPrefix(handleID, "DOCKER:")) pidBytes := []byte(strings.TrimPrefix(handleID, "DOCKER:"))
pid := &dockerPID{} pid := &dockerPID{}
err := json.Unmarshal(pidBytes, pid) err = json.Unmarshal(pidBytes, pid)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err) return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
} }
@ -295,6 +317,8 @@ func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, er
// Return a driver handle // Return a driver handle
h := &dockerHandle{ h := &dockerHandle{
client: client, client: client,
cleanupContainer: cleanupContainer,
cleanupImage: cleanupImage,
logger: d.logger, logger: d.logger,
imageID: pid.ImageID, imageID: pid.ImageID,
containerID: pid.ContainerID, containerID: pid.ContainerID,
@ -338,8 +362,7 @@ func (h *dockerHandle) Kill() error {
log.Printf("[INFO] driver.docker: stopped container %s", h.containerID) log.Printf("[INFO] driver.docker: stopped container %s", h.containerID)
// Cleanup container // Cleanup container
cleanupContainer, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true")) if h.cleanupContainer {
if err == nil && cleanupContainer {
err = h.client.RemoveContainer(docker.RemoveContainerOptions{ err = h.client.RemoveContainer(docker.RemoveContainerOptions{
ID: h.containerID, ID: h.containerID,
RemoveVolumes: true, RemoveVolumes: true,
@ -353,8 +376,7 @@ func (h *dockerHandle) Kill() error {
// Cleanup image. This operation may fail if the image is in use by another // Cleanup image. This operation may fail if the image is in use by another
// job. That is OK. Will we log a message but continue. // job. That is OK. Will we log a message but continue.
cleanupImage, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true")) if h.cleanupImage {
if err == nil && cleanupImage {
err = h.client.RemoveImage(h.imageID) err = h.client.RemoveImage(h.imageID)
if err != nil { if err != nil {
containers, err := h.client.ListContainers(docker.ListContainersOptions{ containers, err := h.client.ListContainers(docker.ListContainersOptions{