github.phpd.cn/hashicorp/packer@v1.3.2/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  			Comm:         &b.config.Comm,
    57  			DebugKeyPath: fmt.Sprintf("oci_%s.pem", b.config.PackerBuildName),
    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: b.config.Comm.SSHConfigFunc(),
    70  		},
    71  		&common.StepProvision{},
    72  		&common.StepCleanupTempKeys{
    73  			Comm: &b.config.Comm,
    74  		},
    75  		&stepImage{},
    76  	}
    77  
    78  	// Run the steps
    79  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
    80  	b.runner.Run(state)
    81  
    82  	// If there was an error, return that
    83  	if rawErr, ok := state.GetOk("error"); ok {
    84  		return nil, rawErr.(error)
    85  	}
    86  
    87  	region, err := b.config.ConfigProvider.Region()
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	// Build the artifact and return it
    93  	artifact := &Artifact{
    94  		Image:  state.Get("image").(core.Image),
    95  		Region: region,
    96  		driver: driver,
    97  	}
    98  
    99  	return artifact, nil
   100  }
   101  
   102  // Cancel terminates a running build.
   103  func (b *Builder) Cancel() {
   104  	if b.runner != nil {
   105  		log.Println("Cancelling the step runner...")
   106  		b.runner.Cancel()
   107  	}
   108  }