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

     1  package lxc
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/hashicorp/packer/common"
     9  	"github.com/hashicorp/packer/helper/config"
    10  	"github.com/hashicorp/packer/packer"
    11  	"github.com/hashicorp/packer/template/interpolate"
    12  	"github.com/mitchellh/mapstructure"
    13  )
    14  
    15  type Config struct {
    16  	common.PackerConfig `mapstructure:",squash"`
    17  	ConfigFile          string   `mapstructure:"config_file"`
    18  	OutputDir           string   `mapstructure:"output_directory"`
    19  	ContainerName       string   `mapstructure:"container_name"`
    20  	CommandWrapper      string   `mapstructure:"command_wrapper"`
    21  	RawInitTimeout      string   `mapstructure:"init_timeout"`
    22  	CreateOptions       []string `mapstructure:"create_options"`
    23  	StartOptions        []string `mapstructure:"start_options"`
    24  	AttachOptions       []string `mapstructure:"attach_options"`
    25  	Name                string   `mapstructure:"template_name"`
    26  	Parameters          []string `mapstructure:"template_parameters"`
    27  	EnvVars             []string `mapstructure:"template_environment_vars"`
    28  	TargetRunlevel      int      `mapstructure:"target_runlevel"`
    29  	InitTimeout         time.Duration
    30  
    31  	ctx interpolate.Context
    32  }
    33  
    34  func NewConfig(raws ...interface{}) (*Config, error) {
    35  	var c Config
    36  
    37  	var md mapstructure.Metadata
    38  	err := config.Decode(&c, &config.DecodeOpts{
    39  		Metadata:    &md,
    40  		Interpolate: true,
    41  	}, raws...)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// Accumulate any errors
    47  	var errs *packer.MultiError
    48  
    49  	if c.OutputDir == "" {
    50  		c.OutputDir = fmt.Sprintf("output-%s", c.PackerBuildName)
    51  	}
    52  
    53  	if c.ContainerName == "" {
    54  		c.ContainerName = fmt.Sprintf("packer-%s", c.PackerBuildName)
    55  	}
    56  
    57  	if c.TargetRunlevel == 0 {
    58  		c.TargetRunlevel = 3
    59  	}
    60  
    61  	if c.CommandWrapper == "" {
    62  		c.CommandWrapper = "{{.Command}}"
    63  	}
    64  
    65  	if c.RawInitTimeout == "" {
    66  		c.RawInitTimeout = "20s"
    67  	}
    68  
    69  	c.InitTimeout, err = time.ParseDuration(c.RawInitTimeout)
    70  	if err != nil {
    71  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed parsing init_timeout: %s", err))
    72  	}
    73  
    74  	if _, err := os.Stat(c.ConfigFile); os.IsNotExist(err) {
    75  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("LXC Config file appears to be missing: %s", c.ConfigFile))
    76  	}
    77  
    78  	if errs != nil && len(errs.Errors) > 0 {
    79  		return nil, errs
    80  	}
    81  
    82  	return &c, nil
    83  }