ohai 8.26.1 → 13.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +3 -4
  3. data/Rakefile +14 -0
  4. data/lib/ohai/application.rb +1 -0
  5. data/lib/ohai/config.rb +2 -88
  6. data/lib/ohai/dsl/plugin.rb +0 -4
  7. data/lib/ohai/exception.rb +0 -1
  8. data/lib/ohai/loader.rb +11 -2
  9. data/lib/ohai/log.rb +3 -1
  10. data/lib/ohai/mixin/command.rb +1 -318
  11. data/lib/ohai/mixin/do_metadata.rb +47 -0
  12. data/lib/ohai/mixin/ec2_metadata.rb +2 -29
  13. data/lib/ohai/mixin/gce_metadata.rb +0 -37
  14. data/lib/ohai/mixin/http_helper.rb +56 -0
  15. data/lib/ohai/mixin/network_constants.rb +0 -5
  16. data/lib/ohai/plugins/cloud.rb +144 -157
  17. data/lib/ohai/plugins/darwin/filesystem.rb +68 -22
  18. data/lib/ohai/plugins/digital_ocean.rb +24 -42
  19. data/lib/ohai/plugins/dmi.rb +81 -77
  20. data/lib/ohai/plugins/ec2.rb +33 -60
  21. data/lib/ohai/plugins/gce.rb +3 -1
  22. data/lib/ohai/plugins/hostname.rb +1 -16
  23. data/lib/ohai/plugins/linux/filesystem.rb +130 -81
  24. data/lib/ohai/plugins/linux/mdadm.rb +3 -45
  25. data/lib/ohai/plugins/linux/network.rb +1 -26
  26. data/lib/ohai/plugins/linux/platform.rb +4 -2
  27. data/lib/ohai/plugins/linux/virtualization.rb +1 -5
  28. data/lib/ohai/plugins/scala.rb +1 -13
  29. data/lib/ohai/plugins/solaris2/network.rb +1 -2
  30. data/lib/ohai/plugins/uptime.rb +0 -9
  31. data/lib/ohai/plugins/windows/cpu.rb +2 -1
  32. data/lib/ohai/plugins/windows/network.rb +1 -1
  33. data/lib/ohai/runner.rb +0 -5
  34. data/lib/ohai/system.rb +38 -8
  35. data/lib/ohai/version.rb +2 -2
  36. data/ohai.gemspec +1 -1
  37. data/spec/functional/application_spec.rb +0 -40
  38. data/spec/functional/plugins/powershell_spec.rb +1 -1
  39. data/spec/functional/plugins/root_group_spec.rb +1 -1
  40. data/spec/functional/plugins/windows/uptime_spec.rb +1 -1
  41. data/spec/ohai_spec.rb +1 -1
  42. data/spec/support/integration_helper.rb +17 -0
  43. data/spec/support/platform_helpers.rb +17 -0
  44. data/spec/unit/application_spec.rb +0 -1
  45. data/spec/unit/config_spec.rb +0 -111
  46. data/spec/unit/dsl/plugin_spec.rb +1 -1
  47. data/spec/unit/hints_spec.rb +1 -1
  48. data/spec/unit/loader_spec.rb +26 -1
  49. data/spec/unit/mixin/command_spec.rb +1 -69
  50. data/spec/unit/mixin/ec2_metadata_spec.rb +1 -1
  51. data/spec/unit/mixin/softlayer_metadata_spec.rb +1 -1
  52. data/spec/unit/plugin_config_spec.rb +0 -1
  53. data/spec/unit/plugins/abort_spec.rb +1 -1
  54. data/spec/unit/plugins/aix/cpu_spec.rb +1 -1
  55. data/spec/unit/plugins/aix/filesystem_spec.rb +2 -1
  56. data/spec/unit/plugins/aix/hostname_spec.rb +1 -1
  57. data/spec/unit/plugins/aix/kernel_spec.rb +1 -1
  58. data/spec/unit/plugins/aix/memory_spec.rb +1 -1
  59. data/spec/unit/plugins/aix/network_spec.rb +1 -1
  60. data/spec/unit/plugins/aix/os_spec.rb +1 -1
  61. data/spec/unit/plugins/aix/platform_spec.rb +1 -1
  62. data/spec/unit/plugins/aix/uptime_spec.rb +1 -1
  63. data/spec/unit/plugins/aix/virtualization_spec.rb +1 -1
  64. data/spec/unit/plugins/azure_spec.rb +1 -1
  65. data/spec/unit/plugins/bsd/filesystem_spec.rb +1 -1
  66. data/spec/unit/plugins/bsd/virtualization_spec.rb +1 -1
  67. data/spec/unit/plugins/c_spec.rb +1 -1
  68. data/spec/unit/plugins/chef_spec.rb +1 -2
  69. data/spec/unit/plugins/cloud_spec.rb +210 -50
  70. data/spec/unit/plugins/darwin/cpu_spec.rb +1 -1
  71. data/spec/unit/plugins/darwin/filesystem_spec.rb +62 -13
  72. data/spec/unit/plugins/darwin/hardware_spec.rb +2 -2
  73. data/spec/unit/plugins/darwin/hostname_spec.rb +1 -1
  74. data/spec/unit/plugins/darwin/kernel_spec.rb +1 -1
  75. data/spec/unit/plugins/darwin/memory_spec.rb +1 -1
  76. data/spec/unit/plugins/darwin/network_spec.rb +1 -1
  77. data/spec/unit/plugins/darwin/platform_spec.rb +1 -1
  78. data/spec/unit/plugins/darwin/system_profiler_spec.rb +2 -9
  79. data/spec/unit/plugins/darwin/virtualization_spec.rb +1 -1
  80. data/spec/unit/plugins/digital_ocean_spec.rb +39 -110
  81. data/spec/unit/plugins/dmi_spec.rb +1 -1
  82. data/spec/unit/plugins/ec2_spec.rb +10 -62
  83. data/spec/unit/plugins/elixir_spec.rb +1 -1
  84. data/spec/unit/plugins/erlang_spec.rb +1 -1
  85. data/spec/unit/plugins/eucalyptus_spec.rb +1 -1
  86. data/spec/unit/plugins/fail_spec.rb +1 -1
  87. data/spec/unit/plugins/freebsd/cpu_spec.rb +1 -1
  88. data/spec/unit/plugins/freebsd/hostname_spec.rb +1 -1
  89. data/spec/unit/plugins/freebsd/kernel_spec.rb +1 -1
  90. data/spec/unit/plugins/freebsd/os_spec.rb +1 -1
  91. data/spec/unit/plugins/freebsd/platform_spec.rb +1 -1
  92. data/spec/unit/plugins/gce_spec.rb +1 -1
  93. data/spec/unit/plugins/go_spec.rb +1 -1
  94. data/spec/unit/plugins/groovy_spec.rb +1 -1
  95. data/spec/unit/plugins/haskell_spec.rb +1 -1
  96. data/spec/unit/plugins/hostname_spec.rb +16 -41
  97. data/spec/unit/plugins/init_package_spec.rb +1 -1
  98. data/spec/unit/plugins/ip_scopes_spec.rb +18 -6
  99. data/spec/unit/plugins/java_spec.rb +1 -1
  100. data/spec/unit/plugins/joyent_spec.rb +17 -1
  101. data/spec/unit/plugins/kernel_spec.rb +1 -1
  102. data/spec/unit/plugins/linode_spec.rb +1 -1
  103. data/spec/unit/plugins/linux/block_device_spec.rb +1 -1
  104. data/spec/unit/plugins/linux/cpu_spec.rb +1 -2
  105. data/spec/unit/plugins/linux/filesystem_spec.rb +207 -201
  106. data/spec/unit/plugins/linux/fips_spec.rb +1 -1
  107. data/spec/unit/plugins/linux/hostname_spec.rb +1 -1
  108. data/spec/unit/plugins/linux/hostnamectl_spec.rb +1 -1
  109. data/spec/unit/plugins/linux/kernel_spec.rb +1 -1
  110. data/spec/unit/plugins/linux/lsb_spec.rb +1 -1
  111. data/spec/unit/plugins/linux/machineid_spec.rb +1 -1
  112. data/spec/unit/plugins/linux/mdadm_spec.rb +2 -68
  113. data/spec/unit/plugins/linux/memory_spec.rb +1 -1
  114. data/spec/unit/plugins/linux/network_spec.rb +3 -44
  115. data/spec/unit/plugins/linux/platform_spec.rb +4 -4
  116. data/spec/unit/plugins/linux/sessions_spec.rb +1 -1
  117. data/spec/unit/plugins/linux/uptime_spec.rb +1 -1
  118. data/spec/unit/plugins/linux/virtualization_spec.rb +1 -28
  119. data/spec/unit/plugins/lua_spec.rb +1 -1
  120. data/spec/unit/plugins/mono_spec.rb +1 -1
  121. data/spec/unit/plugins/netbsd/hostname_spec.rb +1 -1
  122. data/spec/unit/plugins/netbsd/kernel_spec.rb +1 -1
  123. data/spec/unit/plugins/netbsd/platform_spec.rb +1 -1
  124. data/spec/unit/plugins/network_spec.rb +1 -1
  125. data/spec/unit/plugins/nodejs_spec.rb +1 -1
  126. data/spec/unit/plugins/ohai_spec.rb +1 -1
  127. data/spec/unit/plugins/ohai_time_spec.rb +1 -1
  128. data/spec/unit/plugins/openbsd/hostname_spec.rb +1 -1
  129. data/spec/unit/plugins/openbsd/kernel_spec.rb +1 -1
  130. data/spec/unit/plugins/openbsd/platform_spec.rb +1 -1
  131. data/spec/unit/plugins/openstack_spec.rb +2 -3
  132. data/spec/unit/plugins/os_spec.rb +1 -1
  133. data/spec/unit/plugins/packages_spec.rb +1 -1
  134. data/spec/unit/plugins/passwd_spec.rb +17 -1
  135. data/spec/unit/plugins/perl_spec.rb +1 -1
  136. data/spec/unit/plugins/php_spec.rb +1 -1
  137. data/spec/unit/plugins/platform_spec.rb +1 -1
  138. data/spec/unit/plugins/powershell_spec.rb +1 -1
  139. data/spec/unit/plugins/python_spec.rb +1 -1
  140. data/spec/unit/plugins/rackspace_spec.rb +1 -2
  141. data/spec/unit/plugins/root_group_spec.rb +2 -2
  142. data/spec/unit/plugins/ruby_spec.rb +1 -1
  143. data/spec/unit/plugins/rust_spec.rb +1 -1
  144. data/spec/unit/plugins/scala_spec.rb +3 -37
  145. data/spec/unit/plugins/shard_spec.rb +1 -1
  146. data/spec/unit/plugins/shells_spec.rb +1 -1
  147. data/spec/unit/plugins/softlayer_spec.rb +1 -1
  148. data/spec/unit/plugins/solaris2/cpu_spec.rb +1 -1
  149. data/spec/unit/plugins/solaris2/dmi_spec.rb +1 -1
  150. data/spec/unit/plugins/solaris2/hostname_spec.rb +1 -1
  151. data/spec/unit/plugins/solaris2/kernel_spec.rb +1 -1
  152. data/spec/unit/plugins/solaris2/memory_spec.rb +1 -1
  153. data/spec/unit/plugins/solaris2/network_spec.rb +7 -4
  154. data/spec/unit/plugins/solaris2/platform_spec.rb +1 -1
  155. data/spec/unit/plugins/solaris2/virtualization_spec.rb +1 -1
  156. data/spec/unit/plugins/solaris2/zpools_spec.rb +1 -1
  157. data/spec/unit/plugins/ssh_host_keys_spec.rb +1 -1
  158. data/spec/unit/plugins/sysconf_spec.rb +1 -1
  159. data/spec/unit/plugins/timezone_spec.rb +1 -1
  160. data/spec/unit/plugins/virtualbox_spec.rb +1 -1
  161. data/spec/unit/plugins/vmware_spec.rb +2 -1
  162. data/spec/unit/plugins/windows/cpu_spec.rb +9 -2
  163. data/spec/unit/plugins/windows/fips_spec.rb +1 -1
  164. data/spec/unit/plugins/windows/memory_spec.rb +1 -1
  165. data/spec/unit/plugins/windows/uptime_spec.rb +1 -1
  166. data/spec/unit/plugins/windows/virtualization_spec.rb +1 -1
  167. data/spec/unit/provides_map_spec.rb +1 -1
  168. data/spec/unit/runner_spec.rb +1 -1
  169. data/spec/unit/system_spec.rb +68 -76
  170. data/spec/unit/util/file_helper_spec.rb +1 -1
  171. data/spec/unit/util/ip_helper_spec.rb +1 -1
  172. metadata +6 -20
  173. data/lib/ohai/plugins/cloud_v2.rb +0 -315
  174. data/lib/ohai/plugins/darwin/filesystem2.rb +0 -107
  175. data/lib/ohai/plugins/linux/filesystem2.rb +0 -223
  176. data/lib/ohai/plugins/linux/lspci.rb +0 -76
  177. data/lib/ohai/plugins/network_listeners.rb +0 -60
  178. data/lib/ohai/plugins/sigar/cpu.rb +0 -44
  179. data/lib/ohai/plugins/sigar/filesystem.rb +0 -50
  180. data/lib/ohai/plugins/sigar/memory.rb +0 -39
  181. data/lib/ohai/plugins/sigar/network.rb +0 -168
  182. data/lib/ohai/plugins/sigar/network_route.rb +0 -64
  183. data/lib/ohai/plugins/sigar/platform.rb +0 -30
  184. data/spec/unit/plugins/cloud_v2_spec.rb +0 -389
  185. data/spec/unit/plugins/darwin/filesystem2_spec.rb +0 -139
  186. data/spec/unit/plugins/linux/filesystem2_spec.rb +0 -437
  187. data/spec/unit/plugins/linux/lspci_spec.rb +0 -133
  188. data/spec/unit/plugins/sigar/network_route_spec.rb +0 -148
@@ -0,0 +1,47 @@
1
+
2
+ # Author:: Dylan Page (<dpage@digitalocean.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "net/http"
18
+
19
+ module Ohai
20
+ module Mixin
21
+ module DOMetadata
22
+
23
+ DO_METADATA_ADDR = "169.254.169.254" unless defined?(DO_METADATA_ADDR)
24
+ DO_METADATA_URL = "/metadata/v1.json" unless defined?(DO_METADATA_URL)
25
+
26
+ def http_client
27
+ Net::HTTP.start(DO_METADATA_ADDR).tap { |h| h.read_timeout = 6 }
28
+ end
29
+
30
+ def fetch_metadata
31
+ uri = "#{DO_METADATA_URL}"
32
+ response = http_client.get(uri)
33
+ case response.code
34
+ when "200"
35
+ parser = FFI_Yajl::Parser.new
36
+ parser.parse(response.body)
37
+ when "404"
38
+ Ohai::Log.debug("Mixin DOMetadata: Encountered 404 response retreiving Digital Ocean metadata: #{uri} ; continuing.")
39
+ {}
40
+ else
41
+ raise "Mixin DOMetadata: Encountered error retrieving Digital Ocean metadata (#{uri} returned #{response.code} response)"
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -2,7 +2,7 @@
2
2
  # Author:: Tim Dysinger (<tim@dysinger.net>)
3
3
  # Author:: Benjamin Black (<bb@chef.io>)
4
4
  # Author:: Christopher Brown (<cb@chef.io>)
5
- # Copyright:: 2009-2017 Chef Software, Inc.
5
+ # Copyright:: Copyright (c) 2009-2016 Chef Software, Inc.
6
6
  # License:: Apache License, Version 2.0
7
7
  #
8
8
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,7 +18,6 @@
18
18
  # limitations under the License.
19
19
 
20
20
  require "net/http"
21
- require "socket"
22
21
 
23
22
  module Ohai
24
23
  module Mixin
@@ -43,38 +42,12 @@ module Ohai
43
42
  EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR)
44
43
  EC2_SUPPORTED_VERSIONS = %w{ 1.0 2007-01-19 2007-03-01 2007-08-29 2007-10-10 2007-12-15
45
44
  2008-02-01 2008-09-01 2009-04-04 2011-01-01 2011-05-01 2012-01-12
46
- 2014-11-05 2014-02-25 2016-04-19 2016-06-30 2016-09-02}
45
+ 2014-11-05 2014-02-25 }
47
46
 
48
47
  EC2_ARRAY_VALUES = %w{security-groups}
49
48
  EC2_ARRAY_DIR = %w{network/interfaces/macs}
50
49
  EC2_JSON_DIR = %w{iam}
51
50
 
52
- def can_metadata_connect?(addr, port, timeout = 2)
53
- t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
54
- saddr = Socket.pack_sockaddr_in(port, addr)
55
- connected = false
56
-
57
- begin
58
- t.connect_nonblock(saddr)
59
- rescue Errno::EINPROGRESS
60
- r, w, e = IO.select(nil, [t], nil, timeout)
61
- if !w.nil?
62
- connected = true
63
- else
64
- begin
65
- t.connect_nonblock(saddr)
66
- rescue Errno::EISCONN
67
- t.close
68
- connected = true
69
- rescue SystemCallError
70
- end
71
- end
72
- rescue SystemCallError
73
- end
74
- Ohai::Log.debug("ec2 metadata mixin: can_metadata_connect? == #{connected}")
75
- connected
76
- end
77
-
78
51
  def best_api_version
79
52
  response = http_client.get("/")
80
53
  if response.code == "404"
@@ -15,7 +15,6 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require "net/http"
18
- require "socket"
19
18
 
20
19
  module Ohai
21
20
  module Mixin
@@ -25,38 +24,6 @@ module Ohai
25
24
  GCE_METADATA_ADDR = "metadata.google.internal." unless defined?(GCE_METADATA_ADDR)
26
25
  GCE_METADATA_URL = "/computeMetadata/v1/?recursive=true" unless defined?(GCE_METADATA_URL)
27
26
 
28
- def can_metadata_connect?(addr, port, timeout = 2)
29
- t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
30
- begin
31
- saddr = Socket.pack_sockaddr_in(port, addr)
32
- rescue SocketError => e # occurs when non-GCE systems try to resolve metadata.google.internal
33
- Ohai::Log.debug("Mixin GCE: can_metadata_connect? failed setting up socket: #{e}")
34
- return false
35
- end
36
-
37
- connected = false
38
-
39
- begin
40
- t.connect_nonblock(saddr)
41
- rescue Errno::EINPROGRESS
42
- r, w, e = IO.select(nil, [t], nil, timeout)
43
- if !w.nil?
44
- connected = true
45
- else
46
- begin
47
- t.connect_nonblock(saddr)
48
- rescue Errno::EISCONN
49
- t.close
50
- connected = true
51
- rescue SystemCallError
52
- end
53
- end
54
- rescue SystemCallError
55
- end
56
- Ohai::Log.debug("Mixin GCE: can_metadata_connect? == #{connected}")
57
- connected
58
- end
59
-
60
27
  # fetch the meta content with a timeout and the required header
61
28
  def http_get(uri)
62
29
  conn = Net::HTTP.start(GCE_METADATA_ADDR)
@@ -94,10 +61,6 @@ module Ohai
94
61
  end
95
62
  end
96
63
 
97
- def multiline?(data)
98
- data.lines.to_a.size > 1
99
- end
100
-
101
64
  def has_trailing_slash?(data)
102
65
  !! ( data =~ %r{/$} )
103
66
  end
@@ -0,0 +1,56 @@
1
+ #
2
+ # Copyright:: 2017, Chef Software, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "socket"
18
+
19
+ module Ohai
20
+ module Mixin
21
+ module HttpHelper
22
+
23
+ def can_socket_connect?(addr, port, timeout = 2)
24
+ t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
25
+ begin
26
+ saddr = Socket.pack_sockaddr_in(port, addr)
27
+ rescue SocketError => e # generally means dns resolution error
28
+ Ohai::Log.debug("Mixin HttpHelper: can_socket_connect? failed setting up socket connection: #{e}")
29
+ return false
30
+ end
31
+
32
+ connected = false
33
+
34
+ begin
35
+ t.connect_nonblock(saddr)
36
+ rescue Errno::EINPROGRESS
37
+ r, w, e = IO.select(nil, [t], nil, timeout)
38
+ if !w.nil?
39
+ connected = true
40
+ else
41
+ begin
42
+ t.connect_nonblock(saddr)
43
+ rescue Errno::EISCONN
44
+ t.close
45
+ connected = true
46
+ rescue SystemCallError
47
+ end
48
+ end
49
+ rescue SystemCallError
50
+ end
51
+ Ohai::Log.debug("Mixin HttpHelper: can_socket_connect? == #{connected}")
52
+ connected
53
+ end
54
+ end
55
+ end
56
+ end
@@ -24,11 +24,6 @@ module Ohai
24
24
  "inet" => "default",
25
25
  "inet6" => "default_inet6",
26
26
  }
27
-
28
- # From sigar: include/sigar.h sigar_net_route_t
29
- SIGAR_ROUTE_METHODS = [:destination, :gateway, :mask, :flags,
30
- :refcnt, :use, :metric, :mtu, :window,
31
- :irtt, :ifname]
32
27
  end
33
28
  end
34
29
  end
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
  Ohai.plugin(:Cloud) do
18
+ provides "cloud_v2"
18
19
  provides "cloud"
19
20
 
20
21
  depends "ec2"
@@ -25,36 +26,106 @@ Ohai.plugin(:Cloud) do
25
26
  depends "openstack"
26
27
  depends "azure"
27
28
  depends "digital_ocean"
28
- depends "softlayer"
29
29
 
30
- # Make top-level cloud hashes
30
+ # Class to help enforce the interface exposed to node[:cloud] (OHAI-542)
31
31
  #
32
- def create_objects
33
- cloud Mash.new
34
- cloud[:public_ips] = Array.new
35
- cloud[:private_ips] = Array.new
32
+ # cloud[:provider] - (String) the cloud provider the VM is running on.
33
+ #
34
+ # cloud[:public_hostname] - (String) a fully qualified hostname
35
+ # cloud[:local_hostname] - (String) a hostname resolvable on the internal (private) network
36
+ #
37
+ # cloud[:public_ipv4_addrs] - (Array) a list of all publicly accessible IPv4 addresses
38
+ # cloud[:local_ipv4_addrs] - (Array) a list of all private IPv4 addresses
39
+ # cloud[:public_ipv4] - (String) the first public IPv4 address detected
40
+ # cloud[:local_ipv4] - (String) the first private IPv4 address detected
41
+ #
42
+ # cloud[:public_ipv6_addrs] - (Array) a list of all publicly accessible IPv6 addresses
43
+ # cloud[:local_ipv6_addrs] - (Array) a list of all private IPv6 addresses
44
+ # cloud[:public_ipv6] - (String) the first public IPv6 address detected
45
+ # cloud[:local_ipv6] - (String) the first private IPv6 address detected
46
+ #
47
+ class CloudAttrs
48
+ attr_writer :provider, :public_hostname, :local_hostname
49
+
50
+ def initialize
51
+ @cloud = Mash.new
52
+ end
53
+
54
+ def add_ipv4_addr(ip, accessibility)
55
+ return if ip.nil? # just skip if ip is nil
56
+ ipaddr = validate_ip_addr(ip, :ipv4)
57
+
58
+ case accessibility
59
+ when :public
60
+ @cloud[:public_ipv4_addrs] ||= Array.new
61
+ @cloud[:public_ipv4_addrs] << ipaddr.to_s
62
+ when :private
63
+ @cloud[:local_ipv4_addrs] ||= Array.new
64
+ @cloud[:local_ipv4_addrs] << ipaddr.to_s
65
+ else
66
+ raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
67
+ end
68
+ end
69
+
70
+ def add_ipv6_addr(ip, accessibility)
71
+ return if ip.nil? # just skip if ip is nil
72
+ ipaddr = validate_ip_addr(ip, :ipv6)
73
+
74
+ raise "ERROR: invalid ipv6 address of '#{ip}' detected. " unless ipaddr.ipv6?
75
+ case accessibility
76
+ when :public
77
+ @cloud[:public_ipv6_addrs] ||= Array.new
78
+ @cloud[:public_ipv6_addrs] << ipaddr.to_s
79
+ when :private
80
+ @cloud[:local_ipv6_addrs] ||= Array.new
81
+ @cloud[:local_ipv6_addrs] << ipaddr.to_s
82
+ else
83
+ raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
84
+ end
85
+ end
86
+
87
+ def cloud_mash
88
+ @cloud[:provider] = @provider if @provider
89
+
90
+ @cloud[:public_hostname] = @public_hostname if @public_hostname
91
+ @cloud[:local_hostname] = @local_hostname if @local_hostname
92
+
93
+ @cloud[:public_ipv4] = @cloud[:public_ipv4_addrs][0] if @cloud[:public_ipv4_addrs]
94
+ @cloud[:local_ipv4] = @cloud[:local_ipv4_addrs][0] if @cloud[:local_ipv4_addrs]
95
+
96
+ @cloud[:public_ipv6] = @cloud[:public_ipv6_addrs][0] if @cloud[:public_ipv6_addrs]
97
+ @cloud[:local_ipv6] = @cloud[:local_ipv6_addrs][0] if @cloud[:local_ipv6_addrs]
98
+
99
+ # if empty, return nil
100
+ (@cloud.empty?) ? nil : @cloud
101
+ end
102
+
103
+ private
104
+
105
+ def validate_ip_addr(ip, address_family = :ipv4)
106
+ ipaddr = ""
107
+ begin
108
+ ipaddr = IPAddr.new(ip)
109
+ raise ArgumentError, "not valid #{address_family} address" unless (address_family == :ipv4) ? ipaddr.ipv4? : ipaddr.ipv6?
110
+ rescue ArgumentError => e
111
+ raise "ERROR: the ohai 'cloud' plugin failed with an IP address of '#{ip}' : #{e.message}"
112
+ end
113
+ ipaddr
114
+ end
36
115
  end
116
+
37
117
  #---------------------------------------
38
118
  # Google Compute Engine (gce)
39
119
  #--------------------------------------
40
120
 
41
- # Is current cloud gce?
42
- #
43
- # === Return
44
- # true:: If gce Hash is defined
45
- # false:: Otherwise
46
121
  def on_gce?
47
122
  gce != nil
48
123
  end
49
124
 
50
- # Fill cloud hash with gce values
51
125
  def get_gce_values
52
- cloud[:public_ipv4] = []
53
- cloud[:local_ipv4] = []
54
-
55
126
  public_ips = gce["instance"]["networkInterfaces"].collect do |interface|
56
127
  if interface.has_key?("accessConfigs")
57
- interface["accessConfigs"].collect { |ac| ac["externalIp"] }
128
+ interface["accessConfigs"].collect { |ac| ac["externalIp"] unless ac["externalIp"] == "" }
58
129
  end
59
130
  end.flatten.compact
60
131
 
@@ -62,13 +133,10 @@ Ohai.plugin(:Cloud) do
62
133
  interface["ip"]
63
134
  end.compact
64
135
 
65
- cloud[:public_ips] += public_ips
66
- cloud[:private_ips] += private_ips
67
- cloud[:public_ipv4] += public_ips
68
- cloud[:public_hostname] = nil
69
- cloud[:local_ipv4] += private_ips
70
- cloud[:local_hostname] = gce["instance"]["hostname"]
71
- cloud[:provider] = "gce"
136
+ public_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
137
+ private_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
138
+ @cloud_attr_obj.local_hostname = gce["instance"]["hostname"]
139
+ @cloud_attr_obj.provider = "gce"
72
140
  end
73
141
 
74
142
  # ----------------------------------------
@@ -86,13 +154,11 @@ Ohai.plugin(:Cloud) do
86
154
 
87
155
  # Fill cloud hash with ec2 values
88
156
  def get_ec2_values
89
- cloud[:public_ips] << ec2["public_ipv4"]
90
- cloud[:private_ips] << ec2["local_ipv4"]
91
- cloud[:public_ipv4] = ec2["public_ipv4"]
92
- cloud[:public_hostname] = ec2["public_hostname"]
93
- cloud[:local_ipv4] = ec2["local_ipv4"]
94
- cloud[:local_hostname] = ec2["local_hostname"]
95
- cloud[:provider] = "ec2"
157
+ @cloud_attr_obj.add_ipv4_addr(ec2["public_ipv4"], :public)
158
+ @cloud_attr_obj.add_ipv4_addr(ec2["local_ipv4"], :private)
159
+ @cloud_attr_obj.public_hostname = ec2["public_hostname"]
160
+ @cloud_attr_obj.local_hostname = ec2["local_hostname"]
161
+ @cloud_attr_obj.provider = "ec2"
96
162
  end
97
163
 
98
164
  # ----------------------------------------
@@ -110,15 +176,13 @@ Ohai.plugin(:Cloud) do
110
176
 
111
177
  # Fill cloud hash with rackspace values
112
178
  def get_rackspace_values
113
- cloud[:public_ips] << rackspace["public_ipv4"] if rackspace["public_ipv4"]
114
- cloud[:private_ips] << rackspace["local_ipv4"] if rackspace["local_ipv4"]
115
- cloud[:public_ipv4] = rackspace["public_ipv4"]
116
- cloud[:public_ipv6] = rackspace["public_ipv6"]
117
- cloud[:public_hostname] = rackspace["public_hostname"]
118
- cloud[:local_ipv4] = rackspace["local_ipv4"]
119
- cloud[:local_ipv6] = rackspace["local_ipv6"]
120
- cloud[:local_hostname] = rackspace["local_hostname"]
121
- cloud[:provider] = "rackspace"
179
+ @cloud_attr_obj.add_ipv4_addr(rackspace["public_ipv4"], :public)
180
+ @cloud_attr_obj.add_ipv4_addr(rackspace["local_ipv4"], :private)
181
+ @cloud_attr_obj.add_ipv6_addr(rackspace["public_ipv6"], :public)
182
+ @cloud_attr_obj.add_ipv6_addr(rackspace["local_ipv6"], :private)
183
+ @cloud_attr_obj.public_hostname = rackspace["public_hostname"]
184
+ @cloud_attr_obj.local_hostname = rackspace["local_hostname"]
185
+ @cloud_attr_obj.provider = "rackspace"
122
186
  end
123
187
 
124
188
  # ----------------------------------------
@@ -136,13 +200,11 @@ Ohai.plugin(:Cloud) do
136
200
 
137
201
  # Fill cloud hash with linode values
138
202
  def get_linode_values
139
- cloud[:public_ips] << linode["public_ip"]
140
- cloud[:private_ips] << linode["private_ip"]
141
- cloud[:public_ipv4] = linode["public_ipv4"]
142
- cloud[:public_hostname] = linode["public_hostname"]
143
- cloud[:local_ipv4] = linode["local_ipv4"]
144
- cloud[:local_hostname] = linode["local_hostname"]
145
- cloud[:provider] = "linode"
203
+ @cloud_attr_obj.add_ipv4_addr(linode["public_ip"], :public)
204
+ @cloud_attr_obj.add_ipv4_addr(linode["private_ip"], :private)
205
+ @cloud_attr_obj.public_hostname = linode["public_hostname"]
206
+ @cloud_attr_obj.local_hostname = linode["local_hostname"]
207
+ @cloud_attr_obj.provider = "linode"
146
208
  end
147
209
 
148
210
  # ----------------------------------------
@@ -159,13 +221,11 @@ Ohai.plugin(:Cloud) do
159
221
  end
160
222
 
161
223
  def get_eucalyptus_values
162
- cloud[:public_ips] << eucalyptus["public_ipv4"]
163
- cloud[:private_ips] << eucalyptus["local_ipv4"]
164
- cloud[:public_ipv4] = eucalyptus["public_ipv4"]
165
- cloud[:public_hostname] = eucalyptus["public_hostname"]
166
- cloud[:local_ipv4] = eucalyptus["local_ipv4"]
167
- cloud[:local_hostname] = eucalyptus["local_hostname"]
168
- cloud[:provider] = "eucalyptus"
224
+ @cloud_attr_obj.add_ipv4_addr(eucalyptus["public_ipv4"], :public)
225
+ @cloud_attr_obj.add_ipv4_addr(eucalyptus["local_ipv4"], :private)
226
+ @cloud_attr_obj.public_hostname = eucalyptus["public_hostname"]
227
+ @cloud_attr_obj.local_hostname = eucalyptus["local_hostname"]
228
+ @cloud_attr_obj.provider = "eucalyptus"
169
229
  end
170
230
 
171
231
  # ----------------------------------------
@@ -183,13 +243,11 @@ Ohai.plugin(:Cloud) do
183
243
 
184
244
  # Fill cloud hash with openstack values
185
245
  def get_openstack_values
186
- cloud[:public_ips] << openstack["public_ipv4"]
187
- cloud[:private_ips] << openstack["local_ipv4"]
188
- cloud[:public_ipv4] = openstack["public_ipv4"]
189
- cloud[:public_hostname] = openstack["public_hostname"]
190
- cloud[:local_ipv4] = openstack["local_ipv4"]
191
- cloud[:local_hostname] = openstack["local_hostname"]
192
- cloud[:provider] = openstack["provider"]
246
+ @cloud_attr_obj.add_ipv4_addr(openstack["public_ipv4"], :public)
247
+ @cloud_attr_obj.add_ipv4_addr(openstack["local_ipv4"], :private)
248
+ @cloud_attr_obj.public_hostname = openstack["public_hostname"]
249
+ @cloud_attr_obj.local_hostname = openstack["local_hostname"]
250
+ @cloud_attr_obj.provider = openstack["provider"]
193
251
  end
194
252
 
195
253
  # ----------------------------------------
@@ -207,15 +265,10 @@ Ohai.plugin(:Cloud) do
207
265
 
208
266
  # Fill cloud hash with azure values
209
267
  def get_azure_values
210
- cloud[:vm_name] = azure["vm_name"]
211
- cloud[:private_ips] << azure["private_ip"]
212
- cloud[:public_ips] << azure["public_ip"]
213
- cloud[:public_ipv4] = azure["public_ip"]
214
- cloud[:public_fqdn] = azure["public_fqdn"]
215
- cloud[:public_hostname] = azure["public_fqdn"]
216
- cloud[:public_ssh_port] = azure["public_ssh_port"] if azure["public_ssh_port"]
217
- cloud[:public_winrm_port] = azure["public_winrm_port"] if azure["public_winrm_port"]
218
- cloud[:provider] = "azure"
268
+ @cloud_attr_obj.add_ipv4_addr(azure["public_ip"], :public)
269
+ @cloud_attr_obj.add_ipv4_addr(azure["private_ip"], :private)
270
+ @cloud_attr_obj.public_hostname = azure["public_fqdn"]
271
+ @cloud_attr_obj.provider = "azure"
219
272
  end
220
273
 
221
274
  # ----------------------------------------
@@ -231,97 +284,31 @@ Ohai.plugin(:Cloud) do
231
284
  digital_ocean != nil
232
285
  end
233
286
 
234
- # Fill cloud hash with linode values
287
+ # Fill cloud hash with digital_ocean values
235
288
  def get_digital_ocean_values
236
- public_ipv4 = digital_ocean["networks"]["v4"].select { |address| address["type"] == "public" }
237
- private_ipv4 = digital_ocean["networks"]["v4"].select { |address| address["type"] == "private" }
238
- public_ipv6 = digital_ocean["networks"]["v6"].select { |address| address["type"] == "public" }
239
- private_ipv6 = digital_ocean["networks"]["v6"].select { |address| address["type"] == "private" }
240
- cloud[:public_ips].concat public_ipv4 + public_ipv6
241
- cloud[:private_ips].concat private_ipv4 + private_ipv6
242
- cloud[:public_ipv4] = public_ipv4.first
243
- cloud[:public_ipv6] = public_ipv6.first
244
- cloud[:local_ipv4] = private_ipv4.first
245
- cloud[:local_ipv6] = private_ipv6.first
246
- cloud[:public_hostname] = digital_ocean["name"]
247
- cloud[:provider] = "digital_ocean"
248
- end
249
-
250
- # ----------------------------------------
251
- # softlayer
252
- # ----------------------------------------
253
-
254
- # Is current cloud softlayer?
255
- #
256
- # === Return
257
- # true:: If softlayer Hash is defined
258
- # false:: Otherwise
259
- def on_softlayer?
260
- softlayer != nil
261
- end
262
-
263
- # Fill cloud hash with softlayer values
264
- def get_softlayer_values
265
- cloud[:public_ipv4] = softlayer["public_ipv4"]
266
- cloud[:local_ipv4] = softlayer["local_ipv4"]
267
- cloud[:public_ips] << softlayer["public_ipv4"] if softlayer["public_ipv4"]
268
- cloud[:private_ips] << softlayer["local_ipv4"] if softlayer["local_ipv4"]
269
- cloud[:public_hostname] = softlayer["public_fqdn"]
270
- cloud[:provider] = "softlayer"
289
+ @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["public"][0]["ipv4"]["ip_address"], :public) rescue NoMethodError
290
+ @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["private"][0]["ipv4"]["ip_address"], :private) rescue NoMethodError
291
+ @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["public"][0]["ipv6"]["ip_address"], :public) rescue NoMethodError
292
+ @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["private"][0]["ipv6"]["ip_address"], :private) rescue NoMethodError
293
+ @cloud_attr_obj.provider = "digital_ocean"
271
294
  end
272
295
 
273
296
  collect_data do
274
- # setup gce cloud
275
- if on_gce?
276
- create_objects
277
- get_gce_values
278
- end
279
-
280
- # setup ec2 cloud
281
- if on_ec2?
282
- create_objects
283
- get_ec2_values
284
- end
285
-
286
- # setup rackspace cloud
287
- if on_rackspace?
288
- create_objects
289
- get_rackspace_values
290
- end
291
-
292
- # setup linode cloud data
293
- if on_linode?
294
- create_objects
295
- get_linode_values
296
- end
297
-
298
- if on_eucalyptus?
299
- create_objects
300
- get_eucalyptus_values
301
- end
302
-
303
- # setup openstack cloud
304
- if on_openstack?
305
- create_objects
306
- get_openstack_values
307
- end
308
-
309
- # setup azure cloud data
310
- if on_azure?
311
- create_objects
312
- get_azure_values
313
- end
314
-
315
- # setup digital_ocean cloud data
316
- if on_digital_ocean?
317
- create_objects
318
- get_digital_ocean_values
319
- end
320
-
321
- # setup softlayer cloud
322
- if on_softlayer?
323
- create_objects
324
- get_softlayer_values
325
- end
297
+ require "ipaddr"
298
+
299
+ @cloud_attr_obj = CloudAttrs.new()
300
+
301
+ get_gce_values if on_gce?
302
+ get_ec2_values if on_ec2?
303
+ get_rackspace_values if on_rackspace?
304
+ get_linode_values if on_linode?
305
+ get_eucalyptus_values if on_eucalyptus?
306
+ get_openstack_values if on_openstack?
307
+ get_azure_values if on_azure?
308
+ get_digital_ocean_values if on_digital_ocean?
309
+
310
+ # set node[:cloud] and node[:cloud_v2] hash here
311
+ cloud_v2 @cloud_attr_obj.cloud_mash
312
+ cloud @cloud_attr_obj.cloud_mash
326
313
  end
327
314
  end