Revision fa953633
Added by Dominic Cleal about 10 years ago
app/models/concerns/orchestration/compute.rb | ||
---|---|---|
require 'socket'
|
||
require 'timeout'
|
||
|
||
module Orchestration::Compute
|
||
extend ActiveSupport::Concern
|
||
|
||
... | ... | |
# we can't ensure uniqueness of #foreman_attr using normal rails validations as that gets in a later step in the process
|
||
# therefore we must validate its not used already in our db.
|
||
value = vm.send(fog_attr)
|
||
value ||= find_address if foreman_attr == :ip
|
||
self.send("#{foreman_attr}=", value)
|
||
|
||
if value.blank? or (other_host = Host.send("find_by_#{foreman_attr}", value))
|
||
... | ... | |
attrs = compute_resource.provided_attributes
|
||
if attrs.keys.include?(:ip)
|
||
logger.info "waiting for instance to acquire ip address"
|
||
vm.wait_for do
|
||
self.send(attrs[:ip]).present? || (self.public_ip_addresses + self.private_ip_addresses).present?
|
||
end
|
||
vm.wait_for { self.send(attrs[:ip]).present? }
|
||
end
|
||
rescue => e
|
||
failure _("Failed to get IP for %{name}: %{e}") % { :name => name, :e => e }, e.backtrace
|
||
... | ... | |
end
|
||
end
|
||
|
||
def find_address
|
||
# Loop over the addresses waiting for one to come up
|
||
ip = nil
|
||
begin
|
||
Timeout::timeout(120) do
|
||
until ip
|
||
addresses = vm.public_ip_addresses + vm.private_ip_addresses
|
||
addresses.each do |addr|
|
||
ip = addresses.find { |addr| ssh_open?(addr) }
|
||
end
|
||
sleep 2
|
||
end
|
||
end
|
||
rescue
|
||
logger.info "acquisition of ip address timed out"
|
||
# Userdata doesn't require an IP to ssh to, so if we can't
|
||
# reach it, just pick one. SSH will correctly fail for an unreachable IP
|
||
ip = (vm.public_ip_addresses + vm.private_ip_addresses).first if ip.blank?
|
||
end
|
||
ip
|
||
end
|
||
|
||
def ssh_open? ip
|
||
begin
|
||
Timeout::timeout(1) do
|
||
begin
|
||
s = TCPSocket.new(ip, 22)
|
||
s.close
|
||
return true
|
||
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
|
||
return false
|
||
end
|
||
end
|
||
rescue Timeout::Error
|
||
end
|
||
|
||
return false
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
Revert "Fixes #2270 - Detect all OpenStack IPs and test which allows SSH"
This reverts commit eb1bba78e89b21ac1e935c2cfd7dbf7c23e1c646.
In light of #4616 and #4710, further issues with this change were found which
means we need more time to solve it completely and ensure full cross-provider
compatibility.