builder/docker: export the final image

This commit is contained in:
Mitchell Hashimoto 2013-11-09 09:48:36 -08:00
parent d27ceaf509
commit d5ce8ddb4a
3 changed files with 64 additions and 1 deletions

View File

@ -41,6 +41,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepPull{},
&StepRun{},
&StepProvision{},
&StepExport{},
}
// Setup the state bag and initial state for the steps

View File

@ -8,6 +8,7 @@ import (
type Config struct {
common.PackerConfig `mapstructure:",squash"`
ExportPath string `mapstructure:"export_path"`
Image string
tpl *packer.ConfigTemplate

View File

@ -0,0 +1,61 @@
package docker
import (
"bytes"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"os"
"os/exec"
)
// StepExport exports the container to a flat tar file.
type StepExport struct{}
func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
containerId := state.Get("container_id").(string)
ui := state.Get("ui").(packer.Ui)
ui.Say("Exporting the container")
// Args that we're going to pass to Docker
args := []string{"export", containerId}
// 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
}
defer f.Close()
// Export the thing, take stderr and point it to the file
var stderr bytes.Buffer
cmd := exec.Command("docker", args...)
cmd.Stdout = f
cmd.Stderr = &stderr
log.Printf("Starting container with args: %v", args)
if err := cmd.Start(); err != nil {
err := fmt.Errorf("Error exporting: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if err := cmd.Wait(); err != nil {
err := fmt.Errorf("Error exporting: %s\nStderr: %s",
err, stderr.String())
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepExport) Cleanup(state multistep.StateBag) {}