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
7c672289
Commit
7c672289
authored
Dec 09, 2014
by
Dustin Carlino
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check if image already exists before doing anything else on GCE.
This fixes #1729.
parent
20d040dc
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
1 deletion
+86
-1
builder/googlecompute/builder.go
builder/googlecompute/builder.go
+1
-0
builder/googlecompute/driver.go
builder/googlecompute/driver.go
+4
-0
builder/googlecompute/driver_gce.go
builder/googlecompute/driver_gce.go
+8
-1
builder/googlecompute/driver_mock.go
builder/googlecompute/driver_mock.go
+8
-0
builder/googlecompute/step_check_existing_image.go
builder/googlecompute/step_check_existing_image.go
+33
-0
builder/googlecompute/step_check_existing_image_test.go
builder/googlecompute/step_check_existing_image_test.go
+32
-0
No files found.
builder/googlecompute/builder.go
View file @
7c672289
...
@@ -49,6 +49,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
...
@@ -49,6 +49,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps.
// Build the steps.
steps
:=
[]
multistep
.
Step
{
steps
:=
[]
multistep
.
Step
{
new
(
StepCheckExistingImage
),
&
StepCreateSSHKey
{
&
StepCreateSSHKey
{
Debug
:
b
.
config
.
PackerDebug
,
Debug
:
b
.
config
.
PackerDebug
,
DebugKeyPath
:
fmt
.
Sprintf
(
"gce_%s.pem"
,
b
.
config
.
PackerBuildName
),
DebugKeyPath
:
fmt
.
Sprintf
(
"gce_%s.pem"
,
b
.
config
.
PackerBuildName
),
...
...
builder/googlecompute/driver.go
View file @
7c672289
...
@@ -4,6 +4,10 @@ package googlecompute
...
@@ -4,6 +4,10 @@ package googlecompute
// with GCE. The Driver interface exists mostly to allow a mock implementation
// with GCE. The Driver interface exists mostly to allow a mock implementation
// to be used to test the steps.
// to be used to test the steps.
type
Driver
interface
{
type
Driver
interface
{
// ImageExists returns true if the specified image exists. If an error
// occurs calling the API, this method returns false.
ImageExists
(
name
string
)
bool
// CreateImage creates an image from the given disk in Google Compute
// CreateImage creates an image from the given disk in Google Compute
// Engine.
// Engine.
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
...
...
builder/googlecompute/driver_gce.go
View file @
7c672289
...
@@ -7,9 +7,9 @@ import (
...
@@ -7,9 +7,9 @@ import (
"time"
"time"
"code.google.com/p/google-api-go-client/compute/v1"
"code.google.com/p/google-api-go-client/compute/v1"
"github.com/mitchellh/packer/packer"
"golang.org/x/oauth2"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/google"
"github.com/mitchellh/packer/packer"
)
)
// driverGCE is a Driver implementation that actually talks to GCE.
// driverGCE is a Driver implementation that actually talks to GCE.
...
@@ -60,6 +60,13 @@ func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) {
...
@@ -60,6 +60,13 @@ func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) {
},
nil
},
nil
}
}
func
(
d
*
driverGCE
)
ImageExists
(
name
string
)
bool
{
_
,
err
:=
d
.
service
.
Images
.
Get
(
d
.
projectId
,
name
)
.
Do
()
// The API may return an error for reasons other than the image not
// existing, but this heuristic is sufficient for now.
return
err
==
nil
}
func
(
d
*
driverGCE
)
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
{
func
(
d
*
driverGCE
)
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
{
image
:=
&
compute
.
Image
{
image
:=
&
compute
.
Image
{
Description
:
description
,
Description
:
description
,
...
...
builder/googlecompute/driver_mock.go
View file @
7c672289
...
@@ -3,6 +3,9 @@ package googlecompute
...
@@ -3,6 +3,9 @@ package googlecompute
// DriverMock is a Driver implementation that is a mocked out so that
// DriverMock is a Driver implementation that is a mocked out so that
// it can be used for tests.
// it can be used for tests.
type
DriverMock
struct
{
type
DriverMock
struct
{
ImageExistsName
string
ImageExistsResult
bool
CreateImageName
string
CreateImageName
string
CreateImageDesc
string
CreateImageDesc
string
CreateImageZone
string
CreateImageZone
string
...
@@ -37,6 +40,11 @@ type DriverMock struct {
...
@@ -37,6 +40,11 @@ type DriverMock struct {
WaitForInstanceErrCh
<-
chan
error
WaitForInstanceErrCh
<-
chan
error
}
}
func
(
d
*
DriverMock
)
ImageExists
(
name
string
)
bool
{
d
.
ImageExistsName
=
name
return
d
.
ImageExistsResult
}
func
(
d
*
DriverMock
)
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
{
func
(
d
*
DriverMock
)
CreateImage
(
name
,
description
,
zone
,
disk
string
)
<-
chan
error
{
d
.
CreateImageName
=
name
d
.
CreateImageName
=
name
d
.
CreateImageDesc
=
description
d
.
CreateImageDesc
=
description
...
...
builder/googlecompute/step_check_existing_image.go
0 → 100644
View file @
7c672289
package
googlecompute
import
(
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
// StepCheckExistingImage represents a Packer build step that checks if the
// target image already exists, and aborts immediately if so.
type
StepCheckExistingImage
int
// Run executes the Packer build step that checks if the image already exists.
func
(
s
*
StepCheckExistingImage
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
Driver
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Checking image does not exist..."
)
exists
:=
driver
.
ImageExists
(
config
.
ImageName
)
if
exists
{
err
:=
fmt
.
Errorf
(
"Image %s already exists"
,
config
.
ImageName
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
return
multistep
.
ActionContinue
}
// Cleanup.
func
(
s
*
StepCheckExistingImage
)
Cleanup
(
state
multistep
.
StateBag
)
{}
builder/googlecompute/step_check_existing_image_test.go
0 → 100644
View file @
7c672289
package
googlecompute
import
(
"github.com/mitchellh/multistep"
"testing"
)
func
TestStepCheckExistingImage_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepCheckExistingImage
)
}
func
TestStepCheckExistingImage
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCheckExistingImage
)
defer
step
.
Cleanup
(
state
)
state
.
Put
(
"instance_name"
,
"foo"
)
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
driver
.
ImageExistsResult
=
true
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionHalt
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify state
if
driver
.
ImageExistsName
!=
config
.
ImageName
{
t
.
Fatalf
(
"bad: %#v"
,
driver
.
ImageExistsName
)
}
}
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