builder/digitalocean: use text/template for the snapshot name
This commit is contained in:
parent
1e17e90acd
commit
477ac8cd31
|
@ -4,6 +4,7 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
@ -11,12 +12,18 @@ import (
|
|||
"github.com/mitchellh/packer/builder/common"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The unique id for the builder
|
||||
const BuilderId = "pearkes.digitalocean"
|
||||
|
||||
type snapshotNameData struct {
|
||||
CreateTime string
|
||||
}
|
||||
|
||||
// Configuration tells the builder the credentials
|
||||
// to use while communicating with DO and describes the image
|
||||
// you are creating
|
||||
|
@ -27,7 +34,7 @@ type config struct {
|
|||
SizeID uint `mapstructure:"size_id"`
|
||||
ImageID uint `mapstructure:"image_id"`
|
||||
|
||||
SnapshotName string `mapstructure:"snapshot_name"`
|
||||
SnapshotName string
|
||||
SSHUsername string `mapstructure:"ssh_username"`
|
||||
SSHPort uint `mapstructure:"ssh_port"`
|
||||
SSHTimeout time.Duration
|
||||
|
@ -35,8 +42,9 @@ type config struct {
|
|||
|
||||
PackerDebug bool `mapstructure:"packer_debug"`
|
||||
|
||||
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
||||
RawEventDelay string `mapstructure:"event_delay"`
|
||||
RawSnapshotName string `mapstructure:"snapshot_name"`
|
||||
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
||||
RawEventDelay string `mapstructure:"event_delay"`
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
|
@ -80,9 +88,9 @@ func (b *Builder) Prepare(raws ...interface{}) error {
|
|||
b.config.SSHPort = 22
|
||||
}
|
||||
|
||||
if b.config.SnapshotName == "" {
|
||||
if b.config.RawSnapshotName == "" {
|
||||
// Default to packer-{{ unix timestamp (utc) }}
|
||||
b.config.SnapshotName = "packer-{{.CreateTime}}"
|
||||
b.config.RawSnapshotName = "packer-{{.CreateTime}}"
|
||||
}
|
||||
|
||||
if b.config.RawSSHTimeout == "" {
|
||||
|
@ -121,6 +129,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
|
|||
}
|
||||
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 {
|
||||
return &packer.MultiError{errs}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package digitalocean
|
|||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -256,14 +257,35 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) {
|
|||
var b Builder
|
||||
config := testConfig()
|
||||
|
||||
// Test set
|
||||
config["snapshot_name"] = "foo"
|
||||
// Test default
|
||||
err := b.Prepare(config)
|
||||
if err != nil {
|
||||
t.Fatalf("should not have error: %s", err)
|
||||
}
|
||||
|
||||
if b.config.SnapshotName != "foo" {
|
||||
t.Errorf("invalid: %s", b.config.SnapshotName)
|
||||
if b.config.RawSnapshotName != "packer-{{.CreateTime}}" {
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction {
|
|||
c := state["config"].(config)
|
||||
dropletId := state["droplet_id"].(uint)
|
||||
|
||||
ui.Say("Creating snapshot...")
|
||||
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
||||
|
||||
err := client.CreateSnapshot(dropletId, c.SnapshotName)
|
||||
|
||||
|
|
Loading…
Reference in New Issue