Skip to content

Commit

Permalink
Merge pull request #126 from dcosson/ec2tag_improvements
Browse files Browse the repository at this point in the history
ec2tag watcher improvements
  • Loading branch information
jolynch committed Sep 11, 2015
2 parents 45fde7f + 9d9e5ad commit 01a2409
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
18 changes: 11 additions & 7 deletions lib/synapse/service_watcher/ec2tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ def validate_discovery_opts
"Missing server_port_override for service #{@name} - which port are backends listening on?"
end

unless @haproxy['server_port_override'].match(/^\d+$/)
unless @haproxy['server_port_override'].to_s.match(/^\d+$/)
raise ArgumentError, "Invalid server_port_override value"
end

# Required, but can use well-known environment variables.
%w[aws_access_key_id aws_secret_access_key aws_region].each do |attr|
unless (@discovery[attr] || ENV[attr.upcase])
raise ArgumentError, "Missing #{attr} option or #{attr.upcase} environment variable"
end
# aws region is optional in the SDK, aws will use a default value if not provided
unless @discovery['aws_region'] || ENV['AWS_REGION']
log.info "aws region is missing, will use default"
end
# access key id & secret are optional, might be using IAM instance profile for credentials
unless ((@discovery['aws_access_key_id'] || ENV['aws_access_key_id']) \
&& (@discovery['aws_secret_access_key'] || ENV['aws_secret_access_key'] ))
log.info "aws access key id & secret not set in config or env variables for service #{name}, will attempt to use IAM instance profile"
end
end

Expand All @@ -60,10 +63,11 @@ def watch
if set_backends(discover_instances)
log.info "synapse: ec2tag watcher backends have changed."
end
sleep_until_next_check(start)
rescue Exception => e
log.warn "synapse: error in ec2tag watcher thread: #{e.inspect}"
log.warn e.backtrace
ensure
sleep_until_next_check(start)
end
end

Expand Down
33 changes: 27 additions & 6 deletions spec/lib/synapse/service_watcher_ec2tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ def munge_haproxy_arg(name, new_value)
end

context 'when missing arguments' do
it 'complains if aws_region is missing' do
it 'does not break if aws_region is missing' do
expect {
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_region'), mock_synapse)
}.to raise_error(ArgumentError, /Missing aws_region/)
}.not_to raise_error
end
it 'complains if aws_access_key_id is missing' do
it 'does not break if aws_access_key_id is missing' do
expect {
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_access_key_id'), mock_synapse)
}.to raise_error(ArgumentError, /Missing aws_access_key_id/)
}.not_to raise_error
end
it 'complains if aws_secret_access_key is missing' do
it 'does not break if aws_secret_access_key is missing' do
expect {
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_secret_access_key'), mock_synapse)
}.to raise_error(ArgumentError, /Missing aws_secret_access_key/)
}.not_to raise_error
end
it 'complains if server_port_override is missing' do
expect {
Expand All @@ -122,6 +122,27 @@ def munge_haproxy_arg(name, new_value)
let(:instance1) { FakeAWSInstance.new }
let(:instance2) { FakeAWSInstance.new }

context 'watch' do

it 'discovers instances, configures backends, then sleeps' do
fake_backends = [1,2,3]
expect(subject).to receive(:discover_instances).and_return(fake_backends)
expect(subject).to receive(:set_backends).with(fake_backends) { subject.stop }
expect(subject).to receive(:sleep_until_next_check)
subject.send(:watch)
end

it 'sleeps until next check if discover_instances fails' do
expect(subject).to receive(:discover_instances) do
subject.stop
raise "discover failed"
end
expect(subject).to receive(:sleep_until_next_check)
subject.send(:watch)
end

end

context 'using the AWS API' do
let(:ec2_client) { double('AWS::EC2') }
let(:instance_collection) { double('AWS::EC2::InstanceCollection') }
Expand Down

0 comments on commit 01a2409

Please sign in to comment.