packer-cn/builder/docker/driver_docker.go

321 lines
6.7 KiB
Go
Raw Normal View History

package docker
import (
2013-11-09 16:03:01 -05:00
"bytes"
"fmt"
2013-11-09 16:15:51 -05:00
"io"
2013-11-09 16:03:01 -05:00
"log"
"os"
"os/exec"
2015-05-29 12:29:59 -04:00
"regexp"
2013-11-09 16:03:01 -05:00
"strings"
2014-09-05 18:06:07 -04:00
"sync"
2015-05-29 12:29:59 -04:00
"github.com/hashicorp/go-version"
2014-09-05 18:06:07 -04:00
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
)
type DockerDriver struct {
Ui packer.Ui
Ctx *interpolate.Context
2014-09-05 18:06:07 -04:00
l sync.Mutex
}
func (d *DockerDriver) DeleteImage(id string) error {
var stderr bytes.Buffer
cmd := exec.Command("docker", "rmi", id)
cmd.Stderr = &stderr
log.Printf("Deleting image: %s", id)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error deleting image: %s\nStderr: %s",
err, stderr.String())
return err
}
return nil
}
func (d *DockerDriver) Commit(id string, author string, changes []string, message string) (string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
2016-11-24 05:42:34 -05:00
args := []string{"commit"}
if author != "" {
args = append(args, "--author", author)
}
2016-11-24 05:42:34 -05:00
for _, change := range changes {
args = append(args, "--change", change)
}
if message != "" {
args = append(args, "--message", message)
}
2016-11-24 05:42:34 -05:00
args = append(args, id)
2016-11-24 07:07:01 -05:00
log.Printf("Committing container with args: %v", args)
2016-11-24 05:42:34 -05:00
cmd := exec.Command("docker", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return "", err
}
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error committing container: %s\nStderr: %s",
err, stderr.String())
return "", err
}
return strings.TrimSpace(stdout.String()), nil
}
2013-11-09 16:15:51 -05:00
func (d *DockerDriver) Export(id string, dst io.Writer) error {
var stderr bytes.Buffer
cmd := exec.Command("docker", "export", id)
cmd.Stdout = dst
cmd.Stderr = &stderr
log.Printf("Exporting container: %s", id)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error exporting: %s\nStderr: %s",
err, stderr.String())
return err
}
return nil
}
func (d *DockerDriver) Import(path string, repo string) (string, error) {
var stdout bytes.Buffer
cmd := exec.Command("docker", "import", "-", repo)
cmd.Stdout = &stdout
stdin, err := cmd.StdinPipe()
if err != nil {
return "", err
}
// There should be only one artifact of the Docker builder
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
if err := cmd.Start(); err != nil {
return "", err
}
go func() {
defer stdin.Close()
io.Copy(stdin, file)
}()
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error importing container: %s", err)
return "", err
}
return strings.TrimSpace(stdout.String()), nil
}
func (d *DockerDriver) IPAddress(id string) (string, error) {
var stderr, stdout bytes.Buffer
cmd := exec.Command(
"docker",
"inspect",
"--format",
"{{ .NetworkSettings.IPAddress }}",
id)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error: %s\n\nStderr: %s", err, stderr.String())
}
return strings.TrimSpace(stdout.String()), nil
}
func (d *DockerDriver) Login(repo, email, user, pass string) error {
2014-09-05 18:06:07 -04:00
d.l.Lock()
args := []string{"login"}
if email != "" {
args = append(args, "-e", email)
}
if user != "" {
args = append(args, "-u", user)
}
if pass != "" {
args = append(args, "-p", pass)
}
if repo != "" {
args = append(args, repo)
}
cmd := exec.Command("docker", args...)
2014-09-05 18:06:07 -04:00
err := runAndStream(cmd, d.Ui)
if err != nil {
d.l.Unlock()
}
return err
}
func (d *DockerDriver) Logout(repo string) error {
args := []string{"logout"}
if repo != "" {
args = append(args, repo)
}
cmd := exec.Command("docker", args...)
2014-09-05 18:06:07 -04:00
err := runAndStream(cmd, d.Ui)
d.l.Unlock()
return err
}
func (d *DockerDriver) Pull(image string) error {
cmd := exec.Command("docker", "pull", image)
return runAndStream(cmd, d.Ui)
}
2013-11-09 16:03:01 -05:00
func (d *DockerDriver) Push(name string) error {
cmd := exec.Command("docker", "push", name)
return runAndStream(cmd, d.Ui)
}
2014-07-20 16:58:07 -04:00
func (d *DockerDriver) SaveImage(id string, dst io.Writer) error {
var stderr bytes.Buffer
cmd := exec.Command("docker", "save", id)
cmd.Stdout = dst
cmd.Stderr = &stderr
log.Printf("Exporting image: %s", id)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error exporting: %s\nStderr: %s",
err, stderr.String())
return err
}
return nil
}
2013-11-09 16:03:01 -05:00
func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) {
// Build up the template data
var tplData startContainerTemplate
tplData.Image = config.Image
ctx := *d.Ctx
ctx.Data = &tplData
2013-11-09 16:03:01 -05:00
// Args that we're going to pass to Docker
2014-09-05 18:48:42 -04:00
args := []string{"run"}
if config.Privileged {
args = append(args, "--privileged")
}
2014-09-05 18:48:42 -04:00
for host, guest := range config.Volumes {
args = append(args, "-v", fmt.Sprintf("%s:%s", host, guest))
}
for _, v := range config.RunCommand {
v, err := interpolate.Render(v, &ctx)
if err != nil {
return "", err
}
2014-09-05 18:48:42 -04:00
args = append(args, v)
}
d.Ui.Message(fmt.Sprintf(
"Run command: docker %s", strings.Join(args, " ")))
2013-11-09 16:03:01 -05:00
// Start the container
var stdout, stderr bytes.Buffer
cmd := exec.Command("docker", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
log.Printf("Starting container with args: %v", args)
if err := cmd.Start(); err != nil {
return "", err
}
log.Println("Waiting for container to finish starting")
if err := cmd.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("Docker exited with a non-zero exit status.\nStderr: %s",
stderr.String())
}
2013-11-09 16:03:01 -05:00
return "", err
}
// Capture the container ID, which is alone on stdout
return strings.TrimSpace(stdout.String()), nil
}
func (d *DockerDriver) StopContainer(id string) error {
if err := exec.Command("docker", "kill", id).Run(); err != nil {
return err
}
return exec.Command("docker", "rm", id).Run()
2013-11-09 16:03:01 -05:00
}
2015-04-18 00:12:28 -04:00
func (d *DockerDriver) TagImage(id string, repo string, force bool) error {
args := []string{"tag"}
if force {
args = append(args, "-f")
}
args = append(args, id, repo)
var stderr bytes.Buffer
2015-04-18 00:12:28 -04:00
cmd := exec.Command("docker", args...)
cmd.Stderr = &stderr
2014-07-20 14:58:03 -04:00
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
err = fmt.Errorf("Error tagging image: %s\nStderr: %s",
err, stderr.String())
2014-07-20 14:58:03 -04:00
return err
}
return nil
}
func (d *DockerDriver) Verify() error {
if _, err := exec.LookPath("docker"); err != nil {
return err
}
return nil
}
2015-05-29 12:29:59 -04:00
func (d *DockerDriver) Version() (*version.Version, error) {
output, err := exec.Command("docker", "-v").Output()
if err != nil {
return nil, err
}
match := regexp.MustCompile(version.VersionRegexpRaw).FindSubmatch(output)
if match == nil {
return nil, fmt.Errorf("unknown version: %s", output)
}
return version.NewVersion(string(match[0]))
}