2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2014-07-20 16:58:07 -04:00
|
|
|
package dockersave
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2014-07-20 16:58:07 -04:00
|
|
|
"fmt"
|
2015-05-27 17:27:11 -04:00
|
|
|
"os"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/docker"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-12 19:41:13 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2019-03-22 09:56:02 -04:00
|
|
|
dockerimport "github.com/hashicorp/packer/post-processor/docker-import"
|
|
|
|
dockertag "github.com/hashicorp/packer/post-processor/docker-tag"
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2014-07-20 16:58:07 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2015-05-27 17:27:11 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
2015-06-22 15:24:27 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:27:11 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2014-07-20 16:58:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, 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())
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
ui.Message("Saved to: " + path)
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return artifact, true, false, nil
|
2014-07-20 16:58:07 -04:00
|
|
|
}
|