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