25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 25
def forward_ports
ports = {}
edge_ports = []
cfg = @env[:machine].provider_config
cnx = cfg.vcloud_cnx.driver
vapp_id = @env[:machine].get_vapp_id
vm_name = cfg.name ? cfg.name.to_sym : @env[:machine].name
@logger.debug('Getting VM info...')
vm = cnx.get_vapp(vapp_id)
vm_info = vm[:vms_hash][vm_name.to_sym]
network_name = ''
@env[:forwarded_ports].each do |fp|
@env[:ui].info(
"Forwarding Ports: VM port #{fp.guest_port} -> " +
"vShield Edge port #{fp.host_port}"
)
ports["#{fp.network_name}#{fp.edge_network_name}"] = { rules: [] } if !ports["#{fp.network_name}#{fp.edge_network_name}"]
ports["#{fp.network_name}#{fp.edge_network_name}"][:network_name] = fp.network_name
ports["#{fp.network_name}#{fp.edge_network_name}"][:parent_network] = fp.edge_network_id
ports["#{fp.network_name}#{fp.edge_network_name}"][:edge_network_name] = fp.edge_network_name
ports["#{fp.network_name}#{fp.edge_network_name}"][:rules] << {
:guestip => fp.guest_ip,
:nat_internal_port => fp.guest_port,
:hostip => fp.host_ip,
:nat_external_port => fp.host_port,
:name => fp.id,
:nat_vmnic_id => fp.vmnic_id,
:nat_protocol => fp.protocol.upcase,
:vapp_scoped_local_id => vm_info[:vapp_scoped_local_id]
}
end
if !ports.empty?
@logger.debug("Port object to be passed: #{ports.inspect}")
ports.values.each do |port|
add_ports = cnx.add_vapp_port_forwarding_rules(
vapp_id,
port[:network_name],
port[:edge_network_name],
{
:fence_mode => 'natRouted',
:parent_network => port[:parent_network],
:nat_policy_type => 'allowTraffic',
:nat_rules => port[:rules]
}
)
wait = cnx.wait_task_completion(add_ports)
if !wait[:errormsg].nil?
raise Errors::ComposeVAppError, :message => wait[:errormsg]
end
end
if cfg.vdc_edge_gateway_ip && \
cfg.vdc_edge_gateway && \
cfg.network_bridge.nil?
vapp_edge_ip = cnx.get_vapp_edge_public_ip(vapp_id)
@logger.debug('Getting edge gateway port forwarding rules...')
edge_gateway_rules = cnx.get_edge_gateway_rules(cfg.vdc_edge_gateway,
cfg.vdc_id)
vapp_edge_dnat_rules = edge_gateway_rules.select {|r| (r[:rule_type] == 'DNAT' &&
r[:translated_ip] == vapp_edge_ip)}
vapp_edge_ports_in_use = vapp_edge_dnat_rules.map{|r| r[:original_port].to_i}.to_set
ports.values.each do |port|
port[:rules].each do |rule|
if rule[:vapp_scoped_local_id] == vm_info[:vapp_scoped_local_id] &&
!vapp_edge_ports_in_use.include?(rule[:nat_external_port])
@env[:ui].info(
"Creating NAT rules on [#{cfg.vdc_edge_gateway}] " +
"for IP [#{vapp_edge_ip}] port #{rule[:nat_external_port]}."
)
edge_ports << rule[:nat_external_port]
end
end
end
if !edge_ports.empty?
add_ports = cnx.add_edge_gateway_rules(
cfg.vdc_edge_gateway,
cfg.vdc_id,
cfg.vdc_edge_gateway_ip,
vapp_id,
edge_ports
)
wait = cnx.wait_task_completion(add_ports)
if !wait[:errormsg].nil?
raise Errors::ComposeVAppError, :message => wait[:errormsg]
end
end
end
end
end
|