github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/builder/openstack/builder.go (about) 1 // The openstack package contains a packer.Builder implementation that 2 // builds Images for openstack. 3 4 package openstack 5 6 import ( 7 "fmt" 8 "log" 9 10 "github.com/mitchellh/multistep" 11 "github.com/mitchellh/packer/common" 12 "github.com/mitchellh/packer/helper/communicator" 13 "github.com/mitchellh/packer/helper/config" 14 "github.com/mitchellh/packer/packer" 15 "github.com/mitchellh/packer/template/interpolate" 16 ) 17 18 // The unique ID for this builder 19 const BuilderId = "mitchellh.openstack" 20 21 type Config struct { 22 common.PackerConfig `mapstructure:",squash"` 23 24 AccessConfig `mapstructure:",squash"` 25 ImageConfig `mapstructure:",squash"` 26 RunConfig `mapstructure:",squash"` 27 28 ctx interpolate.Context 29 } 30 31 type Builder struct { 32 config Config 33 runner multistep.Runner 34 } 35 36 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 37 err := config.Decode(&b.config, &config.DecodeOpts{ 38 Interpolate: true, 39 InterpolateContext: &b.config.ctx, 40 }, raws...) 41 if err != nil { 42 return nil, err 43 } 44 45 // Accumulate any errors 46 var errs *packer.MultiError 47 errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) 48 errs = packer.MultiErrorAppend(errs, b.config.ImageConfig.Prepare(&b.config.ctx)...) 49 errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) 50 51 if errs != nil && len(errs.Errors) > 0 { 52 return nil, errs 53 } 54 55 log.Println(common.ScrubConfig(b.config, b.config.Password)) 56 return nil, nil 57 } 58 59 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 60 computeClient, err := b.config.computeV2Client() 61 if err != nil { 62 return nil, fmt.Errorf("Error initializing compute client: %s", err) 63 } 64 65 // Setup the state bag and initial state for the steps 66 state := new(multistep.BasicStateBag) 67 state.Put("config", b.config) 68 state.Put("hook", hook) 69 state.Put("ui", ui) 70 71 // Build the steps 72 steps := []multistep.Step{ 73 &StepLoadExtensions{}, 74 &StepLoadFlavor{ 75 Flavor: b.config.Flavor, 76 }, 77 &StepKeyPair{ 78 Debug: b.config.PackerDebug, 79 DebugKeyPath: fmt.Sprintf("os_%s.pem", b.config.PackerBuildName), 80 KeyPairName: b.config.SSHKeyPairName, 81 PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, 82 }, 83 &StepRunSourceServer{ 84 Name: b.config.ImageName, 85 SourceImage: b.config.SourceImage, 86 SecurityGroups: b.config.SecurityGroups, 87 Networks: b.config.Networks, 88 AvailabilityZone: b.config.AvailabilityZone, 89 UserData: b.config.UserData, 90 UserDataFile: b.config.UserDataFile, 91 ConfigDrive: b.config.ConfigDrive, 92 }, 93 &StepWaitForRackConnect{ 94 Wait: b.config.RackconnectWait, 95 }, 96 &StepAllocateIp{ 97 FloatingIpPool: b.config.FloatingIpPool, 98 FloatingIp: b.config.FloatingIp, 99 }, 100 &communicator.StepConnect{ 101 Config: &b.config.RunConfig.Comm, 102 Host: CommHost( 103 computeClient, 104 b.config.SSHInterface), 105 SSHConfig: SSHConfig(b.config.RunConfig.Comm.SSHUsername), 106 }, 107 &common.StepProvision{}, 108 &StepStopServer{}, 109 &stepCreateImage{}, 110 } 111 112 // Run! 113 if b.config.PackerDebug { 114 b.runner = &multistep.DebugRunner{ 115 Steps: steps, 116 PauseFn: common.MultistepDebugFn(ui), 117 } 118 } else { 119 b.runner = &multistep.BasicRunner{Steps: steps} 120 } 121 122 b.runner.Run(state) 123 124 // If there was an error, return that 125 if rawErr, ok := state.GetOk("error"); ok { 126 return nil, rawErr.(error) 127 } 128 129 // If there are no images, then just return 130 if _, ok := state.GetOk("image"); !ok { 131 return nil, nil 132 } 133 134 // Build the artifact and return it 135 artifact := &Artifact{ 136 ImageId: state.Get("image").(string), 137 BuilderIdValue: BuilderId, 138 Client: computeClient, 139 } 140 141 return artifact, nil 142 } 143 144 func (b *Builder) Cancel() { 145 if b.runner != nil { 146 log.Println("Cancelling the step runner...") 147 b.runner.Cancel() 148 } 149 }