2019-05-31 08:27:41 -04:00
//go:generate struct-markdown
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/helper/communicator"
2020-11-11 13:21:37 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
2020-11-12 17:44:02 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
2013-08-27 00:57:23 -04:00
)
2019-06-20 09:24:10 -04:00
// RunConfig contains configuration for running an instance from a source image
// and details on how to access that launched image.
2013-08-27 00:57:23 -04:00
type RunConfig struct {
2018-08-29 08:28:09 -04:00
Comm communicator . Config ` mapstructure:",squash" `
2019-08-21 06:28:34 -04:00
// The type of interface to connect via SSH. Values useful for Rackspace
// are "public" or "private", and the default behavior is to connect via
// whichever is returned first from the OpenStack API.
SSHInterface string ` mapstructure:"ssh_interface" required:"false" `
// The IP version to use for SSH connections, valid values are `4` and `6`.
// Useful on dual stacked instances where the default behavior is to
// connect via whichever IP address is returned first from the OpenStack
// API.
SSHIPVersion string ` mapstructure:"ssh_ip_version" required:"false" `
2019-06-20 09:24:10 -04:00
// The ID or full URL to the base image to use. This is the image that will
// be used to launch a new server and provision it. Unless you specify
// completely custom SSH settings, the source image must have cloud-init
// installed so that the keypair gets assigned properly.
2019-06-06 10:29:25 -04:00
SourceImage string ` mapstructure:"source_image" required:"true" `
2019-06-20 09:24:10 -04:00
// The name of the base image to use. This is an alternative way of
// providing source_image and only either of them can be specified.
2019-06-06 10:29:25 -04:00
SourceImageName string ` mapstructure:"source_image_name" required:"true" `
2020-09-25 08:59:00 -04:00
// The URL of an external base image to use. This is an alternative way of
// providing source_image and only either of them can be specified.
ExternalSourceImageURL string ` mapstructure:"external_source_image_url" required:"true" `
// The format of the external source image to use, e.g. qcow2, raw.
ExternalSourceImageFormat string ` mapstructure:"external_source_image_format" required:"false" `
2019-06-20 09:24:10 -04:00
// Filters used to populate filter options. Example:
//
2020-03-12 10:05:08 -04:00
// ```json
//{
2019-06-20 09:24:10 -04:00
// "source_image_filter": {
// "filters": {
// "name": "ubuntu-16.04",
// "visibility": "protected",
// "owner": "d1a588cf4b0743344508dc145649372d1",
// "tags": ["prod", "ready"],
// "properties": {
// "os_distro": "ubuntu"
// }
// },
// "most_recent": true
// }
2019-08-27 03:50:43 -04:00
// }
// ```
2019-06-20 09:24:10 -04:00
//
// This selects the most recent production Ubuntu 16.04 shared to you by
// the given owner. NOTE: This will fail unless *exactly* one image is
// returned, or `most_recent` is set to true. In the example of multiple
// returned images, `most_recent` will cause this to succeed by selecting
// the newest image of the returned images.
//
// - `filters` (map of strings) - filters used to select a
// `source_image`.
// NOTE: This will fail unless *exactly* one image is returned, or
// `most_recent` is set to true. Of the filters described in
// [ImageService](https://developer.openstack.org/api-ref/image/v2/), the
// following are valid:
//
// - name (string)
// - owner (string)
// - tags (array of strings)
// - visibility (string)
// - properties (map of strings to strings) (fields that can be set
// with `openstack image set --property key=value`)
//
// - `most_recent` (boolean) - Selects the newest created image when
// true.
// This is most useful for selecting a daily distro build.
//
// You may set use this in place of `source_image` If `source_image_filter`
// is provided alongside `source_image`, the `source_image` will override
// the filter. The filter will not be used in this case.
2019-06-06 10:29:25 -04:00
SourceImageFilters ImageFilter ` mapstructure:"source_image_filter" required:"true" `
2019-06-20 09:24:10 -04:00
// The ID, name, or full URL for the desired flavor for the server to be
// created.
2019-06-06 10:29:25 -04:00
Flavor string ` mapstructure:"flavor" required:"true" `
2019-06-20 09:24:10 -04:00
// The availability zone to launch the server in. If this isn't specified,
// the default enforced by your OpenStack cluster will be used. This may be
// required for some OpenStack clusters.
2019-06-06 10:29:25 -04:00
AvailabilityZone string ` mapstructure:"availability_zone" required:"false" `
2019-06-20 09:24:10 -04:00
// For rackspace, whether or not to wait for Rackconnect to assign the
// machine an IP address before connecting via SSH. Defaults to false.
2019-06-06 10:29:25 -04:00
RackconnectWait bool ` mapstructure:"rackconnect_wait" required:"false" `
2019-06-20 09:24:10 -04:00
// The ID or name of an external network that can be used for creation of a
// new floating IP.
2019-06-06 10:29:25 -04:00
FloatingIPNetwork string ` mapstructure:"floating_ip_network" required:"false" `
2019-08-21 06:28:34 -04:00
// The ID of the network to which the instance is attached and which should
// be used to associate with the floating IP. This provides control over
// the floating ip association on multi-homed instances. The association
// otherwise depends on a first-returned-interface policy which could fail
// if the network to which it is connected is unreachable from the floating
// IP network.
InstanceFloatingIPNet string ` mapstructure:"instance_floating_ip_net" required:"false" `
2019-05-28 11:50:58 -04:00
// A specific floating IP to assign to this instance.
2019-06-06 10:29:25 -04:00
FloatingIP string ` mapstructure:"floating_ip" required:"false" `
2019-06-20 09:24:10 -04:00
// Whether or not to attempt to reuse existing unassigned floating ips in
// the project before allocating a new one. Note that it is not possible to
// safely do this concurrently, so if you are running multiple openstack
// builds concurrently, or if other processes are assigning and using
// floating IPs in the same openstack project while packer is running, you
// should not set this to true. Defaults to false.
2019-06-06 10:29:25 -04:00
ReuseIPs bool ` mapstructure:"reuse_ips" required:"false" `
2019-06-20 09:24:10 -04:00
// A list of security groups by name to add to this instance.
2019-06-06 10:29:25 -04:00
SecurityGroups [ ] string ` mapstructure:"security_groups" required:"false" `
2019-06-20 09:24:10 -04:00
// A list of networks by UUID to attach to this instance.
2019-06-06 10:29:25 -04:00
Networks [ ] string ` mapstructure:"networks" required:"false" `
2019-06-20 09:24:10 -04:00
// A list of ports by UUID to attach to this instance.
2019-06-06 10:29:25 -04:00
Ports [ ] string ` mapstructure:"ports" required:"false" `
2019-10-26 17:40:11 -04:00
// A list of network CIDRs to discover the network to attach to this instance.
// The first network whose subnet is contained within any of the given CIDRs
// is used. Ignored if either of the above two options are provided.
NetworkDiscoveryCIDRs [ ] string ` mapstructure:"network_discovery_cidrs" required:"false" `
2019-06-20 09:24:10 -04:00
// User data to apply when launching the instance. Note that you need to be
// careful about escaping characters due to the templates being JSON. It is
// often more convenient to use user_data_file, instead. Packer will not
// automatically wait for a user script to finish before shutting down the
// instance this must be handled in a provisioner.
2019-06-06 10:29:25 -04:00
UserData string ` mapstructure:"user_data" required:"false" `
2019-06-20 09:24:10 -04:00
// Path to a file that will be used for the user data when launching the
// instance.
2019-06-06 10:29:25 -04:00
UserDataFile string ` mapstructure:"user_data_file" required:"false" `
2019-06-20 09:24:10 -04:00
// Name that is applied to the server instance created by Packer. If this
// isn't specified, the default is same as image_name.
2019-06-06 10:29:25 -04:00
InstanceName string ` mapstructure:"instance_name" required:"false" `
2019-06-20 09:24:10 -04:00
// Metadata that is applied to the server instance created by Packer. Also
// called server properties in some documentation. The strings have a max
// size of 255 bytes each.
2019-06-06 10:29:25 -04:00
InstanceMetadata map [ string ] string ` mapstructure:"instance_metadata" required:"false" `
2019-06-20 09:24:10 -04:00
// Whether to force the OpenStack instance to be forcefully deleted. This
// is useful for environments that have reclaim / soft deletion enabled. By
// default this is false.
2019-06-06 10:29:25 -04:00
ForceDelete bool ` mapstructure:"force_delete" required:"false" `
2019-06-20 09:24:10 -04:00
// Whether or not nova should use ConfigDrive for cloud-init metadata.
2019-05-28 11:50:58 -04:00
ConfigDrive bool ` mapstructure:"config_drive" required:"false" `
2019-06-20 09:24:10 -04:00
// Deprecated use floating_ip_network instead.
2019-05-28 11:50:58 -04:00
FloatingIPPool string ` mapstructure:"floating_ip_pool" required:"false" `
2019-06-20 09:24:10 -04:00
// Use Block Storage service volume for the instance root volume instead of
// Compute service local volume (default).
2019-06-06 10:29:25 -04:00
UseBlockStorageVolume bool ` mapstructure:"use_blockstorage_volume" required:"false" `
2019-06-20 09:24:10 -04:00
// Name of the Block Storage service volume. If this isn't specified,
// random string will be used.
2019-06-06 10:29:25 -04:00
VolumeName string ` mapstructure:"volume_name" required:"false" `
2019-06-20 09:24:10 -04:00
// Type of the Block Storage service volume. If this isn't specified, the
// default enforced by your OpenStack cluster will be used.
2019-06-06 10:29:25 -04:00
VolumeType string ` mapstructure:"volume_type" required:"false" `
2019-06-20 09:24:10 -04:00
// Size of the Block Storage service volume in GB. If this isn't specified,
// it is set to source image min disk value (if set) or calculated from the
// source image bytes size. Note that in some cases this needs to be
// specified, if use_blockstorage_volume is true.
2019-06-06 10:29:25 -04:00
VolumeSize int ` mapstructure:"volume_size" required:"false" `
2019-06-20 09:24:10 -04:00
// Availability zone of the Block Storage service volume. If omitted,
// Compute instance availability zone will be used. If both of Compute
// instance and Block Storage volume availability zones aren't specified,
// the default enforced by your OpenStack cluster will be used.
2019-05-28 11:50:58 -04:00
VolumeAvailabilityZone string ` mapstructure:"volume_availability_zone" required:"false" `
2018-05-24 06:20:22 -04:00
2015-06-12 10:05:03 -04:00
// Not really used, but here for BC
OpenstackProvider string ` mapstructure:"openstack_provider" `
2019-06-20 09:24:10 -04:00
// *Deprecated* use `floating_ip` or `floating_ip_pool` instead.
2019-06-06 10:29:25 -04:00
UseFloatingIp bool ` mapstructure:"use_floating_ip" required:"false" `
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 {
2019-06-20 09:24:10 -04:00
// filters used to select a source_image. NOTE: This will fail unless
// exactly one image is returned, or most_recent is set to true. Of the
// filters described in ImageService, the following are valid:
2019-06-06 10:29:25 -04:00
Filters ImageFilterOptions ` mapstructure:"filters" required:"false" `
2019-06-20 09:24:10 -04:00
// Selects the newest created image when true. This is most useful for
// selecting a daily distro build.
2019-06-06 10:29:25 -04:00
MostRecent bool ` mapstructure:"most_recent" required:"false" `
2018-08-22 07:37:43 -04:00
}
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
}
}
2020-09-25 08:59:00 -04:00
if c . SourceImage == "" && c . SourceImageName == "" && c . ExternalSourceImageURL == "" && c . SourceImageFilters . Filters . Empty ( ) {
errs = append ( errs , errors . New ( "Either a source_image, a source_image_name, an external_source_image_url or source_image_filter must be specified" ) )
2020-10-08 04:54:41 -04:00
} else {
// Make sure we've only set one image source option
thereCanBeOnlyOne := [ ] bool { len ( c . SourceImageName ) > 0 , len ( c . SourceImage ) > 0 , len ( c . ExternalSourceImageURL ) > 0 , ! c . SourceImageFilters . Filters . Empty ( ) }
numSet := 0
for _ , val := range thereCanBeOnlyOne {
if val {
numSet += 1
}
}
if numSet > 1 {
errs = append ( errs , errors . New ( "Only one of the options source_image, source_image_name, external_source_image_url, or source_image_filter can be specified, not multiple." ) )
}
2020-09-25 08:59:00 -04:00
}
// if external_source_image_format is not set use qcow2 as default
if c . ExternalSourceImageFormat == "" {
c . ExternalSourceImageFormat = "qcow2"
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 {
2019-06-20 09:24:10 -04:00
// Use Compute instance availability zone for the Block Storage volume
// if it's not provided.
2018-05-24 06:20:22 -04:00
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 ( ) )
}
}
2020-09-25 08:59:00 -04:00
// if neither ID, image name or external image URL is provided outside the filter,
// build the filter
if len ( c . SourceImage ) == 0 && len ( c . SourceImageName ) == 0 && len ( c . ExternalSourceImageURL ) == 0 {
2018-08-19 18:21:32 -04:00
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
}
2020-09-25 08:59:00 -04:00
// if c.ExternalSourceImageURL is set use a generated source image name
if c . ExternalSourceImageURL != "" {
c . SourceImageName = fmt . Sprintf ( "packer_%s" , uuid . TimeOrderedUUID ( ) )
}
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 )
}