-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Driver.Prestart method #2054
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ee17940
Add Driver.Prestart method
schmichael 50934da
Emit "Downloading image" event
schmichael 750d597
Rename InitializationMessage to DriverMessage
schmichael bbcc27e
Use startContainer wrapper
schmichael 6702813
Fix formatting of downloading image message
schmichael aaa70ab
Append host env vars on every task env
schmichael 5ad7eea
Remove unneeded waitClient field
schmichael 630812b
lxc: Set image local env vars
schmichael decee19
Fix tests broken by TaskEnv change
schmichael 904543e
Remove unneeded env building
schmichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,9 @@ const ( | |
|
||
type DockerDriver struct { | ||
DriverContext | ||
|
||
imageID string | ||
driverConfig *DockerDriverConfig | ||
} | ||
|
||
type DockerDriverAuth struct { | ||
|
@@ -339,46 +342,56 @@ func (d *DockerDriver) Abilities() DriverAbilities { | |
} | ||
} | ||
|
||
func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) { | ||
func (d *DockerDriver) Prestart(ctx *ExecContext, task *structs.Task) error { | ||
// Set environment variables. | ||
d.taskEnv.SetAllocDir(allocdir.SharedAllocContainerPath). | ||
SetTaskLocalDir(allocdir.TaskLocalContainerPath).SetSecretsDir(allocdir.TaskSecretsContainerPath).Build() | ||
|
||
driverConfig, err := NewDockerDriverConfig(task, d.taskEnv) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
cleanupImage := d.config.ReadBoolDefault("docker.cleanup.image", true) | ||
|
||
taskDir, ok := ctx.AllocDir.TaskDirs[d.DriverContext.taskName] | ||
if !ok { | ||
return nil, fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName) | ||
return fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. taskDir is a variable in the task_runner now. Pass it through the context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's in the next PR |
||
} | ||
|
||
// Initialize docker API clients | ||
client, waitClient, err := d.dockerClients() | ||
client, _, err := d.dockerClients() | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to connect to docker daemon: %s", err) | ||
return fmt.Errorf("Failed to connect to docker daemon: %s", err) | ||
} | ||
|
||
if err := d.createImage(driverConfig, client, taskDir); err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
image := driverConfig.ImageName | ||
// Now that we have the image we can get the image id | ||
dockerImage, err := client.InspectImage(image) | ||
if err != nil { | ||
d.logger.Printf("[ERR] driver.docker: failed getting image id for %s: %s", image, err) | ||
return nil, fmt.Errorf("Failed to determine image id for `%s`: %s", image, err) | ||
return fmt.Errorf("Failed to determine image id for `%s`: %s", image, err) | ||
} | ||
d.logger.Printf("[DEBUG] driver.docker: identified image %s as %s", image, dockerImage.ID) | ||
|
||
// Set state needed by Start() | ||
d.imageID = dockerImage.ID | ||
d.driverConfig = driverConfig | ||
return nil | ||
} | ||
|
||
func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) { | ||
bin, err := discover.NomadExecutable() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to find the nomad binary: %v", err) | ||
} | ||
|
||
taskDir, ok := ctx.AllocDir.TaskDirs[d.DriverContext.taskName] | ||
if !ok { | ||
return nil, fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName) | ||
} | ||
pluginLogFile := filepath.Join(taskDir, fmt.Sprintf("%s-executor.out", task.Name)) | ||
pluginConfig := &plugin.ClientConfig{ | ||
Cmd: exec.Command(bin, "executor", pluginLogFile), | ||
|
@@ -404,9 +417,9 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
|
||
// Only launch syslog server if we're going to use it! | ||
syslogAddr := "" | ||
if runtime.GOOS == "darwin" && len(driverConfig.Logging) == 0 { | ||
if runtime.GOOS == "darwin" && len(d.driverConfig.Logging) == 0 { | ||
d.logger.Printf("[DEBUG] driver.docker: disabling syslog driver as Docker for Mac workaround") | ||
} else if len(driverConfig.Logging) == 0 || driverConfig.Logging[0].Type == "syslog" { | ||
} else if len(d.driverConfig.Logging) == 0 || d.driverConfig.Logging[0].Type == "syslog" { | ||
ss, err := exec.LaunchSyslogServer() | ||
if err != nil { | ||
pluginClient.Kill() | ||
|
@@ -415,11 +428,11 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
syslogAddr = ss.Addr | ||
} | ||
|
||
config, err := d.createContainerConfig(ctx, task, driverConfig, syslogAddr) | ||
config, err := d.createContainerConfig(ctx, task, d.driverConfig, syslogAddr) | ||
if err != nil { | ||
d.logger.Printf("[ERR] driver.docker: failed to create container configuration for image %s: %s", image, err) | ||
d.logger.Printf("[ERR] driver.docker: failed to create container configuration for image %s: %s", d.imageID, err) | ||
pluginClient.Kill() | ||
return nil, fmt.Errorf("Failed to create container configuration for image %s: %s", image, err) | ||
return nil, fmt.Errorf("Failed to create container configuration for image %s: %s", d.imageID, err) | ||
} | ||
|
||
container, rerr := d.createContainer(config) | ||
|
@@ -432,17 +445,17 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
|
||
d.logger.Printf("[INFO] driver.docker: created container %s", container.ID) | ||
|
||
cleanupImage := d.config.ReadBoolDefault("docker.cleanup.image", true) | ||
|
||
// We don't need to start the container if the container is already running | ||
// since we don't create containers which are already present on the host | ||
// and are running | ||
if !container.State.Running { | ||
// Start the container | ||
err := d.startContainer(container) | ||
if err != nil { | ||
if err := d.startContainer(container); err != nil { | ||
d.logger.Printf("[ERR] driver.docker: failed to start container %s: %s", container.ID, err) | ||
pluginClient.Kill() | ||
err.Err = fmt.Sprintf("Failed to start container %s: %s", container.ID, err) | ||
return nil, err | ||
return nil, fmt.Errorf("Failed to start container %s: %s", container.ID, err) | ||
} | ||
d.logger.Printf("[INFO] driver.docker: started container %s", container.ID) | ||
} else { | ||
|
@@ -459,7 +472,7 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
pluginClient: pluginClient, | ||
cleanupImage: cleanupImage, | ||
logger: d.logger, | ||
imageID: dockerImage.ID, | ||
imageID: d.imageID, | ||
containerID: container.ID, | ||
version: d.config.Version, | ||
killTimeout: GetKillTimeout(task.KillTimeout, maxKill), | ||
|
@@ -935,6 +948,7 @@ func (d *DockerDriver) pullImage(driverConfig *DockerDriverConfig, client *docke | |
} | ||
} | ||
|
||
d.emitEvent("Downloading image %s:%s", repo, tag) | ||
err := client.PullImage(pullOptions, authOptions) | ||
if err != nil { | ||
d.logger.Printf("[ERR] driver.docker: failed pulling container %s:%s: %s", repo, tag, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you emit a message for when it downloads an image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't believe I missed that in this PR! Added (inside the createImage method so it's not emitted if the image already exists).