github.com/aclaygray/packer@v1.3.2/post-processor/vagrant/aws.go (about)

     1  package vagrant
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"text/template"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type AWSProvider struct{}
    13  
    14  func (p *AWSProvider) KeepInputArtifact() bool {
    15  	return true
    16  }
    17  
    18  func (p *AWSProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
    19  	// Create the metadata
    20  	metadata = map[string]interface{}{"provider": "aws"}
    21  
    22  	// Build up the template data to build our Vagrantfile
    23  	tplData := &awsVagrantfileTemplate{
    24  		Images: make(map[string]string),
    25  	}
    26  
    27  	for _, regions := range strings.Split(artifact.Id(), ",") {
    28  		parts := strings.Split(regions, ":")
    29  		if len(parts) != 2 {
    30  			err = fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id())
    31  			return
    32  		}
    33  
    34  		tplData.Images[parts[0]] = parts[1]
    35  	}
    36  
    37  	// Build up the contents
    38  	var contents bytes.Buffer
    39  	t := template.Must(template.New("vf").Parse(defaultAWSVagrantfile))
    40  	err = t.Execute(&contents, tplData)
    41  	vagrantfile = contents.String()
    42  	return
    43  }
    44  
    45  type awsVagrantfileTemplate struct {
    46  	Images map[string]string
    47  }
    48  
    49  var defaultAWSVagrantfile = `
    50  Vagrant.configure("2") do |config|
    51    config.vm.provider "aws" do |aws|
    52      {{ range $region, $ami := .Images }}
    53  	aws.region_config "{{ $region }}", ami: "{{ $ami }}"
    54  	{{ end }}
    55    end
    56  end
    57  `