diff --git a/Gemfile b/Gemfile index 9620557b..38842ccb 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/providers/parameter.rb b/providers/parameter.rb new file mode 100644 index 00000000..52d98667 --- /dev/null +++ b/providers/parameter.rb @@ -0,0 +1,80 @@ +# +# Cookbook Name:: rabbitmq +# Provider:: parameter +# +# Author: Sean Porter +# 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 diff --git a/resources/parameter.rb b/resources/parameter.rb new file mode 100644 index 00000000..45e96393 --- /dev/null +++ b/resources/parameter.rb @@ -0,0 +1,27 @@ +# +# Cookbook Name:: rabbitmq +# Resource:: parameter +# +# Author: Sean Porter +# 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 => {} diff --git a/test/cookbooks/rabbitmq_test/recipes/lwrps.rb b/test/cookbooks/rabbitmq_test/recipes/lwrps.rb index 7f540614..916a0222 100644 --- a/test/cookbooks/rabbitmq_test/recipes/lwrps.rb +++ b/test/cookbooks/rabbitmq_test/recipes/lwrps.rb @@ -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 diff --git a/test/integration/lwrps/rspec/lwrps_spec.rb b/test/integration/lwrps/rspec/lwrps_spec.rb index 5f2353cb..5d554ed5 100644 --- a/test/integration/lwrps/rspec/lwrps_spec.rb +++ b/test/integration/lwrps/rspec/lwrps_spec.rb @@ -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