96 lines
1.9 KiB
Go
Raw Normal View History

2013-11-08 16:55:02 -08:00
package docker
import (
2013-11-09 11:47:32 -08:00
"fmt"
"os"
"github.com/mitchellh/mapstructure"
2013-11-08 16:55:02 -08:00
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/config"
2013-11-08 16:55:02 -08:00
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
2013-11-08 16:55:02 -08:00
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Commit bool
2013-11-09 09:48:36 -08:00
ExportPath string `mapstructure:"export_path"`
Image string
Pull bool
RunCommand []string `mapstructure:"run_command"`
2014-09-05 15:48:42 -07:00
Volumes map[string]string
2013-11-08 22:00:57 -08:00
2014-09-05 15:24:12 -07:00
Login bool
LoginEmail string `mapstructure:"login_email"`
LoginUsername string `mapstructure:"login_username"`
LoginPassword string `mapstructure:"login_password"`
LoginServer string `mapstructure:"login_server"`
ctx interpolate.Context
2013-11-08 16:55:02 -08:00
}
2013-11-09 11:47:32 -08:00
2013-11-09 17:07:14 -08:00
func NewConfig(raws ...interface{}) (*Config, []string, error) {
2015-05-29 09:19:20 -07:00
var c Config
2013-11-09 17:07:14 -08:00
var md mapstructure.Metadata
err := config.Decode(&c, &config.DecodeOpts{
Metadata: &md,
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"run_command",
},
},
}, raws...)
2013-11-09 17:07:14 -08:00
if err != nil {
return nil, nil, err
}
// Defaults
if len(c.RunCommand) == 0 {
c.RunCommand = []string{
"-d", "-i", "-t",
"{{.Image}}",
"/bin/bash",
}
}
// 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
}
var errs *packer.MultiError
2013-11-09 11:47:32 -08:00
if c.Image == "" {
2013-11-09 17:07:14 -08:00
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("image must be specified"))
}
if c.ExportPath != "" && c.Commit {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("both commit and export_path cannot be set"))
}
if c.ExportPath != "" {
if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
errs = packer.MultiErrorAppend(errs, fmt.Errorf(
"export_path must be a file, not a directory"))
}
}
2013-11-09 17:07:14 -08:00
if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs
2013-11-09 11:47:32 -08:00
}
2015-05-29 09:19:20 -07:00
return &c, nil, nil
2013-11-09 11:47:32 -08:00
}