Skip to content

Commit

Permalink
Let user decide join strategy on cluster mode
Browse files Browse the repository at this point in the history
  • Loading branch information
inean committed Dec 18, 2014
1 parent 0e2f76a commit 9b473a5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Installs and configures [Consul][1] client, server and UI.
<td>Consul servers to join</td>
<td><tt>[]</tt></td>
</tr>
<tr>
<td><tt>['consul']['retry_on_join']</tt></td>
<td>Boolean</td>
<td>Set to true to wait for servers to be up before try to elect a leader</td>
<td><tt>false</tt></td>
</tr>
<tr>
<td><tt>['consul']['bind_addr']</tt></td>
<td>String</td>
Expand Down
2 changes: 2 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

# Service attributes
default['consul']['service_mode'] = 'bootstrap'
default['consul']['retry_on_join'] = false

# In the cluster mode, set the default cluster size to 3
default['consul']['bootstrap_expect'] = 3
default['consul']['data_dir'] = '/var/lib/consul'
Expand Down
7 changes: 4 additions & 3 deletions recipes/_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
service_config = JSON.parse(node['consul']['extra_params'].to_json)
service_config['data_dir'] = node['consul']['data_dir']
num_cluster = node['consul']['bootstrap_expect'].to_i
join_mode = node['consul']['retry_on_join'] ? 'retry_join' : 'start_join'

case node['consul']['service_mode']
when 'bootstrap'
Expand All @@ -71,15 +72,15 @@
service_config['server'] = true
if num_cluster > 1
service_config['bootstrap_expect'] = num_cluster
service_config['start_join'] = node['consul']['servers']
service_config[join_mode] = node['consul']['servers']
else
service_config['bootstrap'] = true
end
when 'server'
service_config['server'] = true
service_config['start_join'] = node['consul']['servers']
service_config[join_mode] = node['consul']['servers']
when 'client'
service_config['start_join'] = node['consul']['servers']
service_config[join_mode] = node['consul']['servers']
else
Chef::Application.fatal! %Q(node['consul']['service_mode'] must be "bootstrap", "cluster", "server", or "client")
end
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/recipes/_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@
end
end

context 'with a cluster service_mode, bootstrap_expect > 1 and a server list to join in retry mode' do
let(:chef_run) do
ChefSpec::Runner.new(node_attributes) do |node|
node.set['consul']['service_mode'] = 'cluster'
node.set['consul']['bootstrap_expect'] = '3'
node.set['consul']['servers'] = [ 'server1', 'server2', 'server3' ]
end.converge(described_recipe)
end
it do
expect(chef_run).to create_file('/etc/consul.d/default.json')
.with_content(/retry_join/)
.with_content(/server9/)
.with_content(/server2/)
.with_content(/server3/)
end
end

context 'with a cluster service_mode, bootstrap_expect > 1, and a server list to join' do
let(:chef_run) do
ChefSpec::Runner.new(node_attributes) do |node|
Expand Down

0 comments on commit 9b473a5

Please sign in to comment.