github.com/sneal/packer@v0.5.2/builder/openstack/run_config.go (about)

     1  package openstack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/mitchellh/packer/packer"
     7  	"time"
     8  )
     9  
    10  // RunConfig contains configuration for running an instance from a source
    11  // image and details on how to access that launched image.
    12  type RunConfig struct {
    13  	SourceImage   string `mapstructure:"source_image"`
    14  	Flavor        string `mapstructure:"flavor"`
    15  	RawSSHTimeout string `mapstructure:"ssh_timeout"`
    16  	SSHUsername   string `mapstructure:"ssh_username"`
    17  	SSHPort       int    `mapstructure:"ssh_port"`
    18  
    19  	// Unexported fields that are calculated from others
    20  	sshTimeout time.Duration
    21  }
    22  
    23  func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
    24  	if t == nil {
    25  		var err error
    26  		t, err = packer.NewConfigTemplate()
    27  		if err != nil {
    28  			return []error{err}
    29  		}
    30  	}
    31  
    32  	// Defaults
    33  	if c.SSHUsername == "" {
    34  		c.SSHUsername = "root"
    35  	}
    36  
    37  	if c.SSHPort == 0 {
    38  		c.SSHPort = 22
    39  	}
    40  
    41  	if c.RawSSHTimeout == "" {
    42  		c.RawSSHTimeout = "5m"
    43  	}
    44  
    45  	// Validation
    46  	var err error
    47  	errs := make([]error, 0)
    48  	if c.SourceImage == "" {
    49  		errs = append(errs, errors.New("A source_image must be specified"))
    50  	}
    51  
    52  	if c.Flavor == "" {
    53  		errs = append(errs, errors.New("A flavor must be specified"))
    54  	}
    55  
    56  	if c.SSHUsername == "" {
    57  		errs = append(errs, errors.New("An ssh_username must be specified"))
    58  	}
    59  
    60  	templates := map[string]*string{
    61  		"flavlor":      &c.Flavor,
    62  		"ssh_timeout":  &c.RawSSHTimeout,
    63  		"ssh_username": &c.SSHUsername,
    64  		"source_image": &c.SourceImage,
    65  	}
    66  
    67  	for n, ptr := range templates {
    68  		var err error
    69  		*ptr, err = t.Process(*ptr, nil)
    70  		if err != nil {
    71  			errs = append(
    72  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    73  		}
    74  	}
    75  
    76  	c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
    77  	if err != nil {
    78  		errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
    79  	}
    80  
    81  	return errs
    82  }
    83  
    84  func (c *RunConfig) SSHTimeout() time.Duration {
    85  	return c.sshTimeout
    86  }