Skip to content

Commit e8dbe8d

Browse files
committed
Merge pull request #292 from bogdando/fix3_MODULES_1452
Make rabbitmq providers to retry the commands
2 parents ac67949 + 9b23a90 commit e8dbe8d

File tree

10 files changed

+98
-20
lines changed

10 files changed

+98
-20
lines changed

lib/puppet/provider/rabbitmq_exchange/rabbitmqadmin.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'puppet'
2-
Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin) do
2+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
3+
Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet::Provider::Rabbitmqctl) do
34

45
if Puppet::PUPPETVERSION.to_f < 3
56
commands :rabbitmqctl => 'rabbitmqctl'
@@ -24,15 +25,23 @@ def should_vhost
2425

2526
def self.all_vhosts
2627
vhosts = []
27-
parse_command(rabbitmqctl('list_vhosts')).collect do |vhost|
28+
parse_command(
29+
self.run_with_retries {
30+
rabbitmqctl('list_vhosts')
31+
}
32+
).collect do |vhost|
2833
vhosts.push(vhost)
2934
end
3035
vhosts
3136
end
3237

3338
def self.all_exchanges(vhost)
3439
exchanges = []
35-
parse_command(rabbitmqctl('list_exchanges', '-p', vhost, 'name', 'type'))
40+
parse_command(
41+
self.run_with_retries {
42+
rabbitmqctl('list_exchanges', '-p', vhost, 'name', 'type')
43+
}
44+
)
3645
end
3746

3847
def self.parse_command(cmd_output)

lib/puppet/provider/rabbitmq_plugin/rabbitmqplugins.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Puppet::Type.type(:rabbitmq_plugin).provide(:rabbitmqplugins) do
1+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
2+
Puppet::Type.type(:rabbitmq_plugin).provide(:rabbitmqplugins, :parent => Puppet::Provider::Rabbitmqctl) do
23

34
if Puppet::PUPPETVERSION.to_f < 3
45
if Facter.value(:osfamily) == 'RedHat'
@@ -21,7 +22,9 @@
2122
defaultfor :feature => :posix
2223

2324
def self.instances
24-
rabbitmqplugins('list', '-E', '-m').split(/\n/).map do |line|
25+
self.run_with_retries {
26+
rabbitmqplugins('list', '-E', '-m')
27+
}.split(/\n/).map do |line|
2528
if line =~ /^(\S+)$/
2629
new(:name => $1)
2730
else
@@ -39,7 +42,9 @@ def destroy
3942
end
4043

4144
def exists?
42-
rabbitmqplugins('list', '-E', '-m').split(/\n/).detect do |line|
45+
self.class.run_with_retries {
46+
rabbitmqplugins('list', '-E', '-m')
47+
}.split(/\n/).detect do |line|
4348
line.match(/^#{resource[:name]}$/)
4449
end
4550
end

lib/puppet/provider/rabbitmq_policy/rabbitmqctl.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def self.policies(name, vhost)
1111
@policies = {} unless @policies
1212
unless @policies[vhost]
1313
@policies[vhost] = {}
14-
rabbitmqctl('list_policies', '-q', '-p', vhost).split(/\n/).each do |line|
14+
self.run_with_retries {
15+
rabbitmqctl('list_policies', '-q', '-p', vhost)
16+
}.split(/\n/).each do |line|
1517
# rabbitmq<3.2 does not support the applyto field
1618
# 1 2 3? 4 5 6
1719
# / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0

lib/puppet/provider/rabbitmq_user/rabbitmqctl.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'puppet'
22
require 'set'
3-
Puppet::Type.type(:rabbitmq_user).provide(:rabbitmqctl) do
3+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
4+
Puppet::Type.type(:rabbitmq_user).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do
45

56
if Puppet::PUPPETVERSION.to_f < 3
67
commands :rabbitmqctl => 'rabbitmqctl'
@@ -13,7 +14,9 @@
1314
defaultfor :feature => :posix
1415

1516
def self.instances
16-
rabbitmqctl('-q', 'list_users').split(/\n/).collect do |line|
17+
self.run_with_retries {
18+
rabbitmqctl('-q', 'list_users')
19+
}.split(/\n/).collect do |line|
1720
if line =~ /^(\S+)(\s+\[.*?\]|)$/
1821
new(:name => $1)
1922
else
@@ -55,12 +58,14 @@ def destroy
5558
end
5659

5760
def exists?
58-
rabbitmqctl('-q', 'list_users').split(/\n/).detect do |line|
61+
self.class.run_with_retries {
62+
rabbitmqctl('-q', 'list_users')
63+
}.split(/\n/).detect do |line|
5964
line.match(/^#{Regexp.escape(resource[:name])}(\s+(\[.*?\]|\S+)|)$/)
6065
end
6166
end
6267

63-
68+
6469
def tags
6570
get_user_tags.entries.sort
6671
end

lib/puppet/provider/rabbitmq_user_permissions/rabbitmqctl.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Puppet::Type.type(:rabbitmq_user_permissions).provide(:rabbitmqctl) do
1+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
2+
Puppet::Type.type(:rabbitmq_user_permissions).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do
23

34
if Puppet::PUPPETVERSION.to_f < 3
45
commands :rabbitmqctl => 'rabbitmqctl'
@@ -15,7 +16,9 @@ def self.users(name, vhost)
1516
@users = {} unless @users
1617
unless @users[name]
1718
@users[name] = {}
18-
rabbitmqctl('-q', 'list_user_permissions', name).split(/\n/).each do |line|
19+
self.run_with_retries {
20+
rabbitmqctl('-q', 'list_user_permissions', name)
21+
}.split(/\n/).each do |line|
1922
line = self::strip_backslashes(line)
2023
if line =~ /^(\S+)\s+(\S*)\s+(\S*)\s+(\S*)$/
2124
@users[name][$1] =

lib/puppet/provider/rabbitmq_vhost/rabbitmqctl.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Puppet::Type.type(:rabbitmq_vhost).provide(:rabbitmqctl) do
1+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
2+
Puppet::Type.type(:rabbitmq_vhost).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do
23

34
if Puppet::PUPPETVERSION.to_f < 3
45
commands :rabbitmqctl => 'rabbitmqctl'
@@ -9,7 +10,9 @@
910
end
1011

1112
def self.instances
12-
rabbitmqctl('-q', 'list_vhosts').split(/\n/).map do |line|
13+
self.run_with_retries {
14+
rabbitmqctl('-q', 'list_vhosts')
15+
}.split(/\n/).map do |line|
1316
if line =~ /^(\S+)$/
1417
new(:name => $1)
1518
else
@@ -27,7 +30,9 @@ def destroy
2730
end
2831

2932
def exists?
30-
out = rabbitmqctl('-q', 'list_vhosts').split(/\n/).detect do |line|
33+
out = self.class.run_with_retries {
34+
rabbitmqctl('-q', 'list_vhosts')
35+
}.split(/\n/).detect do |line|
3136
line.match(/^#{Regexp.escape(resource[:name])}$/)
3237
end
3338
end

lib/puppet/provider/rabbitmqctl.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,27 @@ def self.rabbitmq_version
77
version = output.match(/\{rabbit,"RabbitMQ","([\d\.]+)"\}/)
88
version[1] if version
99
end
10+
11+
# Retry the given code block 'count' retries or until the
12+
# command suceeeds. Use 'step' delay between retries.
13+
# Limit each query time by 'timeout'.
14+
# For example:
15+
# users = self.class.run_with_retries { rabbitmqctl 'list_users' }
16+
def self.run_with_retries(count=30, step=6, timeout=10)
17+
count.times do |n|
18+
begin
19+
output = Timeout::timeout(timeout) do
20+
yield
21+
end
22+
rescue Puppet::ExecutionFailure, Timeout
23+
Puppet.debug 'Command failed, retrying'
24+
sleep step
25+
else
26+
Puppet.debug 'Command succeeded'
27+
return output
28+
end
29+
end
30+
raise Puppet::Error, "Command is still failing after #{count * step} seconds expired!"
31+
end
32+
1033
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'puppet'
2+
require 'mocha'
3+
RSpec.configure do |config|
4+
config.mock_with :mocha
5+
end
6+
provider_class = Puppet::Type.type(:rabbitmq_plugin).provider(:rabbitmqplugins)
7+
describe provider_class do
8+
before :each do
9+
@resource = Puppet::Type::Rabbitmq_plugin.new(
10+
{:name => 'foo'}
11+
)
12+
@provider = provider_class.new(@resource)
13+
end
14+
it 'should match plugins' do
15+
@provider.expects(:rabbitmqplugins).with('list', '-E', '-m').returns("foo\n")
16+
@provider.exists?.should == 'foo'
17+
end
18+
it 'should call rabbitmqplugins to enable' do
19+
@provider.expects(:rabbitmqplugins).with('enable', 'foo')
20+
@provider.create
21+
end
22+
it 'should call rabbitmqplugins to disable' do
23+
@provider.expects(:rabbitmqplugins).with('disable', 'foo')
24+
@provider.destroy
25+
end
26+
end

spec/unit/puppet/provider/rabbitmq_user/rabbitmqctl_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
kitchen []
100100
kitchen2 [abc, def, ghi]
101101
EOT
102-
@provider.expects(:rabbitmqctl).with('set_user_tags', 'foo', ['bar','baz', 'administrator'].sort)
102+
@provider.expects(:rabbitmqctl).with('set_user_tags', 'foo', ['bar','baz', 'administrator'].sort)
103103
@provider.admin=:true
104104
end
105105
it 'should be able to unset admin value' do
@@ -118,7 +118,7 @@
118118
kitchen []
119119
kitchen2 [abc, def, ghi]
120120
EOT
121-
@provider.expects(:rabbitmqctl).with('set_user_tags', 'foo', ['bar','baz'].sort)
121+
@provider.expects(:rabbitmqctl).with('set_user_tags', 'foo', ['bar','baz'].sort)
122122
@provider.admin=:false
123123
end
124124

spec/unit/puppet/provider/rabbitmq_user_permissions/rabbitmqctl_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
@provider.instance_variable_set(:@should_vhost, "bar")
4242
@provider.instance_variable_set(:@should_user, "foo")
4343
@provider.expects(:rabbitmqctl).with('set_permissions', '-p', 'bar', 'foo', "''", "''", "''")
44-
@provider.create
44+
@provider.create
4545
end
4646
it 'should destroy permissions' do
4747
@provider.instance_variable_set(:@should_vhost, "bar")
4848
@provider.instance_variable_set(:@should_user, "foo")
4949
@provider.expects(:rabbitmqctl).with('clear_permissions', '-p', 'bar', 'foo')
50-
@provider.destroy
50+
@provider.destroy
5151
end
5252
{:configure_permission => '1', :write_permission => '2', :read_permission => '3'}.each do |k,v|
5353
it "should be able to retrieve #{k}" do

0 commit comments

Comments
 (0)