github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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/hashicorp/packer/common" 10 "github.com/hashicorp/packer/helper/communicator" 11 "github.com/hashicorp/packer/helper/multistep" 12 "github.com/hashicorp/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 return warnings, nil 32 } 33 34 // Run executes a googlecompute Packer build and returns a packer.Artifact 35 // representing a GCE machine image. 36 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 37 driver, err := NewDriverGCE( 38 ui, b.config.ProjectId, &b.config.Account) 39 if err != nil { 40 return nil, err 41 } 42 43 // Set up the state. 44 state := new(multistep.BasicStateBag) 45 state.Put("config", b.config) 46 state.Put("driver", driver) 47 state.Put("hook", hook) 48 state.Put("ui", ui) 49 50 // Build the steps. 51 steps := []multistep.Step{ 52 new(StepCheckExistingImage), 53 &StepCreateSSHKey{ 54 Debug: b.config.PackerDebug, 55 DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName), 56 PrivateKeyFile: b.config.Comm.SSHPrivateKey, 57 }, 58 &StepCreateInstance{ 59 Debug: b.config.PackerDebug, 60 }, 61 &StepCreateWindowsPassword{ 62 Debug: b.config.PackerDebug, 63 DebugKeyPath: fmt.Sprintf("gce_windows_%s.pem", b.config.PackerBuildName), 64 }, 65 &StepInstanceInfo{ 66 Debug: b.config.PackerDebug, 67 }, 68 &communicator.StepConnect{ 69 Config: &b.config.Comm, 70 Host: commHost, 71 SSHConfig: sshConfig, 72 WinRMConfig: winrmConfig, 73 }, 74 new(common.StepProvision), 75 } 76 if _, exists := b.config.Metadata[StartupScriptKey]; exists || b.config.StartupScriptFile != "" { 77 steps = append(steps, new(StepWaitStartupScript)) 78 } 79 steps = append(steps, new(StepTeardownInstance), new(StepCreateImage)) 80 81 // Run the steps. 82 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 83 b.runner.Run(state) 84 85 // Report any errors. 86 if rawErr, ok := state.GetOk("error"); ok { 87 return nil, rawErr.(error) 88 } 89 if _, ok := state.GetOk("image"); !ok { 90 log.Println("Failed to find image in state. Bug?") 91 return nil, nil 92 } 93 94 artifact := &Artifact{ 95 image: state.Get("image").(*Image), 96 driver: driver, 97 config: b.config, 98 } 99 return artifact, nil 100 } 101 102 // Cancel. 103 func (b *Builder) Cancel() { 104 if b.runner != nil { 105 log.Println("Cancelling the step runner...") 106 b.runner.Cancel() 107 } 108 }