github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/oracle/oci/builder.go (about)

     1  // Package oci contains a packer.Builder implementation that builds Oracle
     2  // Bare Metal Cloud Services (OCI) images.
     3  package oci
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  
     9  	ocommon "github.com/hashicorp/packer/builder/oracle/common"
    10  	"github.com/hashicorp/packer/common"
    11  	"github.com/hashicorp/packer/helper/communicator"
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  	"github.com/hashicorp/packer/packer"
    14  	"github.com/oracle/oci-go-sdk/core"
    15  )
    16  
    17  // BuilderId uniquely identifies the builder
    18  const BuilderId = "packer.oracle.oci"
    19  
    20  // OCI API version
    21  const ociAPIVersion = "20160918"
    22  
    23  // Builder is a builder implementation that creates Oracle OCI custom images.
    24  type Builder struct {
    25  	config *Config
    26  	runner multistep.Runner
    27  }
    28  
    29  func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) {
    30  	config, err := NewConfig(rawConfig...)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	b.config = config
    35  
    36  	return nil, nil
    37  }
    38  
    39  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    40  	driver, err := NewDriverOCI(b.config)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Populate the state bag
    46  	state := new(multistep.BasicStateBag)
    47  	state.Put("config", b.config)
    48  	state.Put("driver", driver)
    49  	state.Put("hook", hook)
    50  	state.Put("ui", ui)
    51  
    52  	// Build the steps
    53  	steps := []multistep.Step{
    54  		&ocommon.StepKeyPair{
    55  			Debug:          b.config.PackerDebug,
    56  			DebugKeyPath:   fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
    57  			PrivateKeyFile: b.config.Comm.SSHPrivateKey,
    58  		},
    59  		&stepCreateInstance{},
    60  		&stepInstanceInfo{},
    61  		&stepGetDefaultCredentials{
    62  			Debug:     b.config.PackerDebug,
    63  			Comm:      &b.config.Comm,
    64  			BuildName: b.config.PackerBuildName,
    65  		},
    66  		&communicator.StepConnect{
    67  			Config: &b.config.Comm,
    68  			Host:   ocommon.CommHost,
    69  			SSHConfig: ocommon.SSHConfig(
    70  				b.config.Comm.SSHUsername,
    71  				b.config.Comm.SSHPassword),
    72  		},
    73  		&common.StepProvision{},
    74  		&stepImage{},
    75  	}
    76  
    77  	// Run the steps
    78  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
    79  	b.runner.Run(state)
    80  
    81  	// If there was an error, return that
    82  	if rawErr, ok := state.GetOk("error"); ok {
    83  		return nil, rawErr.(error)
    84  	}
    85  
    86  	region, err := b.config.ConfigProvider.Region()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	// Build the artifact and return it
    92  	artifact := &Artifact{
    93  		Image:  state.Get("image").(core.Image),
    94  		Region: region,
    95  		driver: driver,
    96  	}
    97  
    98  	return artifact, nil
    99  }
   100  
   101  // Cancel terminates a running build.
   102  func (b *Builder) Cancel() {
   103  	if b.runner != nil {
   104  		log.Println("Cancelling the step runner...")
   105  		b.runner.Cancel()
   106  	}
   107  }