diff options
author | Thom May <thom@may.lt> | 2015-11-12 10:15:28 +0000 |
---|---|---|
committer | Thom May <thom@may.lt> | 2015-11-12 10:15:28 +0000 |
commit | ac58788c4c5d010fca13fe65e58e0c3f178ab6eb (patch) | |
tree | 4b18c83a5f6ff6268414520a983fbde9ea12796c /lib/chef | |
parent | 880f744ce9f6cef12b7bdbd746641b87a27f5809 (diff) | |
parent | 68d75f2d22a4a0f7c39d2eed0de6fb802a82941d (diff) | |
download | chef-ac58788c4c5d010fca13fe65e58e0c3f178ab6eb.tar.gz |
Merge pull request #4040 from chef/tm/live_execute_streaming
Implement live streaming for execute resources
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/event_dispatch/dispatcher.rb | 5 | ||||
-rw-r--r-- | lib/chef/event_dispatch/events_output_stream.rb | 8 | ||||
-rw-r--r-- | lib/chef/formatters/base.rb | 7 | ||||
-rw-r--r-- | lib/chef/formatters/indentable_output_stream.rb | 5 | ||||
-rw-r--r-- | lib/chef/provider/execute.rb | 16 | ||||
-rw-r--r-- | lib/chef/resource/execute.rb | 8 |
6 files changed, 47 insertions, 2 deletions
diff --git a/lib/chef/event_dispatch/dispatcher.rb b/lib/chef/event_dispatch/dispatcher.rb index f3e55539a9..ad7df46cd0 100644 --- a/lib/chef/event_dispatch/dispatcher.rb +++ b/lib/chef/event_dispatch/dispatcher.rb @@ -20,6 +20,11 @@ class Chef @subscribers << subscriber end + # Check to see if we are dispatching to a formatter + def formatter? + @subscribers.any? { |s| s.respond_to?(:is_formatter?) && s.is_formatter? } + end + #### # All messages are unconditionally forwarded to all subscribers, so just # define the forwarding in one go: diff --git a/lib/chef/event_dispatch/events_output_stream.rb b/lib/chef/event_dispatch/events_output_stream.rb index 8de9b0fed1..d9c21642b7 100644 --- a/lib/chef/event_dispatch/events_output_stream.rb +++ b/lib/chef/event_dispatch/events_output_stream.rb @@ -21,6 +21,14 @@ class Chef events.stream_output(self, str, options) end + def <<(str) + events.stream_output(self, str, options) + end + + def write(str) + events.stream_output(self, str, options) + end + def close events.stream_closed(self, options) end diff --git a/lib/chef/formatters/base.rb b/lib/chef/formatters/base.rb index d3756ef00c..4c393f7a48 100644 --- a/lib/chef/formatters/base.rb +++ b/lib/chef/formatters/base.rb @@ -215,6 +215,10 @@ class Chef def deprecation(message, location=caller(2..2)[0]) Chef::Log.deprecation("#{message} at #{location}") end + + def is_formatter? + true + end end @@ -225,6 +229,9 @@ class Chef cli_name(:null) + def is_formatter? + false + end end end diff --git a/lib/chef/formatters/indentable_output_stream.rb b/lib/chef/formatters/indentable_output_stream.rb index 1beb286e7f..f7f470b190 100644 --- a/lib/chef/formatters/indentable_output_stream.rb +++ b/lib/chef/formatters/indentable_output_stream.rb @@ -50,6 +50,11 @@ class Chef print(string, from_args(args, :start_line => true, :end_line => true)) end + # Print a raw chunk + def <<(obj) + print(obj) + end + # Print a string. # # == Arguments diff --git a/lib/chef/provider/execute.rb b/lib/chef/provider/execute.rb index 30de0d3b9e..200beb02ad 100644 --- a/lib/chef/provider/execute.rb +++ b/lib/chef/provider/execute.rb @@ -78,6 +78,14 @@ class Chef !!new_resource.sensitive end + def live_stream? + Chef::Config[:stream_execute_output] || !!new_resource.live_stream + end + + def stream_to_stdout? + STDOUT.tty? && !Chef::Config[:daemon] + end + def opts opts = {} opts[:timeout] = timeout @@ -89,8 +97,12 @@ class Chef opts[:umask] = umask if umask opts[:log_level] = :info opts[:log_tag] = new_resource.to_s - if STDOUT.tty? && !Chef::Config[:daemon] && Chef::Log.info? && !sensitive? - opts[:live_stream] = STDOUT + if (Chef::Log.info? || live_stream?) && !sensitive? + if run_context.events.formatter? + opts[:live_stream] = Chef::EventDispatch::EventsOutputStream.new(run_context.events, :name => :execute) + elsif stream_to_stdout? + opts[:live_stream] = STDOUT + end end opts end diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb index 11c4ae045c..4f92a7ad35 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.rb @@ -49,6 +49,7 @@ class Chef @umask = nil @default_guard_interpreter = :execute @is_guard_interpreter = false + @live_stream = false end def umask(arg=nil) @@ -101,6 +102,13 @@ class Chef ) end + def live_stream(arg=nil) + set_or_return( + :live_stream, + arg, + :kind_of => [ TrueClass, FalseClass ]) + end + def path(arg=nil) Chef::Log.warn "The 'path' attribute of 'execute' is not used by any provider in Chef 11 or Chef 12. Use 'environment' attribute to configure 'PATH'. This attribute will be removed in Chef 13." |