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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 45d066cc35bc83eb5adba2d23ebc9a4e39451d072a6e1fda5e53a68c50741246
4
- data.tar.gz: 78bd290648898961bc06912973654fad4e75c374f837998dfb0cb018c9c66fe4
2
+ SHA1:
3
+ metadata.gz: 436fe05bd654044a382621b353229fc436bc3a4c
4
+ data.tar.gz: a785069184b37a9a057e061da997d47d55c1e161
5
5
  SHA512:
6
- metadata.gz: e4f8708b309cc4d5b8fd7ae63a09c2ea07b0fa6e878c03abfea9b1628c39fb26875b4bb3bad8d3713c55a17cdc310d30fe01ac40cbd8b3cf8e42d63993b29167
7
- data.tar.gz: 8a114eb384b9561d749117610a675e630b94d5beae99de7633495eb944b429b096a70664427136f6dfc11ad94759869d21910b67a9311a42c4df81d47bca24ab
6
+ metadata.gz: 334239f6d9138eda450ad1fac4bb8da0439b290dfa1d2551377bf37dcd082778f121cbdb5f4398a936bb98670cb030bbe22bea952cd8fa4216bba06a0b18360e
7
+ data.tar.gz: ea555c1670d775894b74c3b3b5a677e4b69c9097b121e518d1c50c5b7c3dc3dc2a4134bba7f28e1894137865131decf22f4d443d7adfa6b6207ec8301dfd19ac
data/Gemfile CHANGED
@@ -3,9 +3,7 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  group :development do
6
- gem "sigar", :platform => "ruby"
7
-
8
- gem "chefstyle", "0.4.0"
6
+ gem "chefstyle"
9
7
  gem "overcommit", ">= 0.34.1"
10
8
  gem "pry-byebug"
11
9
  gem "pry-stack_explorer"
@@ -16,5 +14,6 @@ group :development do
16
14
  gem "rspec-mocks", "~> 3.0"
17
15
  gem "rspec-collection_matchers", "~> 1.0"
18
16
  gem "rspec_junit_formatter"
19
- gem "activesupport", "< 5.0" if RUBY_VERSION <= "2.2.2" # github_changelog_generator dep
17
+ gem "github_changelog_generator", git: "https://github.com/tduffield/github-changelog-generator", branch: "adjust-tag-section-mapping"
18
+ gem "ipaddr_extensions"
20
19
  end
data/Rakefile CHANGED
@@ -22,3 +22,17 @@ require "rubocop/rake_task"
22
22
  RuboCop::RakeTask.new(:style) do |task|
23
23
  task.options += ["--display-cop-names", "--no-color"]
24
24
  end
25
+
26
+ begin
27
+ require "github_changelog_generator/task"
28
+
29
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
30
+ config.future_release = Ohai::VERSION
31
+ config.max_issues = 0
32
+ config.add_issues_wo_labels = false
33
+ config.enhancement_labels = "enhancement,Enhancement,New Feature,Feature".split(",")
34
+ config.bug_labels = "bug,Bug,Improvement,Upstream Bug".split(",")
35
+ config.exclude_labels = "duplicate,question,invalid,wontfix,no_changelog,Exclude From Changelog,Question,Discussion".split(",")
36
+ end
37
+ rescue LoadError
38
+ end
@@ -88,6 +88,7 @@ class Ohai::Application
88
88
  end
89
89
 
90
90
  def run_application
91
+ config[:invoked_from_cli] = true
91
92
  ohai = Ohai::System.new(config)
92
93
  ohai.all_plugins(@attributes)
93
94
 
data/lib/ohai/config.rb CHANGED
@@ -19,7 +19,6 @@
19
19
 
20
20
  require "chef-config/config"
21
21
  require "ohai/exception"
22
- require "ohai/log"
23
22
  require "ohai/plugin_config"
24
23
 
25
24
  module Ohai
@@ -28,99 +27,14 @@ module Ohai
28
27
  # Reopens ChefConfig::Config to add Ohai configuration settings.
29
28
  # see: https://github.com/chef/chef/blob/master/lib/chef/config.rb
30
29
  class Config
31
- # These methods need to be defined before they are used as config defaults,
32
- # otherwise they will get method_missing'd to nil.
33
-
34
- class << self
35
- def merge_deprecated_config
36
- [ :hints_path, :plugin_path ].each do |option|
37
- if has_key?(option) && send(option) != send("default_#{option}".to_sym)
38
- Ohai::Log.warn(option_deprecated(option))
39
- end
40
- end
41
-
42
- ohai.merge!(configuration)
43
- end
44
-
45
- def default_hints_path
46
- [ ChefConfig::Config.platform_specific_path("/etc/chef/ohai/hints") ]
47
- end
48
-
49
- def default_plugin_path
50
- [ File.expand_path(File.join(File.dirname(__FILE__), "plugins")), ChefConfig::Config.platform_specific_path("/etc/chef/ohai/plugins") ]
51
- end
52
- end
53
-
54
- # Copy deprecated configuration options into the ohai config context.
55
-
56
- # Keep "old" config defaults around so anyone calling Ohai::Config[:key]
57
- # won't be broken. Also allows users to append to configuration options
58
- # (e.g., Ohai::Config[:plugin_path] << some_path) in their config files.
59
- default :disabled_plugins, []
60
- default :hints_path, default_hints_path
61
- default :log_level, :auto
62
- default :log_location, STDERR
63
- default :plugin_path, default_plugin_path
64
-
65
- # Log deprecation warning when a top-level configuration option is set.
66
- # TODO: Should we implement a config_attr_reader so that deprecation
67
- # warnings will be generatd on read?
68
- [
69
- :directory,
70
- :disabled_plugins,
71
- :log_level,
72
- :log_location,
73
- :version,
74
- ].each do |option|
75
- # https://docs.chef.io/config_rb_client.html#ohai-settings
76
- # hints_path and plugin_path are intentionally excluded here; warnings for
77
- # setting these attributes are generated in merge_deprecated_config since
78
- # append (<<) operations bypass the config writer.
79
- config_attr_writer option do |value|
80
- # log_level and log_location are common configuration options for chef
81
- # and other chef applications. When configuration files are read there
82
- # is no distinction between log_level and Ohai::Config[:log_level] and
83
- # we may emit a false deprecation warning. The deprecation warnings for
84
- # these settings reflect that possibility.
85
- # Furthermore, when the top-level config settings are removed we will
86
- # need to ensure that Ohai.config[:log_level] can be set by writing
87
- # log_level in a configuration file for consistent behavior with chef.
88
- deprecation_warning = if [ :log_level, :log_location ].include?(value)
89
- option_might_be_deprecated(option)
90
- else
91
- option_deprecated(option)
92
- end
93
- Ohai::Log.warn(deprecation_warning)
94
- value
95
- end
96
- end
97
30
 
98
31
  config_context :ohai do
99
32
  default :disabled_plugins, []
100
- default :hints_path, Ohai::Config.default_hints_path
33
+ default :hints_path, [ ChefConfig::Config.platform_specific_path("/etc/chef/ohai/hints") ]
101
34
  default :log_level, :auto
102
35
  default :log_location, STDERR
103
36
  default :plugin, Ohai::PluginConfig.new { |h, k| h[k] = Ohai::PluginConfig.new }
104
- default :plugin_path, Ohai::Config.default_plugin_path
105
- default :critical_plugins, []
106
- end
107
-
108
- class << self
109
- def option_deprecated(option)
110
- <<-EOM.chomp!.tr("\n", " ")
111
- Ohai::Config[:#{option}] is set. Ohai::Config[:#{option}] is deprecated and will
112
- be removed in future releases of ohai. Use ohai.#{option} in your configuration
113
- file to configure :#{option} for ohai.
114
- EOM
115
- end
116
-
117
- def option_might_be_deprecated(option)
118
- option_deprecated(option) + <<-EOM.chomp!.tr("\n", " ")
119
- If your configuration file is used with other applications which configure
120
- :#{option}, and you have not configured Ohai::Config[:#{option}], you may
121
- disregard this warning.
122
- EOM
123
- end
37
+ default :plugin_path, [ File.expand_path(File.join(File.dirname(__FILE__), "plugins")), ChefConfig::Config.platform_specific_path("/etc/chef/ohai/plugins") ]
124
38
  end
125
39
  end
126
40
 
@@ -85,12 +85,10 @@ module Ohai
85
85
  include Ohai::Util::FileHelper
86
86
 
87
87
  attr_reader :data
88
- attr_reader :failed
89
88
 
90
89
  def initialize(data)
91
90
  @data = data
92
91
  @has_run = false
93
- @failed = false
94
92
  end
95
93
 
96
94
  def run
@@ -185,10 +183,8 @@ module Ohai
185
183
  begin
186
184
  self.run
187
185
  rescue Ohai::Exceptions::Error => e
188
- @failed = true
189
186
  raise e
190
187
  rescue => e
191
- @failed = true
192
188
  Ohai::Log.debug("Plugin #{self.name} threw #{e.inspect}")
193
189
  e.backtrace.each { |line| Ohai::Log.debug( line ) }
194
190
  end
@@ -29,6 +29,5 @@ module Ohai
29
29
  class DependencyNotFound < Error; end
30
30
  class AttributeSyntaxError < Error; end
31
31
  class PluginConfigError < Error; end
32
- class CriticalPluginFailure < Error; end
33
32
  end
34
33
  end
data/lib/ohai/loader.rb CHANGED
@@ -70,8 +70,8 @@ module Ohai
70
70
 
71
71
  # Searches all plugin paths and returns an Array of PluginFile objects
72
72
  # representing each plugin file.
73
- def plugin_files_by_dir
74
- Array(Ohai.config[:plugin_path]).inject([]) do |plugin_files, plugin_path|
73
+ def plugin_files_by_dir(dir = Ohai.config[:plugin_path])
74
+ Array(dir).inject([]) do |plugin_files, plugin_path|
75
75
  plugin_files + PluginFile.find_all_in(plugin_path)
76
76
  end
77
77
  end
@@ -85,6 +85,15 @@ module Ohai
85
85
  collect_v7_plugins
86
86
  end
87
87
 
88
+ def load_additional(from)
89
+ from = [ Ohai.config[:plugin_path], from].flatten
90
+ plugin_files_by_dir(from).collect do |plugin_file|
91
+ Ohai::Log.debug "Loading additional plugin: #{plugin_file}"
92
+ plugin = load_plugin_class(plugin_file.path, plugin_file.plugin_root)
93
+ load_v7_plugin(plugin)
94
+ end
95
+ end
96
+
88
97
  # Load a specified file as an ohai plugin and creates an instance of it.
89
98
  # Not used by ohai itself, but can be used to load a plugin for testing
90
99
  # purposes.
data/lib/ohai/log.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: Adam Jacob (<adam@chef.io>)
3
- # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
3
+ # Copyright:: Copyright (c) 2008-2017, Chef Software Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,8 @@ module Ohai
22
22
  class Log
23
23
  extend Mixlib::Log
24
24
 
25
+ # this class loading initalization is so that we don't lose early logger
26
+ # messages when run from the CLI?
25
27
  init(STDERR)
26
28
  level = :info
27
29
 
@@ -1,5 +1,6 @@
1
1
  #
2
2
  # Author:: Adam Jacob (<adam@chef.io>)
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
3
4
  # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
4
5
  # License:: Apache License, Version 2.0
5
6
  #
@@ -17,12 +18,7 @@
17
18
  #
18
19
 
19
20
  require "ohai/exception"
20
- require "ohai/config"
21
21
  require "ohai/log"
22
- require "stringio"
23
- require "tmpdir"
24
- require "fcntl"
25
- require "etc"
26
22
  require "mixlib/shellout"
27
23
 
28
24
  module Ohai
@@ -48,319 +44,6 @@ module Ohai
48
44
  end
49
45
 
50
46
  module_function :shell_out
51
-
52
- def run_command(args = {})
53
- Ohai::Log.warn("Ohai::Mixin::Command run_command is deprecated and will be removed in Ohai 9.0.0")
54
- if args.has_key?(:creates)
55
- if File.exists?(args[:creates])
56
- Ohai::Log.debug("Skipping #{args[:command]} - creates #{args[:creates]} exists.")
57
- return false
58
- end
59
- end
60
-
61
- stdout_string = nil
62
- stderr_string = nil
63
-
64
- args[:cwd] ||= Dir.tmpdir
65
- unless File.directory?(args[:cwd])
66
- raise Ohai::Exceptions::Exec, "#{args[:cwd]} does not exist or is not a directory"
67
- end
68
-
69
- status = nil
70
- Dir.chdir(args[:cwd]) do
71
- status, stdout_string, stderr_string = run_command_backend(args[:command], args[:timeout])
72
-
73
- if stdout_string
74
- Ohai::Log.debug("---- Begin #{args[:command]} STDOUT ----")
75
- Ohai::Log.debug(stdout_string.strip)
76
- Ohai::Log.debug("---- End #{args[:command]} STDOUT ----")
77
- end
78
- if stderr_string
79
- Ohai::Log.debug("---- Begin #{args[:command]} STDERR ----")
80
- Ohai::Log.debug(stderr_string.strip)
81
- Ohai::Log.debug("---- End #{args[:command]} STDERR ----")
82
- end
83
-
84
- args[:returns] ||= 0
85
- args[:no_status_check] ||= false
86
- if status.exitstatus != args[:returns] && (not args[:no_status_check])
87
- raise Ohai::Exceptions::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
88
- else
89
- Ohai::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
90
- end
91
- end
92
- return status, stdout_string, stderr_string
93
- end
94
-
95
- module_function :run_command
96
-
97
- def run_command_unix(command, timeout)
98
- stderr_string, stdout_string, status = "", "", nil
99
-
100
- exec_processing_block = lambda do |pid, stdin, stdout, stderr|
101
- stdout_string, stderr_string = stdout.string.chomp, stderr.string.chomp
102
- end
103
-
104
- if timeout
105
- begin
106
- Timeout.timeout(timeout) do
107
- status = popen4(command, {}, &exec_processing_block)
108
- end
109
- rescue Timeout::Error => e
110
- Chef::Log.error("#{command} exceeded timeout #{timeout}")
111
- raise(e)
112
- end
113
- else
114
- status = popen4(command, {}, &exec_processing_block)
115
- end
116
- return status, stdout_string, stderr_string
117
- end
118
-
119
- def run_command_windows(command, timeout)
120
- shellout_opts = {}
121
- shellout_opts[:timeout] = timeout if timeout
122
-
123
- m = Mixlib::ShellOut.new(command, shellout_opts)
124
- m.run_command
125
- [m.status, m.stdout, m.stderr]
126
- end
127
-
128
- if RUBY_PLATFORM =~ /mswin|mingw32|windows/
129
- alias :run_command_backend :run_command_windows
130
- else
131
- alias :run_command_backend :run_command_unix
132
- end
133
- # This is taken directly from Ara T Howard's Open4 library, and then
134
- # modified to suit the needs of Ohai. Any bugs here are most likely
135
- # my own, and not Ara's.
136
- #
137
- # The original appears in external/open4.rb in its unmodified form.
138
- #
139
- # Thanks Ara!
140
- def popen4(cmd, args = {}, &b)
141
- Ohai::Log.warn("Ohai::Mixin::Command popen4 is deprecated and will be removed in Ohai 9.0.0")
142
-
143
- # Disable garbage collection to work around possible bug in MRI
144
- # Ruby 1.8 suffers from intermittent segfaults believed to be due to GC while IO.select
145
- # See OHAI-330 / CHEF-2916 / CHEF-1305
146
- GC.disable
147
-
148
- # Waitlast - this is magic.
149
- #
150
- # Do we wait for the child process to die before we yield
151
- # to the block, or after? That is the magic of waitlast.
152
- #
153
- # By default, we are waiting before we yield the block.
154
- args[:waitlast] ||= false
155
-
156
- args[:user] ||= nil
157
- unless args[:user].kind_of?(Integer)
158
- args[:user] = Etc.getpwnam(args[:user]).uid if args[:user]
159
- end
160
- args[:group] ||= nil
161
- unless args[:group].kind_of?(Integer)
162
- args[:group] = Etc.getgrnam(args[:group]).gid if args[:group]
163
- end
164
- args[:environment] ||= {}
165
-
166
- # Default on C locale so parsing commands output can be done
167
- # independently of the node's default locale.
168
- # "LC_ALL" could be set to nil, in which case we also must ignore it.
169
- unless args[:environment].has_key?("LC_ALL")
170
- args[:environment]["LC_ALL"] = "C"
171
- end
172
-
173
- pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe
174
-
175
- verbose = $VERBOSE
176
- begin
177
- $VERBOSE = nil
178
- ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
179
-
180
- cid = fork do
181
- Process.setsid
182
-
183
- pw.last.close
184
- STDIN.reopen pw.first
185
- pw.first.close
186
-
187
- pr.first.close
188
- STDOUT.reopen pr.last
189
- pr.last.close
190
-
191
- pe.first.close
192
- STDERR.reopen pe.last
193
- pe.last.close
194
-
195
- STDOUT.sync = STDERR.sync = true
196
-
197
- if args[:group]
198
- Process.egid = args[:group]
199
- Process.gid = args[:group]
200
- end
201
-
202
- if args[:user]
203
- Process.euid = args[:user]
204
- Process.uid = args[:user]
205
- end
206
-
207
- args[:environment].each do |key, value|
208
- ENV[key] = value
209
- end
210
-
211
- if args[:umask]
212
- umask = ((args[:umask].respond_to?(:oct) ? args[:umask].oct : args[:umask].to_i) & 007777)
213
- File.umask(umask)
214
- end
215
-
216
- begin
217
- if cmd.kind_of?(Array)
218
- exec(*cmd)
219
- else
220
- exec(cmd)
221
- end
222
- raise "forty-two"
223
- rescue Exception => e
224
- Marshal.dump(e, ps.last)
225
- ps.last.flush
226
- end
227
- ps.last.close unless ps.last.closed?
228
- exit!
229
- end
230
- ensure
231
- $VERBOSE = verbose
232
- end
233
-
234
- [pw.first, pr.last, pe.last, ps.last].each { |fd| fd.close }
235
-
236
- begin
237
- e = Marshal.load ps.first
238
- pw.last.close
239
- pr.first.close
240
- pe.first.close
241
- Process.wait(cid)
242
- raise(Exception === e ? e : "unknown failure!")
243
- rescue EOFError # If we get an EOF error, then the exec was successful
244
- 42
245
- ensure
246
- ps.first.close
247
- end
248
-
249
- pw.last.sync = true
250
-
251
- pi = [pw.last, pr.first, pe.first]
252
-
253
- if b
254
- begin
255
- if args[:waitlast]
256
- b[cid, *pi]
257
- # send EOF so that if the child process is reading from STDIN
258
- # it will actually finish up and exit
259
- pi[0].close_write
260
- Process.waitpid2(cid).last
261
- else
262
- # This took some doing.
263
- # The trick here is to close STDIN
264
- # Then set our end of the childs pipes to be O_NONBLOCK
265
- # Then wait for the child to die, which means any IO it
266
- # wants to do must be done - it's dead. If it isn't,
267
- # it's because something totally skanky is happening,
268
- # and we don't care.
269
- o = StringIO.new
270
- e = StringIO.new
271
-
272
- #pi[0].close
273
-
274
- stdout = pi[1]
275
- stderr = pi[2]
276
-
277
- stdout.sync = true
278
- stderr.sync = true
279
-
280
- stdout.fcntl(Fcntl::F_SETFL, pi[1].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
281
- stderr.fcntl(Fcntl::F_SETFL, pi[2].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
282
-
283
- stdout_finished = false
284
- stderr_finished = false
285
-
286
- results = nil
287
-
288
- while !stdout_finished || !stderr_finished
289
- begin
290
- channels_to_watch = []
291
- channels_to_watch << stdout if !stdout_finished
292
- channels_to_watch << stderr if !stderr_finished
293
- ready = IO.select(channels_to_watch, nil, nil, 1.0)
294
- rescue Errno::EAGAIN
295
- ensure
296
- results = Process.waitpid2(cid, Process::WNOHANG)
297
- if results
298
- stdout_finished = true
299
- stderr_finished = true
300
- end
301
- end
302
-
303
- if ready && ready.first.include?(stdout)
304
- line = results ? stdout.gets(nil) : stdout.gets
305
- if line
306
- o.write(line)
307
- else
308
- stdout_finished = true
309
- end
310
- end
311
- if ready && ready.first.include?(stderr)
312
- line = results ? stderr.gets(nil) : stderr.gets
313
- if line
314
- e.write(line)
315
- else
316
- stderr_finished = true
317
- end
318
- end
319
- end
320
- results = Process.waitpid2(cid) unless results
321
- o.rewind
322
- e.rewind
323
-
324
- # **OHAI-275**
325
- # The way we read from the pipes causes ruby to mark the strings
326
- # as ASCII-8BIT (i.e., binary), but the content should be encoded
327
- # as the default external encoding. For example, a command may
328
- # return data encoded as UTF-8, but the strings will be marked as
329
- # ASCII-8BIT. Later, when you attempt to print the values as
330
- # UTF-8, Ruby will try to convert them and fail, raising an
331
- # error.
332
- #
333
- # Ruby always marks strings as binary when read from IO in
334
- # incomplete chunks, since you may have split the data within a
335
- # multibyte char. In our case, we concat the chunks back
336
- # together, so any multibyte chars will be reassembled.
337
- #
338
- # Note that all of this applies only to Ruby 1.9, which we check
339
- # for by making sure that the Encoding class exists and strings
340
- # have encoding methods.
341
- if "".respond_to?(:force_encoding) && defined?(Encoding)
342
- o.string.force_encoding(Encoding.default_external)
343
- o.string.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
344
- e.string.force_encoding(Encoding.default_external)
345
- e.string.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
346
- end
347
- b[cid, pi[0], o, e]
348
- results.last
349
- end
350
- ensure
351
- pi.each { |fd| fd.close unless fd.closed? }
352
- end
353
- else
354
- [cid, pw.last, pr.first, pe.first]
355
- end
356
- rescue Errno::ENOENT
357
- raise Ohai::Exceptions::Exec, "command #{cmd} doesn't exist or is not in the PATH"
358
- ensure
359
- # we disabled GC entering
360
- GC.enable
361
- end
362
-
363
- module_function :popen4
364
47
  end
365
48
  end
366
49
  end