2013-08-27 00:57:23 -04:00
|
|
|
// The openstack package contains a packer.Builder implementation that
|
|
|
|
// builds Images for openstack.
|
|
|
|
|
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2013-12-22 23:30:13 -05:00
|
|
|
"fmt"
|
2013-08-27 00:57:23 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"log"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2015-05-27 16:02:57 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/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"`
|
|
|
|
AccessConfig `mapstructure:",squash"`
|
|
|
|
ImageConfig `mapstructure:",squash"`
|
|
|
|
RunConfig `mapstructure:",squash"`
|
|
|
|
|
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{
|
|
|
|
Interpolate: true,
|
|
|
|
}, 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
|
|
|
}
|
|
|
|
|
2013-11-02 03:57:33 -04:00
|
|
|
log.Println(common.ScrubConfig(b.config, b.config.Password))
|
2013-11-03 00:03:59 -04:00
|
|
|
return nil, nil
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
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
|
|
|
|
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)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
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{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("os_%s.pem", b.config.PackerBuildName),
|
2013-12-26 19:20:13 -05:00
|
|
|
},
|
2013-08-27 00:57:23 -04:00
|
|
|
&StepRunSourceServer{
|
2015-06-12 11:10:10 -04:00
|
|
|
Name: b.config.ImageName,
|
|
|
|
SourceImage: b.config.SourceImage,
|
|
|
|
SecurityGroups: b.config.SecurityGroups,
|
|
|
|
Networks: b.config.Networks,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
2013-08-27 00:57:23 -04: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{
|
|
|
|
FloatingIpPool: b.config.FloatingIpPool,
|
|
|
|
FloatingIp: b.config.FloatingIp,
|
|
|
|
},
|
2013-08-27 00:57:23 -04:00
|
|
|
&common.StepConnectSSH{
|
2015-06-12 00:16:43 -04:00
|
|
|
SSHAddress: SSHAddress(computeClient, b.config.SSHInterface, b.config.SSHPort),
|
2013-08-27 00:57:23 -04:00
|
|
|
SSHConfig: SSHConfig(b.config.SSHUsername),
|
|
|
|
SSHWaitTimeout: b.config.SSHTimeout(),
|
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
|
|
|
&stepCreateImage{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
|
|
|
if b.config.PackerDebug {
|
|
|
|
b.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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,
|
2015-06-12 00:16:43 -04:00
|
|
|
Client: computeClient,
|
2013-08-28 01:35:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|