ohai 18.0.26 → 18.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +28 -28
  3. data/LICENSE +201 -201
  4. data/bin/ohai +25 -25
  5. data/lib/ohai/application.rb +189 -189
  6. data/lib/ohai/common/dmi.rb +167 -167
  7. data/lib/ohai/config.rb +51 -51
  8. data/lib/ohai/dsl/plugin/versionvii.rb +203 -203
  9. data/lib/ohai/dsl/plugin.rb +232 -232
  10. data/lib/ohai/dsl.rb +22 -22
  11. data/lib/ohai/exception.rb +36 -36
  12. data/lib/ohai/hints.rb +68 -68
  13. data/lib/ohai/loader.rb +178 -178
  14. data/lib/ohai/log.rb +34 -34
  15. data/lib/ohai/mash.rb +22 -22
  16. data/lib/ohai/mixin/alibaba_metadata.rb +86 -86
  17. data/lib/ohai/mixin/azure_metadata.rb +111 -111
  18. data/lib/ohai/mixin/chef_utils_wiring.rb +52 -52
  19. data/lib/ohai/mixin/command.rb +4 -4
  20. data/lib/ohai/mixin/constant_helper.rb +55 -55
  21. data/lib/ohai/mixin/dmi_decode.rb +54 -54
  22. data/lib/ohai/mixin/do_metadata.rb +48 -48
  23. data/lib/ohai/mixin/ec2_metadata.rb +256 -256
  24. data/lib/ohai/mixin/gce_metadata.rb +83 -83
  25. data/lib/ohai/mixin/http_helper.rb +64 -64
  26. data/lib/ohai/mixin/network_helper.rb +65 -65
  27. data/lib/ohai/mixin/oci_metadata.rb +69 -0
  28. data/lib/ohai/mixin/os.rb +128 -128
  29. data/lib/ohai/mixin/scaleway_metadata.rb +51 -51
  30. data/lib/ohai/mixin/seconds_to_human.rb +52 -52
  31. data/lib/ohai/mixin/shell_out.rb +51 -51
  32. data/lib/ohai/mixin/softlayer_metadata.rb +74 -74
  33. data/lib/ohai/mixin/string.rb +31 -31
  34. data/lib/ohai/mixin/train_helpers.rb +36 -36
  35. data/lib/ohai/mixin/which.rb +39 -39
  36. data/lib/ohai/plugin_config.rb +47 -47
  37. data/lib/ohai/plugins/aix/kernel.rb +50 -50
  38. data/lib/ohai/plugins/aix/memory.rb +37 -37
  39. data/lib/ohai/plugins/aix/network.rb +142 -142
  40. data/lib/ohai/plugins/aix/platform.rb +30 -30
  41. data/lib/ohai/plugins/aix/uptime.rb +54 -54
  42. data/lib/ohai/plugins/aix/virtualization.rb +154 -154
  43. data/lib/ohai/plugins/alibaba.rb +72 -72
  44. data/lib/ohai/plugins/azure.rb +154 -154
  45. data/lib/ohai/plugins/bsd/virtualization.rb +121 -121
  46. data/lib/ohai/plugins/c.rb +178 -178
  47. data/lib/ohai/plugins/chef.rb +50 -50
  48. data/lib/ohai/plugins/cloud.rb +379 -357
  49. data/lib/ohai/plugins/command.rb +26 -26
  50. data/lib/ohai/plugins/cpu.rb +635 -635
  51. data/lib/ohai/plugins/darwin/hardware.rb +99 -99
  52. data/lib/ohai/plugins/darwin/memory.rb +62 -62
  53. data/lib/ohai/plugins/darwin/network.rb +207 -207
  54. data/lib/ohai/plugins/darwin/platform.rb +38 -38
  55. data/lib/ohai/plugins/darwin/virtualization.rb +90 -93
  56. data/lib/ohai/plugins/digital_ocean.rb +67 -67
  57. data/lib/ohai/plugins/dmi.rb +134 -134
  58. data/lib/ohai/plugins/docker.rb +58 -58
  59. data/lib/ohai/plugins/dragonflybsd/memory.rb +60 -60
  60. data/lib/ohai/plugins/dragonflybsd/network.rb +128 -128
  61. data/lib/ohai/plugins/dragonflybsd/platform.rb +28 -28
  62. data/lib/ohai/plugins/ec2.rb +148 -148
  63. data/lib/ohai/plugins/elixir.rb +36 -36
  64. data/lib/ohai/plugins/erlang.rb +60 -60
  65. data/lib/ohai/plugins/eucalyptus.rb +86 -86
  66. data/lib/ohai/plugins/filesystem.rb +751 -751
  67. data/lib/ohai/plugins/fips.rb +36 -36
  68. data/lib/ohai/plugins/freebsd/memory.rb +60 -60
  69. data/lib/ohai/plugins/freebsd/network.rb +128 -128
  70. data/lib/ohai/plugins/freebsd/platform.rb +28 -28
  71. data/lib/ohai/plugins/gce.rb +89 -89
  72. data/lib/ohai/plugins/go.rb +34 -34
  73. data/lib/ohai/plugins/groovy.rb +38 -38
  74. data/lib/ohai/plugins/grub2.rb +40 -40
  75. data/lib/ohai/plugins/habitat.rb +73 -73
  76. data/lib/ohai/plugins/haskell.rb +96 -96
  77. data/lib/ohai/plugins/hostname.rb +133 -133
  78. data/lib/ohai/plugins/init_package.rb +26 -26
  79. data/lib/ohai/plugins/java.rb +78 -78
  80. data/lib/ohai/plugins/kernel.rb +292 -292
  81. data/lib/ohai/plugins/keys.rb +27 -27
  82. data/lib/ohai/plugins/languages.rb +26 -26
  83. data/lib/ohai/plugins/libvirt.rb +114 -114
  84. data/lib/ohai/plugins/linode.rb +73 -73
  85. data/lib/ohai/plugins/linux/block_device.rb +48 -48
  86. data/lib/ohai/plugins/linux/hostnamectl.rb +34 -34
  87. data/lib/ohai/plugins/linux/interrupts.rb +84 -84
  88. data/lib/ohai/plugins/linux/ipc.rb +52 -52
  89. data/lib/ohai/plugins/linux/livepatch.rb +38 -38
  90. data/lib/ohai/plugins/linux/lsb.rb +46 -46
  91. data/lib/ohai/plugins/linux/lspci.rb +76 -76
  92. data/lib/ohai/plugins/linux/machineid.rb +36 -36
  93. data/lib/ohai/plugins/linux/mdadm.rb +120 -120
  94. data/lib/ohai/plugins/linux/memory.rb +106 -106
  95. data/lib/ohai/plugins/linux/network.rb +879 -879
  96. data/lib/ohai/plugins/linux/os_release.rb +38 -38
  97. data/lib/ohai/plugins/linux/platform.rb +314 -314
  98. data/lib/ohai/plugins/linux/selinux.rb +69 -69
  99. data/lib/ohai/plugins/linux/sessions.rb +54 -54
  100. data/lib/ohai/plugins/linux/sysctl.rb +39 -39
  101. data/lib/ohai/plugins/linux/systemd_paths.rb +36 -36
  102. data/lib/ohai/plugins/linux/tc.rb +61 -61
  103. data/lib/ohai/plugins/linux/virtualization.rb +300 -300
  104. data/lib/ohai/plugins/lua.rb +39 -39
  105. data/lib/ohai/plugins/mono.rb +50 -50
  106. data/lib/ohai/plugins/netbsd/memory.rb +99 -99
  107. data/lib/ohai/plugins/netbsd/network.rb +122 -122
  108. data/lib/ohai/plugins/netbsd/platform.rb +28 -28
  109. data/lib/ohai/plugins/network.rb +186 -186
  110. data/lib/ohai/plugins/nodejs.rb +40 -40
  111. data/lib/ohai/plugins/oci.rb +94 -0
  112. data/lib/ohai/plugins/ohai.rb +29 -29
  113. data/lib/ohai/plugins/ohai_time.rb +26 -26
  114. data/lib/ohai/plugins/openbsd/memory.rb +99 -99
  115. data/lib/ohai/plugins/openbsd/network.rb +122 -122
  116. data/lib/ohai/plugins/openbsd/platform.rb +28 -28
  117. data/lib/ohai/plugins/openstack.rb +84 -84
  118. data/lib/ohai/plugins/os.rb +55 -55
  119. data/lib/ohai/plugins/packages.rb +234 -234
  120. data/lib/ohai/plugins/passwd.rb +104 -104
  121. data/lib/ohai/plugins/perl.rb +45 -45
  122. data/lib/ohai/plugins/php.rb +52 -52
  123. data/lib/ohai/plugins/platform.rb +29 -29
  124. data/lib/ohai/plugins/powershell.rb +82 -82
  125. data/lib/ohai/plugins/ps.rb +35 -35
  126. data/lib/ohai/plugins/python.rb +43 -43
  127. data/lib/ohai/plugins/rackspace.rb +177 -177
  128. data/lib/ohai/plugins/root_group.rb +41 -41
  129. data/lib/ohai/plugins/rpm.rb +121 -121
  130. data/lib/ohai/plugins/ruby.rb +66 -66
  131. data/lib/ohai/plugins/rust.rb +34 -34
  132. data/lib/ohai/plugins/scala.rb +38 -38
  133. data/lib/ohai/plugins/scaleway.rb +58 -58
  134. data/lib/ohai/plugins/scsi.rb +52 -52
  135. data/lib/ohai/plugins/shard.rb +142 -142
  136. data/lib/ohai/plugins/shells.rb +32 -32
  137. data/lib/ohai/plugins/softlayer.rb +48 -48
  138. data/lib/ohai/plugins/solaris2/dmi.rb +191 -191
  139. data/lib/ohai/plugins/solaris2/memory.rb +32 -32
  140. data/lib/ohai/plugins/solaris2/network.rb +192 -192
  141. data/lib/ohai/plugins/solaris2/platform.rb +58 -58
  142. data/lib/ohai/plugins/solaris2/virtualization.rb +90 -90
  143. data/lib/ohai/plugins/ssh_host_key.rb +84 -84
  144. data/lib/ohai/plugins/sysconf.rb +46 -46
  145. data/lib/ohai/plugins/timezone.rb +45 -25
  146. data/lib/ohai/plugins/train.rb +35 -35
  147. data/lib/ohai/plugins/uptime.rb +95 -95
  148. data/lib/ohai/plugins/virtualbox.rb +197 -197
  149. data/lib/ohai/plugins/vmware.rb +109 -94
  150. data/lib/ohai/plugins/windows/dmi.rb +95 -95
  151. data/lib/ohai/plugins/windows/drivers.rb +52 -52
  152. data/lib/ohai/plugins/windows/memory.rb +39 -39
  153. data/lib/ohai/plugins/windows/network.rb +222 -222
  154. data/lib/ohai/plugins/windows/platform.rb +34 -34
  155. data/lib/ohai/plugins/windows/system_enclosure.rb +29 -29
  156. data/lib/ohai/plugins/windows/virtualization.rb +45 -45
  157. data/lib/ohai/plugins/zpools.rb +94 -94
  158. data/lib/ohai/provides_map.rb +208 -208
  159. data/lib/ohai/runner.rb +128 -128
  160. data/lib/ohai/system.rb +258 -258
  161. data/lib/ohai/train_transport.rb +29 -29
  162. data/lib/ohai/util/file_helper.rb +6 -6
  163. data/lib/ohai/util/ip_helper.rb +56 -56
  164. data/lib/ohai/util/win32.rb +47 -47
  165. data/lib/ohai/version.rb +23 -23
  166. data/lib/ohai.rb +23 -23
  167. data/ohai.gemspec +35 -35
  168. metadata +5 -3
@@ -1,357 +1,379 @@
1
- # frozen_string_literal: true
2
- #
3
- # Author:: Cary Penniman (<cary@rightscale.com>)
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
-
18
- Ohai.plugin(:Cloud) do
19
- provides "cloud"
20
-
21
- depends "alibaba"
22
- depends "ec2"
23
- depends "gce"
24
- depends "rackspace"
25
- depends "eucalyptus"
26
- depends "linode"
27
- depends "openstack"
28
- depends "azure"
29
- depends "digital_ocean"
30
- depends "softlayer"
31
-
32
- # Class to help enforce the interface exposed to node[:cloud] (OHAI-542)
33
- #
34
- # cloud[:provider] - (String) the cloud provider the VM is running on.
35
- #
36
- # cloud[:public_hostname] - (String) a fully qualified hostname
37
- # cloud[:local_hostname] - (String) a hostname resolvable on the internal (private) network
38
- #
39
- # cloud[:public_ipv4_addrs] - (Array) a list of all publicly accessible IPv4 addresses
40
- # cloud[:local_ipv4_addrs] - (Array) a list of all private IPv4 addresses
41
- # cloud[:public_ipv4] - (String) the first public IPv4 address detected
42
- # cloud[:local_ipv4] - (String) the first private IPv4 address detected
43
- #
44
- # cloud[:public_ipv6_addrs] - (Array) a list of all publicly accessible IPv6 addresses
45
- # cloud[:local_ipv6_addrs] - (Array) a list of all private IPv6 addresses
46
- # cloud[:public_ipv6] - (String) the first public IPv6 address detected
47
- # cloud[:local_ipv6] - (String) the first private IPv6 address detected
48
- #
49
- class CloudAttrs
50
- attr_writer :provider, :public_hostname, :local_hostname
51
-
52
- def initialize
53
- @cloud = Mash.new
54
- end
55
-
56
- def add_ipv4_addr(ip, accessibility)
57
- return if ip.nil? # just skip if ip is nil
58
-
59
- ipaddr = validate_ip_addr(ip, :ipv4)
60
-
61
- case accessibility
62
- when :public
63
- @cloud[:public_ipv4_addrs] ||= []
64
- @cloud[:public_ipv4_addrs] << ipaddr.to_s
65
- when :private
66
- @cloud[:local_ipv4_addrs] ||= []
67
- @cloud[:local_ipv4_addrs] << ipaddr.to_s
68
- else
69
- raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
70
- end
71
- end
72
-
73
- def add_ipv6_addr(ip, accessibility)
74
- return if ip.nil? # just skip if ip is nil
75
-
76
- ipaddr = validate_ip_addr(ip, :ipv6)
77
-
78
- raise "ERROR: invalid ipv6 address of '#{ip}' detected. " unless ipaddr.ipv6?
79
-
80
- case accessibility
81
- when :public
82
- @cloud[:public_ipv6_addrs] ||= []
83
- @cloud[:public_ipv6_addrs] << ipaddr.to_s
84
- when :private
85
- @cloud[:local_ipv6_addrs] ||= []
86
- @cloud[:local_ipv6_addrs] << ipaddr.to_s
87
- else
88
- raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
89
- end
90
- end
91
-
92
- def cloud_mash
93
- @cloud[:provider] = @provider if @provider
94
-
95
- @cloud[:public_hostname] = @public_hostname if @public_hostname
96
- @cloud[:local_hostname] = @local_hostname if @local_hostname
97
-
98
- @cloud[:public_ipv4] = @cloud[:public_ipv4_addrs][0] if @cloud[:public_ipv4_addrs]
99
- @cloud[:local_ipv4] = @cloud[:local_ipv4_addrs][0] if @cloud[:local_ipv4_addrs]
100
-
101
- @cloud[:public_ipv6] = @cloud[:public_ipv6_addrs][0] if @cloud[:public_ipv6_addrs]
102
- @cloud[:local_ipv6] = @cloud[:local_ipv6_addrs][0] if @cloud[:local_ipv6_addrs]
103
-
104
- # if empty, return nil
105
- @cloud.empty? ? nil : @cloud
106
- end
107
-
108
- private
109
-
110
- def validate_ip_addr(ip, address_family = :ipv4)
111
- ipaddr = ""
112
- begin
113
- ipaddr = IPAddr.new(ip)
114
- raise ArgumentError, "not valid #{address_family} address" unless address_family == :ipv4 ? ipaddr.ipv4? : ipaddr.ipv6?
115
- rescue ArgumentError => e
116
- raise "ERROR: the ohai 'cloud' plugin failed with an IP address of '#{ip}' : #{e.message}"
117
- end
118
- ipaddr
119
- end
120
- end
121
-
122
- #--------------------------------------
123
- # Alibaba Cloud
124
- #--------------------------------------
125
-
126
- def on_alibaba?
127
- alibaba != nil
128
- end
129
-
130
- def get_alibaba_values
131
- @cloud_attr_obj.add_ipv4_addr(alibaba["meta_data"]["eipv4"], :public)
132
- @cloud_attr_obj.add_ipv4_addr(alibaba["meta_data"]["private_ipv4"], :private)
133
- @cloud_attr_obj.local_hostname = alibaba["meta_data"]["hostname"]
134
- @cloud_attr_obj.provider = "alibaba"
135
- end
136
-
137
- #--------------------------------------
138
- # Google Compute Engine (gce)
139
- #--------------------------------------
140
-
141
- def on_gce?
142
- gce != nil
143
- end
144
-
145
- def get_gce_values
146
- public_ips = gce["instance"]["networkInterfaces"].collect do |interface|
147
- if interface.key?("accessConfigs")
148
- interface["accessConfigs"].collect { |ac| ac["externalIp"] unless ac["externalIp"] == "" }
149
- end
150
- end.flatten.compact
151
-
152
- private_ips = gce["instance"]["networkInterfaces"].filter_map do |interface|
153
- interface["ip"]
154
- end
155
-
156
- public_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
157
- private_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
158
- @cloud_attr_obj.local_hostname = gce["instance"]["hostname"]
159
- @cloud_attr_obj.provider = "gce"
160
- end
161
-
162
- # ----------------------------------------
163
- # ec2
164
- # ----------------------------------------
165
-
166
- # Is current cloud ec2?
167
- #
168
- # === Return
169
- # true:: If ec2 Hash is defined
170
- # false:: Otherwise
171
- def on_ec2?
172
- ec2 != nil
173
- end
174
-
175
- # Fill cloud hash with ec2 values
176
- def get_ec2_values
177
- @cloud_attr_obj.add_ipv4_addr(ec2["public_ipv4"], :public)
178
- @cloud_attr_obj.add_ipv4_addr(ec2["local_ipv4"], :private)
179
- @cloud_attr_obj.public_hostname = ec2["public_hostname"]
180
- @cloud_attr_obj.local_hostname = ec2["local_hostname"]
181
- @cloud_attr_obj.provider = "ec2"
182
- end
183
-
184
- # ----------------------------------------
185
- # rackspace
186
- # ----------------------------------------
187
-
188
- # Is current cloud rackspace?
189
- #
190
- # === Return
191
- # true:: If rackspace Hash is defined
192
- # false:: Otherwise
193
- def on_rackspace?
194
- rackspace != nil
195
- end
196
-
197
- # Fill cloud hash with rackspace values
198
- def get_rackspace_values
199
- @cloud_attr_obj.add_ipv4_addr(rackspace["public_ipv4"], :public)
200
- @cloud_attr_obj.add_ipv4_addr(rackspace["local_ipv4"], :private)
201
- @cloud_attr_obj.add_ipv6_addr(rackspace["public_ipv6"], :public)
202
- @cloud_attr_obj.add_ipv6_addr(rackspace["local_ipv6"], :private)
203
- @cloud_attr_obj.public_hostname = rackspace["public_hostname"]
204
- @cloud_attr_obj.local_hostname = rackspace["local_hostname"]
205
- @cloud_attr_obj.provider = "rackspace"
206
- end
207
-
208
- # ----------------------------------------
209
- # linode
210
- # ----------------------------------------
211
-
212
- # Is current cloud linode?
213
- #
214
- # === Return
215
- # true:: If linode Hash is defined
216
- # false:: Otherwise
217
- def on_linode?
218
- linode != nil
219
- end
220
-
221
- # Fill cloud hash with linode values
222
- def get_linode_values
223
- @cloud_attr_obj.add_ipv4_addr(linode["public_ip"], :public)
224
- @cloud_attr_obj.add_ipv4_addr(linode["private_ip"], :private)
225
- @cloud_attr_obj.public_hostname = linode["public_hostname"]
226
- @cloud_attr_obj.local_hostname = linode["local_hostname"]
227
- @cloud_attr_obj.provider = "linode"
228
- end
229
-
230
- # ----------------------------------------
231
- # eucalyptus
232
- # ----------------------------------------
233
-
234
- # Is current cloud eucalyptus?
235
- #
236
- # === Return
237
- # true:: If eucalyptus Hash is defined
238
- # false:: Otherwise
239
- def on_eucalyptus?
240
- eucalyptus != nil
241
- end
242
-
243
- def get_eucalyptus_values
244
- @cloud_attr_obj.add_ipv4_addr(eucalyptus["public_ipv4"], :public)
245
- @cloud_attr_obj.add_ipv4_addr(eucalyptus["local_ipv4"], :private)
246
- @cloud_attr_obj.public_hostname = eucalyptus["public_hostname"]
247
- @cloud_attr_obj.local_hostname = eucalyptus["local_hostname"]
248
- @cloud_attr_obj.provider = "eucalyptus"
249
- end
250
-
251
- # ----------------------------------------
252
- # openstack
253
- # ----------------------------------------
254
-
255
- # Is current cloud openstack-based?
256
- #
257
- # === Return
258
- # true:: If openstack Hash is defined
259
- # false:: Otherwise
260
- def on_openstack?
261
- openstack != nil
262
- end
263
-
264
- # Fill cloud hash with openstack values
265
- def get_openstack_values
266
- @cloud_attr_obj.add_ipv4_addr(openstack["public_ipv4"], :public)
267
- @cloud_attr_obj.add_ipv4_addr(openstack["local_ipv4"], :private)
268
- @cloud_attr_obj.public_hostname = openstack["public_hostname"]
269
- @cloud_attr_obj.local_hostname = openstack["local_hostname"]
270
- @cloud_attr_obj.provider = openstack["provider"]
271
- end
272
-
273
- # ----------------------------------------
274
- # azure
275
- # ----------------------------------------
276
-
277
- # Is current cloud azure?
278
- #
279
- # === Return
280
- # true:: If azure Hash is defined
281
- # false:: Otherwise
282
- def on_azure?
283
- azure != nil
284
- end
285
-
286
- # Fill cloud hash with azure values
287
- def get_azure_values
288
- azure["metadata"]["network"]["public_ipv4"].each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
289
- azure["metadata"]["network"]["public_ipv6"].each { |ipaddr| @cloud_attr_obj.add_ipv6_addr(ipaddr, :public) }
290
- azure["metadata"]["network"]["local_ipv4"].each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
291
- azure["metadata"]["network"]["local_ipv6"].each { |ipaddr| @cloud_attr_obj.add_ipv6_addr(ipaddr, :private) }
292
- @cloud_attr_obj.public_hostname = azure["public_fqdn"]
293
- @cloud_attr_obj.provider = "azure"
294
- end
295
-
296
- # ----------------------------------------
297
- # digital_ocean
298
- # ----------------------------------------
299
-
300
- # Is current cloud digital_ocean?
301
- #
302
- # === Return
303
- # true:: If digital_ocean Mash is defined
304
- # false:: Otherwise
305
- def on_digital_ocean?
306
- digital_ocean != nil
307
- end
308
-
309
- # Fill cloud hash with digital_ocean values
310
- def get_digital_ocean_values
311
- @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["public"][0]["ipv4"]["ip_address"], :public) rescue NoMethodError
312
- @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["private"][0]["ipv4"]["ip_address"], :private) rescue NoMethodError
313
- @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["public"][0]["ipv6"]["ip_address"], :public) rescue NoMethodError
314
- @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["private"][0]["ipv6"]["ip_address"], :private) rescue NoMethodError
315
- @cloud_attr_obj.provider = "digital_ocean"
316
- end
317
-
318
- # ----------------------------------------
319
- # softlayer
320
- # ----------------------------------------
321
-
322
- # Is current cloud softlayer?
323
- #
324
- # === Return
325
- # true:: If softlayer Hash is defined
326
- # false:: Otherwise
327
- def on_softlayer?
328
- softlayer != nil
329
- end
330
-
331
- # Fill cloud hash with softlayer values
332
- def get_softlayer_values
333
- @cloud_attr_obj.add_ipv4_addr(softlayer["public_ipv4"], :public)
334
- @cloud_attr_obj.add_ipv4_addr(softlayer["local_ipv4"], :private)
335
- @cloud_attr_obj.public_hostname = softlayer["public_fqdn"]
336
- @cloud_attr_obj.provider = "softlayer"
337
- end
338
-
339
- collect_data do
340
- require "ipaddr" unless defined?(IPAddr)
341
-
342
- @cloud_attr_obj = CloudAttrs.new
343
-
344
- get_gce_values if on_gce?
345
- get_ec2_values if on_ec2?
346
- get_rackspace_values if on_rackspace?
347
- get_linode_values if on_linode?
348
- get_eucalyptus_values if on_eucalyptus?
349
- get_openstack_values if on_openstack?
350
- get_azure_values if on_azure?
351
- get_digital_ocean_values if on_digital_ocean?
352
- get_softlayer_values if on_softlayer?
353
- get_alibaba_values if on_alibaba?
354
-
355
- cloud @cloud_attr_obj.cloud_mash
356
- end
357
- end
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Cary Penniman (<cary@rightscale.com>)
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ Ohai.plugin(:Cloud) do
19
+ provides "cloud"
20
+
21
+ depends "alibaba"
22
+ depends "ec2"
23
+ depends "gce"
24
+ depends "rackspace"
25
+ depends "eucalyptus"
26
+ depends "linode"
27
+ depends "openstack"
28
+ depends "azure"
29
+ depends "digital_ocean"
30
+ depends "softlayer"
31
+ depends "oci"
32
+
33
+ # Class to help enforce the interface exposed to node[:cloud] (OHAI-542)
34
+ #
35
+ # cloud[:provider] - (String) the cloud provider the VM is running on.
36
+ #
37
+ # cloud[:public_hostname] - (String) a fully qualified hostname
38
+ # cloud[:local_hostname] - (String) a hostname resolvable on the internal (private) network
39
+ #
40
+ # cloud[:public_ipv4_addrs] - (Array) a list of all publicly accessible IPv4 addresses
41
+ # cloud[:local_ipv4_addrs] - (Array) a list of all private IPv4 addresses
42
+ # cloud[:public_ipv4] - (String) the first public IPv4 address detected
43
+ # cloud[:local_ipv4] - (String) the first private IPv4 address detected
44
+ #
45
+ # cloud[:public_ipv6_addrs] - (Array) a list of all publicly accessible IPv6 addresses
46
+ # cloud[:local_ipv6_addrs] - (Array) a list of all private IPv6 addresses
47
+ # cloud[:public_ipv6] - (String) the first public IPv6 address detected
48
+ # cloud[:local_ipv6] - (String) the first private IPv6 address detected
49
+ #
50
+ class CloudAttrs
51
+ attr_writer :provider, :public_hostname, :local_hostname
52
+
53
+ def initialize
54
+ @cloud = Mash.new
55
+ end
56
+
57
+ def add_ipv4_addr(ip, accessibility)
58
+ return if ip.nil? # just skip if ip is nil
59
+
60
+ ipaddr = validate_ip_addr(ip, :ipv4)
61
+
62
+ case accessibility
63
+ when :public
64
+ @cloud[:public_ipv4_addrs] ||= []
65
+ @cloud[:public_ipv4_addrs] << ipaddr.to_s
66
+ when :private
67
+ @cloud[:local_ipv4_addrs] ||= []
68
+ @cloud[:local_ipv4_addrs] << ipaddr.to_s
69
+ else
70
+ raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
71
+ end
72
+ end
73
+
74
+ def add_ipv6_addr(ip, accessibility)
75
+ return if ip.nil? # just skip if ip is nil
76
+
77
+ ipaddr = validate_ip_addr(ip, :ipv6)
78
+
79
+ raise "ERROR: invalid ipv6 address of '#{ip}' detected. " unless ipaddr.ipv6?
80
+
81
+ case accessibility
82
+ when :public
83
+ @cloud[:public_ipv6_addrs] ||= []
84
+ @cloud[:public_ipv6_addrs] << ipaddr.to_s
85
+ when :private
86
+ @cloud[:local_ipv6_addrs] ||= []
87
+ @cloud[:local_ipv6_addrs] << ipaddr.to_s
88
+ else
89
+ raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
90
+ end
91
+ end
92
+
93
+ def cloud_mash
94
+ @cloud[:provider] = @provider if @provider
95
+
96
+ @cloud[:public_hostname] = @public_hostname if @public_hostname
97
+ @cloud[:local_hostname] = @local_hostname if @local_hostname
98
+
99
+ @cloud[:public_ipv4] = @cloud[:public_ipv4_addrs][0] if @cloud[:public_ipv4_addrs]
100
+ @cloud[:local_ipv4] = @cloud[:local_ipv4_addrs][0] if @cloud[:local_ipv4_addrs]
101
+
102
+ @cloud[:public_ipv6] = @cloud[:public_ipv6_addrs][0] if @cloud[:public_ipv6_addrs]
103
+ @cloud[:local_ipv6] = @cloud[:local_ipv6_addrs][0] if @cloud[:local_ipv6_addrs]
104
+
105
+ # if empty, return nil
106
+ @cloud.empty? ? nil : @cloud
107
+ end
108
+
109
+ private
110
+
111
+ def validate_ip_addr(ip, address_family = :ipv4)
112
+ ipaddr = ""
113
+ begin
114
+ ipaddr = IPAddr.new(ip)
115
+ raise ArgumentError, "not valid #{address_family} address" unless address_family == :ipv4 ? ipaddr.ipv4? : ipaddr.ipv6?
116
+ rescue ArgumentError => e
117
+ raise "ERROR: the ohai 'cloud' plugin failed with an IP address of '#{ip}' : #{e.message}"
118
+ end
119
+ ipaddr
120
+ end
121
+ end
122
+
123
+ #--------------------------------------
124
+ # Alibaba Cloud
125
+ #--------------------------------------
126
+
127
+ def on_alibaba?
128
+ alibaba != nil
129
+ end
130
+
131
+ def get_alibaba_values
132
+ @cloud_attr_obj.add_ipv4_addr(alibaba["meta_data"]["eipv4"], :public)
133
+ @cloud_attr_obj.add_ipv4_addr(alibaba["meta_data"]["private_ipv4"], :private)
134
+ @cloud_attr_obj.local_hostname = alibaba["meta_data"]["hostname"]
135
+ @cloud_attr_obj.provider = "alibaba"
136
+ end
137
+
138
+ #--------------------------------------
139
+ # Google Compute Engine (gce)
140
+ #--------------------------------------
141
+
142
+ def on_gce?
143
+ gce != nil
144
+ end
145
+
146
+ def get_gce_values
147
+ public_ips = gce["instance"]["networkInterfaces"].collect do |interface|
148
+ if interface.key?("accessConfigs")
149
+ interface["accessConfigs"].collect { |ac| ac["externalIp"] unless ac["externalIp"] == "" }
150
+ end
151
+ end.flatten.compact
152
+
153
+ private_ips = gce["instance"]["networkInterfaces"].filter_map do |interface|
154
+ interface["ip"]
155
+ end
156
+
157
+ public_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
158
+ private_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
159
+ @cloud_attr_obj.local_hostname = gce["instance"]["hostname"]
160
+ @cloud_attr_obj.provider = "gce"
161
+ end
162
+
163
+ # ----------------------------------------
164
+ # ec2
165
+ # ----------------------------------------
166
+
167
+ # Is current cloud ec2?
168
+ #
169
+ # === Return
170
+ # true:: If ec2 Hash is defined
171
+ # false:: Otherwise
172
+ def on_ec2?
173
+ ec2 != nil
174
+ end
175
+
176
+ # Fill cloud hash with ec2 values
177
+ def get_ec2_values
178
+ @cloud_attr_obj.add_ipv4_addr(ec2["public_ipv4"], :public)
179
+ @cloud_attr_obj.add_ipv4_addr(ec2["local_ipv4"], :private)
180
+ @cloud_attr_obj.public_hostname = ec2["public_hostname"]
181
+ @cloud_attr_obj.local_hostname = ec2["local_hostname"]
182
+ @cloud_attr_obj.provider = "ec2"
183
+ end
184
+
185
+ # ----------------------------------------
186
+ # rackspace
187
+ # ----------------------------------------
188
+
189
+ # Is current cloud rackspace?
190
+ #
191
+ # === Return
192
+ # true:: If rackspace Hash is defined
193
+ # false:: Otherwise
194
+ def on_rackspace?
195
+ rackspace != nil
196
+ end
197
+
198
+ # Fill cloud hash with rackspace values
199
+ def get_rackspace_values
200
+ @cloud_attr_obj.add_ipv4_addr(rackspace["public_ipv4"], :public)
201
+ @cloud_attr_obj.add_ipv4_addr(rackspace["local_ipv4"], :private)
202
+ @cloud_attr_obj.add_ipv6_addr(rackspace["public_ipv6"], :public)
203
+ @cloud_attr_obj.add_ipv6_addr(rackspace["local_ipv6"], :private)
204
+ @cloud_attr_obj.public_hostname = rackspace["public_hostname"]
205
+ @cloud_attr_obj.local_hostname = rackspace["local_hostname"]
206
+ @cloud_attr_obj.provider = "rackspace"
207
+ end
208
+
209
+ # ----------------------------------------
210
+ # linode
211
+ # ----------------------------------------
212
+
213
+ # Is current cloud linode?
214
+ #
215
+ # === Return
216
+ # true:: If linode Hash is defined
217
+ # false:: Otherwise
218
+ def on_linode?
219
+ linode != nil
220
+ end
221
+
222
+ # Fill cloud hash with linode values
223
+ def get_linode_values
224
+ @cloud_attr_obj.add_ipv4_addr(linode["public_ip"], :public)
225
+ @cloud_attr_obj.add_ipv4_addr(linode["private_ip"], :private)
226
+ @cloud_attr_obj.public_hostname = linode["public_hostname"]
227
+ @cloud_attr_obj.local_hostname = linode["local_hostname"]
228
+ @cloud_attr_obj.provider = "linode"
229
+ end
230
+
231
+ # ----------------------------------------
232
+ # eucalyptus
233
+ # ----------------------------------------
234
+
235
+ # Is current cloud eucalyptus?
236
+ #
237
+ # === Return
238
+ # true:: If eucalyptus Hash is defined
239
+ # false:: Otherwise
240
+ def on_eucalyptus?
241
+ eucalyptus != nil
242
+ end
243
+
244
+ def get_eucalyptus_values
245
+ @cloud_attr_obj.add_ipv4_addr(eucalyptus["public_ipv4"], :public)
246
+ @cloud_attr_obj.add_ipv4_addr(eucalyptus["local_ipv4"], :private)
247
+ @cloud_attr_obj.public_hostname = eucalyptus["public_hostname"]
248
+ @cloud_attr_obj.local_hostname = eucalyptus["local_hostname"]
249
+ @cloud_attr_obj.provider = "eucalyptus"
250
+ end
251
+
252
+ # ----------------------------------------
253
+ # openstack
254
+ # ----------------------------------------
255
+
256
+ # Is current cloud openstack-based?
257
+ #
258
+ # === Return
259
+ # true:: If openstack Hash is defined
260
+ # false:: Otherwise
261
+ def on_openstack?
262
+ openstack != nil
263
+ end
264
+
265
+ # Fill cloud hash with openstack values
266
+ def get_openstack_values
267
+ @cloud_attr_obj.add_ipv4_addr(openstack["public_ipv4"], :public)
268
+ @cloud_attr_obj.add_ipv4_addr(openstack["local_ipv4"], :private)
269
+ @cloud_attr_obj.public_hostname = openstack["public_hostname"]
270
+ @cloud_attr_obj.local_hostname = openstack["local_hostname"]
271
+ @cloud_attr_obj.provider = openstack["provider"]
272
+ end
273
+
274
+ # ----------------------------------------
275
+ # azure
276
+ # ----------------------------------------
277
+
278
+ # Is current cloud azure?
279
+ #
280
+ # === Return
281
+ # true:: If azure Hash is defined
282
+ # false:: Otherwise
283
+ def on_azure?
284
+ azure != nil
285
+ end
286
+
287
+ # Fill cloud hash with azure values
288
+ def get_azure_values
289
+ azure["metadata"]["network"]["public_ipv4"].each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
290
+ azure["metadata"]["network"]["public_ipv6"].each { |ipaddr| @cloud_attr_obj.add_ipv6_addr(ipaddr, :public) }
291
+ azure["metadata"]["network"]["local_ipv4"].each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
292
+ azure["metadata"]["network"]["local_ipv6"].each { |ipaddr| @cloud_attr_obj.add_ipv6_addr(ipaddr, :private) }
293
+ @cloud_attr_obj.public_hostname = azure["public_fqdn"]
294
+ @cloud_attr_obj.provider = "azure"
295
+ end
296
+
297
+ # ----------------------------------------
298
+ # digital_ocean
299
+ # ----------------------------------------
300
+
301
+ # Is current cloud digital_ocean?
302
+ #
303
+ # === Return
304
+ # true:: If digital_ocean Mash is defined
305
+ # false:: Otherwise
306
+ def on_digital_ocean?
307
+ digital_ocean != nil
308
+ end
309
+
310
+ # Fill cloud hash with digital_ocean values
311
+ def get_digital_ocean_values
312
+ @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["public"][0]["ipv4"]["ip_address"], :public) rescue NoMethodError
313
+ @cloud_attr_obj.add_ipv4_addr(digital_ocean["interfaces"]["private"][0]["ipv4"]["ip_address"], :private) rescue NoMethodError
314
+ @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["public"][0]["ipv6"]["ip_address"], :public) rescue NoMethodError
315
+ @cloud_attr_obj.add_ipv6_addr(digital_ocean["interfaces"]["private"][0]["ipv6"]["ip_address"], :private) rescue NoMethodError
316
+ @cloud_attr_obj.provider = "digital_ocean"
317
+ end
318
+
319
+ # ----------------------------------------
320
+ # softlayer
321
+ # ----------------------------------------
322
+
323
+ # Is current cloud softlayer?
324
+ #
325
+ # === Return
326
+ # true:: If softlayer Hash is defined
327
+ # false:: Otherwise
328
+ def on_softlayer?
329
+ softlayer != nil
330
+ end
331
+
332
+ # Fill cloud hash with softlayer values
333
+ def get_softlayer_values
334
+ @cloud_attr_obj.add_ipv4_addr(softlayer["public_ipv4"], :public)
335
+ @cloud_attr_obj.add_ipv4_addr(softlayer["local_ipv4"], :private)
336
+ @cloud_attr_obj.public_hostname = softlayer["public_fqdn"]
337
+ @cloud_attr_obj.provider = "softlayer"
338
+ end
339
+
340
+ # ----------------------------------------
341
+ # OCI
342
+ # ----------------------------------------
343
+
344
+ # Is current Oracle Cloud Infrastructure?
345
+ #
346
+ # === Return
347
+ # true:: If oci Hash is defined
348
+ # false:: Otherwise
349
+ def on_oci?
350
+ oci != nil
351
+ end
352
+
353
+ # Fill cloud hash with OCI values
354
+ def oci_values
355
+ oci["metadata"]["network"]["interface"].each { |vnic| @cloud_attr_obj.add_ipv4_addr(vnic["privateIp"], :private) }
356
+ @cloud_attr_obj.local_hostname = oci["metadata"]["compute"]["hostname"]
357
+ @cloud_attr_obj.provider = "oci"
358
+ end
359
+
360
+ collect_data do
361
+ require "ipaddr" unless defined?(IPAddr)
362
+
363
+ @cloud_attr_obj = CloudAttrs.new
364
+
365
+ get_gce_values if on_gce?
366
+ get_ec2_values if on_ec2?
367
+ get_rackspace_values if on_rackspace?
368
+ get_linode_values if on_linode?
369
+ get_eucalyptus_values if on_eucalyptus?
370
+ get_openstack_values if on_openstack?
371
+ get_azure_values if on_azure?
372
+ get_digital_ocean_values if on_digital_ocean?
373
+ get_softlayer_values if on_softlayer?
374
+ get_alibaba_values if on_alibaba?
375
+ oci_values if on_oci?
376
+
377
+ cloud @cloud_attr_obj.cloud_mash
378
+ end
379
+ end