github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/oracle/classic/builder.go (about) 1 package classic 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 8 "github.com/hashicorp/go-cleanhttp" 9 "github.com/hashicorp/go-oracle-terraform/compute" 10 "github.com/hashicorp/go-oracle-terraform/opc" 11 ocommon "github.com/hashicorp/packer/builder/oracle/common" 12 "github.com/hashicorp/packer/common" 13 "github.com/hashicorp/packer/helper/communicator" 14 "github.com/hashicorp/packer/helper/multistep" 15 "github.com/hashicorp/packer/packer" 16 ) 17 18 // BuilderId uniquely identifies the builder 19 const BuilderId = "packer.oracle.classic" 20 21 // Builder is a builder implementation that creates Oracle OCI custom images. 22 type Builder struct { 23 config *Config 24 runner multistep.Runner 25 } 26 27 func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { 28 config, err := NewConfig(rawConfig...) 29 if err != nil { 30 return nil, err 31 } 32 b.config = config 33 34 return nil, nil 35 } 36 37 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 38 loggingEnabled := os.Getenv("PACKER_OCI_CLASSIC_LOGGING") != "" 39 httpClient := cleanhttp.DefaultClient() 40 config := &opc.Config{ 41 Username: opc.String(b.config.Username), 42 Password: opc.String(b.config.Password), 43 IdentityDomain: opc.String(b.config.IdentityDomain), 44 APIEndpoint: b.config.apiEndpointURL, 45 LogLevel: opc.LogDebug, 46 Logger: &Logger{loggingEnabled}, 47 // Logger: # Leave blank to use the default logger, or provide your own 48 HTTPClient: httpClient, 49 } 50 // Create the Compute Client 51 client, err := compute.NewComputeClient(config) 52 if err != nil { 53 return nil, fmt.Errorf("Error creating OPC Compute Client: %s", err) 54 } 55 56 // Populate the state bag 57 state := new(multistep.BasicStateBag) 58 state.Put("config", b.config) 59 state.Put("hook", hook) 60 state.Put("ui", ui) 61 state.Put("client", client) 62 63 // Build the steps 64 steps := []multistep.Step{ 65 &ocommon.StepKeyPair{ 66 Debug: b.config.PackerDebug, 67 DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), 68 PrivateKeyFile: b.config.Comm.SSHPrivateKey, 69 }, 70 &stepCreateIPReservation{}, 71 &stepAddKeysToAPI{}, 72 &stepSecurity{}, 73 &stepCreateInstance{}, 74 &communicator.StepConnect{ 75 Config: &b.config.Comm, 76 Host: ocommon.CommHost, 77 SSHConfig: ocommon.SSHConfig( 78 b.config.Comm.SSHUsername, 79 b.config.Comm.SSHPassword), 80 }, 81 &common.StepProvision{}, 82 &stepSnapshot{}, 83 &stepListImages{}, 84 } 85 86 // Run the steps 87 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 88 b.runner.Run(state) 89 90 // If there was an error, return that 91 if rawErr, ok := state.GetOk("error"); ok { 92 return nil, rawErr.(error) 93 } 94 95 // If there is no snapshot, then just return 96 if _, ok := state.GetOk("snapshot"); !ok { 97 return nil, nil 98 } 99 100 // Build the artifact and return it 101 artifact := &Artifact{ 102 ImageListVersion: state.Get("image_list_version").(int), 103 MachineImageName: state.Get("machine_image_name").(string), 104 MachineImageFile: state.Get("machine_image_file").(string), 105 driver: client, 106 } 107 108 return artifact, nil 109 } 110 111 // Cancel terminates a running build. 112 func (b *Builder) Cancel() { 113 if b.runner != nil { 114 log.Println("Cancelling the step runner...") 115 b.runner.Cancel() 116 } 117 }