github.com/rothwerx/packer@v0.9.0/builder/docker/step_export.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // StepExport exports the container to a flat tar file.
    12  type StepExport struct{}
    13  
    14  func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction {
    15  	config := state.Get("config").(*Config)
    16  
    17  	driver := state.Get("driver").(Driver)
    18  	containerId := state.Get("container_id").(string)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	// We should catch this in validation, but guard anyway
    22  	if config.ExportPath == "" {
    23  		err := fmt.Errorf("No output file specified, we can't export anything")
    24  		state.Put("error", err)
    25  		ui.Error(err.Error())
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	// Open the file that we're going to write to
    30  	f, err := os.Create(config.ExportPath)
    31  	if err != nil {
    32  		err := fmt.Errorf("Error creating output file: %s", err)
    33  		state.Put("error", err)
    34  		ui.Error(err.Error())
    35  		return multistep.ActionHalt
    36  	}
    37  
    38  	ui.Say("Exporting the container")
    39  	if err := driver.Export(containerId, f); err != nil {
    40  		f.Close()
    41  		os.Remove(f.Name())
    42  
    43  		state.Put("error", err)
    44  		ui.Error(err.Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	f.Close()
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *StepExport) Cleanup(state multistep.StateBag) {}