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
03e22c63
Commit
03e22c63
authored
Jun 06, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/vmware: graceful shutdown
parent
c559ec7d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
2 deletions
+86
-2
builder/vmware/builder.go
builder/vmware/builder.go
+14
-2
builder/vmware/step_shutdown.go
builder/vmware/step_shutdown.go
+72
-0
No files found.
builder/vmware/builder.go
View file @
03e22c63
...
...
@@ -29,12 +29,14 @@ type config struct {
BootCommand
[]
string
`mapstructure:"boot_command"`
BootWait
time
.
Duration
ShutdownCommand
string
`mapstructure:"shutdown_command"`
ShutdownTimeout
time
.
Duration
SSHUser
string
`mapstructure:"ssh_username"`
SSHPassword
string
`mapstructure:"ssh_password"`
SSHWaitTimeout
time
.
Duration
RawBootWait
string
`mapstructure:"boot_wait"`
RawSSHWaitTimeout
string
`mapstructure:"ssh_wait_timeout"`
RawBootWait
string
`mapstructure:"boot_wait"`
RawShutdownTimeout
string
`mapstructure:"shutdown_timeout"`
RawSSHWaitTimeout
string
`mapstructure:"ssh_wait_timeout"`
}
func
(
b
*
Builder
)
Prepare
(
raw
interface
{})
(
err
error
)
{
...
...
@@ -73,6 +75,15 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
}
}
if
b
.
config
.
RawShutdownTimeout
==
""
{
b
.
config
.
RawShutdownTimeout
=
"5m"
}
b
.
config
.
ShutdownTimeout
,
err
=
time
.
ParseDuration
(
b
.
config
.
RawShutdownTimeout
)
if
err
!=
nil
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Failed parsing shutdown_timeout: %s"
,
err
))
}
if
b
.
config
.
RawSSHWaitTimeout
==
""
{
b
.
config
.
RawSSHWaitTimeout
=
"20m"
}
...
...
@@ -104,6 +115,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
&
stepTypeBootCommand
{},
&
stepWaitForSSH
{},
&
stepProvision
{},
&
stepShutdown
{},
}
// Setup the state bag
...
...
builder/vmware/step_shutdown.go
0 → 100644
View file @
03e22c63
package
vmware
import
(
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
// This step shuts down the machine. It first attempts to do so gracefully,
// but ultimately forcefully shuts it down if that fails.
//
// Uses:
// communicator packer.Communicator
// config *config
// driver Driver
// ui packer.Ui
// vmx_path string
//
// Produces:
// <nothing>
type
stepShutdown
struct
{}
func
(
s
*
stepShutdown
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
comm
:=
state
[
"communicator"
]
.
(
packer
.
Communicator
)
config
:=
state
[
"config"
]
.
(
*
config
)
driver
:=
state
[
"driver"
]
.
(
Driver
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
vmxPath
:=
state
[
"vmx_path"
]
.
(
string
)
if
config
.
ShutdownCommand
!=
""
{
ui
.
Say
(
"Gracefully halting virtual machine..."
)
log
.
Printf
(
"Executing shutdown command: %s"
,
config
.
ShutdownCommand
)
cmd
:=
&
packer
.
RemoteCmd
{
Command
:
config
.
ShutdownCommand
}
if
err
:=
comm
.
Start
(
cmd
);
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Failed to send shutdown command: %s"
,
err
))
return
multistep
.
ActionHalt
}
// Wait for the command to run
cmd
.
Wait
()
// Wait for the machine to actually shut down
log
.
Printf
(
"Waiting max %s for shutdown to complete"
,
config
.
ShutdownTimeout
)
shutdownTimer
:=
time
.
After
(
config
.
ShutdownTimeout
)
for
{
running
,
_
:=
driver
.
IsRunning
(
vmxPath
)
if
!
running
{
break
}
select
{
case
<-
shutdownTimer
:
ui
.
Error
(
"Timeout while waiting for machine to shut down."
)
return
multistep
.
ActionHalt
default
:
time
.
Sleep
(
1
*
time
.
Second
)
}
}
}
else
{
if
err
:=
driver
.
Stop
(
vmxPath
);
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error stopping VM: %s"
,
err
))
return
multistep
.
ActionHalt
}
}
log
.
Println
(
"VM shut down."
)
return
multistep
.
ActionContinue
}
func
(
s
*
stepShutdown
)
Cleanup
(
state
map
[
string
]
interface
{})
{}
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