Class: Cloud66::Utils::VitalSigns

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud66_agent/utils/vital_signs.rb

Class Method Summary collapse

Class Method Details

.address_info ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cloud66_agent/utils/vital_signs.rb', line 10

def self.address_info
  # AWS special case
  if $config.is_aws
    result = {}
    reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4`
    result[:ext_ipv4] = reported_ip if reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
    reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
    result[:int_ipv4] = reported_ip if reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
    return result
    # GC special case
  elsif $config.is_gc
    result = {}
    external_ip = `/usr/bin/curl -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip`
    result[:ext_ipv4] = external_ip if external_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
    internal_ip = `/usr/bin/curl -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip`
    result[:int_ipv4] = internal_ip if internal_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
    return result
  else
    interfaces_raw = `facter interfaces`
    interfaces = interfaces_raw.split(',').select { |interface| interface =~ /^eth/ }

    # don't have any ip address info
    raise 'no address information' if interfaces.empty?

    # address information
    facter_command = "facter #{interfaces.map { |interface| "ipaddress_#{interface} ipaddress6_#{interface}" }.join(' ')}"
    hash = parse_data(`#{facter_command}`)

    # don't have any ip address info
    raise 'no address information' if hash.empty?

    # return all interface information
    return hash
  end
end

.is_aws? ⇒ Boolean



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cloud66_agent/utils/vital_signs.rb', line 46

def self.is_aws?
  # 6 seconds to find out if this is a AWS image or not!
  instance = `/usr/bin/curl --connect-timeout 6 -s http://169.254.169.254/latest/meta-data/instance-id` rescue nil
  return false if instance.nil? || instance.empty?

  # now check the external ip to make sure we're not just being routed
  reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4` rescue nil
  return false if reported_ip.nil? || reported_ip.empty?
  return false unless reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/

  # it is aws  
  return true
end

.is_gc? ⇒ Boolean



60
61
62
63
64
65
66
67
68
# File 'lib/cloud66_agent/utils/vital_signs.rb', line 60

def self.is_gc?
  # 6 seconds to find out if this is a AWS image or not!
  external_ip = `/usr/bin/curl --connect-timeout 6 -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip` rescue nil
  return false if external_ip.nil? || external_ip.empty?
  return false unless external_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/

  # it is google cloud
  return true
end

.parse_data(data) ⇒ Object

parse the data, not using YAML due to YAML parsing issues and JSON output not always working



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cloud66_agent/utils/vital_signs.rb', line 73

def self.parse_data(data)
  hash = {}
  data.lines.each do |line|
    split = line.split('=>')
    if split.size == 2
      key = split[0]
      value = split[1]
      unless value.nil?
        value = value.strip
        # exclude empty or long results (like ssh keys)
        if !value.empty? && value.size < 100
          key = key.strip
          hash[key] = value
        end
      end
    end
  end
  return hash
end

.system_info ⇒ Object



5
6
7
8
# File 'lib/cloud66_agent/utils/vital_signs.rb', line 5

def self.system_info
  # system info
  return parse_data(`facter`)
end