2013-11-08 19:55:02 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-11-09 14:47:32 -05:00
|
|
|
"fmt"
|
2015-05-29 19:24:29 -04:00
|
|
|
"os"
|
2019-05-22 10:43:29 -04:00
|
|
|
"runtime"
|
2015-05-27 15:55:36 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-05-27 15:55:36 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-11-08 19:55:02 -05:00
|
|
|
)
|
|
|
|
|
2015-08-18 17:38:32 -04:00
|
|
|
var (
|
2015-08-19 16:12:16 -04:00
|
|
|
errArtifactNotUsed = fmt.Errorf("No instructions given for handling the artifact; expected commit, discard, or export_path")
|
|
|
|
errArtifactUseConflict = fmt.Errorf("Cannot specify more than one of commit, discard, and export_path")
|
|
|
|
errExportPathNotFile = fmt.Errorf("export_path must be a file, not a directory")
|
|
|
|
errImageNotSpecified = fmt.Errorf("Image must be specified")
|
2015-08-18 17:38:32 -04:00
|
|
|
)
|
|
|
|
|
2013-11-08 19:55:02 -05:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-06-15 01:09:38 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2013-11-08 19:55:02 -05:00
|
|
|
|
2019-03-27 15:09:08 -04:00
|
|
|
Author string
|
|
|
|
Changes []string
|
|
|
|
Commit bool
|
2019-05-28 11:50:58 -04:00
|
|
|
// The directory inside container to mount temp
|
|
|
|
// directory from host server for work file
|
|
|
|
// provisioner. This defaults to
|
|
|
|
// c:/packer-files on windows and /packer-files on other systems.
|
|
|
|
ContainerDir string `mapstructure:"container_dir" required:"false"`
|
2019-03-27 15:09:08 -04:00
|
|
|
Discard bool
|
2019-05-28 11:50:58 -04:00
|
|
|
// Username (UID) to run remote commands with. You can
|
|
|
|
// also set the group name/ID if you want: (UID or UID:GID).
|
|
|
|
// You may need this if you get permission errors trying to run the shell or
|
|
|
|
// other provisioners.
|
|
|
|
ExecUser string `mapstructure:"exec_user" required:"false"`
|
2019-03-27 15:09:08 -04:00
|
|
|
ExportPath string `mapstructure:"export_path"`
|
|
|
|
Image string
|
|
|
|
Message string
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true, run the docker container with the
|
|
|
|
// --privileged flag. This defaults to false if not set.
|
|
|
|
Privileged bool `mapstructure:"privileged" required:"false"`
|
2019-03-27 15:09:08 -04:00
|
|
|
Pty bool
|
|
|
|
Pull bool
|
2019-05-28 11:50:58 -04:00
|
|
|
// An array of arguments to pass to
|
|
|
|
// docker run in order to run the container. By default this is set to
|
|
|
|
// ["-d", "-i", "-t", "--entrypoint=/bin/sh", "--", "{{.Image}}"] if you are
|
|
|
|
// using a linux container, and
|
|
|
|
// ["-d", "-i", "-t", "--entrypoint=powershell", "--", "{{.Image}}"] if you
|
|
|
|
// are running a windows container. {{.Image}} is a template variable that
|
|
|
|
// corresponds to the image template option. Passing the entrypoint option
|
|
|
|
// this way will make it the default entrypoint of the resulting image, so
|
|
|
|
// running docker run -it --rm will start the docker image from the
|
|
|
|
// /bin/sh shell interpreter; you could run a script or another shell by
|
|
|
|
// running docker run -it --rm -c /bin/bash. If your docker image
|
|
|
|
// embeds a binary intended to be run often, you should consider changing the
|
|
|
|
// default entrypoint to point to it.
|
|
|
|
RunCommand []string `mapstructure:"run_command" required:"false"`
|
2019-03-27 15:09:08 -04:00
|
|
|
Volumes map[string]string
|
2019-05-28 11:50:58 -04:00
|
|
|
// If true, files uploaded to the container
|
|
|
|
// will be owned by the user the container is running as. If false, the owner
|
|
|
|
// will depend on the version of docker installed in the system. Defaults to
|
|
|
|
// true.
|
|
|
|
FixUploadOwner bool `mapstructure:"fix_upload_owner" required:"false"`
|
|
|
|
// If "true", tells Packer that you are building a
|
|
|
|
// Windows container running on a windows host. This is necessary for building
|
|
|
|
// Windows containers, because our normal docker bindings do not work for them.
|
|
|
|
WindowsContainer bool `mapstructure:"windows_container" required:"false"`
|
2013-11-09 01:00:57 -05:00
|
|
|
|
2015-08-18 19:55:29 -04:00
|
|
|
// This is used to login to dockerhub to pull a private base container. For
|
|
|
|
// pushing to dockerhub, see the docker post-processors
|
2016-09-03 10:45:52 -04:00
|
|
|
Login bool
|
2019-05-28 11:50:58 -04:00
|
|
|
// The password to use to authenticate to login.
|
|
|
|
LoginPassword string `mapstructure:"login_password" required:"false"`
|
|
|
|
// The server address to login to.
|
|
|
|
LoginServer string `mapstructure:"login_server" required:"false"`
|
|
|
|
// The username to use to authenticate to login.
|
|
|
|
LoginUsername string `mapstructure:"login_username" required:"false"`
|
|
|
|
// Defaults to false. If true, the builder will login
|
|
|
|
// in order to pull the image from Amazon EC2 Container Registry
|
|
|
|
// (ECR). The builder only logs in for the
|
|
|
|
// duration of the pull. If true login_server is required and login,
|
|
|
|
// login_username, and login_password will be ignored. For more
|
|
|
|
// information see the section on ECR.
|
|
|
|
EcrLogin bool `mapstructure:"ecr_login" required:"false"`
|
2016-09-03 10:45:52 -04:00
|
|
|
AwsAccessConfig `mapstructure:",squash"`
|
2015-07-16 22:34:36 -04:00
|
|
|
|
2015-05-27 16:03:47 -04:00
|
|
|
ctx interpolate.Context
|
2013-11-08 19:55:02 -05:00
|
|
|
}
|
2013-11-09 14:47:32 -05:00
|
|
|
|
2013-11-09 20:07:14 -05:00
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
2015-06-12 17:02:09 -04:00
|
|
|
c := new(Config)
|
2013-11-09 20:07:14 -05:00
|
|
|
|
2017-10-10 16:45:47 -04:00
|
|
|
c.FixUploadOwner = true
|
|
|
|
|
2015-05-27 15:55:36 -04:00
|
|
|
var md mapstructure.Metadata
|
2015-06-12 17:02:09 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 15:55:36 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-11-09 20:07:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-27 12:17:45 -05:00
|
|
|
// Defaults
|
|
|
|
if len(c.RunCommand) == 0 {
|
2018-12-13 04:23:46 -05:00
|
|
|
c.RunCommand = []string{"-d", "-i", "-t", "--entrypoint=/bin/sh", "--", "{{.Image}}"}
|
2019-03-28 19:46:07 -04:00
|
|
|
if c.WindowsContainer {
|
|
|
|
c.RunCommand = []string{"-d", "-i", "-t", "--entrypoint=powershell", "--", "{{.Image}}"}
|
|
|
|
}
|
2013-12-27 12:17:45 -05:00
|
|
|
}
|
|
|
|
|
2013-11-09 20:21:24 -05:00
|
|
|
// Default Pull if it wasn't set
|
|
|
|
hasPull := false
|
|
|
|
for _, k := range md.Keys {
|
|
|
|
if k == "Pull" {
|
|
|
|
hasPull = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasPull {
|
|
|
|
c.Pull = true
|
|
|
|
}
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
// Default to the normal Docker type
|
|
|
|
if c.Comm.Type == "" {
|
|
|
|
c.Comm.Type = "docker"
|
2019-03-27 15:09:08 -04:00
|
|
|
if c.WindowsContainer {
|
|
|
|
c.Comm.Type = "dockerWindowsContainer"
|
|
|
|
}
|
2015-06-15 01:09:38 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 15:55:36 -04:00
|
|
|
var errs *packer.MultiError
|
2015-06-15 01:09:38 -04:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
2013-11-09 14:47:32 -05:00
|
|
|
if c.Image == "" {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errImageNotSpecified)
|
2013-11-09 20:07:14 -05:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:38:32 -04:00
|
|
|
if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errArtifactUseConflict)
|
2015-08-18 17:38:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ExportPath == "" && !c.Commit && !c.Discard {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errArtifactNotUsed)
|
2014-09-04 21:03:15 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:24:29 -04:00
|
|
|
if c.ExportPath != "" {
|
|
|
|
if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errExportPathNotFile)
|
2015-05-29 19:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:08:33 -04:00
|
|
|
if c.ContainerDir == "" {
|
2019-05-22 10:43:29 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
c.ContainerDir = "c:/packer-files"
|
|
|
|
} else {
|
|
|
|
c.ContainerDir = "/packer-files"
|
|
|
|
}
|
2017-07-17 12:08:33 -04:00
|
|
|
}
|
|
|
|
|
2016-09-03 10:45:52 -04:00
|
|
|
if c.EcrLogin && c.LoginServer == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("ECR login requires login server to be provided."))
|
|
|
|
}
|
|
|
|
|
2013-11-09 20:07:14 -05:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
2013-11-09 14:47:32 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:02:09 -04:00
|
|
|
return c, nil, nil
|
2013-11-09 14:47:32 -05:00
|
|
|
}
|