builder/digitalocean: use new template processing

This commit is contained in:
Mitchell Hashimoto 2013-08-08 15:42:19 -07:00
parent 1268373105
commit 66818ef89f
2 changed files with 35 additions and 31 deletions

View File

@ -4,7 +4,6 @@
package digitalocean package digitalocean
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -12,18 +11,12 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"os" "os"
"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
@ -36,11 +29,10 @@ 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 SnapshotName string `mapstructure:"snapshot_name"`
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPort uint `mapstructure:"ssh_port"` SSHPort uint `mapstructure:"ssh_port"`
RawSnapshotName string `mapstructure:"snapshot_name"`
RawSSHTimeout string `mapstructure:"ssh_timeout"` RawSSHTimeout string `mapstructure:"ssh_timeout"`
RawEventDelay string `mapstructure:"event_delay"` RawEventDelay string `mapstructure:"event_delay"`
RawStateTimeout string `mapstructure:"state_timeout"` RawStateTimeout string `mapstructure:"state_timeout"`
@ -50,6 +42,8 @@ type config struct {
sshTimeout time.Duration sshTimeout time.Duration
eventDelay time.Duration eventDelay time.Duration
stateTimeout time.Duration stateTimeout time.Duration
tpl *common.Template
} }
type Builder struct { type Builder struct {
@ -63,6 +57,11 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return err return err
} }
b.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
@ -92,6 +91,11 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.ImageID = 284203 b.config.ImageID = 284203
} }
if b.config.SnapshotName == "" {
// Default to packer-{{ unix timestamp (utc) }}
b.config.SnapshotName = "packer-{{timestamp}}"
}
if b.config.SSHUsername == "" { if b.config.SSHUsername == "" {
// Default to "root". You can override this if your // Default to "root". You can override this if your
// SourceImage has a different user account then the DO default // SourceImage has a different user account then the DO default
@ -103,11 +107,6 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.SSHPort = 22 b.config.SSHPort = 22
} }
if b.config.RawSnapshotName == "" {
// Default to packer-{{ unix timestamp (utc) }}
b.config.RawSnapshotName = "packer-{{.CreateTime}}"
}
if b.config.RawSSHTimeout == "" { if b.config.RawSSHTimeout == "" {
// Default to 1 minute timeouts // Default to 1 minute timeouts
b.config.RawSSHTimeout = "1m" b.config.RawSSHTimeout = "1m"
@ -125,6 +124,25 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.RawStateTimeout = "6m" b.config.RawStateTimeout = "6m"
} }
templates := map[string]*string{
"client_id": &b.config.ClientID,
"api_key": &b.config.APIKey,
"snapshot_name": &b.config.SnapshotName,
"ssh_username": &b.config.SSHUsername,
"ssh_timeout": &b.config.RawSSHTimeout,
"event_delay": &b.config.RawEventDelay,
"state_timeout": &b.config.RawStateTimeout,
}
for n, ptr := range templates {
var err error
*ptr, err = b.config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
// Required configurations that will display errors if not set // Required configurations that will display errors if not set
if b.config.ClientID == "" { if b.config.ClientID == "" {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
@ -157,20 +175,6 @@ func (b *Builder) Prepare(raws ...interface{}) error {
} }
b.config.stateTimeout = stateTimeout b.config.stateTimeout = stateTimeout
// Parse the name of the snapshot
snapNameBuf := new(bytes.Buffer)
tData := snapshotNameData{
strconv.FormatInt(time.Now().UTC().Unix(), 10),
}
t, err := template.New("snapshot").Parse(b.config.RawSnapshotName)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Failed parsing snapshot_name: %s", err))
} else {
t.Execute(snapNameBuf, tData)
b.config.SnapshotName = snapNameBuf.String()
}
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return errs return errs
} }

View File

@ -332,8 +332,8 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) {
t.Fatalf("should not have error: %s", err) t.Fatalf("should not have error: %s", err)
} }
if b.config.RawSnapshotName != "packer-{{.CreateTime}}" { if b.config.SnapshotName == "" {
t.Errorf("invalid: %d", b.config.RawSnapshotName) t.Errorf("invalid: %s", b.config.SnapshotName)
} }
// Test set // Test set
@ -345,7 +345,7 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) {
} }
// Test set with template // Test set with template
config["snapshot_name"] = "{{.CreateTime}}" config["snapshot_name"] = "{{timestamp}}"
b = Builder{} b = Builder{}
err = b.Prepare(config) err = b.Prepare(config)
if err != nil { if err != nil {