github.com/angdraug/packer@v1.3.2/post-processor/docker-import/post-processor.go (about)

     1  package dockerimport
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/docker"
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/helper/config"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/hashicorp/packer/post-processor/artifice"
    11  	"github.com/hashicorp/packer/template/interpolate"
    12  )
    13  
    14  const BuilderId = "packer.post-processor.docker-import"
    15  
    16  type Config struct {
    17  	common.PackerConfig `mapstructure:",squash"`
    18  
    19  	Repository string `mapstructure:"repository"`
    20  	Tag        string `mapstructure:"tag"`
    21  
    22  	ctx interpolate.Context
    23  }
    24  
    25  type PostProcessor struct {
    26  	config Config
    27  }
    28  
    29  func (p *PostProcessor) Configure(raws ...interface{}) error {
    30  	err := config.Decode(&p.config, &config.DecodeOpts{
    31  		Interpolate:        true,
    32  		InterpolateContext: &p.config.ctx,
    33  		InterpolateFilter: &interpolate.RenderFilter{
    34  			Exclude: []string{},
    35  		},
    36  	}, raws...)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	return nil
    42  
    43  }
    44  
    45  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    46  	switch artifact.BuilderId() {
    47  	case docker.BuilderId, artifice.BuilderId:
    48  		break
    49  	default:
    50  		err := fmt.Errorf(
    51  			"Unknown artifact type: %s\nCan only import from Docker builder and Artifice post-processor artifacts.",
    52  			artifact.BuilderId())
    53  		return nil, false, err
    54  	}
    55  
    56  	importRepo := p.config.Repository
    57  	if p.config.Tag != "" {
    58  		importRepo += ":" + p.config.Tag
    59  	}
    60  
    61  	driver := &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
    62  
    63  	ui.Message("Importing image: " + artifact.Id())
    64  	ui.Message("Repository: " + importRepo)
    65  	id, err := driver.Import(artifact.Files()[0], importRepo)
    66  	if err != nil {
    67  		return nil, false, err
    68  	}
    69  
    70  	ui.Message("Imported ID: " + id)
    71  
    72  	// Build the artifact
    73  	artifact = &docker.ImportArtifact{
    74  		BuilderIdValue: BuilderId,
    75  		Driver:         driver,
    76  		IdValue:        importRepo,
    77  	}
    78  
    79  	return artifact, false, nil
    80  }