auto-consul 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/auto-consul CHANGED
@@ -23,6 +23,14 @@ class Command < OpenStruct
23
23
  @state ||= AutoConsul::RunState::CLIProvider.new
24
24
  end
25
25
 
26
+ def heartbeat_addr
27
+ if advertise.nil?
28
+ bind
29
+ else
30
+ advertise
31
+ end
32
+ end
33
+
26
34
  def do_set_mode
27
35
  cluster.set_mode! local, expiry, servers
28
36
  # Healthy exit
@@ -41,7 +49,9 @@ class Command < OpenStruct
41
49
  runner = :agent_runner if local.agent?
42
50
  runner = :server_runner if local.server?
43
51
  runner = AutoConsul::Runner.method(runner)
44
- monitor = runner.call(node, addr, expiry, local, cluster)
52
+ opt = {}
53
+ opt[:advertise] = advertise if ! advertise.nil?
54
+ monitor = runner.call(node, bind, expiry, local, cluster, opt)
45
55
  if with_heartbeat and ticks > 0
46
56
  monitor.while_up do |ap|
47
57
  # There's data exchange with the agent and with the registries
@@ -62,8 +72,8 @@ class Command < OpenStruct
62
72
 
63
73
  def do_heartbeat
64
74
  if state.running?
65
- cluster.servers.heartbeat! node, addr, expiry if state.server?
66
- cluster.agents.heartbeat! node, addr, expiry if state.agent?
75
+ cluster.servers.heartbeat! node, heartbeat_addr, expiry if state.server?
76
+ cluster.agents.heartbeat! node, heartbeat_addr, expiry if state.agent?
67
77
  # Healthy exit.
68
78
  0
69
79
  else
@@ -82,7 +92,9 @@ class Command < OpenStruct
82
92
  end
83
93
  end
84
94
 
85
- runner = Command.new(:data_dir => '/tmp/consul/state',
95
+ runner = Command.new(:advertise => nil,
96
+ :bind => "0.0.0.0",
97
+ :data_dir => '/tmp/consul/state',
86
98
  :dc => 'dc1',
87
99
  :with_heartbeat => false,
88
100
  :expiry => 120,
@@ -102,7 +114,15 @@ parser = OptionParser.new do |opts|
102
114
  end
103
115
 
104
116
  opts.on("-a", "--address IPADDR", String, "The IP address to bind to and announce for cluster communication.") do |a|
105
- runner.addr = a
117
+ runner.advertise = runner.bind = a
118
+ end
119
+
120
+ opts.on("--advertise IPADDR", String, "The IP address to advertise for cluster communication. Must be routable. Defaults to the bound address.") do |a|
121
+ runner.advertise = a
122
+ end
123
+
124
+ opts.on("-b", "--bind IPADDR", String, "The IP address to bind to for cluster communication. Defaults to 0.0.0.0, which consul handles specially.") do |a|
125
+ runner.bind = a
106
126
  end
107
127
 
108
128
  opts.on("-n", "--node NAME", String, "The unique name by which the node identifies itself within the cluster.") do |n|
@@ -132,10 +132,12 @@ module AutoConsul
132
132
  end
133
133
  end
134
134
 
135
- def self.joining_runner(agent_args, remote_ip=nil)
136
- runner = AgentProcess.new(agent_args)
137
- if not remote_ip.nil?
138
- runner.on_up {|a| join remote_ip}
135
+ def self.joining_runner(agent_args, opt={})
136
+ opt_args = []
137
+ opt_args += ['-advertise', opt[:advertise]] if ! opt[:advertise].nil?
138
+ runner = AgentProcess.new(agent_args + opt_args)
139
+ if not opt[:remote_ip].nil?
140
+ runner.on_up {|a| join opt[:remote_ip]}
139
141
  end
140
142
  runner
141
143
  end
@@ -157,21 +159,21 @@ module AutoConsul
157
159
  hosts[0].data
158
160
  end
159
161
 
160
- def self.agent_runner identity, bind_ip, expiry, local_state, registry
161
- remote_ip = pick_joining_host(registry.agents.members(expiry))
162
+ def self.agent_runner identity, bind_ip, expiry, local_state, registry, opt={}
163
+ opt[:remote_ip] = pick_joining_host(registry.agents.members(expiry))
162
164
  joining_runner(['-bind', bind_ip,
163
165
  '-data-dir', local_state.data_path,
164
- '-node', identity], remote_ip)
166
+ '-node', identity], opt)
165
167
  end
166
168
 
167
- def self.server_runner identity, bind_ip, expiry, local_state, registry
169
+ def self.server_runner identity, bind_ip, expiry, local_state, registry, opt={}
168
170
  members = registry.servers.members(expiry)
169
- remote_ip = members.size > 0 ? pick_joining_host(members) : nil
171
+ opt[:remote_ip] = members.size > 0 ? pick_joining_host(members) : nil
170
172
 
171
173
  args = ['-bind', bind_ip, '-data-dir', local_state.data_path, '-node', identity, '-server']
172
174
  args << '-bootstrap' if members.size < 1
173
175
 
174
- joining_runner(args, remote_ip)
176
+ joining_runner(args, opt)
175
177
  end
176
178
  end
177
179
  end
data/spec/runner_spec.rb CHANGED
@@ -88,7 +88,10 @@ shared_examples_for 'a consul agent process runner' do |method_name, registry_na
88
88
  end
89
89
 
90
90
  callable = AutoConsul::Runner.method(method_name)
91
- expect(callable.call(identity, ip, expiry, local_state, registry)).to be(runner)
91
+
92
+ args = [identity, ip, expiry, local_state, registry]
93
+ args << {:advertise => advertise_ip} unless advertise_ip.nil?
94
+ expect(callable.call(*args)).to be(runner)
92
95
  end
93
96
  end
94
97
 
@@ -420,8 +423,11 @@ describe AutoConsul::Runner::AgentProcess do
420
423
  end
421
424
  end
422
425
 
426
+ ADVERTISE_IP = "172.17.0.31"
427
+
423
428
  describe AutoConsul::Runner do
424
429
  let(:ip) { "192.168.50.101" }
430
+ let(:advertise_ip) { nil && :foo }
425
431
  let(:remote_ip) { "192.168.50.102" }
426
432
  let(:member) { double("ClusterMember", :identity => 'foo', :time => double, :data => remote_ip) }
427
433
  let(:agents_list) { [] }
@@ -440,20 +446,41 @@ describe AutoConsul::Runner do
440
446
  end
441
447
 
442
448
  describe :agent_runner do
443
- it_behaves_like 'a consul agent process runner', :agent_runner, :agents, true, []
449
+ describe 'with no advertise IP' do
450
+ it_behaves_like 'a consul agent process runner', :agent_runner, :agents, true, []
451
+ end
452
+
453
+ describe 'with an advertise IP' do
454
+ let(:advertise_ip) { ADVERTISE_IP }
455
+ it_behaves_like 'a consul agent process runner', :agent_runner, :agents, true, ['-advertise', ADVERTISE_IP]
456
+ end
444
457
  end
445
458
 
446
459
  describe :server_runner do
447
460
  describe 'with empty server registry' do
448
- # consul agent -bind 192.168.50.100 -data-dir /opt/consul/server/data -node vagrant-server -server -bootstrap
449
- it_behaves_like 'a consul agent process runner', :server_runner, :servers, false, ['-server', '-bootstrap']
461
+ describe 'with no advertise IP' do
462
+ # consul agent -bind 192.168.50.100 -data-dir /opt/consul/server/data -node vagrant-server -server -bootstrap
463
+ it_behaves_like 'a consul agent process runner', :server_runner, :servers, false, ['-server', '-bootstrap']
464
+ end
465
+
466
+ describe 'with an advertise IP' do
467
+ let(:advertise_ip) { ADVERTISE_IP }
468
+ # consul agent -bind 192.168.50.100 -data-dir /opt/consul/server/data -node vagrant-server -advertise 172.17.0.31 -server -bootstrap
469
+ it_behaves_like 'a consul agent process runner', :server_runner, :servers, false, ['-server', '-bootstrap', '-advertise', ADVERTISE_IP]
470
+ end
450
471
  end
451
472
 
452
473
  describe 'with other servers in registry' do
453
- # consul agent -bind 192.168.50.100 -data-dir /opt/consul/server/data -node vagrant-server -server
454
- # consul join some_ip
474
+ describe 'and no advertise IP' do
475
+ # consul agent -bind 192.168.50.100 -data-dir /opt/consul/server/data -node vagrant-server -server
476
+ # consul join some_ip
477
+ it_behaves_like 'a consul agent process runner', :server_runner, :servers, true, ['-server']
478
+ end
455
479
 
456
- it_behaves_like 'a consul agent process runner', :server_runner, :servers, true, ['-server']
480
+ describe 'and an advertise IP' do
481
+ let(:advertise_ip) { ADVERTISE_IP }
482
+ it_behaves_like 'a consul agent process runner', :server_runner, :servers, true, ['-server', '-advertise', ADVERTISE_IP]
483
+ end
457
484
  end
458
485
  end
459
486
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto-consul
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: