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

     1  package dockertag
     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/docker-import"
    11  	"github.com/hashicorp/packer/template/interpolate"
    12  )
    13  
    14  const BuilderId = "packer.post-processor.docker-tag"
    15  
    16  type Config struct {
    17  	common.PackerConfig `mapstructure:",squash"`
    18  
    19  	Repository string `mapstructure:"repository"`
    20  	Tag        string `mapstructure:"tag"`
    21  	Force      bool
    22  
    23  	ctx interpolate.Context
    24  }
    25  
    26  type PostProcessor struct {
    27  	Driver docker.Driver
    28  
    29  	config Config
    30  }
    31  
    32  func (p *PostProcessor) Configure(raws ...interface{}) error {
    33  	err := config.Decode(&p.config, &config.DecodeOpts{
    34  		Interpolate:        true,
    35  		InterpolateContext: &p.config.ctx,
    36  		InterpolateFilter: &interpolate.RenderFilter{
    37  			Exclude: []string{},
    38  		},
    39  	}, raws...)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	return nil
    45  
    46  }
    47  
    48  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    49  	if artifact.BuilderId() != BuilderId &&
    50  		artifact.BuilderId() != dockerimport.BuilderId {
    51  		err := fmt.Errorf(
    52  			"Unknown artifact type: %s\nCan only tag from Docker builder artifacts.",
    53  			artifact.BuilderId())
    54  		return nil, false, err
    55  	}
    56  
    57  	driver := p.Driver
    58  	if driver == nil {
    59  		// If no driver is set, then we use the real driver
    60  		driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
    61  	}
    62  
    63  	importRepo := p.config.Repository
    64  	if p.config.Tag != "" {
    65  		importRepo += ":" + p.config.Tag
    66  	}
    67  
    68  	ui.Message("Tagging image: " + artifact.Id())
    69  	ui.Message("Repository: " + importRepo)
    70  	err := driver.TagImage(artifact.Id(), importRepo, p.config.Force)
    71  	if err != nil {
    72  		return nil, false, err
    73  	}
    74  
    75  	// Build the artifact
    76  	artifact = &docker.ImportArtifact{
    77  		BuilderIdValue: BuilderId,
    78  		Driver:         driver,
    79  		IdValue:        importRepo,
    80  	}
    81  
    82  	return artifact, true, nil
    83  }