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
a3800625
Commit
a3800625
authored
Jun 06, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/vmware: Have an overall ssh wait timeout
parent
ca39d236
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
44 deletions
+106
-44
builder/vmware/builder.go
builder/vmware/builder.go
+22
-9
builder/vmware/step_wait_for_ssh.go
builder/vmware/step_wait_for_ssh.go
+84
-35
No files found.
builder/vmware/builder.go
View file @
a3800625
...
...
@@ -5,6 +5,7 @@ import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
const
BuilderId
=
"mitchellh.vmware"
...
...
@@ -24,6 +25,9 @@ type config struct {
BootWait
uint
`mapstructure:"boot_wait"`
SSHUser
string
`mapstructure:"ssh_user"`
SSHPassword
string
`mapstructure:"ssh_password"`
SSHWaitTimeout
time
.
Duration
RawSSHWaitTimeout
string
`mapstructure:"ssh_wait_timeout"`
}
func
(
b
*
Builder
)
Prepare
(
raw
interface
{})
(
err
error
)
{
...
...
@@ -44,6 +48,15 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
b
.
config
.
OutputDir
=
"vmware"
}
if
b
.
config
.
RawSSHWaitTimeout
==
""
{
b
.
config
.
RawSSHWaitTimeout
=
"20m"
}
b
.
config
.
SSHWaitTimeout
,
err
=
time
.
ParseDuration
(
b
.
config
.
RawSSHWaitTimeout
)
if
err
!=
nil
{
return
}
return
nil
}
...
...
builder/vmware/step_wait_for_ssh.go
View file @
a3800625
...
...
@@ -24,18 +24,96 @@ import (
//
// Produces:
// communicator packer.Communicator
type
stepWaitForSSH
struct
{}
type
stepWaitForSSH
struct
{
cancel
bool
conn
net
.
Conn
}
func
(
s
*
stepWaitForSSH
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
config
:=
state
[
"config"
]
.
(
*
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
var
comm
packer
.
Communicator
var
err
error
waitDone
:=
make
(
chan
bool
,
1
)
go
func
()
{
comm
,
err
=
s
.
waitForSSH
(
state
)
waitDone
<-
true
}()
select
{
case
<-
waitDone
:
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error waiting for SSH: %s"
,
err
))
return
multistep
.
ActionHalt
}
state
[
"communicator"
]
=
comm
case
<-
time
.
After
(
config
.
SSHWaitTimeout
)
:
ui
.
Error
(
"Timeout waiting for SSH."
)
s
.
cancel
=
true
return
multistep
.
ActionHalt
}
return
multistep
.
ActionContinue
}
func
(
s
*
stepWaitForSSH
)
Cleanup
(
map
[
string
]
interface
{})
{
if
s
.
conn
!=
nil
{
s
.
conn
.
Close
()
s
.
conn
=
nil
}
}
// Reads the network information for lookup via DHCP.
func
(
s
*
stepWaitForSSH
)
dhcpLeaseLookup
(
vmxPath
string
)
(
GuestIPFinder
,
error
)
{
f
,
err
:=
os
.
Open
(
vmxPath
)
if
err
!=
nil
{
return
nil
,
err
}
defer
f
.
Close
()
vmxBytes
,
err
:=
ioutil
.
ReadAll
(
f
)
if
err
!=
nil
{
return
nil
,
err
}
vmxData
:=
ParseVMX
(
string
(
vmxBytes
))
var
ok
bool
macAddress
:=
""
if
macAddress
,
ok
=
vmxData
[
"ethernet0.address"
];
!
ok
||
macAddress
==
""
{
if
macAddress
,
ok
=
vmxData
[
"ethernet0.generatedAddress"
];
!
ok
||
macAddress
==
""
{
return
nil
,
errors
.
New
(
"couldn't find MAC address in VMX"
)
}
}
return
&
DHCPLeaseGuestLookup
{
"vmnet8"
,
macAddress
},
nil
}
// This blocks until SSH becomes available, and sends the communicator
// on the given channel.
func
(
s
*
stepWaitForSSH
)
waitForSSH
(
state
map
[
string
]
interface
{})
(
packer
.
Communicator
,
error
)
{
config
:=
state
[
"config"
]
.
(
*
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
vmxPath
:=
state
[
"vmx_path"
]
.
(
string
)
ui
.
Say
(
"Waiting for SSH to become available..."
)
var
comm
packer
.
Communicator
var
nc
net
.
Conn
for
{
if
nc
!=
nil
{
nc
.
Close
()
}
time
.
Sleep
(
5
*
time
.
Second
)
if
s
.
cancel
{
log
.
Println
(
"SSH wait cancelled. Exiting loop."
)
return
nil
,
errors
.
New
(
"SSH wait cancelled"
)
}
// First we wait for the IP to become available...
log
.
Println
(
"Lookup up IP information..."
)
ipLookup
,
err
:=
s
.
dhcpLeaseLookup
(
vmxPath
)
...
...
@@ -52,7 +130,7 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
log
.
Printf
(
"Detected IP: %s"
,
ip
)
// Attempt to connect to SSH port
nc
,
err
:
=
net
.
Dial
(
"tcp"
,
fmt
.
Sprintf
(
"%s:22"
,
ip
))
nc
,
err
=
net
.
Dial
(
"tcp"
,
fmt
.
Sprintf
(
"%s:22"
,
ip
))
if
err
!=
nil
{
log
.
Printf
(
"TCP connection to SSH ip/port failed: %s"
,
err
)
continue
...
...
@@ -68,43 +146,14 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
comm
,
err
=
ssh
.
New
(
nc
,
sshConfig
)
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error connecting via SSH: %s"
,
err
))
return
multistep
.
ActionHalt
return
nil
,
err
}
ui
.
Say
(
"Connected via SSH!"
)
break
}
state
[
"communicator"
]
=
comm
return
multistep
.
ActionContinue
}
func
(
s
*
stepWaitForSSH
)
Cleanup
(
map
[
string
]
interface
{})
{}
// Reads the network information for lookup via DHCP.
func
(
s
*
stepWaitForSSH
)
dhcpLeaseLookup
(
vmxPath
string
)
(
GuestIPFinder
,
error
)
{
f
,
err
:=
os
.
Open
(
vmxPath
)
if
err
!=
nil
{
return
nil
,
err
}
defer
f
.
Close
()
vmxBytes
,
err
:=
ioutil
.
ReadAll
(
f
)
if
err
!=
nil
{
return
nil
,
err
}
vmxData
:=
ParseVMX
(
string
(
vmxBytes
))
var
ok
bool
macAddress
:=
""
if
macAddress
,
ok
=
vmxData
[
"ethernet0.address"
];
!
ok
||
macAddress
==
""
{
if
macAddress
,
ok
=
vmxData
[
"ethernet0.generatedAddress"
];
!
ok
||
macAddress
==
""
{
return
nil
,
errors
.
New
(
"couldn't find MAC address in VMX"
)
}
}
return
&
DHCPLeaseGuestLookup
{
"vmnet8"
,
macAddress
},
nil
// Store the connection so we can close it later
s
.
conn
=
nc
return
comm
,
nil
}
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