Skip to content

Commit

Permalink
Merge pull request rabbitmq#352 from RoboticCheese/jdh-execute-env-home
Browse files Browse the repository at this point in the history
Pass a HOME environment variable to all rabbitmq execute blocks
  • Loading branch information
JJ Asghar committed Mar 8, 2016
2 parents 2dc1ba9 + fb7a13e commit f4d8da4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ def format_ssl_versions
def format_ssl_ciphers
Array(node['rabbitmq']['ssl_ciphers']).join(',')
end

def shell_environment
{ 'HOME' => ENV.fetch('HOME', '/var/lib/rabbitmq') }
end
end
end
3 changes: 2 additions & 1 deletion providers/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
# limitations under the License.
#

include Opscode::RabbitMQ
include Chef::Mixin::ShellOut

use_inline_resources

# Get ShellOut
def get_shellout(cmd)
sh_cmd = Mixlib::ShellOut.new(cmd)
sh_cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
sh_cmd.environment = shell_environment
sh_cmd
end

Expand Down
7 changes: 6 additions & 1 deletion providers/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

require 'shellwords'

include Opscode::RabbitMQ

def parameter_exists?(vhost, name)
cmd = 'rabbitmqctl list_parameters'
cmd << " -p #{Shellwords.escape vhost}" unless vhost.nil?
cmd << " |grep '#{name}\\b'"

cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
begin
cmd.error!
Expand All @@ -51,6 +53,7 @@ def parameter_exists?(vhost, name)

execute "set_parameter #{parameter}" do
command cmd
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand All @@ -64,6 +67,7 @@ def parameter_exists?(vhost, name)

execute "clear_parameter #{parameter}" do
command "rabbitmqctl clear_parameter #{parameter}"
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand All @@ -74,6 +78,7 @@ def parameter_exists?(vhost, name)
action :list do
execute 'list_parameters' do
command 'rabbitmqctl list_parameters'
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand Down
12 changes: 9 additions & 3 deletions providers/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
# limitations under the License.
#

include Opscode::RabbitMQ

def plugin_enabled?(name)
ENV['PATH'] = "#{ENV['PATH']}:/usr/lib/rabbitmq/bin"
cmdstr = "rabbitmq-plugins list -e '#{name}\\b'"
cmd = Mixlib::ShellOut.new(cmdstr)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
Chef::Log.debug "rabbitmq_plugin_enabled?: #{cmdstr}"
Chef::Log.debug "rabbitmq_plugin_enabled?: #{cmd.stdout}"
Expand All @@ -36,7 +38,9 @@ def plugin_enabled?(name)
execute "rabbitmq-plugins enable #{new_resource.plugin}" do
umask 0022
Chef::Log.info "Enabling RabbitMQ plugin '#{new_resource.plugin}'."
environment 'PATH' => "#{ENV['PATH']}:/usr/lib/rabbitmq/bin"
environment shell_environment.merge(
'PATH' => "#{ENV['PATH']}:/usr/lib/rabbitmq/bin"
)
new_resource.updated_by_last_action(true)
end
end
Expand All @@ -47,7 +51,9 @@ def plugin_enabled?(name)
execute "rabbitmq-plugins disable #{new_resource.plugin}" do
umask 0022
Chef::Log.info "Disabling RabbitMQ plugin '#{new_resource.plugin}'."
environment 'PATH' => "#{ENV['PATH']}:/usr/lib/rabbitmq/bin"
environment shell_environment.merge(
'PATH' => "#{ENV['PATH']}:/usr/lib/rabbitmq/bin"
)
new_resource.updated_by_last_action(true)
end
end
Expand Down
7 changes: 6 additions & 1 deletion providers/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

require 'shellwords'

include Opscode::RabbitMQ

def policy_exists?(vhost, name)
cmd = 'rabbitmqctl list_policies'
cmd << " -p #{Shellwords.escape vhost}" unless vhost.nil?
cmd << " |grep '#{name}\\b'"

cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
begin
cmd.error!
Expand Down Expand Up @@ -66,6 +68,7 @@ def policy_exists?(vhost, name)

execute "set_policy #{new_resource.policy}" do
command cmd
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand All @@ -79,6 +82,7 @@ def policy_exists?(vhost, name)
cmd << " -p #{new_resource.vhost}" unless new_resource.vhost.nil?
execute "clear_policy #{new_resource.policy}" do
command cmd
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand All @@ -89,6 +93,7 @@ def policy_exists?(vhost, name)
action :list do
execute 'list_policies' do
command 'rabbitmqctl list_policies'
environment shell_environment
end

new_resource.updated_by_last_action(true)
Expand Down
15 changes: 12 additions & 3 deletions providers/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
# limitations under the License.
#

include Opscode::RabbitMQ

use_inline_resources

def user_exists?(name)
cmd = "rabbitmqctl -q list_users |grep '^#{name}\\s'"
cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
Chef::Log.debug "rabbitmq_user_exists?: #{cmd}"
Chef::Log.debug "rabbitmq_user_exists?: #{cmd.stdout}"
Expand All @@ -37,7 +39,7 @@ def user_exists?(name)
def user_has_tag?(name, tag) # rubocop:disable all
cmd = 'rabbitmqctl -q list_users'
cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
user_list = cmd.stdout
tags = user_list.match(/^#{name}\s+\[(.*?)\]/)[1].split
Expand All @@ -61,7 +63,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
vhost = '/' if vhost.nil? # rubocop:enable all
cmd = "rabbitmqctl -q list_user_permissions #{name} | grep \"^#{vhost}\\s\""
cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd}"
Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd.stdout}"
Expand Down Expand Up @@ -92,6 +94,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
execute "rabbitmqctl add_user #{new_resource.user}" do # ~FC009
sensitive true if Gem::Version.new(Chef::VERSION.to_s) >= Gem::Version.new('11.14.2')
command cmd
environment shell_environment
Chef::Log.info "Adding RabbitMQ user '#{new_resource.user}'."
end
end
Expand All @@ -101,6 +104,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
if user_exists?(new_resource.user)
cmd = "rabbitmqctl delete_user #{new_resource.user}"
execute cmd do
environment shell_environment
Chef::Log.debug "rabbitmq_user_delete: #{cmd}"
Chef::Log.info "Deleting RabbitMQ user '#{new_resource.user}'."
end
Expand All @@ -117,6 +121,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
vhostopt = "-p #{vhost}" unless vhost.nil?
cmd = "rabbitmqctl set_permissions #{vhostopt} #{new_resource.user} \"#{perm_list.join("\" \"")}\""
execute cmd do
environment shell_environment
Chef::Log.debug "rabbitmq_user_set_permissions: #{cmd}"
Chef::Log.info "Setting RabbitMQ user permissions for '#{new_resource.user}' on vhost #{vhost}."
end
Expand All @@ -132,6 +137,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
vhostopt = "-p #{vhost}" unless vhost.nil?
cmd = "rabbitmqctl clear_permissions #{vhostopt} #{new_resource.user}"
execute cmd do
environment shell_environment
Chef::Log.debug "rabbitmq_user_clear_permissions: #{cmd}"
Chef::Log.info "Clearing RabbitMQ user permissions for '#{new_resource.user}' from vhost #{vhost}."
end
Expand All @@ -144,6 +150,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
unless user_has_tag?(new_resource.user, new_resource.tag)
cmd = "rabbitmqctl set_user_tags #{new_resource.user} #{new_resource.tag}"
execute cmd do
environment shell_environment
Chef::Log.debug "rabbitmq_user_set_tags: #{cmd}"
Chef::Log.info "Setting RabbitMQ user '#{new_resource.user}' tags '#{new_resource.tag}'"
end
Expand All @@ -156,6 +163,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
unless user_has_tag?(new_resource.user, '"\[\]"')
cmd = "rabbitmqctl set_user_tags #{new_resource.user}"
execute cmd do
environment shell_environment
Chef::Log.debug "rabbitmq_user_clear_tags: #{cmd}"
Chef::Log.info "Clearing RabbitMQ user '#{new_resource.user}' tags."
end
Expand All @@ -168,6 +176,7 @@ def user_has_permissions?(name, vhost, perm_list = nil) # rubocop:disable all
execute "rabbitmqctl change_password #{new_resource.user}" do # ~FC009
sensitive true if Gem::Version.new(Chef::VERSION.to_s) >= Gem::Version.new('11.14.2')
command cmd
environment shell_environment
Chef::Log.debug "rabbitmq_user_change_password: #{cmd}"
Chef::Log.info "Editing RabbitMQ user '#{new_resource.user}'."
end
Expand Down
6 changes: 5 additions & 1 deletion providers/vhost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
# limitations under the License.
#

include Opscode::RabbitMQ

def vhost_exists?(name)
cmd = "rabbitmqctl -q list_vhosts | grep ^#{name}$"
cmd = Mixlib::ShellOut.new(cmd)
cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
cmd.environment = shell_environment
cmd.run_command
Chef::Log.debug "rabbitmq_vhost_exists?: #{cmd}"
Chef::Log.debug "rabbitmq_vhost_exists?: #{cmd.stdout}"
Expand All @@ -38,6 +40,7 @@ def vhost_exists?(name)
execute cmd do
Chef::Log.debug "rabbitmq_vhost_add: #{cmd}"
Chef::Log.info "Adding RabbitMQ vhost '#{new_resource.vhost}'."
environment shell_environment
new_resource.updated_by_last_action(true)
end
end
Expand All @@ -49,6 +52,7 @@ def vhost_exists?(name)
execute cmd do
Chef::Log.debug "rabbitmq_vhost_delete: #{cmd}"
Chef::Log.info "Deleting RabbitMQ vhost '#{new_resource.vhost}'."
environment shell_environment
new_resource.updated_by_last_action(true)
end
end
Expand Down

0 comments on commit f4d8da4

Please sign in to comment.