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
|
|
|
"log"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2015-06-13 18:34:37 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
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"`
|
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
|
|
|
}
|
|
|
|
|
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 18:04:46 -04:00
|
|
|
&StepLoadExtensions{},
|
2015-06-12 10:32:31 -04:00
|
|
|
&StepLoadFlavor{
|
|
|
|
Flavor: b.config.Flavor,
|
|
|
|
},
|
2013-12-22 23:30:13 -05:00
|
|
|
&StepKeyPair{
|
2017-03-13 02:29:59 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("os_%s.pem", b.config.PackerBuildName),
|
|
|
|
KeyPairName: b.config.SSHKeyPairName,
|
|
|
|
TemporaryKeyPairName: b.config.TemporaryKeyPairName,
|
|
|
|
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
|
|
|
|
SSHAgentAuth: b.config.RunConfig.Comm.SSHAgentAuth,
|
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,
|
2015-08-10 09:35:56 -04:00
|
|
|
SourceImageName: b.config.SourceImageName,
|
2015-06-12 11:10:10 -04:00
|
|
|
SecurityGroups: b.config.SecurityGroups,
|
|
|
|
Networks: b.config.Networks,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
2015-06-12 22:55:39 -04:00
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
2015-12-08 07:45:26 -05:00
|
|
|
ConfigDrive: b.config.ConfigDrive,
|
2017-01-04 21:15:16 -05:00
|
|
|
InstanceMetadata: b.config.InstanceMetadata,
|
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{
|
|
|
|
FloatingIpPool: b.config.FloatingIpPool,
|
|
|
|
FloatingIp: b.config.FloatingIp,
|
2017-02-16 07:35:27 -05:00
|
|
|
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,
|
2016-02-13 15:53:46 -05:00
|
|
|
b.config.SSHInterface,
|
|
|
|
b.config.SSHIPVersion),
|
2017-03-10 11:45:13 -05:00
|
|
|
SSHConfig: SSHConfig(
|
|
|
|
b.config.RunConfig.Comm.SSHAgentAuth,
|
|
|
|
b.config.RunConfig.Comm.SSHUsername,
|
2016-10-08 17:18:19 -04:00
|
|
|
b.config.RunConfig.Comm.SSHPassword),
|
2013-08-27 00:57:23 -04:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2015-06-12 18:04:46 -04:00
|
|
|
&StepStopServer{},
|
2013-08-27 00:57:23 -04:00
|
|
|
&stepCreateImage{},
|
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)
|
2013-08-27 00:57:23 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|