Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix adding nil if hostname isn't set up first #18

Merged
merged 6 commits into from May 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 35 additions & 22 deletions recipes/default.rb
Expand Up @@ -6,28 +6,39 @@
errors = Array.new

# Verify either node["kafka"]["brokers"] or node["kafka"]["server.properties"]["broker.id"] is set properly
if (node["kafka"]["brokers"].nil? || !(node["kafka"]["brokers"].is_a? Array) || node["kafka"]["brokers"].empty?) && !node["kafka"]["server.properties"].has_key?("broker.id")
errors.push "node[:kafka][:brokers] or node[:kafka][:server.properties][:broker.id] must be set properly"
elsif !node["kafka"]["server.properties"].has_key?("broker.id")
# Generate brokerId for Kafka (uses the index of the brokers list to figure out which ID this broker should have). We add 1 to ensure
# we have a positive (non zero) number
brokerId = (node["kafka"]["brokers"].index{|broker| broker == node["fqdn"] || broker == node["ipaddress"] || broker == node["hostname"]} ) + 1
if brokerId.nil?
errors.push "Unable to find node in node[:kafka][:brokers] : #{node["kafka"]["brokers"]}"
ruby_block 'assert broker and zookeeper lists are correct' do # ~FC014
block_name 'attribute_assertions'
block do
if (node['kafka']['brokers'].to_a.empty?) && !node['kafka']['server.properties'].has_key?('broker.id')
errors.push 'node[:kafka][:brokers] or node[:kafka][:server.properties][:broker.id] must be set properly'
elsif !node['kafka']['server.properties'].has_key?('broker.id')
# Generate brokerId for Kafka (uses the index of the brokers list to figure out which ID this broker should have). We add 1 to ensure
# we have a positive (non zero) number
brokerId = ( node['kafka']['brokers'].to_a.index do |broker|
broker == node["fqdn"] || broker == node["ipaddress"] || broker == node["hostname"]
end
)
if brokerId.nil?
errors.push "Unable to find #{node['fqdn']}, #{node['ipaddress']} or "\
"#{node['hostname']} in node[:kafka][:brokers] : #{node['kafka']['brokers']}"
else
brokerId += 1
node.default['kafka']['server.properties']['broker.id'] = brokerId
end
end

# Verify we have a list of zookeeper instances
if (node['kafka']['zookeepers'].to_a.empty?) && !node['kafka']['server.properties'].has_key?('zookeeper.connect')
errors.push 'node[:kafka][:zookeepers] or node[:kafka][:server.properties][:zookeeper.connect] was not set properly'
elsif !node['kafka']['server.properties'].has_key?('zookeeper.connect')
node.default['kafka']['server.properties']['zookeeper.connect'] = node['kafka']['zookeepers'].to_a.join ','
end

# Raise an exception if there are any problems
raise "Unable to run kafka::default : \n -#{errors.join "\n -"}]\n" unless errors.empty?
end
node.default["kafka"]["server.properties"]["broker.id"] = brokerId
end

# Verify we have a list of zookeeper instances
if (node["kafka"]["zookeepers"].nil? || (!node["kafka"]["zookeepers"].is_a? Array) || node["kafka"]["zookeepers"].empty?) && !node["kafka"]["server.properties"].has_key?("zookeeper.connect")
errors.push "node[:kafka][:zookeepers] or node[:kafka][:server.properties][:zookeeper.connect] was not set properly"
elsif !node["kafka"]["server.properties"].has_key?("zookeeper.connect")
node.default["kafka"]["server.properties"]["zookeeper.connect"] = node["kafka"]["zookeepers"].join ","
end

# Raise an exception if there are any problems
raise "Unable to run kafka::default : [#{errors.join ", "}]" unless errors.empty?

# Set all default attributes that are built from other attributes
node.default["kafka"]["install_dir"] = "#{node["kafka"]["base_dir"]}/kafka"

Expand Down Expand Up @@ -185,9 +196,11 @@
owner node["kafka"]["user"]
group node["kafka"]["group"]
mode 00755
variables({
:properties => node["kafka"][template_file].to_hash
})
variables(
lazy {
{ :properties => node["kafka"][template_file].to_hash }
}
)
notifies :restart, "service[kafka]"
end
end
Expand Down
25 changes: 5 additions & 20 deletions spec/default_spec.rb
Expand Up @@ -13,6 +13,11 @@
end
end

it 'runs the code block to assure that broker and zookeeper node attributes are set' do
chef_run.converge(described_recipe)
expect(chef_run).to run_ruby_block('attribute_assertions')
end

it 'use zookeepers and brokers attributes' do
chef_run.converge(described_recipe)
expect(chef_run).to start_service('kafka')
Expand Down Expand Up @@ -48,26 +53,6 @@
expect(chef).to start_service('kafka')
end

it 'no brokers or broker.id attribute set' do
chef = ChefSpec::SoloRunner.new do |node|
node.set['kafka']['zookeepers'] = ['localhost:2181']
end

expect {
chef.converge(described_recipe)
}.to raise_error(RuntimeError)
end

it 'no zookeepers or zookeeper.connect attribute set' do
chef = ChefSpec::SoloRunner.new do |node|
node.set['kafka']['brokers'] = ['chefspec']
end

expect {
chef.converge(described_recipe)
}.to raise_error(RuntimeError)
end

context 'with version set to 0.8.0' do

let(:chef_run) do
Expand Down