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
ebdd0d99
Commit
ebdd0d99
authored
May 29, 2015
by
Andrew Bayer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds support for using the internal IP rather than NAT IP in GCE
parent
7ce76d28
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
119 additions
and
16 deletions
+119
-16
builder/googlecompute/config.go
builder/googlecompute/config.go
+1
-0
builder/googlecompute/config_test.go
builder/googlecompute/config_test.go
+15
-0
builder/googlecompute/driver.go
builder/googlecompute/driver.go
+3
-0
builder/googlecompute/driver_gce.go
builder/googlecompute/driver_gce.go
+16
-1
builder/googlecompute/driver_mock.go
builder/googlecompute/driver_mock.go
+11
-0
builder/googlecompute/ssh.go
builder/googlecompute/ssh.go
+1
-1
builder/googlecompute/step_instance_info.go
builder/googlecompute/step_instance_info.go
+32
-14
builder/googlecompute/step_instance_info_test.go
builder/googlecompute/step_instance_info_test.go
+40
-0
No files found.
builder/googlecompute/config.go
View file @
ebdd0d99
...
@@ -36,6 +36,7 @@ type Config struct {
...
@@ -36,6 +36,7 @@ type Config struct {
RawSSHTimeout
string
`mapstructure:"ssh_timeout"`
RawSSHTimeout
string
`mapstructure:"ssh_timeout"`
RawStateTimeout
string
`mapstructure:"state_timeout"`
RawStateTimeout
string
`mapstructure:"state_timeout"`
Tags
[]
string
`mapstructure:"tags"`
Tags
[]
string
`mapstructure:"tags"`
UseInternalIP
bool
`mapstructure:"use_internal_ip"`
Zone
string
`mapstructure:"zone"`
Zone
string
`mapstructure:"zone"`
account
accountFile
account
accountFile
...
...
builder/googlecompute/config_test.go
View file @
ebdd0d99
...
@@ -116,6 +116,21 @@ func TestConfigPrepare(t *testing.T) {
...
@@ -116,6 +116,21 @@ func TestConfigPrepare(t *testing.T) {
"5s"
,
"5s"
,
false
,
false
,
},
},
{
"use_internal_ip"
,
nil
,
false
,
},
{
"use_internal_ip"
,
false
,
false
,
},
{
"use_internal_ip"
,
"SO VERY BAD"
,
true
,
},
}
}
for
_
,
tc
:=
range
cases
{
for
_
,
tc
:=
range
cases
{
...
...
builder/googlecompute/driver.go
View file @
ebdd0d99
...
@@ -24,6 +24,9 @@ type Driver interface {
...
@@ -24,6 +24,9 @@ type Driver interface {
// GetNatIP gets the NAT IP address for the instance.
// GetNatIP gets the NAT IP address for the instance.
GetNatIP
(
zone
,
name
string
)
(
string
,
error
)
GetNatIP
(
zone
,
name
string
)
(
string
,
error
)
// GetInternalIP gets the GCE-internal IP address for the instance.
GetInternalIP
(
zone
,
name
string
)
(
string
,
error
)
// RunInstance takes the given config and launches an instance.
// RunInstance takes the given config and launches an instance.
RunInstance
(
*
InstanceConfig
)
(
<-
chan
error
,
error
)
RunInstance
(
*
InstanceConfig
)
(
<-
chan
error
,
error
)
...
...
builder/googlecompute/driver_gce.go
View file @
ebdd0d99
...
@@ -157,7 +157,6 @@ func (d *driverGCE) GetNatIP(zone, name string) (string, error) {
...
@@ -157,7 +157,6 @@ func (d *driverGCE) GetNatIP(zone, name string) (string, error) {
if
ni
.
AccessConfigs
==
nil
{
if
ni
.
AccessConfigs
==
nil
{
continue
continue
}
}
for
_
,
ac
:=
range
ni
.
AccessConfigs
{
for
_
,
ac
:=
range
ni
.
AccessConfigs
{
if
ac
.
NatIP
!=
""
{
if
ac
.
NatIP
!=
""
{
return
ac
.
NatIP
,
nil
return
ac
.
NatIP
,
nil
...
@@ -168,6 +167,22 @@ func (d *driverGCE) GetNatIP(zone, name string) (string, error) {
...
@@ -168,6 +167,22 @@ func (d *driverGCE) GetNatIP(zone, name string) (string, error) {
return
""
,
nil
return
""
,
nil
}
}
func
(
d
*
driverGCE
)
GetInternalIP
(
zone
,
name
string
)
(
string
,
error
)
{
instance
,
err
:=
d
.
service
.
Instances
.
Get
(
d
.
projectId
,
zone
,
name
)
.
Do
()
if
err
!=
nil
{
return
""
,
err
}
for
_
,
ni
:=
range
instance
.
NetworkInterfaces
{
if
ni
.
NetworkIP
==
""
{
continue
}
return
ni
.
NetworkIP
,
nil
}
return
""
,
nil
}
func
(
d
*
driverGCE
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
func
(
d
*
driverGCE
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
// Get the zone
// Get the zone
d
.
ui
.
Message
(
fmt
.
Sprintf
(
"Loading zone: %s"
,
c
.
Zone
))
d
.
ui
.
Message
(
fmt
.
Sprintf
(
"Loading zone: %s"
,
c
.
Zone
))
...
...
builder/googlecompute/driver_mock.go
View file @
ebdd0d99
...
@@ -30,6 +30,11 @@ type DriverMock struct {
...
@@ -30,6 +30,11 @@ type DriverMock struct {
GetNatIPResult
string
GetNatIPResult
string
GetNatIPErr
error
GetNatIPErr
error
GetInternalIPZone
string
GetInternalIPName
string
GetInternalIPResult
string
GetInternalIPErr
error
RunInstanceConfig
*
InstanceConfig
RunInstanceConfig
*
InstanceConfig
RunInstanceErrCh
<-
chan
error
RunInstanceErrCh
<-
chan
error
RunInstanceErr
error
RunInstanceErr
error
...
@@ -108,6 +113,12 @@ func (d *DriverMock) GetNatIP(zone, name string) (string, error) {
...
@@ -108,6 +113,12 @@ func (d *DriverMock) GetNatIP(zone, name string) (string, error) {
return
d
.
GetNatIPResult
,
d
.
GetNatIPErr
return
d
.
GetNatIPResult
,
d
.
GetNatIPErr
}
}
func
(
d
*
DriverMock
)
GetInternalIP
(
zone
,
name
string
)
(
string
,
error
)
{
d
.
GetInternalIPZone
=
zone
d
.
GetInternalIPName
=
name
return
d
.
GetInternalIPResult
,
d
.
GetInternalIPErr
}
func
(
d
*
DriverMock
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
func
(
d
*
DriverMock
)
RunInstance
(
c
*
InstanceConfig
)
(
<-
chan
error
,
error
)
{
d
.
RunInstanceConfig
=
c
d
.
RunInstanceConfig
=
c
...
...
builder/googlecompute/ssh.go
View file @
ebdd0d99
package
googlecompute
package
googlecompute
import
(
import
(
"golang.org/x/crypto/ssh"
"fmt"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/multistep"
"golang.org/x/crypto/ssh"
)
)
// sshAddress returns the ssh address.
// sshAddress returns the ssh address.
...
...
builder/googlecompute/step_instance_info.go
View file @
ebdd0d99
...
@@ -40,23 +40,41 @@ func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
...
@@ -40,23 +40,41 @@ func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
return
multistep
.
ActionHalt
return
multistep
.
ActionHalt
}
}
ip
,
err
:=
driver
.
GetNatIP
(
config
.
Zone
,
instanceName
)
if
config
.
UseInternalIP
{
if
err
!=
nil
{
ip
,
err
:=
driver
.
GetInternalIP
(
config
.
Zone
,
instanceName
)
err
:=
fmt
.
Errorf
(
"Error retrieving instance nat ip address: %s"
,
err
)
if
err
!=
nil
{
state
.
Put
(
"error"
,
err
)
err
:=
fmt
.
Errorf
(
"Error retrieving instance internal ip address: %s"
,
err
)
ui
.
Error
(
err
.
Error
())
state
.
Put
(
"error"
,
err
)
return
multistep
.
ActionHalt
ui
.
Error
(
err
.
Error
())
}
return
multistep
.
ActionHalt
}
if
s
.
Debug
{
if
s
.
Debug
{
if
ip
!=
""
{
if
ip
!=
""
{
ui
.
Message
(
fmt
.
Sprintf
(
"Public IP: %s"
,
ip
))
ui
.
Message
(
fmt
.
Sprintf
(
"Internal IP: %s"
,
ip
))
}
}
ui
.
Message
(
fmt
.
Sprintf
(
"IP: %s"
,
ip
))
state
.
Put
(
"instance_ip"
,
ip
)
return
multistep
.
ActionContinue
}
else
{
ip
,
err
:=
driver
.
GetNatIP
(
config
.
Zone
,
instanceName
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error retrieving instance nat ip address: %s"
,
err
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
}
}
ui
.
Message
(
fmt
.
Sprintf
(
"IP: %s"
,
ip
))
if
s
.
Debug
{
state
.
Put
(
"instance_ip"
,
ip
)
if
ip
!=
""
{
return
multistep
.
ActionContinue
ui
.
Message
(
fmt
.
Sprintf
(
"Public IP: %s"
,
ip
))
}
}
ui
.
Message
(
fmt
.
Sprintf
(
"IP: %s"
,
ip
))
state
.
Put
(
"instance_ip"
,
ip
)
return
multistep
.
ActionContinue
}
}
}
// Cleanup.
// Cleanup.
...
...
builder/googlecompute/step_instance_info_test.go
View file @
ebdd0d99
...
@@ -49,6 +49,46 @@ func TestStepInstanceInfo(t *testing.T) {
...
@@ -49,6 +49,46 @@ func TestStepInstanceInfo(t *testing.T) {
}
}
}
}
func
TestStepInstanceInfo_InternalIP
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepInstanceInfo
)
defer
step
.
Cleanup
(
state
)
state
.
Put
(
"instance_name"
,
"foo"
)
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
config
.
UseInternalIP
=
true
driver
:=
state
.
Get
(
"driver"
)
.
(
*
DriverMock
)
driver
.
GetNatIPResult
=
"1.2.3.4"
driver
.
GetInternalIPResult
=
"5.6.7.8"
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify state
if
driver
.
WaitForInstanceState
!=
"RUNNING"
{
t
.
Fatalf
(
"bad: %#v"
,
driver
.
WaitForInstanceState
)
}
if
driver
.
WaitForInstanceZone
!=
config
.
Zone
{
t
.
Fatalf
(
"bad: %#v"
,
driver
.
WaitForInstanceZone
)
}
if
driver
.
WaitForInstanceName
!=
"foo"
{
t
.
Fatalf
(
"bad: %#v"
,
driver
.
WaitForInstanceName
)
}
ipRaw
,
ok
:=
state
.
GetOk
(
"instance_ip"
)
if
!
ok
{
t
.
Fatal
(
"should have ip"
)
}
if
ip
,
ok
:=
ipRaw
.
(
string
);
!
ok
{
t
.
Fatal
(
"ip is not a string"
)
}
else
if
ip
!=
"5.6.7.8"
{
t
.
Fatalf
(
"bad ip: %s"
,
ip
)
}
}
func
TestStepInstanceInfo_getNatIPError
(
t
*
testing
.
T
)
{
func
TestStepInstanceInfo_getNatIPError
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
state
:=
testState
(
t
)
step
:=
new
(
StepInstanceInfo
)
step
:=
new
(
StepInstanceInfo
)
...
...
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