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
4f32692f
Commit
4f32692f
authored
Dec 24, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/vmware: StepShutdown
parent
6c4af2d7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
205 additions
and
18 deletions
+205
-18
builder/vmware/common/driver_mock.go
builder/vmware/common/driver_mock.go
+7
-0
builder/vmware/common/step_shutdown.go
builder/vmware/common/step_shutdown.go
+20
-17
builder/vmware/common/step_shutdown_test.go
builder/vmware/common/step_shutdown_test.go
+174
-0
builder/vmware/iso/builder.go
builder/vmware/iso/builder.go
+4
-1
No files found.
builder/vmware/common/driver_mock.go
View file @
4f32692f
package
common
import
(
"sync"
"github.com/mitchellh/multistep"
)
type
DriverMock
struct
{
sync
.
Mutex
CompactDiskCalled
bool
CompactDiskPath
string
CompactDiskErr
error
...
...
@@ -65,6 +69,9 @@ func (d *DriverMock) CreateDisk(output string, size string, typeId string) error
}
func
(
d
*
DriverMock
)
IsRunning
(
path
string
)
(
bool
,
error
)
{
d
.
Lock
()
defer
d
.
Unlock
()
d
.
IsRunningCalled
=
true
d
.
IsRunningPath
=
path
return
d
.
IsRunningResult
,
d
.
IsRunningErr
...
...
builder/vmware/
iso
/step_shutdown.go
→
builder/vmware/
common
/step_shutdown.go
View file @
4f32692f
package
iso
package
common
import
(
"bytes"
"errors"
"fmt"
"github.com/mitchellh/multistep"
vmwcommon
"github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/packer"
"log"
"regexp"
...
...
@@ -19,30 +18,32 @@ import (
//
// Uses:
// communicator packer.Communicator
//
config *config
//
dir OutputDir
// driver Driver
// ui packer.Ui
// vmx_path string
//
// Produces:
// <nothing>
type
stepShutdown
struct
{}
type
StepShutdown
struct
{
Command
string
Timeout
time
.
Duration
}
func
(
s
*
s
tepShutdown
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
func
(
s
*
S
tepShutdown
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
comm
:=
state
.
Get
(
"communicator"
)
.
(
packer
.
Communicator
)
config
:=
state
.
Get
(
"config"
)
.
(
*
config
)
dir
:=
state
.
Get
(
"dir"
)
.
(
vmwcommon
.
OutputDir
)
driver
:=
state
.
Get
(
"driver"
)
.
(
vmwcommon
.
Driver
)
dir
:=
state
.
Get
(
"dir"
)
.
(
OutputDir
)
driver
:=
state
.
Get
(
"driver"
)
.
(
Driver
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
vmxPath
:=
state
.
Get
(
"vmx_path"
)
.
(
string
)
if
config
.
Shutdown
Command
!=
""
{
if
s
.
Command
!=
""
{
ui
.
Say
(
"Gracefully halting virtual machine..."
)
log
.
Printf
(
"Executing shutdown command: %s"
,
config
.
Shutdown
Command
)
log
.
Printf
(
"Executing shutdown command: %s"
,
s
.
Command
)
var
stdout
,
stderr
bytes
.
Buffer
cmd
:=
&
packer
.
RemoteCmd
{
Command
:
config
.
Shutdown
Command
,
Command
:
s
.
Command
,
Stdout
:
&
stdout
,
Stderr
:
&
stderr
,
}
...
...
@@ -68,8 +69,8 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
log
.
Printf
(
"Shutdown stderr: %s"
,
stderr
.
String
())
// Wait for the machine to actually shut down
log
.
Printf
(
"Waiting max %s for shutdown to complete"
,
config
.
shutdown
Timeout
)
shutdownTimer
:=
time
.
After
(
config
.
shutdown
Timeout
)
log
.
Printf
(
"Waiting max %s for shutdown to complete"
,
s
.
Timeout
)
shutdownTimer
:=
time
.
After
(
s
.
Timeout
)
for
{
running
,
_
:=
driver
.
IsRunning
(
vmxPath
)
if
!
running
{
...
...
@@ -83,7 +84,7 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
default
:
time
.
Sleep
(
1
*
time
.
S
econd
)
time
.
Sleep
(
1
50
*
time
.
Millis
econd
)
}
}
}
else
{
...
...
@@ -101,7 +102,9 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
LockWaitLoop
:
for
{
files
,
err
:=
dir
.
ListFiles
()
if
err
==
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Error listing files in outputdir: %s"
,
err
)
}
else
{
var
locks
[]
string
for
_
,
file
:=
range
files
{
if
lockRegex
.
MatchString
(
file
)
{
...
...
@@ -126,7 +129,7 @@ LockWaitLoop:
case
<-
timer
:
log
.
Println
(
"Reached timeout on waiting for clean VMware. Assuming clean."
)
break
LockWaitLoop
case
<-
time
.
After
(
1
*
time
.
S
econd
)
:
case
<-
time
.
After
(
1
50
*
time
.
Millis
econd
)
:
}
}
...
...
@@ -141,4 +144,4 @@ LockWaitLoop:
return
multistep
.
ActionContinue
}
func
(
s
*
s
tepShutdown
)
Cleanup
(
state
multistep
.
StateBag
)
{}
func
(
s
*
S
tepShutdown
)
Cleanup
(
state
multistep
.
StateBag
)
{}
builder/vmware/common/step_shutdown_test.go
0 → 100644
View file @
4f32692f
package
common
import
(
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
func
testStepShutdownState
(
t
*
testing
.
T
)
multistep
.
StateBag
{
dir
:=
testOutputDir
(
t
)
if
err
:=
dir
.
MkdirAll
();
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
state
:=
testState
(
t
)
state
.
Put
(
"communicator"
,
new
(
packer
.
MockCommunicator
))
state
.
Put
(
"dir"
,
dir
)
state
.
Put
(
"vmx_path"
,
"foo"
)
return
state
}
func
TestStepShutdown_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepShutdown
)
}
func
TestStepShutdown_command
(
t
*
testing
.
T
)
{
state
:=
testStepShutdownState
(
t
)
step
:=
new
(
StepShutdown
)
step
.
Command
=
"foo"
step
.
Timeout
=
10
*
time
.
Second
comm
:=
state
.
Get
(
"communicator"
)
.
(
*
packer
.
MockCommunicator
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
driver
.
IsRunningResult
=
true
// Set not running after some time
go
func
()
{
time
.
Sleep
(
100
*
time
.
Millisecond
)
driver
.
Lock
()
defer
driver
.
Unlock
()
driver
.
IsRunningResult
=
false
}()
resultCh
:=
make
(
chan
multistep
.
StepAction
,
1
)
go
func
()
{
resultCh
<-
step
.
Run
(
state
)
}()
select
{
case
<-
resultCh
:
t
.
Fatal
(
"should not have returned so quickly"
)
case
<-
time
.
After
(
50
*
time
.
Millisecond
)
:
}
var
action
multistep
.
StepAction
select
{
case
action
=
<-
resultCh
:
case
<-
time
.
After
(
300
*
time
.
Millisecond
)
:
t
.
Fatal
(
"should've returned by now"
)
}
// Test the run
if
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
// Test the driver
if
driver
.
StopCalled
{
t
.
Fatal
(
"stop should not be called"
)
}
if
!
comm
.
StartCalled
{
t
.
Fatal
(
"start should be called"
)
}
if
comm
.
StartCmd
.
Command
!=
"foo"
{
t
.
Fatalf
(
"bad: %#v"
,
comm
.
StartCmd
.
Command
)
}
}
func
TestStepShutdown_noCommand
(
t
*
testing
.
T
)
{
state
:=
testStepShutdownState
(
t
)
step
:=
new
(
StepShutdown
)
comm
:=
state
.
Get
(
"communicator"
)
.
(
*
packer
.
MockCommunicator
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
// Test the run
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
// Test the driver
if
!
driver
.
StopCalled
{
t
.
Fatal
(
"stop should be called"
)
}
if
driver
.
StopPath
!=
"foo"
{
t
.
Fatal
(
"should call with right path"
)
}
if
comm
.
StartCalled
{
t
.
Fatal
(
"start should not be called"
)
}
}
func
TestStepShutdown_locks
(
t
*
testing
.
T
)
{
state
:=
testStepShutdownState
(
t
)
step
:=
new
(
StepShutdown
)
dir
:=
state
.
Get
(
"dir"
)
.
(
*
LocalOutputDir
)
comm
:=
state
.
Get
(
"communicator"
)
.
(
*
packer
.
MockCommunicator
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
// Create some lock files
lockPath
:=
filepath
.
Join
(
dir
.
dir
,
"nope.lck"
)
err
:=
ioutil
.
WriteFile
(
lockPath
,
[]
byte
(
"foo"
),
0644
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
)
}
// Remove the lock file after a certain time
go
func
()
{
time
.
Sleep
(
100
*
time
.
Millisecond
)
os
.
Remove
(
lockPath
)
}()
resultCh
:=
make
(
chan
multistep
.
StepAction
,
1
)
go
func
()
{
resultCh
<-
step
.
Run
(
state
)
}()
select
{
case
<-
resultCh
:
t
.
Fatal
(
"should not have returned so quickly"
)
case
<-
time
.
After
(
50
*
time
.
Millisecond
)
:
}
var
action
multistep
.
StepAction
select
{
case
action
=
<-
resultCh
:
case
<-
time
.
After
(
300
*
time
.
Millisecond
)
:
t
.
Fatal
(
"should've returned by now"
)
}
// Test the run
if
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
// Test the driver
if
!
driver
.
StopCalled
{
t
.
Fatal
(
"stop should be called"
)
}
if
driver
.
StopPath
!=
"foo"
{
t
.
Fatal
(
"should call with right path"
)
}
if
comm
.
StartCalled
{
t
.
Fatal
(
"start should not be called"
)
}
}
builder/vmware/iso/builder.go
View file @
4f32692f
...
...
@@ -410,7 +410,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
&
stepUploadTools
{},
&
common
.
StepProvision
{},
&
stepShutdown
{},
&
vmwcommon
.
StepShutdown
{
Command
:
b
.
config
.
ShutdownCommand
,
Timeout
:
b
.
config
.
shutdownTimeout
,
},
&
vmwcommon
.
StepCleanFiles
{},
&
vmwcommon
.
StepCleanVMX
{},
&
vmwcommon
.
StepCompactDisk
{
...
...
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