Class: VagrantPlugins::VCloud::Action::ReadSSHInfo

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env, port = 22) ⇒ ReadSSHInfo

Returns a new instance of ReadSSHInfo.


5
6
7
8
9
# File 'lib/vagrant-vcloud/action/read_ssh_info.rb', line 5

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

Instance Method Details

#call(env) ⇒ Object


11
12
13
14
15
# File 'lib/vagrant-vcloud/action/read_ssh_info.rb', line 11

def call(env)
  env[:machine_ssh_info] = read_ssh_info(env)

  @app.call env
end

#check_for_port(ip, port, port_name) ⇒ Object

Small method to check the tcp connection to an ip:port works. Return false if anything fails, and true if it succeeded.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-vcloud/action/read_ssh_info.rb', line 19

def check_for_port(ip, port, port_name)
  @logger.debug("Checking #{port_name} (#{ip}:#{port})...")
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        @logger.debug("#{port_name} Connection successful !")
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::EHOSTDOWN
        @logger.debug("#{port_name} Connection Refused/Host Unreachable...")
        return false
      end
    end
  rescue Timeout::Error
    @logger.debug("#{port_name} Connection Timeout...")
  end

  return false
end

#read_ssh_info(env) ⇒ Object


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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vagrant-vcloud/action/read_ssh_info.rb', line 40

def read_ssh_info(env)
  return nil if env[:machine].id.nil?

  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 vApp information...')
  vm = cnx.get_vapp(vapp_id)
  myhash = vm[:vms_hash][vm_name.to_sym]

  if vm.nil?
    # The Virtual Machine couldn't be found.
    @logger.info(
      'Machine couldn\'t be found, assuming it got destroyed.'
    )
    machine.id = nil
    return nil
  end

  if !cfg.network_bridge.nil?
    @logger.debug(
      'We\'re running in bridged mode, ' \
      'fetching the IP directly from the VM'
    )
    vm_info = cnx.get_vm(env[:machine].id)
    @logger.debug(
      "IP address for #{vm_name}: " \
      "#{vm_info[:networks]['Vagrant-vApp-Net'][:ip]}"
    )

    @external_ip = vm_info[:networks]['Vagrant-vApp-Net'][:ip]
    @external_port = "#{@port}"
  else
    network_name = nil
    if cfg.nics
      cfg.nics.each do |nic|
        next if nic[:forwarded_port].nil?
        if !cfg.networks.nil? && !cfg.networks[:vapp].nil?
          cfg.networks[:vapp].each do |net|
            next if net[:name] != nic[:network]
            network_name = net[:vdc_network_name]
            break
          end
        end
      end
    end

    @logger.debug('Getting port forwarding rules...')
    rules = cnx.get_vapp_port_forwarding_rules(vapp_id, network_name)

    rules.each do |rule|
      if rule[:vapp_scoped_local_id] == myhash[:vapp_scoped_local_id] && rule[:nat_internal_port] == "#{@port}"
        @external_ip = rule[:nat_external_ip]
        @external_port = rule[:nat_external_port]
        break

      end
    end

    if cfg.vdc_edge_gateway_ip && cfg.vdc_edge_gateway
      @logger.debug(
        "We're running vagrant behind an Organization vDC Edge"
      )

      #
      # Add config.ssh.host support
      # http://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html
      #
      if env[:machine].config.ssh.host
        @logger.debug(
          'SSH Host setting configured too: ' \
          "#{env[:machine].config.ssh.host}"
        )
        @external_ip = env[:machine].config.ssh.host
      else
        @logger.debug(
          "Using Edge Gateway IP: #{cfg.vdc_edge_gateway_ip}"
        )
        @external_ip = cfg.vdc_edge_gateway_ip
      end
    end
  end

  port_name = "SSH"
  if @port == 5985
    port_name = "WinRM"
  end

  @logger.debug(
    "#{port_name} INFO: IP #{@external_ip} and Port #{@external_port}"
  )

  # tsugliani: Temporary Fix for Issue #56
  # SSH unavailable makes the deployment fails.
  # Wait infinitely right now for SSH...
  # sleep_counter incremented by 1s each loop.
  #
  # This should be fixed with implementing Vagrant::Util::Retryable
  # and something like:
  #
  # retryable(:on => Vagrant::Errors::SSHSomething, :tries => 10, :sleep => 5) do
  #   check_for_port(ip, port, "SSH", :error_class => Vagrant::Errors::SSHSomething)
  # end
  #
  sleep_counter = 5

  if @port == 22 || @port == 5985
    while check_for_port(@external_ip, @external_port, port_name) == false
      env[:ui].info(
        "Waiting for #{port_name} Access on #{@external_ip}:#{@external_port} ... "
      )
      sleep sleep_counter
      sleep_counter += 1
    end
  end

  # If we are here, then SSH is ready, continue
  {
    :host => @external_ip,
    :port => @external_port
  }
end