-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
digitalocean.go
52 lines (43 loc) · 1.25 KB
/
digitalocean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package vagrant
import (
"bytes"
"fmt"
"github.com/mitchellh/packer/packer"
"strings"
"text/template"
)
type digitalOceanVagrantfileTemplate struct {
Image string ""
Region string ""
}
type DigitalOceanProvider struct{}
func (p *DigitalOceanProvider) KeepInputArtifact() bool {
return true
}
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"}
// Determine the image and region...
tplData := &digitalOceanVagrantfileTemplate{}
parts := strings.Split(artifact.Id(), ":")
if len(parts) != 2 {
err = fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id())
return
}
tplData.Region = parts[0]
tplData.Image = parts[1]
// 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
`