packer-cn/builder/openstack/builder.go

176 lines
4.8 KiB
Go
Raw Normal View History

// The openstack package contains a packer.Builder implementation that
// builds Images for openstack.
package openstack
import (
"fmt"
"log"
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"
"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"
)
// The unique ID for this builder
const BuilderId = "mitchellh.openstack"
type Config struct {
common.PackerConfig `mapstructure:",squash"`
AccessConfig `mapstructure:",squash"`
ImageConfig `mapstructure:",squash"`
RunConfig `mapstructure:",squash"`
ctx interpolate.Context
}
type Builder struct {
config Config
runner multistep.Runner
}
2013-11-03 00:03:59 -04:00
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
err := config.Decode(&b.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &b.config.ctx,
}, raws...)
if err != nil {
2013-11-03 00:03:59 -04:00
return nil, err
}
// Accumulate any errors
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)...)
if errs != nil && len(errs.Errors) > 0 {
2013-11-03 00:03:59 -04:00
return nil, errs
}
// By default, instance name is same as image name
if b.config.InstanceName == "" {
b.config.InstanceName = b.config.ImageName
}
packer.LogSecretFilter.Set(b.config.Password)
log.Println(b.config)
2013-11-03 00:03:59 -04:00
return nil, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
2015-06-12 00:16:43 -04:00
computeClient, err := b.config.computeV2Client()
if err != nil {
2015-06-12 00:16:43 -04:00
return nil, fmt.Errorf("Error initializing compute client: %s", err)
}
// Setup the state bag and initial state for the steps
2013-08-31 15:37:07 -04:00
state := new(multistep.BasicStateBag)
state.Put("config", b.config)
state.Put("hook", hook)
state.Put("ui", ui)
// Build the steps
steps := []multistep.Step{
&StepLoadFlavor{
Flavor: b.config.Flavor,
},
&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
},
&StepSourceImageInfo{
SourceImage: b.config.RunConfig.SourceImage,
SourceImageName: b.config.RunConfig.SourceImageName,
SourceImageOpts: b.config.RunConfig.sourceImageOpts,
SourceMostRecent: b.config.SourceImageFilters.MostRecent,
},
&StepCreateVolume{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
SourceImage: b.config.SourceImage,
VolumeName: b.config.VolumeName,
VolumeType: b.config.VolumeType,
VolumeAvailabilityZone: b.config.VolumeAvailabilityZone,
},
&StepRunSourceServer{
Name: b.config.InstanceName,
SourceImage: b.config.SourceImage,
SourceImageName: b.config.SourceImageName,
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,
},
&StepGetPassword{
2016-04-22 13:44:54 -04:00
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
},
&StepWaitForRackConnect{
Wait: b.config.RackconnectWait,
},
2014-02-27 03:34:24 -05:00
&StepAllocateIp{
FloatingIPNetwork: b.config.FloatingIPNetwork,
FloatingIP: b.config.FloatingIP,
ReuseIPs: b.config.ReuseIPs,
2014-02-27 03:34:24 -05:00
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Host: CommHost(
computeClient,
b.config.Comm.SSHInterface,
b.config.Comm.SSHIPVersion),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
&common.StepProvision{},
&StepStopServer{},
&StepDetachVolume{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
},
&stepCreateImage{
UseBlockStorageVolume: b.config.UseBlockStorageVolume,
},
2016-12-13 19:28:54 -05:00
&stepUpdateImageVisibility{},
2016-12-13 19:32:30 -05:00
&stepAddImageMembers{},
}
// Run!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
// If there was an error, return that
2013-08-31 15:37:07 -04:00
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// 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,
2015-06-12 00:16:43 -04:00
Client: computeClient,
2013-08-28 01:35:59 -04:00
}
return artifact, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}