Commit 477ac8cd authored by Jack Pearkes's avatar Jack Pearkes

builder/digitalocean: use text/template for the snapshot name

parent 1e17e90a
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package digitalocean package digitalocean
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
...@@ -11,12 +12,18 @@ import ( ...@@ -11,12 +12,18 @@ import (
"github.com/mitchellh/packer/builder/common" "github.com/mitchellh/packer/builder/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"strconv"
"text/template"
"time" "time"
) )
// The unique id for the builder // The unique id for the builder
const BuilderId = "pearkes.digitalocean" const BuilderId = "pearkes.digitalocean"
type snapshotNameData struct {
CreateTime string
}
// Configuration tells the builder the credentials // Configuration tells the builder the credentials
// to use while communicating with DO and describes the image // to use while communicating with DO and describes the image
// you are creating // you are creating
...@@ -27,7 +34,7 @@ type config struct { ...@@ -27,7 +34,7 @@ type config struct {
SizeID uint `mapstructure:"size_id"` SizeID uint `mapstructure:"size_id"`
ImageID uint `mapstructure:"image_id"` ImageID uint `mapstructure:"image_id"`
SnapshotName string `mapstructure:"snapshot_name"` SnapshotName string
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPort uint `mapstructure:"ssh_port"` SSHPort uint `mapstructure:"ssh_port"`
SSHTimeout time.Duration SSHTimeout time.Duration
...@@ -35,8 +42,9 @@ type config struct { ...@@ -35,8 +42,9 @@ type config struct {
PackerDebug bool `mapstructure:"packer_debug"` PackerDebug bool `mapstructure:"packer_debug"`
RawSSHTimeout string `mapstructure:"ssh_timeout"` RawSnapshotName string `mapstructure:"snapshot_name"`
RawEventDelay string `mapstructure:"event_delay"` RawSSHTimeout string `mapstructure:"ssh_timeout"`
RawEventDelay string `mapstructure:"event_delay"`
} }
type Builder struct { type Builder struct {
...@@ -80,9 +88,9 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -80,9 +88,9 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.SSHPort = 22 b.config.SSHPort = 22
} }
if b.config.SnapshotName == "" { if b.config.RawSnapshotName == "" {
// Default to packer-{{ unix timestamp (utc) }} // Default to packer-{{ unix timestamp (utc) }}
b.config.SnapshotName = "packer-{{.CreateTime}}" b.config.RawSnapshotName = "packer-{{.CreateTime}}"
} }
if b.config.RawSSHTimeout == "" { if b.config.RawSSHTimeout == "" {
...@@ -121,6 +129,15 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -121,6 +129,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
} }
b.config.EventDelay = delay b.config.EventDelay = delay
// Parse the name of the snapshot
snapNameBuf := new(bytes.Buffer)
tData := snapshotNameData{
strconv.FormatInt(time.Now().UTC().Unix(), 10),
}
t := template.Must(template.New("snapshot").Parse(b.config.RawSnapshotName))
t.Execute(snapNameBuf, tData)
b.config.SnapshotName = snapNameBuf.String()
if len(errs) > 0 { if len(errs) > 0 {
return &packer.MultiError{errs} return &packer.MultiError{errs}
} }
......
...@@ -2,6 +2,7 @@ package digitalocean ...@@ -2,6 +2,7 @@ package digitalocean
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"strconv"
"testing" "testing"
) )
...@@ -256,14 +257,35 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) { ...@@ -256,14 +257,35 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
// Test set // Test default
config["snapshot_name"] = "foo"
err := b.Prepare(config) err := b.Prepare(config)
if err != nil { if err != nil {
t.Fatalf("should not have error: %s", err) t.Fatalf("should not have error: %s", err)
} }
if b.config.SnapshotName != "foo" { if b.config.RawSnapshotName != "packer-{{.CreateTime}}" {
t.Errorf("invalid: %s", b.config.SnapshotName) t.Errorf("invalid: %d", b.config.RawSnapshotName)
}
// Test set
config["snapshot_name"] = "foobarbaz"
b = Builder{}
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
} }
// Test set with template
config["snapshot_name"] = "{{.CreateTime}}"
b = Builder{}
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
_, err = strconv.ParseInt(b.config.SnapshotName, 0, 0)
if err != nil {
t.Fatalf("failed to parse int in template: %s", err)
}
} }
package digitalocean package digitalocean
import ( import (
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
...@@ -13,7 +14,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction { ...@@ -13,7 +14,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction {
c := state["config"].(config) c := state["config"].(config)
dropletId := state["droplet_id"].(uint) dropletId := state["droplet_id"].(uint)
ui.Say("Creating snapshot...") ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
err := client.CreateSnapshot(dropletId, c.SnapshotName) err := client.CreateSnapshot(dropletId, c.SnapshotName)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment