2013-06-26 20:37:46 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-12-19 16:37:09 -05:00
|
|
|
"bytes"
|
2013-06-26 21:55:11 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2013-12-19 16:37:09 -05:00
|
|
|
"text/template"
|
2013-12-19 16:41:48 -05:00
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-06-26 20:37:46 -04:00
|
|
|
)
|
|
|
|
|
2013-12-19 16:37:09 -05:00
|
|
|
type AWSProvider struct{}
|
2013-08-15 15:09:22 -04:00
|
|
|
|
2014-02-21 23:02:40 -05:00
|
|
|
func (p *AWSProvider) KeepInputArtifact() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (p *AWSProvider) Process(ui packersdk.Ui, artifact packersdk.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
2013-12-19 16:37:09 -05:00
|
|
|
// Create the metadata
|
|
|
|
metadata = map[string]interface{}{"provider": "aws"}
|
2013-06-26 20:37:46 -04:00
|
|
|
|
2013-12-19 16:37:09 -05:00
|
|
|
// Build up the template data to build our Vagrantfile
|
|
|
|
tplData := &awsVagrantfileTemplate{
|
2013-06-26 21:55:11 -04:00
|
|
|
Images: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, regions := range strings.Split(artifact.Id(), ",") {
|
|
|
|
parts := strings.Split(regions, ":")
|
|
|
|
if len(parts) != 2 {
|
2013-12-19 16:37:09 -05:00
|
|
|
err = fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id())
|
|
|
|
return
|
2013-06-26 21:55:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tplData.Images[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:37:09 -05:00
|
|
|
// 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
|
|
|
|
}
|
2013-06-26 21:55:11 -04:00
|
|
|
|
2013-12-19 16:37:09 -05:00
|
|
|
type awsVagrantfileTemplate struct {
|
|
|
|
Images map[string]string
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
2013-06-26 21:55:11 -04:00
|
|
|
|
2013-06-27 10:40:33 -04:00
|
|
|
var defaultAWSVagrantfile = `
|
2013-06-26 21:55:11 -04:00
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
config.vm.provider "aws" do |aws|
|
|
|
|
{{ range $region, $ami := .Images }}
|
|
|
|
aws.region_config "{{ $region }}", ami: "{{ $ami }}"
|
|
|
|
{{ end }}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
`
|