2014-07-20 16:58:07 -04:00
|
|
|
package dockersave
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-27 17:27:11 -04:00
|
|
|
"os"
|
|
|
|
|
2014-07-20 16:58:07 -04:00
|
|
|
"github.com/mitchellh/packer/builder/docker"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-05-27 17:27:11 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
2014-07-20 16:58:07 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/post-processor/docker-import"
|
2015-06-05 09:01:29 -04:00
|
|
|
"github.com/mitchellh/packer/post-processor/docker-tag"
|
2015-05-27 17:27:11 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2014-07-20 16:58:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "packer.post-processor.docker-save"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
|
2015-05-27 17:27:11 -04:00
|
|
|
ctx interpolate.Context
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
Driver docker.Driver
|
|
|
|
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2015-05-27 17:27:11 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2014-07-20 16:58:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2015-06-05 09:01:29 -04:00
|
|
|
if artifact.BuilderId() != dockerimport.BuilderId &&
|
|
|
|
artifact.BuilderId() != dockertag.BuilderId {
|
2014-07-20 16:58:07 -04:00
|
|
|
err := fmt.Errorf(
|
|
|
|
"Unknown artifact type: %s\nCan only save Docker builder artifacts.",
|
|
|
|
artifact.BuilderId())
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := p.config.Path
|
|
|
|
|
|
|
|
// Open the file that we're going to write to
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating output file: %s", err)
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := p.Driver
|
|
|
|
if driver == nil {
|
|
|
|
// If no driver is set, then we use the real driver
|
2015-05-27 17:27:11 -04:00
|
|
|
driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Saving image: " + artifact.Id())
|
|
|
|
|
|
|
|
if err := driver.SaveImage(artifact.Id(), f); err != nil {
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
ui.Message("Saved to: " + path)
|
|
|
|
|
|
|
|
return artifact, true, nil
|
|
|
|
}
|