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