github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/googlecompute/builder.go (about) 1 // The googlecompute package contains a packer.Builder implementation that 2 // builds images for Google Compute Engine. 3 package googlecompute 4 5 import ( 6 "fmt" 7 "log" 8 9 "github.com/mitchellh/multistep" 10 "github.com/mitchellh/packer/common" 11 "github.com/mitchellh/packer/helper/communicator" 12 "github.com/mitchellh/packer/packer" 13 ) 14 15 // The unique ID for this builder. 16 const BuilderId = "packer.googlecompute" 17 18 // Builder represents a Packer Builder. 19 type Builder struct { 20 config *Config 21 runner multistep.Runner 22 } 23 24 // Prepare processes the build configuration parameters. 25 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 26 c, warnings, errs := NewConfig(raws...) 27 if errs != nil { 28 return warnings, errs 29 } 30 b.config = c 31 32 return warnings, nil 33 } 34 35 // Run executes a googlecompute Packer build and returns a packer.Artifact 36 // representing a GCE machine image. 37 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 38 driver, err := NewDriverGCE( 39 ui, b.config.ProjectId, &b.config.Account) 40 if err != nil { 41 return nil, err 42 } 43 44 // Set up the state. 45 state := new(multistep.BasicStateBag) 46 state.Put("config", b.config) 47 state.Put("driver", driver) 48 state.Put("hook", hook) 49 state.Put("ui", ui) 50 51 // Build the steps. 52 steps := []multistep.Step{ 53 new(StepCheckExistingImage), 54 &StepCreateSSHKey{ 55 Debug: b.config.PackerDebug, 56 DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName), 57 PrivateKeyFile: b.config.Comm.SSHPrivateKey, 58 }, 59 &StepCreateInstance{ 60 Debug: b.config.PackerDebug, 61 }, 62 &StepCreateWindowsPassword{ 63 Debug: b.config.PackerDebug, 64 DebugKeyPath: fmt.Sprintf("gce_windows_%s.pem", b.config.PackerBuildName), 65 }, 66 &StepInstanceInfo{ 67 Debug: b.config.PackerDebug, 68 }, 69 &communicator.StepConnect{ 70 Config: &b.config.Comm, 71 Host: commHost, 72 SSHConfig: sshConfig, 73 WinRMConfig: winrmConfig, 74 }, 75 new(common.StepProvision), 76 } 77 if _, exists := b.config.Metadata[StartupScriptKey]; exists || b.config.StartupScriptFile != "" { 78 steps = append(steps, new(StepWaitStartupScript)) 79 } 80 steps = append(steps, new(StepTeardownInstance), new(StepCreateImage)) 81 82 // Run the steps. 83 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 84 b.runner.Run(state) 85 86 // Report any errors. 87 if rawErr, ok := state.GetOk("error"); ok { 88 return nil, rawErr.(error) 89 } 90 if _, ok := state.GetOk("image"); !ok { 91 log.Println("Failed to find image in state. Bug?") 92 return nil, nil 93 } 94 95 artifact := &Artifact{ 96 image: state.Get("image").(*Image), 97 driver: driver, 98 config: b.config, 99 } 100 return artifact, nil 101 } 102 103 // Cancel. 104 func (b *Builder) Cancel() { 105 if b.runner != nil { 106 log.Println("Cancelling the step runner...") 107 b.runner.Cancel() 108 } 109 }