github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/docker/step_export.go (about)

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