builder/docker: convert to new interpolation
This commit is contained in:
parent
7d0f94834e
commit
faf327eed0
|
@ -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) {
|
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 {
|
if err := driver.Verify(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,12 @@ package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
|
"github.com/mitchellh/packer/helper/config"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -22,23 +26,26 @@ type Config struct {
|
||||||
LoginPassword string `mapstructure:"login_password"`
|
LoginPassword string `mapstructure:"login_password"`
|
||||||
LoginServer string `mapstructure:"login_server"`
|
LoginServer string `mapstructure:"login_server"`
|
||||||
|
|
||||||
tpl *packer.ConfigTemplate
|
ctx *interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
||||||
c := new(Config)
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.tpl, err = packer.NewConfigTemplate()
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.tpl.UserVars = c.PackerUserVars
|
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
if len(c.RunCommand) == 0 {
|
if len(c.RunCommand) == 0 {
|
||||||
c.RunCommand = []string{
|
c.RunCommand = []string{
|
||||||
|
@ -61,37 +68,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
||||||
c.Pull = true
|
c.Pull = true
|
||||||
}
|
}
|
||||||
|
|
||||||
errs := common.CheckUnusedConfig(md)
|
var errs *packer.MultiError
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Image == "" {
|
if c.Image == "" {
|
||||||
errs = packer.MultiErrorAppend(errs,
|
errs = packer.MultiErrorAppend(errs,
|
||||||
fmt.Errorf("image must be specified"))
|
fmt.Errorf("image must be specified"))
|
||||||
|
|
|
@ -11,11 +11,12 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DockerDriver struct {
|
type DockerDriver struct {
|
||||||
Ui packer.Ui
|
Ui packer.Ui
|
||||||
Tpl *packer.ConfigTemplate
|
Ctx *interpolate.Context
|
||||||
|
|
||||||
l sync.Mutex
|
l sync.Mutex
|
||||||
}
|
}
|
||||||
|
@ -185,6 +186,8 @@ func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) {
|
||||||
// Build up the template data
|
// Build up the template data
|
||||||
var tplData startContainerTemplate
|
var tplData startContainerTemplate
|
||||||
tplData.Image = config.Image
|
tplData.Image = config.Image
|
||||||
|
ctx := *d.Ctx
|
||||||
|
ctx.Data = &tplData
|
||||||
|
|
||||||
// Args that we're going to pass to Docker
|
// Args that we're going to pass to Docker
|
||||||
args := []string{"run"}
|
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))
|
args = append(args, "-v", fmt.Sprintf("%s:%s", host, guest))
|
||||||
}
|
}
|
||||||
for _, v := range config.RunCommand {
|
for _, v := range config.RunCommand {
|
||||||
v, err := d.Tpl.Process(v, &tplData)
|
v, err := interpolate.Render(v, &ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue