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
20a074b4
Commit
20a074b4
authored
Dec 13, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/googlecompute: StepCreateImage
parent
37903337
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
12 deletions
+114
-12
builder/googlecompute/builder.go
builder/googlecompute/builder.go
+1
-1
builder/googlecompute/step_create_image.go
builder/googlecompute/step_create_image.go
+17
-11
builder/googlecompute/step_create_image_test.go
builder/googlecompute/step_create_image_test.go
+96
-0
No files found.
builder/googlecompute/builder.go
View file @
20a074b4
...
...
@@ -59,9 +59,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
new
(
common
.
StepProvision
),
new
(
StepUpdateGsutil
),
new
(
StepCreateImage
),
}
/*
new(stepCreateImage),
new(stepUploadImage),
new(stepRegisterImage),
}*/
...
...
builder/googlecompute/step_create_image.go
View file @
20a074b4
...
...
@@ -8,39 +8,45 @@ import (
"github.com/mitchellh/packer/packer"
)
//
s
tepCreateImage represents a Packer build step that creates GCE machine
//
S
tepCreateImage represents a Packer build step that creates GCE machine
// images.
type
s
tepCreateImage
int
type
S
tepCreateImage
int
// Run executes the Packer build step that creates a GCE machine image.
//
// Currently the only way to create a GCE image is to run the gcimagebundle
// command on the running GCE instance.
func
(
s
*
stepCreateImage
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
var
(
config
=
state
.
Get
(
"config"
)
.
(
*
Config
)
comm
=
state
.
Get
(
"communicator"
)
.
(
packer
.
Communicator
)
sudoPrefix
=
""
ui
=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
)
ui
.
Say
(
"Creating image..."
)
func
(
s
*
StepCreateImage
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
comm
:=
state
.
Get
(
"communicator"
)
.
(
packer
.
Communicator
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
sudoPrefix
:=
""
if
config
.
SSHUsername
!=
"root"
{
sudoPrefix
=
"sudo "
}
imageFilename
:=
fmt
.
Sprintf
(
"%s.tar.gz"
,
config
.
ImageName
)
imageBundleCmd
:=
"/usr/bin/gcimagebundle -d /dev/sda -o /tmp/"
ui
.
Say
(
"Creating image..."
)
cmd
:=
new
(
packer
.
RemoteCmd
)
cmd
.
Command
=
fmt
.
Sprintf
(
"%s%s --output_file_name %s"
,
sudoPrefix
,
imageBundleCmd
,
imageFilename
)
err
:=
cmd
.
StartWithUi
(
comm
,
ui
)
if
err
==
nil
&&
cmd
.
ExitStatus
!=
0
{
err
=
fmt
.
Errorf
(
"gcimagebundle exited with non-zero exit status: %d"
,
cmd
.
ExitStatus
)
}
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating image: %s"
,
err
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
state
.
Put
(
"image_file_name"
,
filepath
.
Join
(
"/tmp"
,
imageFilename
))
return
multistep
.
ActionContinue
}
func
(
s
*
s
tepCreateImage
)
Cleanup
(
state
multistep
.
StateBag
)
{}
func
(
s
*
S
tepCreateImage
)
Cleanup
(
state
multistep
.
StateBag
)
{}
builder/googlecompute/step_create_image_test.go
0 → 100644
View file @
20a074b4
package
googlecompute
import
(
"strings"
"testing"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
func
TestStepCreateImage_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepCreateImage
)
}
func
TestStepCreateImage
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCreateImage
)
defer
step
.
Cleanup
(
state
)
comm
:=
new
(
packer
.
MockCommunicator
)
state
.
Put
(
"communicator"
,
comm
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify
if
!
comm
.
StartCalled
{
t
.
Fatal
(
"start should be called"
)
}
if
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatal
(
"should not sudo"
)
}
if
!
strings
.
Contains
(
comm
.
StartCmd
.
Command
,
"gcimagebundle"
)
{
t
.
Fatalf
(
"bad command: %#v"
,
comm
.
StartCmd
.
Command
)
}
if
_
,
ok
:=
state
.
GetOk
(
"image_file_name"
);
!
ok
{
t
.
Fatal
(
"should have image"
)
}
}
func
TestStepCreateImage_badExitStatus
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCreateImage
)
defer
step
.
Cleanup
(
state
)
comm
:=
new
(
packer
.
MockCommunicator
)
comm
.
StartExitStatus
=
12
state
.
Put
(
"communicator"
,
comm
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionHalt
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
!
ok
{
t
.
Fatal
(
"should have error"
)
}
if
_
,
ok
:=
state
.
GetOk
(
"image_file_name"
);
ok
{
t
.
Fatal
(
"should NOT have image"
)
}
}
func
TestStepCreateImage_nonRoot
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCreateImage
)
defer
step
.
Cleanup
(
state
)
comm
:=
new
(
packer
.
MockCommunicator
)
state
.
Put
(
"communicator"
,
comm
)
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
config
.
SSHUsername
=
"bob"
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify
if
!
comm
.
StartCalled
{
t
.
Fatal
(
"start should be called"
)
}
if
!
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatal
(
"should sudo"
)
}
if
!
strings
.
Contains
(
comm
.
StartCmd
.
Command
,
"gcimagebundle"
)
{
t
.
Fatalf
(
"bad command: %#v"
,
comm
.
StartCmd
.
Command
)
}
if
_
,
ok
:=
state
.
GetOk
(
"image_file_name"
);
!
ok
{
t
.
Fatal
(
"should have image"
)
}
}
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