From faf327eed09586ed18d79e15d6df6ae578437950 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 May 2015 12:55:36 -0700 Subject: [PATCH] builder/docker: convert to new interpolation --- builder/docker/builder.go | 2 +- builder/docker/config.go | 57 ++++++++++----------------------- builder/docker/driver_docker.go | 7 ++-- 3 files changed, 23 insertions(+), 43 deletions(-) diff --git a/builder/docker/builder.go b/builder/docker/builder.go index 6ab6fb593..a94177070 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -26,7 +26,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { - driver := &DockerDriver{Tpl: b.config.tpl, Ui: ui} + driver := &DockerDriver{Ctx: b.config.ctx, Ui: ui} if err := driver.Verify(); err != nil { return nil, err } diff --git a/builder/docker/config.go b/builder/docker/config.go index dda726225..dd525497e 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -2,8 +2,12 @@ package docker import ( "fmt" + + "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) type Config struct { @@ -22,23 +26,26 @@ type Config struct { LoginPassword string `mapstructure:"login_password"` LoginServer string `mapstructure:"login_server"` - tpl *packer.ConfigTemplate + ctx *interpolate.Context } func NewConfig(raws ...interface{}) (*Config, []string, error) { c := new(Config) - md, err := common.DecodeConfig(c, raws...) + + var md mapstructure.Metadata + err := config.Decode(&c, &config.DecodeOpts{ + Metadata: &md, + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{ + "run_command", + }, + }, + }, raws...) if err != nil { return nil, nil, err } - c.tpl, err = packer.NewConfigTemplate() - if err != nil { - return nil, nil, err - } - - c.tpl.UserVars = c.PackerUserVars - // Defaults if len(c.RunCommand) == 0 { c.RunCommand = []string{ @@ -61,37 +68,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { c.Pull = true } - errs := common.CheckUnusedConfig(md) - - templates := map[string]*string{ - "export_path": &c.ExportPath, - "image": &c.Image, - "login_email": &c.LoginEmail, - "login_username": &c.LoginUsername, - "login_password": &c.LoginPassword, - "login_server": &c.LoginServer, - } - - for n, ptr := range templates { - var err error - *ptr, err = c.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", n, err)) - } - } - - for k, v := range c.Volumes { - var err error - v, err = c.tpl.Process(v, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing volumes[%s]: %s", k, err)) - } - - c.Volumes[k] = v - } - + var errs *packer.MultiError if c.Image == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("image must be specified")) diff --git a/builder/docker/driver_docker.go b/builder/docker/driver_docker.go index f724e37ec..038aa046e 100644 --- a/builder/docker/driver_docker.go +++ b/builder/docker/driver_docker.go @@ -11,11 +11,12 @@ import ( "sync" "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) type DockerDriver struct { Ui packer.Ui - Tpl *packer.ConfigTemplate + Ctx *interpolate.Context l sync.Mutex } @@ -185,6 +186,8 @@ 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 // Args that we're going to pass to Docker args := []string{"run"} @@ -192,7 +195,7 @@ func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) { args = append(args, "-v", fmt.Sprintf("%s:%s", host, guest)) } for _, v := range config.RunCommand { - v, err := d.Tpl.Process(v, &tplData) + v, err := interpolate.Render(v, &ctx) if err != nil { return "", err }