-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also refactored out a container module, and got rid of the state comp…
…letely
- Loading branch information
Showing
8 changed files
with
107 additions
and
202 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package container | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// Options represents the configuration for running a docker container to | ||
// be used as backdrop. | ||
type Options struct { | ||
Client *client.Client | ||
Image string | ||
Name string | ||
Interactive bool | ||
Interpreter []string | ||
Entrypoint string | ||
Script string | ||
Command []string | ||
Environment []string | ||
Volumes []string | ||
VolumesFrom []string | ||
User string | ||
WorkingDir string | ||
} | ||
|
||
// Run runs a docker container as backdrop. | ||
func Run(ctx context.Context, options Options) error { | ||
if options.Client == nil { | ||
return errors.New("client may not be nil") | ||
} | ||
|
||
containerID, err := createContainer(ctx, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = uploadEntrypoint(ctx, containerID, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return runContainer(ctx, containerID, options) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package container | ||
|
||
import ( | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/network" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func createContainer(ctx context.Context, options Options) (string, error) { | ||
response, err := options.Client.ContainerCreate( | ||
ctx, | ||
&container.Config{ | ||
User: options.User, | ||
AttachStdin: true, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: true, | ||
StdinOnce: true, | ||
Env: options.Environment, | ||
Cmd: options.Command, | ||
Image: options.Image, | ||
WorkingDir: options.WorkingDir, | ||
Entrypoint: getEntrypoint(options), | ||
}, | ||
&container.HostConfig{ | ||
AutoRemove: true, | ||
Binds: options.Volumes, | ||
VolumesFrom: options.VolumesFrom, | ||
}, | ||
&network.NetworkingConfig{}, | ||
options.Name, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
return response.ID, nil | ||
} | ||
|
||
func getEntrypoint(options Options) []string { | ||
entrypoint := []string{"/bin/sh"} | ||
if len(options.Interpreter) > 0 { | ||
entrypoint = options.Interpreter | ||
} | ||
if !options.Interactive { | ||
entrypoint = append(entrypoint, options.Entrypoint) | ||
} | ||
return entrypoint | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.