ohai 16.10.6 → 17.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 586f4b057af4307f5fb1abd420939bdb2320dade9b63723fbbcc58135a645a9b
4
- data.tar.gz: b2c45c824a6ef7d69702f365f3e4d24bafe53b03afbfe0911e4bca8484556ad4
3
+ metadata.gz: 4d6aab3006a40779de433a6634b5be22fcb2ed1950c4b67d22eaa1fc18cc8993
4
+ data.tar.gz: ae3152a23474650f3d5c497ff8e966273a14656dc6d8f454ce4ac769a6259f1a
5
5
  SHA512:
6
- metadata.gz: 1e9e3ef34974d09ee07eb5c492a9e80028ea8d80c6afa38e86921de16f6a1f85884bb032129fe8322b0bb1942d9447012f3f89f7d027591feecd82a8211a389a
7
- data.tar.gz: 01fa6b22eee2fdfbead5f062efb6d346fc508b5b375edcddba0d56ed41c107b6a168e7532c573195b39f3a9b1ccdb24a67a3f914c056d2bedf88f1ac3e8aabe4
6
+ metadata.gz: 1d7331b209e4d513403cd87949ab0eb4f65a00597da848e22042c2658734cd999c9419ea2fa135e8fcb253eb69c97881324ccbb519198726ba445aec71cb8732
7
+ data.tar.gz: 6673a8e0fcdaa7cc26fddf35ef54fc88169bcccbecb9f00d85cfa3584661c0c4803869c86b71615f9f69a65dd45aefdd0e22bd6a3a426a34ab8180370312f49e
data/Gemfile CHANGED
@@ -3,20 +3,20 @@ source "https://rubygems.org"
3
3
 
4
4
  gemspec
5
5
 
6
- # pull these gems from master of chef/chef so that we're testing against what we will release
7
- gem "chef-config", git: "https://github.com/chef/chef", branch: "chef-16", glob: "chef-config/chef-config.gemspec"
8
- gem "chef-utils", git: "https://github.com/chef/chef", branch: "chef-16", glob: "chef-utils/chef-utils.gemspec"
6
+ # pull these gems from main of chef/chef so that we're testing against what we will release
7
+ gem "chef-config", git: "https://github.com/chef/chef", branch: "main", glob: "chef-config/chef-config.gemspec"
8
+ gem "chef-utils", git: "https://github.com/chef/chef", branch: "main", glob: "chef-utils/chef-utils.gemspec"
9
9
 
10
10
  # NOTE: do not submit PRs to add pry as a dep, add to your Gemfile.local
11
11
  group :development do
12
- gem "chefstyle", "1.6.2"
12
+ gem "chefstyle", "2.1.3"
13
13
  gem "ipaddr_extensions"
14
14
  gem "rake", ">= 10.1.0"
15
15
  gem "rspec-collection_matchers", "~> 1.0"
16
16
  gem "rspec-core", "~> 3.0"
17
17
  gem "rspec-expectations", "~> 3.0"
18
18
  gem "rspec-mocks", "~> 3.0"
19
- gem "rubocop-performance", "1.9.2"
19
+ gem "rubocop-performance", "1.12.0"
20
20
  gem "rubocop-rspec"
21
21
  end
22
22
 
data/lib/ohai/config.rb CHANGED
@@ -26,7 +26,7 @@ module Ohai
26
26
  Config = ChefConfig::Config
27
27
 
28
28
  # Reopens ChefConfig::Config to add Ohai configuration settings.
29
- # see: https://github.com/chef/chef/blob/master/lib/chef/config.rb
29
+ # see: https://github.com/chef/chef/blob/main/lib/chef/config.rb
30
30
  class Config
31
31
  config_context :ohai do
32
32
  default :disabled_plugins, []
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ # Copyright:: Copyright (c) Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require "net/http" unless defined?(Net::HTTP)
20
+
21
+ module Ohai
22
+ module Mixin
23
+ #
24
+ # This code parses the Alibaba Instance Metadata API to provide details
25
+ # of the running instance.
26
+ #
27
+ # Note: As of 2021-02-07 there is only one API release so we're not implementing
28
+ # logic like the ec2 or azure mixins where we have to find the latest supported
29
+ # release
30
+ module AlibabaMetadata
31
+
32
+ ALI_METADATA_ADDR ||= "100.100.100.200"
33
+
34
+ def http_get(uri)
35
+ conn = Net::HTTP.start(ALI_METADATA_ADDR)
36
+ conn.read_timeout = 6
37
+ conn.keep_alive_timeout = 6
38
+ conn.get("/2016-01-01/#{uri}", { "User-Agent" => "chef-ohai/#{Ohai::VERSION}" })
39
+ end
40
+
41
+ def fetch_metadata(id = "")
42
+ response = http_get(id)
43
+ return nil unless response.code == "200"
44
+
45
+ if json?(response.body)
46
+ data = String(response.body)
47
+ parser = FFI_Yajl::Parser.new
48
+ parser.parse(data)
49
+ elsif response.body.include?("\n")
50
+ temp = {}
51
+ response.body.split("\n").each do |sub_attr|
52
+ temp[sanitize_key(sub_attr)] = fetch_metadata("#{id}/#{sub_attr}")
53
+ end
54
+ temp
55
+ else
56
+ response.body
57
+ end
58
+ end
59
+
60
+ # @param [String] data that might be JSON
61
+ #
62
+ # @return [Boolean] is the data JSON or not?
63
+ def json?(data)
64
+ data = String(data)
65
+ parser = FFI_Yajl::Parser.new
66
+ begin
67
+ parser.parse(data)
68
+ true
69
+ rescue FFI_Yajl::ParseError
70
+ false
71
+ end
72
+ end
73
+
74
+ # @param data [String]
75
+ #
76
+ # @return [Boolean] is there a trailing /?
77
+ def has_trailing_slash?(data)
78
+ !!( data =~ %r{/$} )
79
+ end
80
+
81
+ def sanitize_key(key)
82
+ key.gsub(%r{\-|/}, "_")
83
+ end
84
+ end
85
+ end
86
+ end
@@ -23,7 +23,7 @@ module Ohai
23
23
  module ConstantHelper
24
24
 
25
25
  def remove_constants
26
- new_object_constants = Object.constants - @object_pristine.constants
26
+ new_object_constants = Object.constants - @object_pristine.constants - [ :SortedSet ]
27
27
  new_object_constants.each do |constant|
28
28
  Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module)
29
29
  end
@@ -41,10 +41,32 @@ module Ohai
41
41
  module Ec2Metadata
42
42
 
43
43
  EC2_METADATA_ADDR ||= "169.254.169.254"
44
- EC2_SUPPORTED_VERSIONS ||= %w{ 1.0 2007-01-19 2007-03-01 2007-08-29 2007-10-10 2007-12-15
45
- 2008-02-01 2008-09-01 2009-04-04 2011-01-01 2011-05-01 2012-01-12
46
- 2014-02-25 2014-11-05 2015-10-20 2016-04-19 2016-06-30 2016-09-02
47
- 2016-11-30 2018-08-17 2018-11-29 2019-10-01 2020-08-24 2020-11-01 }.freeze
44
+ EC2_SUPPORTED_VERSIONS ||= %w{ 1.0
45
+ 2007-01-19
46
+ 2007-03-01
47
+ 2007-08-29
48
+ 2007-10-10
49
+ 2007-12-15
50
+ 2008-02-01
51
+ 2008-09-01
52
+ 2009-04-04
53
+ 2011-01-01
54
+ 2011-05-01
55
+ 2012-01-12
56
+ 2014-02-25
57
+ 2014-11-05
58
+ 2015-10-20
59
+ 2016-04-19
60
+ 2016-06-30
61
+ 2016-09-02
62
+ 2018-03-28
63
+ 2018-08-17
64
+ 2018-09-24
65
+ 2019-10-01
66
+ 2020-10-27
67
+ 2021-01-03
68
+ 2021-03-23
69
+ 2021-07-15 }.freeze
48
70
  EC2_ARRAY_VALUES ||= %w{security-groups local_ipv4s}.freeze
49
71
  EC2_ARRAY_DIR ||= %w{network/interfaces/macs}.freeze
50
72
  EC2_JSON_DIR ||= %w{iam}.freeze
@@ -18,6 +18,8 @@
18
18
  # limitations under the License.
19
19
  #
20
20
 
21
+ require "socket" unless defined?(Socket)
22
+
21
23
  module Ohai
22
24
  module Mixin
23
25
  module NetworkHelper
@@ -32,6 +34,30 @@ module Ohai
32
34
  [2, 4, 6].each { |n| dec = dec + "." + netmask[n..n + 1].to_i(16).to_s(10) }
33
35
  dec
34
36
  end
37
+
38
+ # This does a forward and reverse lookup on the hostname to return what should be
39
+ # the FQDN for the host determined by name lookup (generally DNS). If the forward
40
+ # lookup fails this will throw. If the reverse lookup fails this will return the
41
+ # hostname back. The behavior on failure of the reverse lookup is both vitally important
42
+ # to this API, and completely untested, so changes to this method (not recommended) need
43
+ # to be manually validated by hand by setting up a DNS server with a broken A record to
44
+ # an IP without a PTR record (e.g. any RFC1918 space not served by the configured DNS
45
+ # server), and the method should return the hostname and not the IP address.
46
+ #
47
+ def canonicalize_hostname(hostname)
48
+ Addrinfo.getaddrinfo(hostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
49
+ end
50
+
51
+ def canonicalize_hostname_with_retries(hostname)
52
+ retries = 3
53
+ begin
54
+ canonicalize_hostname(hostname)
55
+ rescue
56
+ retries -= 1
57
+ retry if retries > 0
58
+ nil
59
+ end
60
+ end
35
61
  end
36
62
  end
37
63
  end
data/lib/ohai/mixin/os.rb CHANGED
@@ -82,13 +82,13 @@ module Ohai
82
82
  else
83
83
  # now we have something like an IPMI console that isn't Unix-like or Windows, presumably cannot run ruby, and
84
84
  # so we just trust the train O/S information.
85
- transport_connection.os
85
+ transport_connection.os.name
86
86
  end
87
87
  end
88
88
 
89
89
  # @api private
90
90
  def nonruby_target?
91
- transport_connection && !transport_connection.os.unix? && !transport_connection.os.windows
91
+ transport_connection && !transport_connection.os.unix? && !transport_connection.os.windows?
92
92
  end
93
93
 
94
94
  # @api private
@@ -24,14 +24,15 @@ Ohai.plugin(:Virtualization) do
24
24
  virtualization Mash.new
25
25
 
26
26
  lpar_no, lpar_name = shell_out("uname -L").stdout.split(nil, 2)
27
+ lpar_no = lpar_no.to_i
27
28
 
28
- unless lpar_no.to_i == -1 || (lpar_no.to_i == 1 && lpar_name == "NULL")
29
+ unless lpar_no == -1 || (lpar_no == 1 && lpar_name == "NULL")
29
30
  virtualization[:lpar_no] = lpar_no
30
31
  virtualization[:lpar_name] = lpar_name
31
32
  end
32
33
 
33
- wpar_no = shell_out("uname -W").stdout.strip
34
- if wpar_no.to_i > 0
34
+ wpar_no = shell_out("uname -W").stdout.strip.to_i
35
+ if wpar_no > 0
35
36
  virtualization[:wpar_no] = wpar_no
36
37
  else
37
38
  # the below parses the output of lswpar in the long format
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ # Copyright:: Copyright (c) Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ # How we detect Alibaba from easiest to hardest & least reliable
20
+ # 1. Ohai alibaba hint exists. This always works
21
+ # 2. DMI sys_vendor data mentions alibaba.
22
+
23
+ Ohai.plugin(:Alibaba) do
24
+ require_relative "../mixin/alibaba_metadata"
25
+ require_relative "../mixin/http_helper"
26
+
27
+ include Ohai::Mixin::AlibabaMetadata
28
+ include Ohai::Mixin::HttpHelper
29
+
30
+ provides "alibaba"
31
+
32
+ # look for alibaba string in dmi sys_vendor data within the sys tree.
33
+ # this works even if the system lacks dmidecode use by the Dmi plugin
34
+ # @return [Boolean] do we have Alibaba DMI data?
35
+ def has_ali_dmi?
36
+ if /Alibaba/.match?(file_val_if_exists("/sys/class/dmi/id/sys_vendor"))
37
+ logger.trace("Plugin Alibaba: has_ali_dmi? == true")
38
+ true
39
+ else
40
+ logger.trace("Plugin Alibaba: has_ali_dmi? == false")
41
+ false
42
+ end
43
+ end
44
+
45
+ # return the contents of a file if the file exists
46
+ # @param path[String] abs path to the file
47
+ # @return [String] contents of the file if it exists
48
+ def file_val_if_exists(path)
49
+ if file_exist?(path)
50
+ file_read(path)
51
+ end
52
+ end
53
+
54
+ # a single check that combines all the various detection methods for Alibaba
55
+ # @return [Boolean] Does the system appear to be on Alibaba
56
+ def looks_like_alibaba?
57
+ return true if hint?("alibaba") || has_ali_dmi?
58
+ end
59
+
60
+ collect_data do
61
+ if looks_like_alibaba?
62
+ logger.trace("Plugin Alibaba: looks_like_alibaba? == true")
63
+ alibaba Mash.new
64
+ fetch_metadata.each do |k, v|
65
+ alibaba[k] = v
66
+ end
67
+ else
68
+ logger.trace("Plugin Alibaba: looks_like_alibaba? == false")
69
+ false
70
+ end
71
+ end
72
+ end
@@ -48,7 +48,7 @@ Ohai.plugin(:Azure) do
48
48
  # check for either the waagent or the unknown-245 DHCP option that Azure uses
49
49
  # http://blog.mszcool.com/index.php/2015/04/detecting-if-a-virtual-machine-runs-in-microsoft-azure-linux-windows-to-protect-your-software-when-distributed-via-the-azure-marketplace/
50
50
  def has_waagent?
51
- if file_exist?("/usr/sbin/waagent") || dir_exist?('C:\WindowsAzure')
51
+ if file_exist?("/usr/sbin/waagent") || dir_exist?("C:\\WindowsAzure")
52
52
  logger.trace("Plugin Azure: Found waagent used by Azure.")
53
53
  true
54
54
  end
@@ -85,14 +85,12 @@ Ohai.plugin(:C) do
85
85
 
86
86
  def collect_glibc
87
87
  # glibc
88
- ["/lib/libc.so.6", "/lib64/libc.so.6"].each do |glibc|
89
- collect( Ohai.abs_path( glibc )) do |so|
90
- description = so.stdout.split($/).first
91
- if description =~ /(\d+\.\d+\.?\d*)/
92
- @c[:glibc] = Mash.new
93
- @c[:glibc][:version] = $1
94
- @c[:glibc][:description] = description
95
- end
88
+ collect("ldd --version") do |so|
89
+ description = so.stdout.split($/).first
90
+ if description =~ /(\d+\.\d+\.?\d*)/
91
+ @c[:glibc] = Mash.new
92
+ @c[:glibc][:version] = $1
93
+ @c[:glibc][:description] = description
96
94
  end
97
95
  end
98
96
  end
@@ -20,10 +20,20 @@
20
20
  Ohai.plugin(:Chef) do
21
21
  provides "chef_packages/chef"
22
22
 
23
+ def chef_effortless?
24
+ # Determine if client is being run as a Habitat package.
25
+ if Chef::CHEF_ROOT.include?("hab/pkgs/chef/chef")
26
+ # Determine if client is running in zero mode which would show it is using the Effortless pattern.
27
+ # Explicitly set response to true or nil, not false
28
+ ChefConfig::Config["chef_server_url"].include?("chefzero://")
29
+ end
30
+ end
31
+
23
32
  collect_data(:default, :target) do
24
33
  begin
25
34
  require "chef/version"
26
- rescue Gem::LoadError
35
+ require "chef-config/config" unless defined?(ChefConfig::Config)
36
+ rescue LoadError
27
37
  logger.trace("Plugin Chef: Unable to load the chef gem to determine the version")
28
38
  # this catches when you've done a major version bump of ohai, but
29
39
  # your chef gem is incompatible, so we can't load it in the same VM
@@ -35,5 +45,6 @@ Ohai.plugin(:Chef) do
35
45
  chef_packages[:chef] = Mash.new
36
46
  chef_packages[:chef][:version] = Chef::VERSION
37
47
  chef_packages[:chef][:chef_root] = Chef::CHEF_ROOT
48
+ chef_packages[:chef][:chef_effortless] = chef_effortless?
38
49
  end
39
50
  end
@@ -18,6 +18,7 @@
18
18
  Ohai.plugin(:Cloud) do
19
19
  provides "cloud"
20
20
 
21
+ depends "alibaba"
21
22
  depends "ec2"
22
23
  depends "gce"
23
24
  depends "rackspace"
@@ -118,7 +119,22 @@ Ohai.plugin(:Cloud) do
118
119
  end
119
120
  end
120
121
 
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
+ #--------------------------------------
122
138
  # Google Compute Engine (gce)
123
139
  #--------------------------------------
124
140
 
@@ -133,9 +149,9 @@ Ohai.plugin(:Cloud) do
133
149
  end
134
150
  end.flatten.compact
135
151
 
136
- private_ips = gce["instance"]["networkInterfaces"].collect do |interface|
152
+ private_ips = gce["instance"]["networkInterfaces"].filter_map do |interface|
137
153
  interface["ip"]
138
- end.compact
154
+ end
139
155
 
140
156
  public_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :public) }
141
157
  private_ips.each { |ipaddr| @cloud_attr_obj.add_ipv4_addr(ipaddr, :private) }
@@ -334,6 +350,7 @@ Ohai.plugin(:Cloud) do
334
350
  get_azure_values if on_azure?
335
351
  get_digital_ocean_values if on_digital_ocean?
336
352
  get_softlayer_values if on_softlayer?
353
+ get_alibaba_values if on_alibaba?
337
354
 
338
355
  cloud @cloud_attr_obj.cloud_mash
339
356
  end