Last active
March 21, 2021 20:56
-
-
Save kwilczynski/bc1c19bd89b712ce3f35 to your computer and use it in GitHub Desktop.
Mixlib::ShellOut live stream reader in Chef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Only for Chef client 11 family! | |
# Bug was fixed in https://github.com/opscode/chef/commit/d6f6928fd1097709189cd78689e89032d4c9318d | |
class Chef | |
module Mixin | |
module ShellOut | |
def shell_out(*command_args) | |
cmd = Mixlib::ShellOut.new(*run_command_compatible_options(command_args)) | |
cmd.live_stream ||= io_for_live_stream | |
cmd.run_command | |
cmd | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See also https://gist.github.com/kwilczynski/2c68a486e230433d8cd2 | |
class Chef | |
class Recipe | |
class StreamReader | |
require 'stringio' | |
def initialize(&block) | |
@block = block | |
@buffer = StringIO.new | |
@buffer.sync = true if @buffer.respond_to?(:sync) | |
end | |
def <<(chunk) | |
overflow = '' | |
@buffer.write(chunk) | |
@buffer.rewind | |
@buffer.each_line do |line| | |
if line.match(/\r?\n/) | |
@block.call(line.strip) | |
else | |
overflow = line | |
end | |
end | |
@buffer.truncate(@buffer.rewind) | |
@buffer.write(overflow) | |
end | |
end | |
end | |
end | |
stream_reader = StreamReader.new do |line| | |
Chef::Log.info(line) | |
end | |
include Chef::Mixin::ShellOut | |
ruby_block 'list current directory' do | |
block do | |
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut) | |
shell_out!('ls -l', :live_stream => stream_reader) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment