github.phpd.cn/hashicorp/packer@v1.3.2/builder/lxd/config.go (about)

     1  package lxd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/common"
     7  	"github.com/hashicorp/packer/helper/config"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/hashicorp/packer/template/interpolate"
    10  	"github.com/mitchellh/mapstructure"
    11  )
    12  
    13  type Config struct {
    14  	common.PackerConfig `mapstructure:",squash"`
    15  	OutputImage         string            `mapstructure:"output_image"`
    16  	ContainerName       string            `mapstructure:"container_name"`
    17  	CommandWrapper      string            `mapstructure:"command_wrapper"`
    18  	Image               string            `mapstructure:"image"`
    19  	Profile             string            `mapstructure:"profile"`
    20  	InitSleep           string            `mapstructure:"init_sleep"`
    21  	PublishProperties   map[string]string `mapstructure:"publish_properties"`
    22  	LaunchConfig        map[string]string `mapstructure:"launch_config"`
    23  
    24  	ctx interpolate.Context
    25  }
    26  
    27  func NewConfig(raws ...interface{}) (*Config, error) {
    28  	var c Config
    29  
    30  	var md mapstructure.Metadata
    31  	err := config.Decode(&c, &config.DecodeOpts{
    32  		Metadata:    &md,
    33  		Interpolate: true,
    34  	}, raws...)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	// Accumulate any errors
    40  	var errs *packer.MultiError
    41  
    42  	if c.ContainerName == "" {
    43  		c.ContainerName = fmt.Sprintf("packer-%s", c.PackerBuildName)
    44  	}
    45  
    46  	if c.OutputImage == "" {
    47  		c.OutputImage = c.ContainerName
    48  	}
    49  
    50  	if c.CommandWrapper == "" {
    51  		c.CommandWrapper = "{{.Command}}"
    52  	}
    53  
    54  	if c.Image == "" {
    55  		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`"))
    56  	}
    57  
    58  	if c.Profile == "" {
    59  		c.Profile = "default"
    60  	}
    61  
    62  	// Sadly we have to wait a few seconds for /tmp to be intialized and networking
    63  	// to finish starting. There isn't a great cross platform to check when things are ready.
    64  	if c.InitSleep == "" {
    65  		c.InitSleep = "3"
    66  	}
    67  
    68  	if errs != nil && len(errs.Errors) > 0 {
    69  		return nil, errs
    70  	}
    71  
    72  	return &c, nil
    73  }