From fedb3152c9045c984b1296e435997ad7fc851b9e Mon Sep 17 00:00:00 2001 From: Sofer Athlan-Guyot Date: Wed, 6 Apr 2016 23:09:54 +0200 Subject: [PATCH] Add the possibility to execute without retry. It may be useful to be able to execute a command without a retry. A good use case is provider in [1], where the keystone_user resource try to get an user by fetching it. It is expected to fail when the user is absent. With the current implementation, it will takes 60 seconds for the provider to give up on the user. [1] https://review.openstack.org/299301 Closes-Bug: #1563898 Change-Id: I5b334e3ffd26df4ba8584d77a5e41b56e73536c8 --- lib/puppet/provider/openstack.rb | 44 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/puppet/provider/openstack.rb b/lib/puppet/provider/openstack.rb index 47ee2cf2..6ae89fd0 100644 --- a/lib/puppet/provider/openstack.rb +++ b/lib/puppet/provider/openstack.rb @@ -13,10 +13,19 @@ class Puppet::Provider::Openstack < Puppet::Provider initvars # so commands will work commands :openstack_command => 'openstack' - # this actions are not idempotent and retries can cause - # duplications or endless loops - def self.no_retry_actions - %w(create remove delete) + @@no_retry_actions = %w(create remove delete) + @@command_timeout = 20 + @@request_timeout = 60 + @@retry_sleep = 3 + class << self + [:no_retry_actions, :request_timeout, :retry_sleep].each do |m| + define_method m do + self.class_variable_get("@@#{m}") + end + define_method :"#{m}=" do |value| + self.class_variable_set("@@#{m}", value) + end + end end # timeout the openstack command @@ -26,22 +35,9 @@ class Puppet::Provider::Openstack < Puppet::Provider def self.command_timeout(action=nil) # give no_retry actions the full time limit to finish return self.request_timeout() if no_retry_actions.include? action - 20 + self.class_variable_get("@@command_timeout") end - # timeout the entire request with error - # after this number of seconds - def self.request_timeout - 60 - end - - # sleep for this number of seconds - # between command retries - def self.retry_sleep - 3 - end - - # run the openstack command # with command_timeout def self.openstack(*args) begin @@ -59,6 +55,18 @@ class Puppet::Provider::Openstack < Puppet::Provider Time.now.to_i end + def self.request_without_retry(&block) + previous_timeout = self.request_timeout + rc = nil + if block_given? + self.request_timeout = 0 + rc = yield + end + ensure + self.request_timeout = previous_timeout + rc + end + # Returns an array of hashes, where the keys are the downcased CSV headers # with underscores instead of spaces def self.request(service, action, properties, credentials=nil)