2013-10-05 22:29:02 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-12-19 16:41:48 -05:00
|
|
|
"bytes"
|
2013-10-05 22:29:02 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"strings"
|
2013-12-19 16:41:48 -05:00
|
|
|
"text/template"
|
2013-10-05 22:29:02 -04:00
|
|
|
)
|
|
|
|
|
2013-12-19 16:41:48 -05:00
|
|
|
type digitalOceanVagrantfileTemplate struct {
|
2013-10-05 22:29:02 -04:00
|
|
|
Image string ""
|
|
|
|
Region string ""
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:41:48 -05:00
|
|
|
type DigitalOceanProvider struct{}
|
2013-10-05 22:29:02 -04:00
|
|
|
|
2013-12-19 16:41:48 -05:00
|
|
|
func (p *DigitalOceanProvider) 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": "digital_ocean"}
|
2013-10-05 22:29:02 -04:00
|
|
|
|
|
|
|
// Determine the image and region...
|
2013-12-19 16:41:48 -05:00
|
|
|
tplData := &digitalOceanVagrantfileTemplate{}
|
2013-10-05 22:29:02 -04:00
|
|
|
|
|
|
|
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
|
2013-10-05 22:29:02 -04:00
|
|
|
}
|
|
|
|
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
|
2013-10-05 22:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
`
|