github.com/sneal/packer@v0.5.2/post-processor/docker-push/post-processor.go (about)

     1  package dockerpush
     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  	"strings"
    10  )
    11  
    12  type Config struct {
    13  	common.PackerConfig `mapstructure:",squash"`
    14  
    15  	tpl *packer.ConfigTemplate
    16  }
    17  
    18  type PostProcessor struct {
    19  	config Config
    20  }
    21  
    22  func (p *PostProcessor) Configure(raws ...interface{}) error {
    23  	_, err := common.DecodeConfig(&p.config, raws...)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	p.config.tpl, err = packer.NewConfigTemplate()
    29  	if err != nil {
    30  		return err
    31  	}
    32  	p.config.tpl.UserVars = p.config.PackerUserVars
    33  
    34  	// Accumulate any errors
    35  	errs := new(packer.MultiError)
    36  	if len(errs.Errors) > 0 {
    37  		return errs
    38  	}
    39  
    40  	return nil
    41  
    42  }
    43  
    44  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    45  	if artifact.BuilderId() != dockerimport.BuilderId {
    46  		err := fmt.Errorf(
    47  			"Unknown artifact type: %s\nCan only import from docker-import artifacts.",
    48  			artifact.BuilderId())
    49  		return nil, false, err
    50  	}
    51  
    52  	driver := &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
    53  
    54  	// Get the name. We strip off any tags from the name because the
    55  	// push doesn't use those.
    56  	name := artifact.Id()
    57  	if i := strings.Index(name, ":"); i >= 0 {
    58  		name = name[:i]
    59  	}
    60  
    61  	ui.Message("Pushing: " + name)
    62  	if err := driver.Push(name); err != nil {
    63  		return nil, false, err
    64  	}
    65  
    66  	return nil, false, nil
    67  }