github.com/sneal/packer@v0.5.2/builder/docker/builder.go (about)

     1  package docker
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/common"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  )
     9  
    10  const BuilderId = "packer.docker"
    11  
    12  type Builder struct {
    13  	config *Config
    14  	runner multistep.Runner
    15  }
    16  
    17  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    18  	c, warnings, errs := NewConfig(raws...)
    19  	if errs != nil {
    20  		return warnings, errs
    21  	}
    22  	b.config = c
    23  
    24  	return warnings, nil
    25  }
    26  
    27  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    28  	driver := &DockerDriver{Tpl: b.config.tpl, Ui: ui}
    29  	if err := driver.Verify(); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	steps := []multistep.Step{
    34  		&StepTempDir{},
    35  		&StepPull{},
    36  		&StepRun{},
    37  		&StepProvision{},
    38  		&StepExport{},
    39  	}
    40  
    41  	// Setup the state bag and initial state for the steps
    42  	state := new(multistep.BasicStateBag)
    43  	state.Put("config", b.config)
    44  	state.Put("hook", hook)
    45  	state.Put("ui", ui)
    46  
    47  	// Setup the driver that will talk to Docker
    48  	state.Put("driver", driver)
    49  
    50  	// Run!
    51  	if b.config.PackerDebug {
    52  		b.runner = &multistep.DebugRunner{
    53  			Steps:   steps,
    54  			PauseFn: common.MultistepDebugFn(ui),
    55  		}
    56  	} else {
    57  		b.runner = &multistep.BasicRunner{Steps: steps}
    58  	}
    59  
    60  	b.runner.Run(state)
    61  
    62  	// If there was an error, return that
    63  	if rawErr, ok := state.GetOk("error"); ok {
    64  		return nil, rawErr.(error)
    65  	}
    66  
    67  	// No errors, must've worked
    68  	artifact := &ExportArtifact{path: b.config.ExportPath}
    69  	return artifact, nil
    70  }
    71  
    72  func (b *Builder) Cancel() {
    73  	if b.runner != nil {
    74  		log.Println("Cancelling the step runner...")
    75  		b.runner.Cancel()
    76  	}
    77  }