2013-11-09 15:12:23 -05:00
|
|
|
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"
|
2014-01-19 22:55:01 -05:00
|
|
|
"os"
|
2013-11-09 15:12:23 -05:00
|
|
|
"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"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-11-09 15:12:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DockerDriver struct {
|
2013-12-27 12:17:45 -05:00
|
|
|
Ui packer.Ui
|
2015-05-27 15:55:36 -04:00
|
|
|
Ctx *interpolate.Context
|
2014-09-05 18:06:07 -04:00
|
|
|
|
|
|
|
l sync.Mutex
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:33:42 -05:00
|
|
|
func (d *DockerDriver) Commit(id string, author string, changes []string, message string) (string, error) {
|
2014-07-20 14:57:10 -04:00
|
|
|
var stdout bytes.Buffer
|
2014-07-21 15:39:17 -04:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2016-11-24 05:42:34 -05:00
|
|
|
args := []string{"commit"}
|
2016-11-24 07:33:42 -05:00
|
|
|
if author != "" {
|
|
|
|
args = append(args, "--author", author)
|
|
|
|
}
|
2016-11-24 05:42:34 -05:00
|
|
|
for _, change := range changes {
|
|
|
|
args = append(args, "--change", change)
|
|
|
|
}
|
2016-11-24 07:33:42 -05:00
|
|
|
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...)
|
2014-07-20 14:57:10 -04:00
|
|
|
cmd.Stdout = &stdout
|
2014-07-21 15:39:17 -04:00
|
|
|
cmd.Stderr = &stderr
|
2014-07-20 14:57:10 -04:00
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2014-07-21 15:39:17 -04:00
|
|
|
err = fmt.Errorf("Error committing container: %s\nStderr: %s",
|
|
|
|
err, stderr.String())
|
2014-07-20 14:57:10 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:04:43 -05:00
|
|
|
func (d *DockerDriver) Import(path string, changes []string, repo string) (string, error) {
|
2017-02-09 23:39:55 -05:00
|
|
|
var stdout, stderr bytes.Buffer
|
2014-01-19 22:55:01 -05:00
|
|
|
|
2018-12-17 23:04:43 -05:00
|
|
|
args := []string{"import"}
|
|
|
|
|
2018-12-17 23:37:32 -05:00
|
|
|
for _, change := range changes {
|
|
|
|
args = append(args, "--change", change)
|
|
|
|
}
|
2018-12-17 23:04:43 -05:00
|
|
|
|
2018-12-17 23:37:32 -05:00
|
|
|
args = append(args, "-")
|
2018-12-17 23:04:43 -05:00
|
|
|
args = append(args, repo)
|
2018-12-17 23:59:41 -05:00
|
|
|
|
2018-12-17 23:37:32 -05:00
|
|
|
cmd := exec.Command("docker", args...)
|
2018-12-17 23:59:41 -05:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-12-17 23:04:43 -05:00
|
|
|
|
2014-01-19 22:55:01 -05:00
|
|
|
// There should be only one artifact of the Docker builder
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-12-17 23:59:41 -05:00
|
|
|
log.Printf("Importing tarball with args: %v", args)
|
2018-12-17 23:37:32 -05:00
|
|
|
|
2014-01-19 22:55:01 -05:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.Copy(stdin, file)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2017-02-09 23:39:55 -05:00
|
|
|
return "", fmt.Errorf("Error importing container: %s\n\nStderr: %s", err, stderr.String())
|
2014-01-19 22:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(stdout.String()), nil
|
|
|
|
}
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:08:44 -04:00
|
|
|
func (d *DockerDriver) Sha256(id string) (string, error) {
|
|
|
|
var stderr, stdout bytes.Buffer
|
|
|
|
cmd := exec.Command(
|
|
|
|
"docker",
|
|
|
|
"inspect",
|
|
|
|
"--format",
|
|
|
|
"{{ .Id }}",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:53:51 -04:00
|
|
|
func (d *DockerDriver) Login(repo, user, pass string) error {
|
2014-09-05 18:06:07 -04:00
|
|
|
d.l.Lock()
|
|
|
|
|
2017-12-04 08:27:56 -05:00
|
|
|
version_running, err := d.Version()
|
|
|
|
if err != nil {
|
|
|
|
d.l.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version 17.07.0 of Docker adds support for the new
|
|
|
|
// `--password-stdin` option which can be used to offer
|
|
|
|
// password via the standard input, rather than passing
|
|
|
|
// the password and/or token using a command line switch.
|
|
|
|
constraint, err := version.NewConstraint(">= 17.07.0")
|
|
|
|
if err != nil {
|
|
|
|
d.l.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("docker")
|
|
|
|
cmd.Args = append(cmd.Args, "login")
|
|
|
|
|
2014-09-05 17:26:19 -04:00
|
|
|
if user != "" {
|
2017-12-04 08:27:56 -05:00
|
|
|
cmd.Args = append(cmd.Args, "-u", user)
|
2014-09-05 17:26:19 -04:00
|
|
|
}
|
2017-12-04 08:27:56 -05:00
|
|
|
|
2014-09-05 17:26:19 -04:00
|
|
|
if pass != "" {
|
2017-12-04 08:27:56 -05:00
|
|
|
if constraint.Check(version_running) {
|
|
|
|
cmd.Args = append(cmd.Args, "--password-stdin")
|
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
d.l.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, pass)
|
|
|
|
stdin.Close()
|
|
|
|
} else {
|
|
|
|
cmd.Args = append(cmd.Args, "-p", pass)
|
|
|
|
}
|
2014-09-05 17:26:19 -04:00
|
|
|
}
|
2017-12-04 08:27:56 -05:00
|
|
|
|
2014-09-05 17:26:19 -04:00
|
|
|
if repo != "" {
|
2017-12-04 08:27:56 -05:00
|
|
|
cmd.Args = append(cmd.Args, repo)
|
2014-09-05 17:26:19 -04:00
|
|
|
}
|
|
|
|
|
2017-12-04 08:27:56 -05:00
|
|
|
err = runAndStream(cmd, d.Ui)
|
2014-09-05 18:06:07 -04:00
|
|
|
if err != nil {
|
|
|
|
d.l.Unlock()
|
2017-12-04 08:27:56 -05:00
|
|
|
return err
|
2014-09-05 18:06:07 -04:00
|
|
|
}
|
|
|
|
|
2017-12-04 08:27:56 -05:00
|
|
|
return nil
|
2014-09-05 17:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-09-05 17:26:19 -04:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-12-27 12:17:45 -05:00
|
|
|
// Build up the template data
|
|
|
|
var tplData startContainerTemplate
|
|
|
|
tplData.Image = config.Image
|
2019-03-29 11:50:02 -04:00
|
|
|
ictx := *d.Ctx
|
|
|
|
ictx.Data = &tplData
|
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
|
2014-09-05 18:48:42 -04:00
|
|
|
args := []string{"run"}
|
2020-07-11 12:23:55 -04:00
|
|
|
for _, v := range config.Device {
|
|
|
|
args = append(args, "--device", v)
|
|
|
|
}
|
2020-07-11 11:02:31 -04:00
|
|
|
for _, v := range config.CapAdd {
|
|
|
|
args = append(args, "--cap-add", v)
|
|
|
|
}
|
|
|
|
for _, v := range config.CapDrop {
|
|
|
|
args = append(args, "--cap-drop", v)
|
|
|
|
}
|
2016-04-29 22:12:20 -04:00
|
|
|
if config.Privileged {
|
|
|
|
args = append(args, "--privileged")
|
|
|
|
}
|
2020-07-11 11:02:31 -04:00
|
|
|
for _, v := range config.TmpFs {
|
|
|
|
args = append(args, "--tmpfs", v)
|
|
|
|
}
|
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 {
|
2019-03-29 11:50:02 -04:00
|
|
|
v, err := interpolate.Render(v, &ictx)
|
2013-12-27 12:17:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-05 18:48:42 -04:00
|
|
|
|
|
|
|
args = append(args, v)
|
2013-12-27 12:17:45 -05:00
|
|
|
}
|
|
|
|
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 {
|
2019-03-27 17:51:50 -04:00
|
|
|
if err := exec.Command("docker", "stop", id).Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DockerDriver) KillContainer(id string) error {
|
2014-09-04 20:03:24 -04:00
|
|
|
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
|
|
|
}
|
2013-11-10 00:26:05 -05:00
|
|
|
|
2015-04-18 00:12:28 -04:00
|
|
|
func (d *DockerDriver) TagImage(id string, repo string, force bool) error {
|
|
|
|
args := []string{"tag"}
|
2017-02-15 11:30:32 -05:00
|
|
|
|
|
|
|
// detect running docker version before tagging
|
|
|
|
// flag `force` for docker tagging was removed after Docker 1.12.0
|
|
|
|
// to keep its backward compatibility, we are not going to remove `force`
|
|
|
|
// option, but to ignore it when Docker version >= 1.12.0
|
|
|
|
//
|
|
|
|
// for more detail, please refer to the following links:
|
|
|
|
// - https://docs.docker.com/engine/deprecated/#/f-flag-on-docker-tag
|
|
|
|
// - https://github.com/docker/docker/pull/23090
|
2017-02-15 19:26:44 -05:00
|
|
|
version_running, err := d.Version()
|
2017-02-15 11:30:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-04 08:27:56 -05:00
|
|
|
version_deprecated, err := version.NewVersion("1.12.0")
|
2017-02-15 19:26:44 -05:00
|
|
|
if err != nil {
|
|
|
|
// should never reach this line
|
|
|
|
return err
|
2017-02-15 11:30:32 -05:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:26:44 -05:00
|
|
|
if force {
|
|
|
|
if version_running.LessThan(version_deprecated) {
|
2017-02-15 11:30:32 -05:00
|
|
|
args = append(args, "-f")
|
2017-02-15 19:26:44 -05:00
|
|
|
} else {
|
|
|
|
// do nothing if Docker version >= 1.12.0
|
|
|
|
log.Printf("[WARN] option: \"force\" will be ignored here")
|
|
|
|
log.Printf("since it was removed after Docker 1.12.0 released")
|
2017-02-15 11:30:32 -05:00
|
|
|
}
|
2015-04-18 00:12:28 -04:00
|
|
|
}
|
|
|
|
args = append(args, id, repo)
|
|
|
|
|
2014-07-21 15:39:17 -04:00
|
|
|
var stderr bytes.Buffer
|
2015-04-18 00:12:28 -04:00
|
|
|
cmd := exec.Command("docker", args...)
|
2014-07-21 15:39:17 -04:00
|
|
|
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 {
|
2014-07-21 15:39:17 -04:00
|
|
|
err = fmt.Errorf("Error tagging image: %s\nStderr: %s",
|
|
|
|
err, stderr.String())
|
2014-07-20 14:58:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-10 00:26:05 -05:00
|
|
|
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]))
|
|
|
|
}
|