Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
477ac8cd
Commit
477ac8cd
authored
Jun 17, 2013
by
Jack Pearkes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/digitalocean: use text/template for the snapshot name
parent
1e17e90a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
10 deletions
+50
-10
builder/digitalocean/builder.go
builder/digitalocean/builder.go
+22
-5
builder/digitalocean/builder_test.go
builder/digitalocean/builder_test.go
+26
-4
builder/digitalocean/step_snapshot.go
builder/digitalocean/step_snapshot.go
+2
-1
No files found.
builder/digitalocean/builder.go
View file @
477ac8cd
...
...
@@ -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,6 +42,7 @@ type config struct {
PackerDebug
bool
`mapstructure:"packer_debug"`
RawSnapshotName
string
`mapstructure:"snapshot_name"`
RawSSHTimeout
string
`mapstructure:"ssh_timeout"`
RawEventDelay
string
`mapstructure:"event_delay"`
}
...
...
@@ -80,9 +88,9 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b
.
config
.
SSHPort
=
22
}
if
b
.
config
.
SnapshotName
==
""
{
if
b
.
config
.
Raw
SnapshotName
==
""
{
// Default to packer-{{ unix timestamp (utc) }}
b
.
config
.
SnapshotName
=
"packer-{{.CreateTime}}"
b
.
config
.
Raw
SnapshotName
=
"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
}
}
...
...
builder/digitalocean/builder_test.go
View file @
477ac8cd
...
...
@@ -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
)
}
}
builder/digitalocean/step_snapshot.go
View file @
477ac8cd
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment