From 54514b418a814f7acfada4b648329f608ea6f66e Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Thu, 22 Jan 2015 11:40:04 -0800 Subject: [PATCH 1/3] [parameter] LWRP for managing RabbitMQ parameters --- Gemfile | 4 + providers/parameter.rb | 91 +++++++++++++++++++ resources/parameter.rb | 27 ++++++ test/cookbooks/rabbitmq_test/recipes/lwrps.rb | 10 ++ test/integration/lwrps/rspec/lwrps_spec.rb | 4 + 5 files changed, 136 insertions(+) create mode 100644 providers/parameter.rb create mode 100644 resources/parameter.rb 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..6f14b111 --- /dev/null +++ b/providers/parameter.rb @@ -0,0 +1,91 @@ +# +# Cookbook Name:: rabbitmq +# Provider:: parameter +# +# Author: Robert Choi , Sean Porter +# Copyright 2013 by Robert Choi +# +# 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 << " '{" + + first_param = true + new_resource.params.each do |key, value| + cmd << ',' unless first_param + + if value.is_a? String + cmd << "\"#{key}\":\"#{value}\"" + else + cmd << "\"#{key}\":#{value}" + end + first_param = false + end + + 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..74bf5d41 --- /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 diff --git a/test/cookbooks/rabbitmq_test/recipes/lwrps.rb b/test/cookbooks/rabbitmq_test/recipes/lwrps.rb index 7f540614..dbc2be70 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 From 03669431e32b9c5861805d08c47d811ab38eda32 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Thu, 22 Jan 2015 12:13:58 -0800 Subject: [PATCH 2/3] [parameter] hash rockets in hashes --- test/cookbooks/rabbitmq_test/recipes/lwrps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cookbooks/rabbitmq_test/recipes/lwrps.rb b/test/cookbooks/rabbitmq_test/recipes/lwrps.rb index dbc2be70..916a0222 100644 --- a/test/cookbooks/rabbitmq_test/recipes/lwrps.rb +++ b/test/cookbooks/rabbitmq_test/recipes/lwrps.rb @@ -65,5 +65,5 @@ rabbitmq_parameter 'sensu-dc-1' do vhost '/sensu' component 'federation-upstream' - params uri: 'amqp://dc-cluster-node' + params 'uri' => 'amqp://dc-cluster-node' end From 9f7e43390c6b9ab9a649bd8c8dc6d4e923ec62bb Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Mon, 26 Jan 2015 16:05:30 -0800 Subject: [PATCH 3/3] [parameter] support params array, use JSON serialization --- providers/parameter.rb | 21 +++++---------------- resources/parameter.rb | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/providers/parameter.rb b/providers/parameter.rb index 6f14b111..52d98667 100644 --- a/providers/parameter.rb +++ b/providers/parameter.rb @@ -2,8 +2,8 @@ # Cookbook Name:: rabbitmq # Provider:: parameter # -# Author: Robert Choi , Sean Porter -# Copyright 2013 by Robert Choi +# 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. @@ -42,21 +42,10 @@ def parameter_exists?(vhost, name) cmd << " -p #{new_resource.vhost}" unless new_resource.vhost.nil? cmd << " #{new_resource.component}" cmd << " #{new_resource.parameter}" - cmd << " '{" - first_param = true - new_resource.params.each do |key, value| - cmd << ',' unless first_param - - if value.is_a? String - cmd << "\"#{key}\":\"#{value}\"" - else - cmd << "\"#{key}\":#{value}" - end - first_param = false - end - - cmd << "}'" + cmd << " '" + cmd << JSON.dump(new_resource.params) + cmd << "'" parameter = "#{new_resource.component} #{new_resource.parameter}" diff --git a/resources/parameter.rb b/resources/parameter.rb index 74bf5d41..45e96393 100644 --- a/resources/parameter.rb +++ b/resources/parameter.rb @@ -24,4 +24,4 @@ attribute :parameter, :kind_of => String, :name_attribute => true attribute :component, :kind_of => String attribute :vhost, :kind_of => String -attribute :params, :kind_of => Hash +attribute :params, :kind_of => [Hash, Array], :default => {}