Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LWRP for managing RabbitMQ parameters #207

Merged
merged 3 commits into from
Jan 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ group :kitchen_vagrant do
gem 'kitchen-vagrant'
end

group :kitchen_docker do
gem 'kitchen-docker'
end

group :kitchen_cloud do
gem 'kitchen-digitalocean'
gem 'kitchen-ec2'
Expand Down
80 changes: 80 additions & 0 deletions providers/parameter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Cookbook Name:: rabbitmq
# Provider:: parameter
#
# Author: Sean Porter <portertech@gmail.com>
# Copyright 2015 by Heavy Water Operations, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'shellwords'

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.run_command
begin
cmd.error!
true
rescue
false
end
end

action :set do
unless parameter_exists?(new_resource.vhost, new_resource.parameter)
cmd = 'rabbitmqctl set_parameter'
cmd << " -p #{new_resource.vhost}" unless new_resource.vhost.nil?
cmd << " #{new_resource.component}"
cmd << " #{new_resource.parameter}"

cmd << " '"
cmd << JSON.dump(new_resource.params)
cmd << "'"

parameter = "#{new_resource.component} #{new_resource.parameter}"

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

new_resource.updated_by_last_action(true)
Chef::Log.info "Done setting RabbitMQ parameter #{parameter}."
end
end

action :clear do
if parameter_exists?(new_resource.vhost, new_resource.parameter)
parameter = "#{new_resource.component} #{new_resource.parameter}"

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

new_resource.updated_by_last_action(true)
Chef::Log.info "Done clearing RabbitMQ parameter #{parameter}."
end
end

action :list do
execute 'list_parameters' do
command 'rabbitmqctl list_parameters'
end

new_resource.updated_by_last_action(true)
end
27 changes: 27 additions & 0 deletions resources/parameter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Cookbook Name:: rabbitmq
# Resource:: parameter
#
# Author: Sean Porter <portertech@gmail.com>
# Copyright 2015 by Sean Porter
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

actions :set, :clear, :list
default_action :set

attribute :parameter, :kind_of => String, :name_attribute => true
attribute :component, :kind_of => String
attribute :vhost, :kind_of => String
attribute :params, :kind_of => [Hash, Array], :default => {}
10 changes: 10 additions & 0 deletions test/cookbooks/rabbitmq_test/recipes/lwrps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@
apply_to 'queues'
action :set
end

rabbitmq_plugin 'rabbitmq_federation'

rabbitmq_vhost '/sensu'

rabbitmq_parameter 'sensu-dc-1' do
vhost '/sensu'
component 'federation-upstream'
params 'uri' => 'amqp://dc-cluster-node'
end
4 changes: 4 additions & 0 deletions test/integration/lwrps/rspec/lwrps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
describe command('rabbitmqctl list_policies') do
its(:stdout) { should match /\/\s+rabbitmq_cluster\s+queues\s+cluster\.\*\s+{"ha-mode":"all","ha-sync-mode":"automatic"}\s+0/ } # rubocop:disable all
end

describe command('rabbitmqctl list_parameters -p /sensu') do
its(:stdout) { should match /federation-upstream\s+sensu-dc-1\s+{"uri":"amqp:\/\/dc-cluster-node"}/ } # rubocop:disable all
end