github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/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  	client "github.com/hashicorp/packer/builder/oracle/oci/client"
    10  	"github.com/hashicorp/packer/common"
    11  	"github.com/hashicorp/packer/helper/communicator"
    12  	"github.com/hashicorp/packer/packer"
    13  	"github.com/mitchellh/multistep"
    14  )
    15  
    16  // BuilderId uniquely identifies the builder
    17  const BuilderId = "packer.oracle.oci"
    18  
    19  // OCI API version
    20  const ociAPIVersion = "20160918"
    21  
    22  // Builder is a builder implementation that creates Oracle OCI custom images.
    23  type Builder struct {
    24  	config *Config
    25  	runner multistep.Runner
    26  }
    27  
    28  func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) {
    29  	config, err := NewConfig(rawConfig...)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	b.config = config
    34  
    35  	return nil, nil
    36  }
    37  
    38  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    39  	driver, err := NewDriverOCI(b.config)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	// Populate the state bag
    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  		&stepKeyPair{
    54  			Debug:          b.config.PackerDebug,
    55  			DebugKeyPath:   fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
    56  			PrivateKeyFile: b.config.Comm.SSHPrivateKey,
    57  		},
    58  		&stepCreateInstance{},
    59  		&stepInstanceInfo{},
    60  		&communicator.StepConnect{
    61  			Config: &b.config.Comm,
    62  			Host:   commHost,
    63  			SSHConfig: SSHConfig(
    64  				b.config.Comm.SSHUsername,
    65  				b.config.Comm.SSHPassword),
    66  		},
    67  		&common.StepProvision{},
    68  		&stepImage{},
    69  	}
    70  
    71  	// Run the steps
    72  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
    73  	b.runner.Run(state)
    74  
    75  	// If there was an error, return that
    76  	if rawErr, ok := state.GetOk("error"); ok {
    77  		return nil, rawErr.(error)
    78  	}
    79  
    80  	// Build the artifact and return it
    81  	artifact := &Artifact{
    82  		Image:  state.Get("image").(client.Image),
    83  		Region: b.config.AccessCfg.Region,
    84  		driver: driver,
    85  	}
    86  
    87  	return artifact, nil
    88  }
    89  
    90  // Cancel terminates a running build.
    91  func (b *Builder) Cancel() {
    92  	if b.runner != nil {
    93  		log.Println("Cancelling the step runner...")
    94  		b.runner.Cancel()
    95  	}
    96  }