github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/lxc/config.go (about)

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