github.phpd.cn/hashicorp/packer@v1.3.2/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  			Comm:         &b.config.Comm,
    68  			DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName),
    69  		},
    70  		&stepCreateIPReservation{},
    71  		&stepAddKeysToAPI{},
    72  		&stepSecurity{},
    73  		&stepCreateInstance{},
    74  		&communicator.StepConnect{
    75  			Config:    &b.config.Comm,
    76  			Host:      ocommon.CommHost,
    77  			SSHConfig: b.config.Comm.SSHConfigFunc(),
    78  		},
    79  		&common.StepProvision{},
    80  		&common.StepCleanupTempKeys{
    81  			Comm: &b.config.Comm,
    82  		},
    83  		&common.StepCleanupTempKeys{
    84  			Comm: &b.config.Comm,
    85  		},
    86  		&stepSnapshot{},
    87  		&stepListImages{},
    88  	}
    89  
    90  	// Run the steps
    91  	b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
    92  	b.runner.Run(state)
    93  
    94  	// If there was an error, return that
    95  	if rawErr, ok := state.GetOk("error"); ok {
    96  		return nil, rawErr.(error)
    97  	}
    98  
    99  	// If there is no snapshot, then just return
   100  	if _, ok := state.GetOk("snapshot"); !ok {
   101  		return nil, nil
   102  	}
   103  
   104  	// Build the artifact and return it
   105  	artifact := &Artifact{
   106  		ImageListVersion: state.Get("image_list_version").(int),
   107  		MachineImageName: state.Get("machine_image_name").(string),
   108  		MachineImageFile: state.Get("machine_image_file").(string),
   109  		driver:           client,
   110  	}
   111  
   112  	return artifact, nil
   113  }
   114  
   115  // Cancel terminates a running build.
   116  func (b *Builder) Cancel() {
   117  	if b.runner != nil {
   118  		log.Println("Cancelling the step runner...")
   119  		b.runner.Cancel()
   120  	}
   121  }