2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-01-04 21:15:16 -05:00
|
|
|
"fmt"
|
2015-05-27 16:02:57 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunConfig contains configuration for running an instance from a source
|
|
|
|
// image and details on how to access that launched image.
|
|
|
|
type RunConfig struct {
|
2017-03-13 02:29:59 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
SSHKeyPairName string `mapstructure:"ssh_keypair_name"`
|
|
|
|
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
|
|
|
|
SSHInterface string `mapstructure:"ssh_interface"`
|
|
|
|
SSHIPVersion string `mapstructure:"ssh_ip_version"`
|
2015-06-13 18:34:37 -04:00
|
|
|
|
2017-01-04 21:15:16 -05:00
|
|
|
SourceImage string `mapstructure:"source_image"`
|
|
|
|
SourceImageName string `mapstructure:"source_image_name"`
|
|
|
|
Flavor string `mapstructure:"flavor"`
|
|
|
|
AvailabilityZone string `mapstructure:"availability_zone"`
|
|
|
|
RackconnectWait bool `mapstructure:"rackconnect_wait"`
|
|
|
|
FloatingIpPool string `mapstructure:"floating_ip_pool"`
|
|
|
|
FloatingIp string `mapstructure:"floating_ip"`
|
2017-02-16 07:35:27 -05:00
|
|
|
ReuseIps bool `mapstructure:"reuse_ips"`
|
2017-01-04 21:15:16 -05:00
|
|
|
SecurityGroups []string `mapstructure:"security_groups"`
|
|
|
|
Networks []string `mapstructure:"networks"`
|
2018-08-03 16:46:36 -04:00
|
|
|
Ports []string `mapstructure:"ports"`
|
2017-01-04 21:15:16 -05:00
|
|
|
UserData string `mapstructure:"user_data"`
|
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
2018-03-20 10:01:58 -04:00
|
|
|
InstanceName string `mapstructure:"instance_name"`
|
2017-01-04 21:15:16 -05:00
|
|
|
InstanceMetadata map[string]string `mapstructure:"instance_metadata"`
|
2015-06-12 10:05:03 -04:00
|
|
|
|
2015-12-08 07:45:26 -05:00
|
|
|
ConfigDrive bool `mapstructure:"config_drive"`
|
|
|
|
|
2015-06-12 10:05:03 -04:00
|
|
|
// Not really used, but here for BC
|
|
|
|
OpenstackProvider string `mapstructure:"openstack_provider"`
|
|
|
|
UseFloatingIp bool `mapstructure:"use_floating_ip"`
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 16:02:57 -04:00
|
|
|
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
2017-03-13 02:29:59 -04:00
|
|
|
// If we are not given an explicit ssh_keypair_name or
|
|
|
|
// ssh_private_key_file, then create a temporary one, but only if the
|
|
|
|
// temporary_key_pair_name has not been provided and we are not using
|
|
|
|
// ssh_password.
|
|
|
|
if c.SSHKeyPairName == "" && c.TemporaryKeyPairName == "" &&
|
|
|
|
c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" {
|
|
|
|
|
|
|
|
c.TemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
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
|
2015-06-13 18:34:37 -04:00
|
|
|
errs := c.Comm.Prepare(ctx)
|
2017-03-14 15:47:40 -04:00
|
|
|
|
|
|
|
if c.SSHKeyPairName != "" {
|
|
|
|
if c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" && c.Comm.SSHPrivateKey == "" {
|
2017-05-18 05:40:16 -04:00
|
|
|
errs = append(errs, errors.New("A ssh_private_key_file must be provided to retrieve the winrm password when using ssh_keypair_name."))
|
2017-03-14 15:47:40 -04:00
|
|
|
} else if c.Comm.SSHPrivateKey == "" && !c.Comm.SSHAgentAuth {
|
2017-05-18 05:40:16 -04:00
|
|
|
errs = append(errs, errors.New("A ssh_private_key_file must be provided or ssh_agent_auth enabled when ssh_keypair_name is specified."))
|
2017-03-14 15:47:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 09:35:56 -04:00
|
|
|
if c.SourceImage == "" && c.SourceImageName == "" {
|
|
|
|
errs = append(errs, errors.New("Either a source_image or a source_image_name must be specified"))
|
|
|
|
} else if len(c.SourceImage) > 0 && len(c.SourceImageName) > 0 {
|
|
|
|
errs = append(errs, errors.New("Only a source_image or a source_image_name can be specified, not both."))
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Flavor == "" {
|
|
|
|
errs = append(errs, errors.New("A flavor must be specified"))
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:23:56 -05:00
|
|
|
if c.SSHIPVersion != "" && c.SSHIPVersion != "4" && c.SSHIPVersion != "6" {
|
|
|
|
errs = append(errs, errors.New("SSH IP version must be either 4 or 6"))
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:15:16 -05:00
|
|
|
for key, value := range c.InstanceMetadata {
|
|
|
|
if len(key) > 255 {
|
|
|
|
errs = append(errs, fmt.Errorf("Instance metadata key too long (max 255 bytes): %s", key))
|
|
|
|
}
|
|
|
|
if len(value) > 255 {
|
|
|
|
errs = append(errs, fmt.Errorf("Instance metadata value too long (max 255 bytes): %s", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
return errs
|
|
|
|
}
|