ohai 18.0.14 → 18.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) 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 -163
  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 -63
  27. data/lib/ohai/mixin/os.rb +128 -128
  28. data/lib/ohai/mixin/scaleway_metadata.rb +51 -51
  29. data/lib/ohai/mixin/seconds_to_human.rb +52 -52
  30. data/lib/ohai/mixin/shell_out.rb +51 -51
  31. data/lib/ohai/mixin/softlayer_metadata.rb +74 -74
  32. data/lib/ohai/mixin/string.rb +31 -31
  33. data/lib/ohai/mixin/train_helpers.rb +36 -36
  34. data/lib/ohai/mixin/which.rb +39 -39
  35. data/lib/ohai/plugin_config.rb +47 -47
  36. data/lib/ohai/plugins/aix/kernel.rb +50 -50
  37. data/lib/ohai/plugins/aix/memory.rb +37 -37
  38. data/lib/ohai/plugins/aix/network.rb +142 -142
  39. data/lib/ohai/plugins/aix/platform.rb +30 -30
  40. data/lib/ohai/plugins/aix/uptime.rb +54 -54
  41. data/lib/ohai/plugins/aix/virtualization.rb +154 -154
  42. data/lib/ohai/plugins/alibaba.rb +72 -72
  43. data/lib/ohai/plugins/azure.rb +154 -154
  44. data/lib/ohai/plugins/bsd/virtualization.rb +121 -121
  45. data/lib/ohai/plugins/c.rb +178 -178
  46. data/lib/ohai/plugins/chef.rb +50 -50
  47. data/lib/ohai/plugins/cloud.rb +357 -357
  48. data/lib/ohai/plugins/command.rb +26 -26
  49. data/lib/ohai/plugins/cpu.rb +635 -626
  50. data/lib/ohai/plugins/darwin/hardware.rb +99 -99
  51. data/lib/ohai/plugins/darwin/memory.rb +62 -62
  52. data/lib/ohai/plugins/darwin/network.rb +207 -207
  53. data/lib/ohai/plugins/darwin/platform.rb +38 -38
  54. data/lib/ohai/plugins/darwin/virtualization.rb +93 -93
  55. data/lib/ohai/plugins/digital_ocean.rb +67 -67
  56. data/lib/ohai/plugins/dmi.rb +134 -134
  57. data/lib/ohai/plugins/docker.rb +58 -58
  58. data/lib/ohai/plugins/dragonflybsd/memory.rb +60 -60
  59. data/lib/ohai/plugins/dragonflybsd/network.rb +128 -128
  60. data/lib/ohai/plugins/dragonflybsd/platform.rb +28 -28
  61. data/lib/ohai/plugins/ec2.rb +148 -155
  62. data/lib/ohai/plugins/elixir.rb +36 -36
  63. data/lib/ohai/plugins/erlang.rb +60 -60
  64. data/lib/ohai/plugins/eucalyptus.rb +86 -86
  65. data/lib/ohai/plugins/filesystem.rb +751 -751
  66. data/lib/ohai/plugins/fips.rb +36 -35
  67. data/lib/ohai/plugins/freebsd/memory.rb +60 -60
  68. data/lib/ohai/plugins/freebsd/network.rb +128 -128
  69. data/lib/ohai/plugins/freebsd/platform.rb +28 -28
  70. data/lib/ohai/plugins/gce.rb +89 -89
  71. data/lib/ohai/plugins/go.rb +34 -34
  72. data/lib/ohai/plugins/groovy.rb +38 -38
  73. data/lib/ohai/plugins/grub2.rb +40 -40
  74. data/lib/ohai/plugins/habitat.rb +73 -73
  75. data/lib/ohai/plugins/haskell.rb +96 -96
  76. data/lib/ohai/plugins/hostname.rb +133 -133
  77. data/lib/ohai/plugins/init_package.rb +26 -26
  78. data/lib/ohai/plugins/java.rb +78 -78
  79. data/lib/ohai/plugins/kernel.rb +292 -292
  80. data/lib/ohai/plugins/keys.rb +27 -27
  81. data/lib/ohai/plugins/languages.rb +26 -26
  82. data/lib/ohai/plugins/libvirt.rb +114 -114
  83. data/lib/ohai/plugins/linode.rb +73 -73
  84. data/lib/ohai/plugins/linux/block_device.rb +48 -48
  85. data/lib/ohai/plugins/linux/hostnamectl.rb +34 -34
  86. data/lib/ohai/plugins/linux/interrupts.rb +84 -84
  87. data/lib/ohai/plugins/linux/ipc.rb +52 -52
  88. data/lib/ohai/plugins/linux/livepatch.rb +38 -38
  89. data/lib/ohai/plugins/linux/lsb.rb +46 -46
  90. data/lib/ohai/plugins/linux/lspci.rb +76 -76
  91. data/lib/ohai/plugins/linux/machineid.rb +36 -36
  92. data/lib/ohai/plugins/linux/mdadm.rb +120 -120
  93. data/lib/ohai/plugins/linux/memory.rb +106 -106
  94. data/lib/ohai/plugins/linux/network.rb +879 -879
  95. data/lib/ohai/plugins/linux/os_release.rb +38 -38
  96. data/lib/ohai/plugins/linux/platform.rb +314 -313
  97. data/lib/ohai/plugins/linux/selinux.rb +69 -69
  98. data/lib/ohai/plugins/linux/sessions.rb +54 -54
  99. data/lib/ohai/plugins/linux/sysctl.rb +39 -39
  100. data/lib/ohai/plugins/linux/systemd_paths.rb +36 -36
  101. data/lib/ohai/plugins/linux/tc.rb +61 -61
  102. data/lib/ohai/plugins/linux/virtualization.rb +300 -300
  103. data/lib/ohai/plugins/lua.rb +39 -39
  104. data/lib/ohai/plugins/mono.rb +50 -50
  105. data/lib/ohai/plugins/netbsd/memory.rb +99 -99
  106. data/lib/ohai/plugins/netbsd/network.rb +122 -122
  107. data/lib/ohai/plugins/netbsd/platform.rb +28 -28
  108. data/lib/ohai/plugins/network.rb +186 -186
  109. data/lib/ohai/plugins/nodejs.rb +40 -40
  110. data/lib/ohai/plugins/ohai.rb +29 -29
  111. data/lib/ohai/plugins/ohai_time.rb +26 -26
  112. data/lib/ohai/plugins/openbsd/memory.rb +99 -99
  113. data/lib/ohai/plugins/openbsd/network.rb +122 -122
  114. data/lib/ohai/plugins/openbsd/platform.rb +28 -28
  115. data/lib/ohai/plugins/openstack.rb +84 -84
  116. data/lib/ohai/plugins/os.rb +55 -55
  117. data/lib/ohai/plugins/packages.rb +234 -234
  118. data/lib/ohai/plugins/passwd.rb +104 -104
  119. data/lib/ohai/plugins/perl.rb +45 -45
  120. data/lib/ohai/plugins/php.rb +52 -52
  121. data/lib/ohai/plugins/platform.rb +29 -29
  122. data/lib/ohai/plugins/powershell.rb +82 -82
  123. data/lib/ohai/plugins/ps.rb +35 -35
  124. data/lib/ohai/plugins/python.rb +43 -43
  125. data/lib/ohai/plugins/rackspace.rb +177 -177
  126. data/lib/ohai/plugins/root_group.rb +41 -41
  127. data/lib/ohai/plugins/rpm.rb +121 -119
  128. data/lib/ohai/plugins/ruby.rb +66 -66
  129. data/lib/ohai/plugins/rust.rb +34 -34
  130. data/lib/ohai/plugins/scala.rb +38 -38
  131. data/lib/ohai/plugins/scaleway.rb +58 -58
  132. data/lib/ohai/plugins/scsi.rb +52 -52
  133. data/lib/ohai/plugins/shard.rb +142 -142
  134. data/lib/ohai/plugins/shells.rb +32 -32
  135. data/lib/ohai/plugins/softlayer.rb +48 -48
  136. data/lib/ohai/plugins/solaris2/dmi.rb +191 -191
  137. data/lib/ohai/plugins/solaris2/memory.rb +32 -32
  138. data/lib/ohai/plugins/solaris2/network.rb +192 -192
  139. data/lib/ohai/plugins/solaris2/platform.rb +58 -58
  140. data/lib/ohai/plugins/solaris2/virtualization.rb +90 -90
  141. data/lib/ohai/plugins/ssh_host_key.rb +84 -84
  142. data/lib/ohai/plugins/sysconf.rb +46 -46
  143. data/lib/ohai/plugins/timezone.rb +25 -25
  144. data/lib/ohai/plugins/train.rb +35 -35
  145. data/lib/ohai/plugins/uptime.rb +95 -95
  146. data/lib/ohai/plugins/virtualbox.rb +197 -197
  147. data/lib/ohai/plugins/vmware.rb +94 -94
  148. data/lib/ohai/plugins/windows/dmi.rb +95 -95
  149. data/lib/ohai/plugins/windows/drivers.rb +52 -52
  150. data/lib/ohai/plugins/windows/memory.rb +39 -39
  151. data/lib/ohai/plugins/windows/network.rb +222 -222
  152. data/lib/ohai/plugins/windows/platform.rb +34 -34
  153. data/lib/ohai/plugins/windows/system_enclosure.rb +29 -29
  154. data/lib/ohai/plugins/windows/virtualization.rb +45 -45
  155. data/lib/ohai/plugins/zpools.rb +94 -94
  156. data/lib/ohai/provides_map.rb +208 -208
  157. data/lib/ohai/runner.rb +128 -128
  158. data/lib/ohai/system.rb +258 -258
  159. data/lib/ohai/train_transport.rb +29 -29
  160. data/lib/ohai/util/file_helper.rb +6 -6
  161. data/lib/ohai/util/ip_helper.rb +56 -56
  162. data/lib/ohai/util/win32.rb +47 -47
  163. data/lib/ohai/version.rb +23 -23
  164. data/lib/ohai.rb +23 -23
  165. data/ohai.gemspec +35 -35
  166. metadata +3 -3
@@ -1,357 +1,357 @@
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
+
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