github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/openstack/run_config.go (about)

     1  package openstack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/common/uuid"
     8  	"github.com/hashicorp/packer/helper/communicator"
     9  	"github.com/hashicorp/packer/template/interpolate"
    10  )
    11  
    12  // RunConfig contains configuration for running an instance from a source
    13  // image and details on how to access that launched image.
    14  type RunConfig struct {
    15  	Comm                 communicator.Config `mapstructure:",squash"`
    16  	SSHKeyPairName       string              `mapstructure:"ssh_keypair_name"`
    17  	TemporaryKeyPairName string              `mapstructure:"temporary_key_pair_name"`
    18  	SSHInterface         string              `mapstructure:"ssh_interface"`
    19  	SSHIPVersion         string              `mapstructure:"ssh_ip_version"`
    20  
    21  	SourceImage      string            `mapstructure:"source_image"`
    22  	SourceImageName  string            `mapstructure:"source_image_name"`
    23  	Flavor           string            `mapstructure:"flavor"`
    24  	AvailabilityZone string            `mapstructure:"availability_zone"`
    25  	RackconnectWait  bool              `mapstructure:"rackconnect_wait"`
    26  	FloatingIpPool   string            `mapstructure:"floating_ip_pool"`
    27  	FloatingIp       string            `mapstructure:"floating_ip"`
    28  	ReuseIps         bool              `mapstructure:"reuse_ips"`
    29  	SecurityGroups   []string          `mapstructure:"security_groups"`
    30  	Networks         []string          `mapstructure:"networks"`
    31  	UserData         string            `mapstructure:"user_data"`
    32  	UserDataFile     string            `mapstructure:"user_data_file"`
    33  	InstanceMetadata map[string]string `mapstructure:"instance_metadata"`
    34  
    35  	ConfigDrive bool `mapstructure:"config_drive"`
    36  
    37  	// Not really used, but here for BC
    38  	OpenstackProvider string `mapstructure:"openstack_provider"`
    39  	UseFloatingIp     bool   `mapstructure:"use_floating_ip"`
    40  }
    41  
    42  func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
    43  	// If we are not given an explicit ssh_keypair_name or
    44  	// ssh_private_key_file, then create a temporary one, but only if the
    45  	// temporary_key_pair_name has not been provided and we are not using
    46  	// ssh_password.
    47  	if c.SSHKeyPairName == "" && c.TemporaryKeyPairName == "" &&
    48  		c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" {
    49  
    50  		c.TemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
    51  	}
    52  
    53  	if c.UseFloatingIp && c.FloatingIpPool == "" {
    54  		c.FloatingIpPool = "public"
    55  	}
    56  
    57  	// Validation
    58  	errs := c.Comm.Prepare(ctx)
    59  
    60  	if c.SSHKeyPairName != "" {
    61  		if c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" && c.Comm.SSHPrivateKey == "" {
    62  			errs = append(errs, errors.New("A ssh_private_key_file must be provided to retrieve the winrm password when using ssh_keypair_name."))
    63  		} else if c.Comm.SSHPrivateKey == "" && !c.Comm.SSHAgentAuth {
    64  			errs = append(errs, errors.New("A ssh_private_key_file must be provided or ssh_agent_auth enabled when ssh_keypair_name is specified."))
    65  		}
    66  	}
    67  
    68  	if c.SourceImage == "" && c.SourceImageName == "" {
    69  		errs = append(errs, errors.New("Either a source_image or a source_image_name must be specified"))
    70  	} else if len(c.SourceImage) > 0 && len(c.SourceImageName) > 0 {
    71  		errs = append(errs, errors.New("Only a source_image or a source_image_name can be specified, not both."))
    72  	}
    73  
    74  	if c.Flavor == "" {
    75  		errs = append(errs, errors.New("A flavor must be specified"))
    76  	}
    77  
    78  	if c.SSHIPVersion != "" && c.SSHIPVersion != "4" && c.SSHIPVersion != "6" {
    79  		errs = append(errs, errors.New("SSH IP version must be either 4 or 6"))
    80  	}
    81  
    82  	for key, value := range c.InstanceMetadata {
    83  		if len(key) > 255 {
    84  			errs = append(errs, fmt.Errorf("Instance metadata key too long (max 255 bytes): %s", key))
    85  		}
    86  		if len(value) > 255 {
    87  			errs = append(errs, fmt.Errorf("Instance metadata value too long (max 255 bytes): %s", value))
    88  		}
    89  	}
    90  
    91  	return errs
    92  }