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