github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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  	OpenstackProvider string   `mapstructure:"openstack_provider"`
    19  	UseFloatingIp     bool     `mapstructure:"use_floating_ip"`
    20  	FloatingIpPool    string   `mapstructure:"floating_ip_pool"`
    21  	FloatingIp        string   `mapstructure:"floating_ip"`
    22  	SecurityGroups    []string `mapstructure:"security_groups"`
    23  	Networks          []string `mapstructure:"networks"`
    24  
    25  	// Unexported fields that are calculated from others
    26  	sshTimeout time.Duration
    27  }
    28  
    29  func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
    30  	if t == nil {
    31  		var err error
    32  		t, err = packer.NewConfigTemplate()
    33  		if err != nil {
    34  			return []error{err}
    35  		}
    36  	}
    37  
    38  	// Defaults
    39  	if c.SSHUsername == "" {
    40  		c.SSHUsername = "root"
    41  	}
    42  
    43  	if c.SSHPort == 0 {
    44  		c.SSHPort = 22
    45  	}
    46  
    47  	if c.RawSSHTimeout == "" {
    48  		c.RawSSHTimeout = "5m"
    49  	}
    50  
    51  	if c.UseFloatingIp && c.FloatingIpPool == "" {
    52  		c.FloatingIpPool = "public"
    53  	}
    54  
    55  	// Validation
    56  	var err error
    57  	errs := make([]error, 0)
    58  	if c.SourceImage == "" {
    59  		errs = append(errs, errors.New("A source_image must be specified"))
    60  	}
    61  
    62  	if c.Flavor == "" {
    63  		errs = append(errs, errors.New("A flavor must be specified"))
    64  	}
    65  
    66  	if c.SSHUsername == "" {
    67  		errs = append(errs, errors.New("An ssh_username must be specified"))
    68  	}
    69  
    70  	templates := map[string]*string{
    71  		"flavor":       &c.Flavor,
    72  		"ssh_timeout":  &c.RawSSHTimeout,
    73  		"ssh_username": &c.SSHUsername,
    74  		"source_image": &c.SourceImage,
    75  	}
    76  
    77  	for n, ptr := range templates {
    78  		var err error
    79  		*ptr, err = t.Process(*ptr, nil)
    80  		if err != nil {
    81  			errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
    82  		}
    83  	}
    84  
    85  	c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
    86  	if err != nil {
    87  		errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
    88  	}
    89  
    90  	return errs
    91  }
    92  
    93  func (c *RunConfig) SSHTimeout() time.Duration {
    94  	return c.sshTimeout
    95  }