github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/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 }, 81 &StepRunSourceServer{ 82 Name: b.config.ImageName, 83 SourceImage: b.config.SourceImage, 84 SecurityGroups: b.config.SecurityGroups, 85 Networks: b.config.Networks, 86 AvailabilityZone: b.config.AvailabilityZone, 87 UserData: b.config.UserData, 88 UserDataFile: b.config.UserDataFile, 89 }, 90 &StepWaitForRackConnect{ 91 Wait: b.config.RackconnectWait, 92 }, 93 &StepAllocateIp{ 94 FloatingIpPool: b.config.FloatingIpPool, 95 FloatingIp: b.config.FloatingIp, 96 }, 97 &communicator.StepConnect{ 98 Config: &b.config.RunConfig.Comm, 99 Host: CommHost( 100 computeClient, 101 b.config.SSHInterface), 102 SSHConfig: SSHConfig(b.config.RunConfig.Comm.SSHUsername), 103 }, 104 &common.StepProvision{}, 105 &StepStopServer{}, 106 &stepCreateImage{}, 107 } 108 109 // Run! 110 if b.config.PackerDebug { 111 b.runner = &multistep.DebugRunner{ 112 Steps: steps, 113 PauseFn: common.MultistepDebugFn(ui), 114 } 115 } else { 116 b.runner = &multistep.BasicRunner{Steps: steps} 117 } 118 119 b.runner.Run(state) 120 121 // If there was an error, return that 122 if rawErr, ok := state.GetOk("error"); ok { 123 return nil, rawErr.(error) 124 } 125 126 // If there are no images, then just return 127 if _, ok := state.GetOk("image"); !ok { 128 return nil, nil 129 } 130 131 // Build the artifact and return it 132 artifact := &Artifact{ 133 ImageId: state.Get("image").(string), 134 BuilderIdValue: BuilderId, 135 Client: computeClient, 136 } 137 138 return artifact, nil 139 } 140 141 func (b *Builder) Cancel() { 142 if b.runner != nil { 143 log.Println("Cancelling the step runner...") 144 b.runner.Cancel() 145 } 146 }