Class: VagrantPlugins::VCloud::Action::UnmapPortForwardings

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vcloud/action/unmap_port_forwardings.rb

Overview

This middleware class will detect and handle collisions with forwarded ports, whether that means raising an error or repairing them automatically.

Parameters it takes from the environment hash:

* `:port_collision_repair` - If true, it will attempt to repair
  port collisions. If false, it will raise an exception when
  there is a collision.

* `:port_collision_extra_in_use` - An array of ports that are
  considered in use.

* `:port_collision_remap` - A hash remapping certain host ports
  to other host ports.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ UnmapPortForwardings

Returns a new instance of UnmapPortForwardings.



27
28
29
30
# File 'lib/vagrant-vcloud/action/unmap_port_forwardings.rb', line 27

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

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-vcloud/action/unmap_port_forwardings.rb', line 32

def call(env)
  
  cfg = env[:machine].provider_config
  cnx = cfg.vcloud_cnx.driver
  vmName = env[:machine].name
  vAppId = env[:machine].get_vapp_id

  cfg.org = cnx.get_organization_by_name(cfg.org_name)
  cfg.vdc_network_id = cfg.org[:networks][cfg.vdc_network_name]

  @logger.debug("Getting vapp info...")
  vm = cnx.get_vapp(vAppId)
  myhash = vm[:vms_hash][vmName.to_sym]

  @logger.debug("Getting port forwarding rules...")
  rules = cnx.get_vapp_port_forwarding_rules(vAppId)

  newRuleSet = rules.select { |h| !myhash[:vapp_scoped_local_id].include? h[:vapp_scoped_local_id] }

  @logger.debug("OUR NEW RULE SET, PURGED: #{newRuleSet}")

  removePorts = cnx.set_vapp_port_forwarding_rules(
    vAppId,
    "Vagrant-vApp-Net",
    {
      :fence_mode => "natRouted",
      :parent_network => cfg.vdc_network_id,
      :nat_policy_type => "allowTraffic",
      :nat_rules => newRuleSet
    })

  wait = cnx.wait_task_completion(removePorts)

  if !wait[:errormsg].nil?
    raise Errors::ComposeVAppError, :message => wait[:errormsg]
  end


  @app.call(env)
end