2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RunConfig contains configuration for running an instance from a source
|
|
|
|
// image and details on how to access that launched image.
|
|
|
|
type RunConfig struct {
|
2014-01-17 18:46:35 -05:00
|
|
|
SourceImage string `mapstructure:"source_image"`
|
|
|
|
Flavor string `mapstructure:"flavor"`
|
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPort int `mapstructure:"ssh_port"`
|
|
|
|
OpenstackProvider string `mapstructure:"openstack_provider"`
|
|
|
|
UseFloatingIp bool `mapstructure:"use_floating_ip"`
|
|
|
|
FloatingIpPool string `mapstructure:"floating_ip_pool"`
|
|
|
|
FloatingIp string `mapstructure:"floating_ip"`
|
|
|
|
SecurityGroups []string `mapstructure:"security_groups"`
|
2014-06-15 14:56:19 -04:00
|
|
|
Networks []string `mapstructure:"networks"`
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
// Unexported fields that are calculated from others
|
|
|
|
sshTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
|
|
|
|
if t == nil {
|
|
|
|
var err error
|
|
|
|
t, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if c.SSHUsername == "" {
|
|
|
|
c.SSHUsername = "root"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawSSHTimeout == "" {
|
2013-09-01 17:36:59 -04:00
|
|
|
c.RawSSHTimeout = "5m"
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 03:34:24 -05:00
|
|
|
if c.UseFloatingIp && c.FloatingIpPool == "" {
|
|
|
|
c.FloatingIpPool = "public"
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
// Validation
|
|
|
|
var err error
|
|
|
|
errs := make([]error, 0)
|
|
|
|
if c.SourceImage == "" {
|
|
|
|
errs = append(errs, errors.New("A source_image must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Flavor == "" {
|
|
|
|
errs = append(errs, errors.New("A flavor must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHUsername == "" {
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
templates := map[string]*string{
|
2014-06-07 16:04:29 -04:00
|
|
|
"flavor": &c.Flavor,
|
2013-08-27 00:57:23 -04:00
|
|
|
"ssh_timeout": &c.RawSSHTimeout,
|
|
|
|
"ssh_username": &c.SSHUsername,
|
|
|
|
"source_image": &c.SourceImage,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = t.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
2014-01-17 18:46:35 -05:00
|
|
|
errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunConfig) SSHTimeout() time.Duration {
|
|
|
|
return c.sshTimeout
|
|
|
|
}
|