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

54 lines
1.3 KiB
Go
Raw Normal View History

package vagrant
import (
2013-12-19 16:41:48 -05:00
"bytes"
"fmt"
"strings"
2013-12-19 16:41:48 -05:00
"text/template"
2018-01-22 20:21:10 -05:00
2020-12-17 16:29:25 -05:00
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
2013-12-19 16:41:48 -05:00
type digitalOceanVagrantfileTemplate struct {
Image string ""
Region string ""
}
2013-12-19 16:41:48 -05:00
type DigitalOceanProvider struct{}
func (p *DigitalOceanProvider) KeepInputArtifact() bool {
return true
}
func (p *DigitalOceanProvider) Process(ui packersdk.Ui, artifact packersdk.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
2013-12-19 16:41:48 -05:00
// Create the metadata
metadata = map[string]interface{}{"provider": "digital_ocean"}
// Determine the image and region...
2013-12-19 16:41:48 -05:00
tplData := &digitalOceanVagrantfileTemplate{}
parts := strings.Split(artifact.Id(), ":")
if len(parts) != 2 {
2013-12-19 16:41:48 -05:00
err = fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id())
return
}
tplData.Region = parts[0]
tplData.Image = parts[1]
2013-12-19 16:41:48 -05:00
// Build up the Vagrantfile
var contents bytes.Buffer
t := template.Must(template.New("vf").Parse(defaultDigitalOceanVagrantfile))
err = t.Execute(&contents, tplData)
vagrantfile = contents.String()
return
}
var defaultDigitalOceanVagrantfile = `
Vagrant.configure("2") do |config|
config.vm.provider :digital_ocean do |digital_ocean|
digital_ocean.image = "{{ .Image }}"
digital_ocean.region = "{{ .Region }}"
end
end
`