Files

93 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-10-14 16:43:59 +02:00
//go:generate mapstructure-to-hcl2 -type Config
2014-01-02 14:49:14 -08:00
package dockerimport
import (
2019-03-22 14:56:02 +01:00
"context"
2014-01-02 14:49:14 -08:00
"fmt"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2020-12-17 13:29:25 -08:00
"github.com/hashicorp/packer-plugin-sdk/common"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/post-processor/artifice"
2014-01-02 14:49:14 -08:00
)
const BuilderId = "packer.post-processor.docker-import"
2014-01-02 14:49:14 -08:00
type Config struct {
common.PackerConfig `mapstructure:",squash"`
2018-12-18 00:48:58 -06:00
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Changes []string `mapstructure:"changes"`
2014-01-02 14:49:14 -08:00
ctx interpolate.Context
2014-01-02 14:49:14 -08:00
}
type PostProcessor struct {
config Config
}
2019-12-17 11:25:56 +01:00
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
2014-01-02 14:49:14 -08:00
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
PluginType: BuilderId,
2015-06-22 12:24:27 -07:00
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
2014-01-02 14:49:14 -08:00
if err != nil {
return err
}
return nil
}
2020-11-19 12:17:11 -08:00
func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
2015-06-23 14:28:03 +03:00
switch artifact.BuilderId() {
2015-11-02 11:21:15 +00:00
case docker.BuilderId, artifice.BuilderId:
2015-06-23 14:28:03 +03:00
break
default:
err := fmt.Errorf(
2020-07-20 07:58:38 -07:00
"Unknown artifact type: %s\nCan only import from Docker builder "+
"and Artifice post-processor artifacts. If you are getting this "+
"error after having run the docker builder, it may be because you "+
"set commit: true in your Docker builder, so the image is "+
"already imported. ",
artifact.BuilderId())
2019-04-02 16:51:58 -07:00
return nil, false, false, err
}
2014-01-19 19:15:25 -08:00
importRepo := p.config.Repository
if p.config.Tag != "" {
importRepo += ":" + p.config.Tag
}
2014-01-02 14:49:14 -08:00
driver := &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
2014-01-02 14:49:14 -08:00
2014-01-19 19:15:25 -08:00
ui.Message("Importing image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
id, err := driver.Import(artifact.Files()[0], p.config.Changes, importRepo)
if err != nil {
2019-04-02 16:51:58 -07:00
return nil, false, false, err
}
2014-01-02 14:49:14 -08:00
ui.Message("Imported ID: " + id)
2014-01-02 14:49:14 -08:00
// Build the artifact
artifact = &docker.ImportArtifact{
BuilderIdValue: BuilderId,
Driver: driver,
IdValue: importRepo,
2014-01-02 14:49:14 -08:00
}
2019-04-02 16:51:58 -07:00
return artifact, false, false, nil
2014-01-02 14:49:14 -08:00
}