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

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