Skip to content

Commit

Permalink
Rubocop auto-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wyardley committed Feb 17, 2022
1 parent e7817b6 commit 44e677a
Show file tree
Hide file tree
Showing 67 changed files with 765 additions and 607 deletions.
2 changes: 2 additions & 0 deletions lib/facter/erl_ssl_path.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Fact to get the ssl path for the erlang distribution in the current
# system as described in the RabbitMQ docs [1].
#
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/rabbitmq_clustername.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Facter.add(:rabbitmq_clustername) do
setcode do
if Facter::Util::Resolution.which('rabbitmqctl')
Expand Down
6 changes: 4 additions & 2 deletions lib/facter/rabbitmq_nodename.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

Facter.add(:rabbitmq_nodename) do
setcode do
if Facter::Util::Resolution.which('rabbitmqctl')
rabbitmq_nodename = Facter::Core::Execution.execute('rabbitmqctl status 2>&1')
begin
%r{^Status of node '?([\w\.\-]+@[\w\.\-]+)'?}.match(rabbitmq_nodename)[1]
rescue
%r{^Status of node '?([\w.\-]+@[\w.\-]+)'?}.match(rabbitmq_nodename)[1]
rescue StandardError
Facter.debug("Error: rabbitmq_nodename facter failed. Output was #{rabbitmq_nodename}")
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/facter/rabbitmq_plugins_dirs.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

Facter.add(:rabbitmq_plugins_dirs) do
setcode do
if Facter::Util::Resolution.which('rabbitmqctl')
rabbitmq_pluginsdirs_env = Facter::Core::Execution.execute("rabbitmqctl eval 'application:get_env(rabbit, plugins_dir).'")
rabbitmq_plugins_dirs_match = %r{^\{ok\,\"(\/.+\/\w+)}.match(rabbitmq_pluginsdirs_env)
rabbitmq_plugins_dirs_match = %r{^\{ok,"(/.+/\w+)}.match(rabbitmq_pluginsdirs_env)
rabbitmq_plugins_dirs_match[1].split(':') if rabbitmq_plugins_dirs_match
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/facter/rabbitmq_version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

Facter.add(:rabbitmq_version) do
setcode do
if Facter::Util::Resolution.which('rabbitmqadmin')
rabbitmq_version = Facter::Core::Execution.execute('rabbitmqadmin --version 2>&1')
%r{^rabbitmqadmin ([\w\.]+)}.match(rabbitmq_version).to_a[1]
%r{^rabbitmqadmin ([\w.]+)}.match(rabbitmq_version).to_a[1]
end
end
end
15 changes: 7 additions & 8 deletions lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'puppet'
require 'digest'
Expand All @@ -10,11 +12,7 @@
mk_resource_methods

def should_vhost
if @should_vhost
@should_vhost
else
@should_vhost = resource[:vhost]
end
@should_vhost || @should_vhost = resource[:vhost]
end

def self.all_vhosts
Expand All @@ -35,14 +33,15 @@ def self.instances
all_bindings(vhost).map do |line|
source_name, destination_name, destination_type, routing_key, arguments = line.split(%r{\t})
# Convert output of arguments from the rabbitmqctl command to a json string.
if !arguments.nil?
if arguments.nil?
arguments = '{}'
else
arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
arguments = '{}' if arguments == ''
else
arguments = '{}'
end
hashed_name = Digest::SHA256.hexdigest format('%s@%s@%s@%s', source_name, destination_name, vhost, routing_key)
next if source_name.empty?

binding = {
source: source_name,
destination: destination_name,
Expand Down
29 changes: 14 additions & 15 deletions lib/puppet/provider/rabbitmq_cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Puppet::Provider::RabbitmqCli < Puppet::Provider
initvars

Expand Down Expand Up @@ -38,7 +40,7 @@ def self.rabbitmq_version
return @rabbitmq_version if defined? @rabbitmq_version

output = rabbitmqctl('-q', 'status')
version = output.match(%r{RabbitMQ.*?([\d\.]+)})
version = output.match(%r{RabbitMQ.*?([\d.]+)})
@rabbitmq_version = version[1] if version
@rabbitmq_version
end
Expand All @@ -56,35 +58,32 @@ def self.rabbitmqctl_list(resource, *opts)

def self.rabbitmq_running
rabbitmqctl('-q', 'status')
return true
true
rescue Puppet::ExecutionFailure, Timeout::Error
return false
false
end

# Retry the given code block 'count' retries or until the
# command succeeds. Use 'step' delay between retries.
# Limit each query time by 'timeout'.
# For example:
# users = self.class.run_with_retries { rabbitmqctl 'list_users' }
def self.run_with_retries(count = 30, step = 6, timeout = 10)
def self.run_with_retries(count = 30, step = 6, timeout = 10, &block)
count.times do |_n|
begin
output = Timeout.timeout(timeout) do
yield
end
rescue Puppet::ExecutionFailure, Timeout::Error
Puppet.debug 'Command failed, retrying'
sleep step
else
Puppet.debug 'Command succeeded'
return output
end
output = Timeout.timeout(timeout, &block)
rescue Puppet::ExecutionFailure, Timeout::Error
Puppet.debug 'Command failed, retrying'
sleep step
else
Puppet.debug 'Command succeeded'
return output
end
raise Puppet::Error, "Command is still failing after #{count * step} seconds expired!"
end

def self.define_instance_method(name)
return if method_defined?(name)

define_method(name) do |*args, &block|
self.class.send(name, *args, &block)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/provider/rabbitmq_cluster/rabbitmqctl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmq_cli'))
Puppet::Type.type(:rabbitmq_cluster).provide(
:rabbitmqctl,
Expand Down
4 changes: 3 additions & 1 deletion lib/puppet/provider/rabbitmq_erlang_cookie/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppet'
require 'set'
Puppet::Type.type(:rabbitmq_erlang_cookie).provide(:ruby) do
Expand All @@ -13,7 +15,7 @@ def content=(value)
raise('The current erlang cookie needs to change. In order to do this the RabbitMQ database needs to be wiped. Please set force => true to allow this to happen automatically.') unless resource[:force] == :true # Danger!

Puppet::Type.type(:service).new(name: resource[:service_name]).provider.stop
FileUtils.rm_rf(resource[:rabbitmq_home] + File::SEPARATOR + 'mnesia')
FileUtils.rm_rf("#{resource[:rabbitmq_home]}#{File::SEPARATOR}mnesia")
File.open(resource[:path], 'w') do |cookie|
cookie.chmod(0o400)
cookie.write(value)
Expand Down
18 changes: 8 additions & 10 deletions lib/puppet/provider/rabbitmq_exchange/rabbitmqadmin.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

require 'puppet'
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmq_cli'))
Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, parent: Puppet::Provider::RabbitmqCli) do
confine feature: :posix

def should_vhost
if @should_vhost
@should_vhost
else
@should_vhost = resource[:name].split('@')[1]
end
@should_vhost || @should_vhost = resource[:name].split('@')[1]
end

def self.all_vhosts
Expand All @@ -19,7 +17,7 @@ def self.all_exchanges(vhost)
exchange_list = run_with_retries do
rabbitmqctl_list('exchanges', '-p', vhost, 'name', 'type', 'internal', 'durable', 'auto_delete', 'arguments')
end
exchange_list.split(%r{\n}).reject { |exchange| exchange =~ %r{^federation:} }
exchange_list.split(%r{\n}).grep_v(%r{^federation:})
end

def self.instances
Expand All @@ -34,11 +32,11 @@ def self.instances
name = ''
end
# Convert output of arguments from the rabbitmqctl command to a json string.
if !arguments.nil?
if arguments.nil?
arguments = '{}'
else
arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
arguments = '{}' if arguments == ''
else
arguments = '{}'
end
exchange = {
type: type,
Expand All @@ -57,7 +55,7 @@ def self.instances

def self.prefetch(resources)
packages = instances
resources.keys.each do |name|
resources.each_key do |name|
if (provider = packages.find { |pkg| pkg.name == name })
resources[name].provider = provider
end
Expand Down
5 changes: 4 additions & 1 deletion lib/puppet/provider/rabbitmq_parameter/rabbitmqctl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'puppet/util/package'

Expand Down Expand Up @@ -25,6 +27,7 @@ def self.instances
all_vhosts.each do |vhost|
all_parameters(vhost).map do |line|
raise Puppet::Error, "cannot parse line from list_parameter:#{line}" unless line =~ %r{^(\S+)\s+(\S+)\s+(\S+)$}

parameter = {
ensure: :present,
component_name: Regexp.last_match(1),
Expand All @@ -39,7 +42,7 @@ def self.instances

def self.prefetch(resources)
packages = instances
resources.keys.each do |name|
resources.each_key do |name|
Puppet.info "Calling prefetch: #{name}"
if (provider = packages.find { |pkg| pkg.name == name })
resources[name].provider = provider
Expand Down
3 changes: 3 additions & 0 deletions lib/puppet/provider/rabbitmq_plugin/rabbitmqplugins.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppet/util/package'

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmq_cli'))
Expand All @@ -11,6 +13,7 @@ def self.instances

plugin_list.split(%r{\n}).map do |line|
raise Puppet::Error, "Cannot parse invalid plugins line: #{line}" unless line =~ %r{^(\S+)$}

new(name: Regexp.last_match(1))
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/puppet/provider/rabbitmq_policy/rabbitmqctl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'puppet/util/package'

Expand All @@ -7,7 +9,7 @@

# cache policies
def self.policies(vhost, name)
@policies = {} unless @policies
@policies ||= {}
unless @policies[vhost]
@policies[vhost] = {}
policy_list = run_with_retries do
Expand All @@ -30,6 +32,7 @@ def self.policies(vhost, name)

policy_list.split(%r{\n}).each do |line|
raise Puppet::Error, "cannot parse line from list_policies:#{line}" unless line =~ regex

n = Regexp.last_match(2)
applyto = Regexp.last_match(applyto_index) || 'all'
priority = Regexp.last_match(6)
Expand Down Expand Up @@ -107,6 +110,7 @@ def priority=(_priority)

def set_policy
return if @set_policy

@set_policy = true
resource[:applyto] ||= applyto
resource[:definition] ||= definition
Expand Down
17 changes: 8 additions & 9 deletions lib/puppet/provider/rabbitmq_queue/rabbitmqadmin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'puppet'

Expand All @@ -6,11 +8,7 @@
confine feature: :posix

def should_vhost
if @should_vhost
@should_vhost
else
@should_vhost = resource[:name].rpartition('@').last
end
@should_vhost || @should_vhost = resource[:name].rpartition('@').last
end

def self.all_vhosts
Expand All @@ -26,13 +24,14 @@ def self.instances
all_vhosts.each do |vhost|
all_queues(vhost).map do |line|
next if line =~ %r{^federation:}

name, durable, auto_delete, arguments = line.split("\t")
# Convert output of arguments from the rabbitmqctl command to a json string.
if !arguments.nil?
if arguments.nil?
arguments = '{}'
else
arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
arguments = '{}' if arguments == ''
else
arguments = '{}'
end
queue = {
durable: durable,
Expand All @@ -49,7 +48,7 @@ def self.instances

def self.prefetch(resources)
packages = instances
resources.keys.each do |name|
resources.each_key do |name|
if (provider = packages.find { |pkg| pkg.name == name })
resources[name].provider = provider
end
Expand Down
5 changes: 5 additions & 0 deletions lib/puppet/provider/rabbitmq_user/rabbitmqctl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmq_cli'))
Puppet::Type.type(:rabbitmq_user).provide(
:rabbitmqctl,
Expand All @@ -17,6 +19,7 @@ def self.instances

user_list.split(%r{\n}).map do |line|
raise Puppet::Error, "Cannot parse invalid user line: #{line}" unless line =~ %r{^(\S+)\s+\[(.*?)\]$}

user = Regexp.last_match(1)
tags = Regexp.last_match(2).split(%r{,\s*})
new(
Expand Down Expand Up @@ -88,6 +91,7 @@ def tags=(tags)
def admin
usertags = get_user_tags
raise Puppet::Error, "Could not match line '#{resource[:name]} (true|false)' from list_users (perhaps you are running on an older version of rabbitmq that does not support admin users?)" unless usertags

(:true if usertags.include?('administrator')) || :false
end

Expand All @@ -111,6 +115,7 @@ def admin=(state)

def flush
return if @property_flush.empty?

tags = @property_flush[:tags] || @resource[:tags]
tags << admin_tag if @resource[:admin] == :true
rabbitmqctl('set_user_tags', @resource[:name], tags)
Expand Down
Loading

0 comments on commit 44e677a

Please sign in to comment.