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
|
|
|
|
2018-08-19 18:21:32 -04:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
|
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 {
|
2019-07-02 13:46:10 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
SSHInterface string `mapstructure:"ssh_interface"`
|
|
|
|
SSHIPVersion string `mapstructure:"ssh_ip_version"`
|
2015-06-13 18:34:37 -04:00
|
|
|
|
2019-07-17 18:10:52 -04:00
|
|
|
SourceImage string `mapstructure:"source_image"`
|
|
|
|
SourceImageName string `mapstructure:"source_image_name"`
|
|
|
|
SourceImageFilters ImageFilter `mapstructure:"source_image_filter"`
|
|
|
|
Flavor string `mapstructure:"flavor"`
|
|
|
|
AvailabilityZone string `mapstructure:"availability_zone"`
|
|
|
|
RackconnectWait bool `mapstructure:"rackconnect_wait"`
|
|
|
|
FloatingIPNetwork string `mapstructure:"floating_ip_network"`
|
|
|
|
FloatingIP string `mapstructure:"floating_ip"`
|
|
|
|
ReuseIPs bool `mapstructure:"reuse_ips"`
|
|
|
|
SecurityGroups []string `mapstructure:"security_groups"`
|
|
|
|
Networks []string `mapstructure:"networks"`
|
|
|
|
InstanceFloatingIPNet string `mapstructure:"instance_floating_ip_net"`
|
|
|
|
Ports []string `mapstructure:"ports"`
|
|
|
|
UserData string `mapstructure:"user_data"`
|
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
|
|
|
InstanceName string `mapstructure:"instance_name"`
|
|
|
|
InstanceMetadata map[string]string `mapstructure:"instance_metadata"`
|
|
|
|
ForceDelete bool `mapstructure:"force_delete"`
|
2015-06-12 10:05:03 -04:00
|
|
|
|
2015-12-08 07:45:26 -05:00
|
|
|
ConfigDrive bool `mapstructure:"config_drive"`
|
|
|
|
|
2018-06-12 07:08:17 -04:00
|
|
|
// Used for BC, value will be passed to the "floating_ip_network"
|
|
|
|
FloatingIPPool string `mapstructure:"floating_ip_pool"`
|
|
|
|
|
2018-05-24 06:20:22 -04:00
|
|
|
UseBlockStorageVolume bool `mapstructure:"use_blockstorage_volume"`
|
|
|
|
VolumeName string `mapstructure:"volume_name"`
|
|
|
|
VolumeType string `mapstructure:"volume_type"`
|
2018-12-18 17:58:27 -05:00
|
|
|
VolumeSize int `mapstructure:"volume_size"`
|
2018-05-24 06:20:22 -04:00
|
|
|
VolumeAvailabilityZone string `mapstructure:"volume_availability_zone"`
|
|
|
|
|
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"`
|
2018-08-22 07:37:43 -04:00
|
|
|
|
2019-05-02 04:51:40 -04:00
|
|
|
sourceImageOpts images.ListOpts
|
2018-08-22 07:37:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImageFilter struct {
|
|
|
|
Filters ImageFilterOptions `mapstructure:"filters"`
|
|
|
|
MostRecent bool `mapstructure:"most_recent"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageFilterOptions struct {
|
2019-04-30 10:34:40 -04:00
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Owner string `mapstructure:"owner"`
|
|
|
|
Tags []string `mapstructure:"tags"`
|
|
|
|
Visibility string `mapstructure:"visibility"`
|
|
|
|
Properties map[string]string `mapstructure:"properties"`
|
2018-08-22 07:37:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ImageFilterOptions) Empty() bool {
|
2019-04-30 10:34:40 -04:00
|
|
|
return f.Name == "" && f.Owner == "" && len(f.Tags) == 0 && f.Visibility == "" && len(f.Properties) == 0
|
2018-08-22 07:37:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ImageFilterOptions) Build() (*images.ListOpts, error) {
|
|
|
|
opts := images.ListOpts{}
|
|
|
|
// Set defaults for status, member_status, and sort
|
|
|
|
opts.Status = images.ImageStatusActive
|
|
|
|
opts.MemberStatus = images.ImageMemberStatusAccepted
|
|
|
|
opts.Sort = "created_at:desc"
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if f.Name != "" {
|
|
|
|
opts.Name = f.Name
|
|
|
|
}
|
|
|
|
if f.Owner != "" {
|
|
|
|
opts.Owner = f.Owner
|
|
|
|
}
|
|
|
|
if len(f.Tags) > 0 {
|
|
|
|
opts.Tags = f.Tags
|
|
|
|
}
|
|
|
|
if f.Visibility != "" {
|
|
|
|
v, err := getImageVisibility(f.Visibility)
|
|
|
|
if err == nil {
|
|
|
|
opts.Visibility = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &opts, err
|
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.
|
2018-08-28 11:48:01 -04:00
|
|
|
if c.Comm.SSHKeyPairName == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
|
2018-08-23 10:35:07 -04:00
|
|
|
c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" {
|
2017-03-13 02:29:59 -04:00
|
|
|
|
2018-08-28 11:48:01 -04:00
|
|
|
c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
2017-03-13 02:29:59 -04:00
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2018-08-16 17:15:18 -04:00
|
|
|
if c.FloatingIPPool != "" && c.FloatingIPNetwork == "" {
|
2018-06-12 07:08:17 -04:00
|
|
|
c.FloatingIPNetwork = c.FloatingIPPool
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-08-28 11:48:01 -04:00
|
|
|
if c.Comm.SSHKeyPairName != "" {
|
2018-08-23 10:35:07 -04:00
|
|
|
if c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" && c.Comm.SSHPrivateKeyFile == "" {
|
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."))
|
2018-08-23 10:35:07 -04:00
|
|
|
} else if c.Comm.SSHPrivateKeyFile == "" && !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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
if c.SourceImage == "" && c.SourceImageName == "" && c.SourceImageFilters.Filters.Empty() {
|
2018-07-16 13:00:02 -04:00
|
|
|
errs = append(errs, errors.New("Either a source_image, a source_image_name, or source_image_filter must be specified"))
|
2015-08-10 09:35:56 -04:00
|
|
|
} 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"))
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:46:10 -04:00
|
|
|
if c.SSHIPVersion != "" && c.SSHIPVersion != "4" && c.SSHIPVersion != "6" {
|
2016-02-14 15:23:56 -05:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 06:20:22 -04:00
|
|
|
if c.UseBlockStorageVolume {
|
|
|
|
// Use Compute instance availability zone for the Block Storage volume if
|
|
|
|
// it's not provided.
|
|
|
|
if c.VolumeAvailabilityZone == "" {
|
|
|
|
c.VolumeAvailabilityZone = c.AvailabilityZone
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use random name for the Block Storage volume if it's not provided.
|
|
|
|
if c.VolumeName == "" {
|
|
|
|
c.VolumeName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:21:32 -04:00
|
|
|
// if neither ID or image name is provided outside the filter, build the filter
|
|
|
|
if len(c.SourceImage) == 0 && len(c.SourceImageName) == 0 {
|
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
listOpts, filterErr := c.SourceImageFilters.Filters.Build()
|
2018-08-19 18:21:32 -04:00
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
if filterErr != nil {
|
|
|
|
errs = append(errs, filterErr)
|
2018-08-19 18:21:32 -04:00
|
|
|
}
|
2018-08-22 07:37:43 -04:00
|
|
|
c.sourceImageOpts = *listOpts
|
2018-08-19 18:21:32 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
return errs
|
|
|
|
}
|
2018-08-22 07:37:43 -04:00
|
|
|
|
|
|
|
// Retrieve the specific ImageVisibility using the exported const from images
|
|
|
|
func getImageVisibility(visibility string) (*images.ImageVisibility, error) {
|
|
|
|
visibilities := [...]images.ImageVisibility{
|
|
|
|
images.ImageVisibilityPublic,
|
|
|
|
images.ImageVisibilityPrivate,
|
|
|
|
images.ImageVisibilityCommunity,
|
|
|
|
images.ImageVisibilityShared,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range visibilities {
|
|
|
|
if string(v) == visibility {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Not a valid visibility: %s", visibility)
|
|
|
|
}
|