diff options
author | Brandon Raabe <brandocorp@gmail.com> | 2015-03-15 18:56:14 -0700 |
---|---|---|
committer | Brandon Raabe <brandocorp@gmail.com> | 2015-03-15 18:56:14 -0700 |
commit | e871f26bc8144dc8385ea87fd146ea0c3bfcff0c (patch) | |
tree | 945887254e054560031d45842c45b8bdc16bbae5 /lib | |
parent | 8af291b0e7cc4569eb28f09b68df4998332c9aa8 (diff) | |
download | chef-e871f26bc8144dc8385ea87fd146ea0c3bfcff0c.tar.gz |
add progress interval, add ProgressBar class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/http.rb | 66 | ||||
-rw-r--r-- | lib/chef/provider/remote_file/http.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource/remote_file.rb | 9 |
3 files changed, 74 insertions, 2 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 376a61d7a2..fa17ca0006 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -35,6 +35,65 @@ class Chef # == Chef::HTTP # Basic HTTP client, with support for adding features via middleware class HTTP + class ProgressBar + + attr_reader :display + attr_reader :interval + + def initialize(size, opts = {}) + @total = size + @status = 0 + @display = opts[:display] || false + @interval = opts[:interval] || 10 + progress_indicator if @display + end + + def update(progress) + @progress = progress + display_progress + end + + def display_progress + return unless display + progress_indicator if next_interval_met? + end + + def next_interval_met? + return false unless percent_complete % interval == 0 + if percent_complete > @status + @status = percent_complete + return true + end + return false + end + + def percent_complete + (@progress.to_f / @total.to_f * 100).to_i + end + + def display_status + length = @status / 2 + progress_text = '=' * length + progress_text + end + + def display_remainder + length = 100 / 2 - @status / 2 + progress_text = ' ' * length + progress_text + end + + def display_percentage + length = 4 - @status.to_s.length + progress_text = ' ' * length + "#{@status}/100 %" + progress_text + end + + def progress_indicator + Chef::Log.info '[' + display_status + display_remainder + ']' + display_percentage + end + end + # Class for applying middleware behaviors to streaming # responses. Collects stream handlers (if any) from each @@ -74,6 +133,7 @@ class Chef attr_reader :sign_on_redirect attr_reader :redirect_limit attr_reader :show_progress + attr_reader :progress_interval attr_reader :middlewares # Create a HTTP client object. The supplied +url+ is used as the base for @@ -87,6 +147,7 @@ class Chef @redirects_followed = 0 @redirect_limit = 10 @show_progress = options[:show_progress] || false + @progress_interval = options[:progress_interval] || 1 @middlewares = [] self.class.middlewares.each do |middleware_class| @@ -367,7 +428,7 @@ class Chef end def stream_to_tempfile(url, response) - total = response['Content-Length'] + progress = ProgressBar.new(response['Content-Length'], display: show_progress, interval: progress_interval) tf = Tempfile.open("chef-rest") if Chef::Platform.windows? tf.binmode # required for binary files on Windows platforms @@ -380,7 +441,7 @@ class Chef response.read_body do |chunk| tf.write(stream_handler.handle_chunk(chunk)) - Chef::Log.info("#{tf.size} / #{total}") if show_progress + progress.update tf.size end tf.close tf @@ -405,3 +466,4 @@ class Chef end end + diff --git a/lib/chef/provider/remote_file/http.rb b/lib/chef/provider/remote_file/http.rb index 8d5fe91f0a..41727bd202 100644 --- a/lib/chef/provider/remote_file/http.rb +++ b/lib/chef/provider/remote_file/http.rb @@ -115,6 +115,7 @@ class Chef if want_progress? Chef::Log.debug("enabling progress output for streaming requests") opts[:show_progress] = true + opts[:progress_interval] = new_resource.progress_interval end opts end diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index 112fa51532..5da20371da 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -133,6 +133,15 @@ class Chef ) end + def progress_interval(args=nil) + set_or_return( + :progress_interval, + args, + :default => 10, + :equal_to => [1, 5, 10, 20, 25] + ) + end + def after_created validate_source(@source) end |