2013-11-09 12:48:36 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-11-09 12:48:36 -05:00
|
|
|
"fmt"
|
2015-08-18 16:48:18 -04:00
|
|
|
"os"
|
2017-01-22 02:50:24 -05:00
|
|
|
"path/filepath"
|
2015-08-18 16:48:18 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-11-09 12:48:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepExport exports the container to a flat tar file.
|
|
|
|
type StepExport struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-11-09 12:48:36 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-12-17 17:34:58 -05:00
|
|
|
config, ok := state.Get("config").(*Config)
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("error encountered obtaining docker config")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-11-09 12:48:36 -05:00
|
|
|
|
2015-08-18 16:48:18 -04:00
|
|
|
// We should catch this in validation, but guard anyway
|
|
|
|
if config.ExportPath == "" {
|
|
|
|
err := fmt.Errorf("No output file specified, we can't export anything")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-01-22 02:50:24 -05:00
|
|
|
// Make the directory we're exporting to if it doesn't exist
|
|
|
|
exportDir := filepath.Dir(config.ExportPath)
|
|
|
|
if err := os.MkdirAll(exportDir, 0755); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-09 12:48:36 -05:00
|
|
|
// Open the file that we're going to write to
|
|
|
|
f, err := os.Create(config.ExportPath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating output file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:34:58 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
containerId := state.Get("container_id").(string)
|
|
|
|
|
2013-11-09 16:15:51 -05:00
|
|
|
ui.Say("Exporting the container")
|
|
|
|
if err := driver.Export(containerId, f); err != nil {
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
2013-11-09 12:48:36 -05:00
|
|
|
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-09 16:15:51 -05:00
|
|
|
f.Close()
|
2013-11-09 12:48:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExport) Cleanup(state multistep.StateBag) {}
|