Files

201 lines
6.2 KiB
Go
Raw Permalink Normal View History

2019-10-14 16:43:59 +02:00
//go:generate mapstructure-to-hcl2 -type Config,ImageFilter,ImageFilterOptions
2020-12-01 13:42:11 -08:00
// The openstack package contains a packersdk.Builder implementation that
2013-08-26 21:57:23 -07:00
// builds Images for openstack.
package openstack
import (
"context"
"fmt"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2020-12-17 13:29:25 -08:00
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
2013-08-26 21:57:23 -07:00
)
// The unique ID for this builder
const BuilderId = "mitchellh.openstack"
2015-05-27 13:02:57 -07:00
type Config struct {
2013-08-26 21:57:23 -07:00
common.PackerConfig `mapstructure:",squash"`
2015-06-13 18:34:37 -04:00
AccessConfig `mapstructure:",squash"`
ImageConfig `mapstructure:",squash"`
RunConfig `mapstructure:",squash"`
2013-08-26 21:57:23 -07:00
2015-05-27 13:02:57 -07:00
ctx interpolate.Context
2013-08-26 21:57:23 -07:00
}
type Builder struct {
2015-05-27 13:02:57 -07:00
config Config
2013-08-26 21:57:23 -07:00
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2015-05-27 13:02:57 -07:00
err := config.Decode(&b.config, &config.DecodeOpts{
PluginType: BuilderId,
Interpolate: true,
InterpolateContext: &b.config.ctx,
2015-05-27 13:02:57 -07:00
}, raws...)
2013-08-26 21:57:23 -07:00
if err != nil {
return nil, nil, err
2013-08-26 21:57:23 -07:00
}
// Accumulate any errors
var errs *packersdk.MultiError
errs = packersdk.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
errs = packersdk.MultiErrorAppend(errs, b.config.ImageConfig.Prepare(&b.config.ctx)...)
errs = packersdk.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
2013-08-26 21:57:23 -07:00
if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs
2013-08-26 21:57:23 -07:00
}
if b.config.ImageConfig.ImageDiskFormat != "" && !b.config.RunConfig.UseBlockStorageVolume {
return nil, nil, fmt.Errorf("use_blockstorage_volume must be true if image_disk_format is specified.")
}
// By default, instance name is same as image name
if b.config.InstanceName == "" {
b.config.InstanceName = b.config.ImageName
}
2020-11-19 14:03:11 -08:00
packersdk.LogSecretFilter.Set(b.config.Password)
return nil, nil, nil
2013-08-26 21:57:23 -07:00
}
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
if b.config.PackerDebug {
b.config.enableDebug(ui)
}
2015-06-12 00:16:43 -04:00
computeClient, err := b.config.computeV2Client()
2013-08-26 21:57:23 -07:00
if err != nil {
2015-06-12 00:16:43 -04:00
return nil, fmt.Errorf("Error initializing compute client: %s", err)
}
2013-08-26 21:57:23 -07:00
imageClient, err := b.config.imageV2Client()
if err != nil {
return nil, fmt.Errorf("Error initializing image client: %s", err)
}
// Setup the state bag and initial state for the steps
2013-08-31 12:37:07 -07:00
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
2013-08-31 12:37:07 -07:00
state.Put("hook", hook)
state.Put("ui", ui)
2013-08-26 21:57:23 -07:00
// Build the steps
steps := []multistep.Step{
2015-06-12 10:32:31 -04:00
&StepLoadFlavor{
Flavor: b.config.Flavor,
},
&StepKeyPair{
2018-08-28 17:48:01 +02:00
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
DebugKeyPath: fmt.Sprintf("os_%s.pem", b.config.PackerBuildName),
2013-12-26 17:20:13 -07:00
},
&StepSourceImageInfo{
SourceImage: b.config.RunConfig.SourceImage,
SourceImageName: b.config.RunConfig.SourceImageName,
ExternalSourceImageURL: b.config.RunConfig.ExternalSourceImageURL,
ExternalSourceImageFormat: b.config.RunConfig.ExternalSourceImageFormat,
ExternalSourceImageProperties: b.config.RunConfig.ExternalSourceImageProperties,
SourceImageOpts: b.config.RunConfig.sourceImageOpts,
SourceMostRecent: b.config.SourceImageFilters.MostRecent,
SourceProperties: b.config.SourceImageFilters.Filters.Properties,
},
&StepDiscoverNetwork{
Networks: b.config.Networks,
NetworkDiscoveryCIDRs: b.config.NetworkDiscoveryCIDRs,
Ports: b.config.Ports,
},
&StepCreateVolume{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
VolumeName: b.config.VolumeName,
VolumeType: b.config.VolumeType,
VolumeAvailabilityZone: b.config.VolumeAvailabilityZone,
},
2013-08-26 21:57:23 -07:00
&StepRunSourceServer{
Name: b.config.InstanceName,
SecurityGroups: b.config.SecurityGroups,
AvailabilityZone: b.config.AvailabilityZone,
UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile,
ConfigDrive: b.config.ConfigDrive,
InstanceMetadata: b.config.InstanceMetadata,
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
2019-03-11 18:39:47 +00:00
ForceDelete: b.config.ForceDelete,
2013-08-26 21:57:23 -07:00
},
&StepGetPassword{
2016-04-22 10:44:54 -07:00
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
},
2015-02-17 05:23:21 +00:00
&StepWaitForRackConnect{
2015-05-27 13:02:57 -07:00
Wait: b.config.RackconnectWait,
2015-02-17 05:23:21 +00:00
},
2014-02-27 00:34:24 -08:00
&StepAllocateIp{
2019-07-17 17:10:52 -05:00
FloatingIPNetwork: b.config.FloatingIPNetwork,
FloatingIP: b.config.FloatingIP,
ReuseIPs: b.config.ReuseIPs,
InstanceFloatingIPNet: b.config.InstanceFloatingIPNet,
2014-02-27 00:34:24 -08:00
},
2015-06-13 18:34:37 -04:00
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
2015-06-13 19:23:33 -04:00
Host: CommHost(
2019-07-02 14:00:51 -07:00
b.config.RunConfig.Comm.SSHHost,
2015-06-13 18:34:37 -04:00
computeClient,
b.config.SSHInterface,
b.config.SSHIPVersion),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
2013-08-26 21:57:23 -07:00
},
&commonsteps.StepProvision{},
&commonsteps.StepCleanupTempKeys{
Comm: &b.config.RunConfig.Comm,
},
&StepStopServer{},
2018-05-24 14:15:03 +03:00
&StepDetachVolume{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
},
&stepCreateImage{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
},
&stepUpdateImageTags{},
2016-12-13 16:28:54 -08:00
&stepUpdateImageVisibility{},
2016-12-13 16:32:30 -08:00
&stepAddImageMembers{},
2019-02-07 14:54:12 +09:00
&stepUpdateImageMinDisk{},
2013-08-26 21:57:23 -07:00
}
// Run!
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(ctx, state)
2013-08-26 21:57:23 -07:00
// If there was an error, return that
2013-08-31 12:37:07 -07:00
if rawErr, ok := state.GetOk("error"); ok {
2013-08-26 21:57:23 -07:00
return nil, rawErr.(error)
}
// If there are no images, then just return
if _, ok := state.GetOk("image"); !ok {
return nil, nil
}
2013-08-27 22:35:59 -07:00
// Build the artifact and return it
artifact := &Artifact{
2013-08-31 12:37:07 -07:00
ImageId: state.Get("image").(string),
2013-08-27 22:35:59 -07:00
BuilderIdValue: BuilderId,
Client: imageClient,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
2013-08-27 22:35:59 -07:00
}
return artifact, nil
2013-08-26 21:57:23 -07:00
}