diff --git a/lib/synapse/service_watcher/ec2tag.rb b/lib/synapse/service_watcher/ec2tag.rb index 43f3e192..9df77490 100644 --- a/lib/synapse/service_watcher/ec2tag.rb +++ b/lib/synapse/service_watcher/ec2tag.rb @@ -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 @@ -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 diff --git a/spec/lib/synapse/service_watcher_ec2tags_spec.rb b/spec/lib/synapse/service_watcher_ec2tags_spec.rb index 30aeab37..bbcf15bd 100644 --- a/spec/lib/synapse/service_watcher_ec2tags_spec.rb +++ b/spec/lib/synapse/service_watcher_ec2tags_spec.rb @@ -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 { @@ -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') }