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
57f707df
Commit
57f707df
authored
Dec 12, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/googlecompute: delete instance
parent
f72f7a37
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
22 deletions
+70
-22
builder/googlecompute/driver.go
builder/googlecompute/driver.go
+3
-0
builder/googlecompute/driver_gce.go
builder/googlecompute/driver_gce.go
+11
-0
builder/googlecompute/driver_mock.go
builder/googlecompute/driver_mock.go
+19
-0
builder/googlecompute/step_create_instance.go
builder/googlecompute/step_create_instance.go
+22
-21
builder/googlecompute/step_create_instance_test.go
builder/googlecompute/step_create_instance_test.go
+15
-1
No files found.
builder/googlecompute/driver.go
View file @
57f707df
...
...
@@ -4,6 +4,9 @@ package googlecompute
// with GCE. The Driver interface exists mostly to allow a mock implementation
// to be used to test the steps.
type
Driver
interface
{
// DeleteInstance deletes the given instance.
DeleteInstance
(
zone
,
name
string
)
(
<-
chan
error
,
error
)
// RunInstance takes the given config and launches an instance.
RunInstance
(
*
InstanceConfig
)
(
<-
chan
error
,
error
)
}
...
...
builder/googlecompute/driver_gce.go
View file @
57f707df
...
...
@@ -59,6 +59,17 @@ func NewDriverGCE(ui packer.Ui, projectId string, c *clientSecrets, key []byte)
},
nil
}
func
(
d
*
driverGCE
)
DeleteInstance
(
zone
,
name
string
)
(
<-
chan
error
,
error
)
{
op
,
err
:=
d
.
service
.
Instances
.
Delete
(
d
.
projectId
,
zone
,
name
)
.
Do
()
if
err
!=
nil
{
return
nil
,
err
}
errCh
:=
make
(
chan
error
,
1
)
go
waitForState
(
errCh
,
"DONE"
,
d
.
refreshZoneOp
(
zone
,
op
))
return
errCh
,
nil
}
func
(
d
*
driverGCE
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
// Get the zone
d
.
ui
.
Message
(
fmt
.
Sprintf
(
"Loading zone: %s"
,
c
.
Zone
))
...
...
builder/googlecompute/driver_mock.go
View file @
57f707df
...
...
@@ -3,11 +3,30 @@ package googlecompute
// DriverMock is a Driver implementation that is a mocked out so that
// it can be used for tests.
type
DriverMock
struct
{
DeleteInstanceZone
string
DeleteInstanceName
string
DeleteInstanceErrCh
<-
chan
error
DeleteInstanceErr
error
RunInstanceConfig
*
InstanceConfig
RunInstanceErrCh
<-
chan
error
RunInstanceErr
error
}
func
(
d
*
DriverMock
)
DeleteInstance
(
zone
,
name
string
)
(
<-
chan
error
,
error
)
{
d
.
DeleteInstanceZone
=
zone
d
.
DeleteInstanceName
=
name
resultCh
:=
d
.
DeleteInstanceErrCh
if
resultCh
==
nil
{
ch
:=
make
(
chan
error
)
close
(
ch
)
resultCh
=
ch
}
return
resultCh
,
d
.
DeleteInstanceErr
}
func
(
d
*
DriverMock
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
d
.
RunInstanceConfig
=
c
...
...
builder/googlecompute/step_create_instance.go
View file @
57f707df
...
...
@@ -68,27 +68,28 @@ func (s *StepCreateInstance) Cleanup(state multistep.StateBag) {
if
s
.
instanceName
==
""
{
return
}
/*
var (
client = state.Get("client").(*GoogleComputeClient)
config = state.Get("config").(*Config)
ui = state.Get("ui").(packer.Ui)
)
ui.Say("Destroying instance...")
operation, err := client.DeleteInstance(config.Zone, s.instanceName)
if err != nil {
ui.Error(fmt.Sprintf("Error destroying instance. Please destroy it manually: %v", s.instanceName))
}
ui.Say("Waiting for the instance to be deleted...")
for {
status, err := client.ZoneOperationStatus(config.Zone, operation.Name)
if err != nil {
ui.Error(fmt.Sprintf("Error destroying instance. Please destroy it manually: %v", s.instanceName))
}
if status == "DONE" {
break
}
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
Driver
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Deleting instance..."
)
errCh
,
err
:=
driver
.
DeleteInstance
(
config
.
Zone
,
s
.
instanceName
)
if
err
==
nil
{
select
{
case
err
=
<-
errCh
:
case
<-
time
.
After
(
config
.
stateTimeout
)
:
err
=
errors
.
New
(
"time out while waiting for instance to delete"
)
}
*/
}
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error deleting instance. Please delete it manually.
\n\n
"
+
"Name: %s
\n
"
+
"Error: %s"
,
s
.
instanceName
,
err
))
}
s
.
instanceName
=
""
return
}
builder/googlecompute/step_create_instance_test.go
View file @
57f707df
...
...
@@ -18,15 +18,29 @@ func TestStepCreateInstance(t *testing.T) {
state
.
Put
(
"ssh_public_key"
,
"key"
)
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify state
if
_
,
ok
:=
state
.
GetOk
(
"instance_name"
);
!
ok
{
nameRaw
,
ok
:=
state
.
GetOk
(
"instance_name"
)
if
!
ok
{
t
.
Fatal
(
"should have instance name"
)
}
// cleanup
step
.
Cleanup
(
state
)
if
driver
.
DeleteInstanceName
!=
nameRaw
.
(
string
)
{
t
.
Fatal
(
"should've deleted instance"
)
}
if
driver
.
DeleteInstanceZone
!=
config
.
Zone
{
t
.
Fatal
(
"bad zone: %#v"
,
driver
.
DeleteInstanceZone
)
}
}
func
TestStepCreateInstance_error
(
t
*
testing
.
T
)
{
...
...
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