github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/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  	Driver docker.Driver
    20  
    21  	config Config
    22  }
    23  
    24  func (p *PostProcessor) Configure(raws ...interface{}) error {
    25  	_, err := common.DecodeConfig(&p.config, raws...)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	p.config.tpl, err = packer.NewConfigTemplate()
    31  	if err != nil {
    32  		return err
    33  	}
    34  	p.config.tpl.UserVars = p.config.PackerUserVars
    35  
    36  	// Accumulate any errors
    37  	errs := new(packer.MultiError)
    38  	if len(errs.Errors) > 0 {
    39  		return errs
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    46  	if artifact.BuilderId() != dockerimport.BuilderId {
    47  		err := fmt.Errorf(
    48  			"Unknown artifact type: %s\nCan only import from docker-import artifacts.",
    49  			artifact.BuilderId())
    50  		return nil, false, err
    51  	}
    52  
    53  	driver := p.Driver
    54  	if driver == nil {
    55  		// If no driver is set, then we use the real driver
    56  		driver = &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
    57  	}
    58  
    59  	// Get the name. We strip off any tags from the name because the
    60  	// push doesn't use those.
    61  	name := artifact.Id()
    62  
    63  	if i := strings.Index(name, "/"); i >= 0 {
    64  		// This should always be true because the / is required. But we have
    65  		// to get the index to this so we don't accidentally strip off the port
    66  		if j := strings.Index(name[i:], ":"); j >= 0 {
    67  			name = name[:i+j]
    68  		}
    69  	}
    70  
    71  	ui.Message("Pushing: " + name)
    72  	if err := driver.Push(name); err != nil {
    73  		return nil, false, err
    74  	}
    75  
    76  	return nil, false, nil
    77  }