github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/post-processor/docker-tag/post-processor.go (about)

     1  package dockertag
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/builder/docker"
     6  	"github.com/mitchellh/packer/common"
     7  	"github.com/mitchellh/packer/packer"
     8  	"github.com/mitchellh/packer/post-processor/docker-import"
     9  )
    10  
    11  const BuilderId = "packer.post-processor.docker-tag"
    12  
    13  type Config struct {
    14  	common.PackerConfig `mapstructure:",squash"`
    15  
    16  	Repository string `mapstructure:"repository"`
    17  	Tag        string `mapstructure:"tag"`
    18  
    19  	tpl *packer.ConfigTemplate
    20  }
    21  
    22  type PostProcessor struct {
    23  	Driver docker.Driver
    24  
    25  	config Config
    26  }
    27  
    28  func (p *PostProcessor) Configure(raws ...interface{}) error {
    29  	_, err := common.DecodeConfig(&p.config, raws...)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	p.config.tpl, err = packer.NewConfigTemplate()
    35  	if err != nil {
    36  		return err
    37  	}
    38  	p.config.tpl.UserVars = p.config.PackerUserVars
    39  
    40  	// Accumulate any errors
    41  	errs := new(packer.MultiError)
    42  
    43  	templates := map[string]*string{
    44  		"repository": &p.config.Repository,
    45  		"tag":        &p.config.Tag,
    46  	}
    47  
    48  	for key, ptr := range templates {
    49  		if *ptr == "" {
    50  			errs = packer.MultiErrorAppend(
    51  				errs, fmt.Errorf("%s must be set", key))
    52  		}
    53  
    54  		*ptr, err = p.config.tpl.Process(*ptr, nil)
    55  		if err != nil {
    56  			errs = packer.MultiErrorAppend(
    57  				errs, fmt.Errorf("Error processing %s: %s", key, err))
    58  		}
    59  	}
    60  
    61  	if len(errs.Errors) > 0 {
    62  		return errs
    63  	}
    64  
    65  	return nil
    66  
    67  }
    68  
    69  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    70  	if artifact.BuilderId() != dockerimport.BuilderId {
    71  		err := fmt.Errorf(
    72  			"Unknown artifact type: %s\nCan only tag from Docker builder artifacts.",
    73  			artifact.BuilderId())
    74  		return nil, false, err
    75  	}
    76  
    77  	driver := p.Driver
    78  	if driver == nil {
    79  		// If no driver is set, then we use the real driver
    80  		driver = &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
    81  	}
    82  
    83  	importRepo := p.config.Repository
    84  	if p.config.Tag != "" {
    85  		importRepo += ":" + p.config.Tag
    86  	}
    87  
    88  	ui.Message("Tagging image: " + artifact.Id())
    89  	ui.Message("Repository: " + importRepo)
    90  	err := driver.TagImage(artifact.Id(), importRepo)
    91  	if err != nil {
    92  		return nil, false, err
    93  	}
    94  
    95  	// Build the artifact
    96  	artifact = &docker.ImportArtifact{
    97  		BuilderIdValue: BuilderId,
    98  		Driver:         driver,
    99  		IdValue:        importRepo,
   100  	}
   101  
   102  	return artifact, true, nil
   103  }