Class: VagrantPlugins::VCloud::Action::ForwardPorts

Inherits:
Object
  • Object
show all
Includes:
Util::CompileForwardedPorts
Defined in:
lib/vagrant-vcloud/action/forward_ports.rb

Instance Method Summary collapse

Methods included from Util::CompileForwardedPorts

#compile_forwarded_ports

Constructor Details

#initialize(app, env) ⇒ ForwardPorts

Returns a new instance of ForwardPorts.



7
8
9
10
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 7

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_vcloud::action::forward_ports')
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 12

def call(env)
  @env = env

  # Get the ports we are forwarding
  env[:forwarded_ports] ||= compile_forwarded_ports(
    env[:machine]
  )

  forward_ports

  @app.call(env)
end

#forward_portsObject



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

  # FIXME: why are we overriding this here ?
  #        It's already been taken care during the initial
  #        InventoryCheck. (tsugliani)
  #
  # cfg.org = cnx.get_organization_by_name(cfg.org_name)
  # cfg.vdc_network_id = cfg.org[:networks][cfg.vdc_network_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}"
    )

    # Add the options to the ports array to send to the driver later
    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?
    # We only need to forward ports if there are any to forward
    @logger.debug("Port object to be passed: #{ports.inspect}")

    ### Here we apply the nat_rules to the vApp we just built
    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 the vShield Edge Gateway rules
        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