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
aee611db
Commit
aee611db
authored
Jun 05, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/vmware: Support reading guest IP from DHCP
parent
323647e3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
5 deletions
+104
-5
builder/vmware/guest_ip.go
builder/vmware/guest_ip.go
+54
-2
builder/vmware/step_wait_for_ssh.go
builder/vmware/step_wait_for_ssh.go
+50
-3
No files found.
builder/vmware/guest_ip.go
View file @
aee611db
package
vmware
package
vmware
import
(
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
)
// Interface to help find the IP address of a running virtual machine.
// Interface to help find the IP address of a running virtual machine.
type
GuestIPFinder
interface
{
type
GuestIPFinder
interface
{
GuestIP
()
(
string
,
error
)
GuestIP
()
(
string
,
error
)
...
@@ -16,5 +25,48 @@ type DHCPLeaseGuestLookup struct {
...
@@ -16,5 +25,48 @@ type DHCPLeaseGuestLookup struct {
}
}
func
(
f
*
DHCPLeaseGuestLookup
)
GuestIP
()
(
string
,
error
)
{
func
(
f
*
DHCPLeaseGuestLookup
)
GuestIP
()
(
string
,
error
)
{
return
""
,
nil
fh
,
err
:=
os
.
Open
(
fmt
.
Sprintf
(
"/var/db/vmware/vmnet-dhcpd-%s.leases"
,
f
.
Device
))
if
err
!=
nil
{
return
""
,
err
}
defer
fh
.
Close
()
dhcpBytes
,
err
:=
ioutil
.
ReadAll
(
fh
)
if
err
!=
nil
{
return
""
,
err
}
var
lastIp
string
var
lastLeaseEnd
time
.
Time
var
curIp
string
var
curLeaseEnd
time
.
Time
ipLineRe
:=
regexp
.
MustCompile
(
`^lease (.+?) {$`
)
endTimeLineRe
:=
regexp
.
MustCompile
(
`^\s*ends \d (.+?);$`
)
macLineRe
:=
regexp
.
MustCompile
(
`^\s*hardware ethernet (.+?);$`
)
for
_
,
line
:=
range
strings
.
Split
(
string
(
dhcpBytes
),
"
\n
"
)
{
matches
:=
ipLineRe
.
FindStringSubmatch
(
line
)
if
matches
!=
nil
{
lastIp
=
matches
[
1
]
continue
}
matches
=
endTimeLineRe
.
FindStringSubmatch
(
line
)
if
matches
!=
nil
{
lastLeaseEnd
,
_
=
time
.
Parse
(
"2006/01/02 15:04:05"
,
matches
[
1
])
continue
}
// If the mac address matches and this lease ends farther in the
// future than the last match we might have, then choose it.
matches
=
macLineRe
.
FindStringSubmatch
(
line
)
if
matches
!=
nil
&&
matches
[
1
]
==
f
.
MACAddress
&&
curLeaseEnd
.
Before
(
lastLeaseEnd
)
{
curIp
=
lastIp
curLeaseEnd
=
lastLeaseEnd
}
}
return
curIp
,
nil
}
}
builder/vmware/step_wait_for_ssh.go
View file @
aee611db
package
vmware
package
vmware
import
(
import
(
"errors"
"github.com/mitchellh/multistep"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"log"
"os"
"time"
)
)
// This step waits for SSH to become available and establishes an SSH
// This step waits for SSH to become available and establishes an SSH
...
@@ -11,21 +16,63 @@ import (
...
@@ -11,21 +16,63 @@ import (
// Uses:
// Uses:
// config *config
// config *config
// ui packer.Ui
// ui packer.Ui
// vmx_path string
//
//
// Produces:
// Produces:
// <nothing>
// <nothing>
type
stepWaitForSSH
struct
{}
type
stepWaitForSSH
struct
{}
func
(
stepWaitForSSH
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
func
(
s
*
s
tepWaitForSSH
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
vmxPath
:=
state
[
"vmx_path"
]
.
(
string
)
ui
.
Say
(
"Waiting for SSH to become available..."
)
ui
.
Say
(
"Waiting for SSH to become available..."
)
for
{
for
{
time
.
Sleep
(
5
*
time
.
Second
)
log
.
Println
(
"Lookup up IP information..."
)
// First we wait for the IP to become available...
// First we wait for the IP to become available...
ipLookup
,
err
:=
s
.
dhcpLeaseLookup
(
vmxPath
)
if
err
!=
nil
{
log
.
Printf
(
"Can't lookup via DHCP lease: %s"
,
err
)
}
ip
,
err
:=
ipLookup
.
GuestIP
()
if
err
!=
nil
{
log
.
Printf
(
"IP lookup failed: %s"
,
err
)
continue
}
log
.
Printf
(
"Detected IP: %s"
,
ip
)
}
}
return
multistep
.
ActionContinue
return
multistep
.
ActionContinue
}
}
func
(
stepWaitForSSH
)
Cleanup
(
map
[
string
]
interface
{})
{}
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
}
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