packer-cn/post-processor/vagrant/aws.go

58 lines
1.3 KiB
Go
Raw Normal View History

2013-06-26 20:37:46 -04:00
package vagrant
import (
"bytes"
"fmt"
"strings"
"text/template"
2013-12-19 16:41:48 -05:00
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2013-06-26 20:37:46 -04:00
)
type AWSProvider struct{}
func (p *AWSProvider) KeepInputArtifact() bool {
return true
}
func (p *AWSProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
// Create the metadata
metadata = map[string]interface{}{"provider": "aws"}
2013-06-26 20:37:46 -04:00
// Build up the template data to build our Vagrantfile
tplData := &awsVagrantfileTemplate{
Images: make(map[string]string),
}
for _, regions := range strings.Split(artifact.Id(), ",") {
parts := strings.Split(regions, ":")
if len(parts) != 2 {
err = fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id())
return
}
tplData.Images[parts[0]] = parts[1]
}
// Build up the contents
var contents bytes.Buffer
t := template.Must(template.New("vf").Parse(defaultAWSVagrantfile))
err = t.Execute(&contents, tplData)
vagrantfile = contents.String()
return
}
type awsVagrantfileTemplate struct {
Images map[string]string
2013-06-26 20:37:46 -04:00
}
var defaultAWSVagrantfile = `
Vagrant.configure("2") do |config|
config.vm.provider "aws" do |aws|
{{ range $region, $ami := .Images }}
aws.region_config "{{ $region }}", ami: "{{ $ami }}"
{{ end }}
end
end
`