packer-cn/builder/lxd/config.go

74 lines
1.9 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"fmt"
2018-01-22 20:21:10 -05:00
2017-09-05 17:09:15 -04:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2016-05-26 23:21:55 -04:00
"github.com/mitchellh/mapstructure"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
OutputImage string `mapstructure:"output_image"`
ContainerName string `mapstructure:"container_name"`
CommandWrapper string `mapstructure:"command_wrapper"`
Image string `mapstructure:"image"`
Profile string `mapstructure:"profile"`
InitSleep string `mapstructure:"init_sleep"`
PublishProperties map[string]string `mapstructure:"publish_properties"`
LaunchConfig map[string]string `mapstructure:"launch_config"`
2016-05-26 23:21:55 -04:00
ctx interpolate.Context
}
func NewConfig(raws ...interface{}) (*Config, error) {
var c Config
var md mapstructure.Metadata
err := config.Decode(&c, &config.DecodeOpts{
Metadata: &md,
Interpolate: true,
}, raws...)
if err != nil {
return nil, err
}
// Accumulate any errors
var errs *packer.MultiError
if c.ContainerName == "" {
c.ContainerName = fmt.Sprintf("packer-%s", c.PackerBuildName)
}
2016-05-30 19:13:59 -04:00
if c.OutputImage == "" {
c.OutputImage = c.ContainerName
}
2016-05-26 23:21:55 -04:00
if c.CommandWrapper == "" {
c.CommandWrapper = "{{.Command}}"
}
if c.Image == "" {
2016-05-31 19:53:50 -04:00
errs = packer.MultiErrorAppend(errs, fmt.Errorf("`image` is a required parameter for LXD. Please specify an image by alias or fingerprint. e.g. `ubuntu-daily:x`"))
2016-05-26 23:21:55 -04:00
}
if c.Profile == "" {
c.Profile = "default"
}
// Sadly we have to wait a few seconds for /tmp to be intialized and networking
// to finish starting. There isn't a great cross platform to check when things are ready.
if c.InitSleep == "" {
c.InitSleep = "3"
}
2016-05-26 23:21:55 -04:00
if errs != nil && len(errs.Errors) > 0 {
return nil, errs
}
return &c, nil
}