-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto cluster configuration support
- added clustername fact and spec tests for it - added rabbitmq_cluster type and provider with tests - added related doc - based from PR#736, credits to @vchepkov
- Loading branch information
Showing
11 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This sets the cluster name to `test_cluster` | ||
# If run on another host than host1, this will join the host1's cluster | ||
rabbitmq_cluster { 'test_cluster': | ||
init_node => 'host1', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Facter.add(:rabbitmq_clustername) do | ||
setcode do | ||
if Facter::Util::Resolution.which('rabbitmqctl') | ||
ret = nil | ||
cluster_status = Facter::Core::Execution.execute('rabbitmqctl -q cluster_status 2>&1') | ||
[%r!{cluster_name,<<"(\S+)">>}!, %r!^Cluster name: (\S+)$!].each do |r| | ||
if (data = r.match(cluster_status)) | ||
ret = data[1] | ||
break | ||
end | ||
end | ||
end | ||
ret | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmq_cli')) | ||
Puppet::Type.type(:rabbitmq_cluster).provide( | ||
:rabbitmqctl, | ||
parent: Puppet::Provider::RabbitmqCli | ||
) do | ||
confine feature: :posix | ||
|
||
def exists? | ||
cluster_name == @resource[:name].to_s | ||
end | ||
|
||
def create | ||
storage_type = @resource[:node_disc_type].to_s | ||
|
||
init_node = @resource[:init_node].to_s.gsub(%r{^.*@}, '') | ||
|
||
if [Facter.value(:hostname), Facter.value(:fqdn)].include? init_node | ||
return rabbitmqctl('set_cluster_name', @resource[:name]) unless cluster_name == resource[:name].to_s | ||
else | ||
rabbitmqctl('stop_app') | ||
rabbitmqctl('join_cluster', "rabbit@#{init_node}", "--#{storage_type}") | ||
rabbitmqctl('start_app') | ||
end | ||
end | ||
|
||
def destroy | ||
rabbitmqctl('stop_app') | ||
rabbitmqctl('reset') | ||
rabbitmqctl('start_app') | ||
end | ||
|
||
def cluster_name | ||
cluster_status = rabbitmqctl('-q', 'cluster_status') | ||
[%r!{cluster_name,<<"(\S+)">>}!, %r!^Cluster name: (\S+)$!].each do |r| | ||
if (data = r.match(cluster_status)) | ||
return data[1] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Puppet::Type.newtype(:rabbitmq_cluster) do | ||
desc <<-DESC | ||
Native type for managing rabbitmq cluster | ||
@example Configure a cluster, rabbit_cluster | ||
rabbitmq_cluster { 'rabbit_cluster': | ||
init_node => 'host1' | ||
} | ||
@example Optional parameter tags will set further rabbitmq tags like monitoring, policymaker, etc. | ||
To set the cluster name use cluster_name. | ||
rabbitmq_cluster { 'rabbit_cluster': | ||
init_node => 'host1', | ||
node_disc_type => 'ram', | ||
} | ||
DESC | ||
|
||
ensurable do | ||
defaultto(:present) | ||
newvalue(:present) do | ||
provider.create | ||
end | ||
newvalue(:absent) do | ||
provider.destroy | ||
end | ||
end | ||
|
||
autorequire(:service) { 'rabbitmq-server' } | ||
|
||
newparam(:name, namevar: true) do | ||
desc 'The cluster name' | ||
end | ||
|
||
newparam(:init_node) do | ||
desc 'Name of which cluster node to join.' | ||
validate do |value| | ||
resource.validate_init_node(value) | ||
end | ||
end | ||
|
||
newparam(:node_disc_type) do | ||
desc 'Storage type of node, default disc.' | ||
newvalues(%r{disc|ram}) | ||
defaultto('disc') | ||
end | ||
|
||
def validate_init_node(value) | ||
raise ArgumentError, 'init_node must be defined' if value.empty? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'spec_helper' | ||
|
||
describe Facter::Util::Fact do | ||
before do | ||
Facter.clear | ||
end | ||
|
||
describe 'rabbitmq_clusternam' do | ||
context 'with value' do | ||
it do | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(true) | ||
Facter::Core::Execution.expects(:execute).with('rabbitmqctl -q cluster_status 2>&1').returns(' {cluster_name,<<"monty">>},') | ||
expect(Facter.fact(:rabbitmq_clustername).value).to eq('monty') | ||
end | ||
end | ||
|
||
context 'with dashes in hostname' do | ||
it do | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(true) | ||
Facter::Core::Execution.expects(:execute).with('rabbitmqctl -q cluster_status 2>&1').returns('Cluster name: rabbit-1') | ||
expect(Facter.fact(:rabbitmq_clustername).value).to eq('rabbit-1') | ||
end | ||
end | ||
|
||
context 'with dashes in clustername/hostname' do | ||
it do | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(true) | ||
Facter::Core::Execution.expects(:execute).with('rabbitmqctl -q cluster_status 2>&1').returns(' {cluster_name,<<"monty-python@rabbit-1">>},') | ||
expect(Facter.fact(:rabbitmq_clustername).value).to eq('monty-python@rabbit-1') | ||
end | ||
end | ||
|
||
context 'with quotes around node name' do | ||
it do | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(true) | ||
Facter::Core::Execution.expects(:execute).with('rabbitmqctl -q cluster_status 2>&1').returns("monty\npython\nCluster name: 'monty@rabbit-1'\nend\nof\nfile") | ||
expect(Facter.fact(:rabbitmq_clustername).value).to eq("'monty@rabbit-1'") | ||
end | ||
end | ||
|
||
context 'rabbitmq is not running' do | ||
it do | ||
error_string = <<-EOS | ||
Status of node 'monty@rabbit-1' ... | ||
Error: unable to connect to node 'monty@rabbit-1': nodedown | ||
DIAGNOSTICS | ||
=========== | ||
attempted to contact: ['monty@rabbit-1'] | ||
monty@rabbit-1: | ||
* connected to epmd (port 4369) on centos-7-x64 | ||
* epmd reports: node 'rabbit' not running at all | ||
no other nodes on centos-7-x64 | ||
* suggestion: start the node | ||
current node details: | ||
- node name: 'rabbitmq-cli-73@centos-7-x64' | ||
- home dir: /var/lib/rabbitmq | ||
- cookie hash: 6WdP0nl6d3HYqA5vTKMkIg== | ||
EOS | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(true) | ||
Facter::Core::Execution.expects(:execute).with('rabbitmqctl -q cluster_status 2>&1').returns(error_string) | ||
expect(Facter.fact(:rabbitmq_clustername).value).to be_nil | ||
end | ||
end | ||
|
||
context 'rabbitmqctl is not in path' do | ||
it do | ||
Facter::Util::Resolution.expects(:which).with('rabbitmqctl').returns(false) | ||
expect(Facter.fact(:rabbitmq_clustername).value).to be_nil | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.