2013-11-09 15:12:23 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-11-09 16:03:01 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-11-09 15:12:23 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-11-09 16:15:51 -05:00
|
|
|
"io"
|
2013-11-09 16:03:01 -05:00
|
|
|
"log"
|
2014-01-19 22:55:01 -05:00
|
|
|
"os"
|
2013-11-09 15:12:23 -05:00
|
|
|
"os/exec"
|
2013-11-09 16:03:01 -05:00
|
|
|
"strings"
|
2013-11-09 15:12:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DockerDriver struct {
|
2013-12-27 12:17:45 -05:00
|
|
|
Ui packer.Ui
|
|
|
|
Tpl *packer.ConfigTemplate
|
2013-11-09 15:12:23 -05:00
|
|
|
}
|
|
|
|
|
2014-01-19 22:48:06 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-01-19 22:55:01 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-11-09 15:12:23 -05:00
|
|
|
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
|
|
|
|
2014-01-19 23:42:42 -05:00
|
|
|
func (d *DockerDriver) Push(name string) error {
|
|
|
|
cmd := exec.Command("docker", "push", name)
|
|
|
|
return runAndStream(cmd, d.Ui)
|
|
|
|
}
|
|
|
|
|
2013-11-09 16:03:01 -05:00
|
|
|
func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) {
|
2013-12-27 12:17:45 -05:00
|
|
|
// Build up the template data
|
|
|
|
var tplData startContainerTemplate
|
|
|
|
tplData.Image = config.Image
|
2013-11-09 16:03:01 -05:00
|
|
|
if len(config.Volumes) > 0 {
|
|
|
|
volumes := make([]string, 0, len(config.Volumes))
|
|
|
|
for host, guest := range config.Volumes {
|
|
|
|
volumes = append(volumes, fmt.Sprintf("%s:%s", host, guest))
|
|
|
|
}
|
|
|
|
|
2013-12-27 12:17:45 -05:00
|
|
|
tplData.Volumes = strings.Join(volumes, ",")
|
2013-11-09 16:03:01 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 12:17:45 -05:00
|
|
|
// Args that we're going to pass to Docker
|
|
|
|
args := config.RunCommand
|
|
|
|
for i, v := range args {
|
|
|
|
var err error
|
|
|
|
args[i], err = d.Tpl.Process(v, &tplData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2013-11-09 20:27:30 -05:00
|
|
|
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 {
|
|
|
|
return exec.Command("docker", "kill", id).Run()
|
|
|
|
}
|
2013-11-10 00:26:05 -05:00
|
|
|
|
|
|
|
func (d *DockerDriver) Verify() error {
|
|
|
|
if _, err := exec.LookPath("docker"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|