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