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
94b76036
Commit
94b76036
authored
Aug 31, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/digitalocean: new multistep API
parent
0b830c92
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
73 additions
and
72 deletions
+73
-72
builder/digitalocean/builder.go
builder/digitalocean/builder.go
+9
-9
builder/digitalocean/ssh.go
builder/digitalocean/ssh.go
+7
-6
builder/digitalocean/step_create_droplet.go
builder/digitalocean/step_create_droplet.go
+11
-11
builder/digitalocean/step_create_ssh_key.go
builder/digitalocean/step_create_ssh_key.go
+10
-10
builder/digitalocean/step_droplet_info.go
builder/digitalocean/step_droplet_info.go
+9
-9
builder/digitalocean/step_power_off.go
builder/digitalocean/step_power_off.go
+7
-7
builder/digitalocean/step_shutdown.go
builder/digitalocean/step_shutdown.go
+8
-8
builder/digitalocean/step_snapshot.go
builder/digitalocean/step_snapshot.go
+12
-12
No files found.
builder/digitalocean/builder.go
View file @
94b76036
...
...
@@ -189,11 +189,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
client
:=
DigitalOceanClient
{}
.
New
(
b
.
config
.
ClientID
,
b
.
config
.
APIKey
)
// Set up the state
state
:=
make
(
map
[
string
]
interface
{}
)
state
[
"config"
]
=
b
.
config
state
[
"client"
]
=
client
state
[
"hook"
]
=
hook
state
[
"ui"
]
=
ui
state
:=
new
(
multistep
.
BasicStateBag
)
state
.
Put
(
"config"
,
b
.
config
)
state
.
Put
(
"client"
,
client
)
state
.
Put
(
"hook"
,
hook
)
state
.
Put
(
"ui"
,
ui
)
// Build the steps
steps
:=
[]
multistep
.
Step
{
...
...
@@ -224,18 +224,18 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b
.
runner
.
Run
(
state
)
// If there was an error, return that
if
rawErr
,
ok
:=
state
[
"error"
]
;
ok
{
if
rawErr
,
ok
:=
state
.
GetOk
(
"error"
)
;
ok
{
return
nil
,
rawErr
.
(
error
)
}
if
_
,
ok
:=
state
[
"snapshot_name"
]
;
!
ok
{
if
_
,
ok
:=
state
.
GetOk
(
"snapshot_name"
)
;
!
ok
{
log
.
Println
(
"Failed to find snapshot_name in state. Bug?"
)
return
nil
,
nil
}
artifact
:=
&
Artifact
{
snapshotName
:
state
[
"snapshot_name"
]
.
(
string
),
snapshotId
:
state
[
"snapshot_image_id"
]
.
(
uint
),
snapshotName
:
state
.
Get
(
"snapshot_name"
)
.
(
string
),
snapshotId
:
state
.
Get
(
"snapshot_image_id"
)
.
(
uint
),
client
:
client
,
}
...
...
builder/digitalocean/ssh.go
View file @
94b76036
...
...
@@ -3,18 +3,19 @@ package digitalocean
import
(
gossh
"code.google.com/p/go.crypto/ssh"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/communicator/ssh"
)
func
sshAddress
(
state
m
ap
[
string
]
interface
{}
)
(
string
,
error
)
{
config
:=
state
[
"config"
]
.
(
config
)
ipAddress
:=
state
[
"droplet_ip"
]
.
(
string
)
func
sshAddress
(
state
m
ultistep
.
StateBag
)
(
string
,
error
)
{
config
:=
state
.
Get
(
"config"
)
.
(
config
)
ipAddress
:=
state
.
Get
(
"droplet_ip"
)
.
(
string
)
return
fmt
.
Sprintf
(
"%s:%d"
,
ipAddress
,
config
.
SSHPort
),
nil
}
func
sshConfig
(
state
m
ap
[
string
]
interface
{}
)
(
*
gossh
.
ClientConfig
,
error
)
{
config
:=
state
[
"config"
]
.
(
config
)
privateKey
:=
state
[
"privateKey"
]
.
(
string
)
func
sshConfig
(
state
m
ultistep
.
StateBag
)
(
*
gossh
.
ClientConfig
,
error
)
{
config
:=
state
.
Get
(
"config"
)
.
(
config
)
privateKey
:=
state
.
Get
(
"privateKey"
)
.
(
string
)
keyring
:=
new
(
ssh
.
SimpleKeychain
)
if
err
:=
keyring
.
AddPEMKey
(
privateKey
);
err
!=
nil
{
...
...
builder/digitalocean/step_create_droplet.go
View file @
94b76036
...
...
@@ -14,11 +14,11 @@ type stepCreateDroplet struct {
dropletId
uint
}
func
(
s
*
stepCreateDroplet
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
c
:=
state
[
"config"
]
.
(
config
)
sshKeyId
:=
state
[
"ssh_key_id"
]
.
(
uint
)
func
(
s
*
stepCreateDroplet
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
sshKeyId
:=
state
.
Get
(
"ssh_key_id"
)
.
(
uint
)
ui
.
Say
(
"Creating droplet..."
)
...
...
@@ -29,7 +29,7 @@ func (s *stepCreateDroplet) Run(state map[string]interface{}) multistep.StepActi
dropletId
,
err
:=
client
.
CreateDroplet
(
name
,
c
.
SizeID
,
c
.
ImageID
,
c
.
RegionID
,
sshKeyId
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating droplet: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -38,20 +38,20 @@ func (s *stepCreateDroplet) Run(state map[string]interface{}) multistep.StepActi
s
.
dropletId
=
dropletId
// Store the droplet id for later
state
[
"droplet_id"
]
=
dropletId
state
.
Put
(
"droplet_id"
,
dropletId
)
return
multistep
.
ActionContinue
}
func
(
s
*
stepCreateDroplet
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepCreateDroplet
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// If the dropletid isn't there, we probably never created it
if
s
.
dropletId
==
0
{
return
}
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
c
:=
state
[
"config"
]
.
(
config
)
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
// Destroy the droplet we just created
ui
.
Say
(
"Destroying droplet..."
)
...
...
builder/digitalocean/step_create_ssh_key.go
View file @
94b76036
...
...
@@ -18,9 +18,9 @@ type stepCreateSSHKey struct {
keyId
uint
}
func
(
s
*
stepCreateSSHKey
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
func
(
s
*
stepCreateSSHKey
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Creating temporary ssh key for droplet..."
)
...
...
@@ -35,7 +35,7 @@ func (s *stepCreateSSHKey) Run(state map[string]interface{}) multistep.StepActio
}
// Set the private key in the statebag for later
state
[
"privateKey"
]
=
string
(
pem
.
EncodeToMemory
(
&
priv_blk
))
state
.
Put
(
"privateKey"
,
string
(
pem
.
EncodeToMemory
(
&
priv_blk
)
))
// Marshal the public key into SSH compatible format
pub
:=
priv
.
PublicKey
...
...
@@ -48,7 +48,7 @@ func (s *stepCreateSSHKey) Run(state map[string]interface{}) multistep.StepActio
keyId
,
err
:=
client
.
CreateKey
(
name
,
pub_sshformat
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating temporary SSH key: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -59,20 +59,20 @@ func (s *stepCreateSSHKey) Run(state map[string]interface{}) multistep.StepActio
log
.
Printf
(
"temporary ssh key name: %s"
,
name
)
// Remember some state for the future
state
[
"ssh_key_id"
]
=
keyId
state
.
Put
(
"ssh_key_id"
,
keyId
)
return
multistep
.
ActionContinue
}
func
(
s
*
stepCreateSSHKey
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepCreateSSHKey
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// If no key name is set, then we never created it, so just return
if
s
.
keyId
==
0
{
return
}
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
c
:=
state
[
"config"
]
.
(
config
)
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
ui
.
Say
(
"Deleting temporary ssh key..."
)
err
:=
client
.
DestroyKey
(
s
.
keyId
)
...
...
builder/digitalocean/step_droplet_info.go
View file @
94b76036
...
...
@@ -8,18 +8,18 @@ import (
type
stepDropletInfo
struct
{}
func
(
s
*
stepDropletInfo
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
c
:=
state
[
"config"
]
.
(
config
)
dropletId
:=
state
[
"droplet_id"
]
.
(
uint
)
func
(
s
*
stepDropletInfo
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
dropletId
:=
state
.
Get
(
"droplet_id"
)
.
(
uint
)
ui
.
Say
(
"Waiting for droplet to become active..."
)
err
:=
waitForDropletState
(
"active"
,
dropletId
,
client
,
c
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error waiting for droplet to become active: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -28,16 +28,16 @@ func (s *stepDropletInfo) Run(state map[string]interface{}) multistep.StepAction
ip
,
_
,
err
:=
client
.
DropletStatus
(
dropletId
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error retrieving droplet ID: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
state
[
"droplet_ip"
]
=
ip
state
.
Put
(
"droplet_ip"
,
ip
)
return
multistep
.
ActionContinue
}
func
(
s
*
stepDropletInfo
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepDropletInfo
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// no cleanup
}
builder/digitalocean/step_power_off.go
View file @
94b76036
...
...
@@ -10,11 +10,11 @@ import (
type
stepPowerOff
struct
{}
func
(
s
*
stepPowerOff
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
c
:=
state
[
"config"
]
.
(
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
dropletId
:=
state
[
"droplet_id"
]
.
(
uint
)
func
(
s
*
stepPowerOff
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
dropletId
:=
state
.
Get
(
"droplet_id"
)
.
(
uint
)
// Sleep arbitrarily before sending power off request
// Otherwise we get "pending event" errors, even though there isn't
...
...
@@ -27,7 +27,7 @@ func (s *stepPowerOff) Run(state map[string]interface{}) multistep.StepAction {
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error powering off droplet: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -44,6 +44,6 @@ func (s *stepPowerOff) Run(state map[string]interface{}) multistep.StepAction {
return
multistep
.
ActionContinue
}
func
(
s
*
stepPowerOff
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepPowerOff
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// no cleanup
}
builder/digitalocean/step_shutdown.go
View file @
94b76036
...
...
@@ -10,11 +10,11 @@ import (
type
stepShutdown
struct
{}
func
(
s
*
stepShutdown
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
c
:=
state
[
"config"
]
.
(
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
dropletId
:=
state
[
"droplet_id"
]
.
(
uint
)
func
(
s
*
stepShutdown
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
dropletId
:=
state
.
Get
(
"droplet_id"
)
.
(
uint
)
// Sleep arbitrarily before sending the request
// Otherwise we get "pending event" errors, even though there isn't
...
...
@@ -26,7 +26,7 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error shutting down droplet: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -36,7 +36,7 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
err
=
waitForDropletState
(
"off"
,
dropletId
,
client
,
c
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error waiting for droplet to become 'off': %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -44,6 +44,6 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
return
multistep
.
ActionContinue
}
func
(
s
*
stepShutdown
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepShutdown
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// no cleanup
}
builder/digitalocean/step_snapshot.go
View file @
94b76036
...
...
@@ -10,17 +10,17 @@ import (
type
stepSnapshot
struct
{}
func
(
s
*
stepSnapshot
)
Run
(
state
m
ap
[
string
]
interface
{}
)
multistep
.
StepAction
{
client
:=
state
[
"client"
]
.
(
*
DigitalOceanClient
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
c
:=
state
[
"config"
]
.
(
config
)
dropletId
:=
state
[
"droplet_id"
]
.
(
uint
)
func
(
s
*
stepSnapshot
)
Run
(
state
m
ultistep
.
StateBag
)
multistep
.
StepAction
{
client
:=
state
.
Get
(
"client"
)
.
(
*
DigitalOceanClient
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
c
:=
state
.
Get
(
"config"
)
.
(
config
)
dropletId
:=
state
.
Get
(
"droplet_id"
)
.
(
uint
)
ui
.
Say
(
fmt
.
Sprintf
(
"Creating snapshot: %v"
,
c
.
SnapshotName
))
err
:=
client
.
CreateSnapshot
(
dropletId
,
c
.
SnapshotName
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating snapshot: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -29,7 +29,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction {
err
=
waitForDropletState
(
"active"
,
dropletId
,
client
,
c
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error waiting for snapshot to complete: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -38,7 +38,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction {
images
,
err
:=
client
.
Images
()
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error looking up snapshot ID: %s"
,
err
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
...
...
@@ -53,19 +53,19 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction {
if
imageId
==
0
{
err
:=
errors
.
New
(
"Couldn't find snapshot to get the image ID. Bug?"
)
state
[
"error"
]
=
err
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
log
.
Printf
(
"Snapshot image ID: %d"
,
imageId
)
state
[
"snapshot_image_id"
]
=
imageId
state
[
"snapshot_name"
]
=
c
.
SnapshotName
state
.
Put
(
"snapshot_image_id"
,
imageId
)
state
.
Put
(
"snapshot_name"
,
c
.
SnapshotName
)
return
multistep
.
ActionContinue
}
func
(
s
*
stepSnapshot
)
Cleanup
(
state
m
ap
[
string
]
interface
{}
)
{
func
(
s
*
stepSnapshot
)
Cleanup
(
state
m
ultistep
.
StateBag
)
{
// no cleanup
}
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