Module: EasyAws

Extended by:
EasyAws
Included in:
EasyAws
Defined in:
lib/easy_aws/sns.rb,
lib/easy_aws/sqs.rb,
lib/easy_aws/helpers.rb,
lib/easy_aws/route53.rb

Constant Summary collapse

CNAME_RRS_TYPE =
'CNAME'

Instance Method Summary collapse

Instance Method Details

#add_sqs_subscriber(topic, queue) ⇒ Object



36
37
38
39
# File 'lib/easy_aws/sns.rb', line 36

def add_sqs_subscriber(topic, queue)
  puts "Subscribing queue to topic #{topic.name}"
  topic.subscribe queue
end

#add_subscribers(topic, subscribers) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easy_aws/sns.rb', line 22

def add_subscribers(topic, subscribers)
  return if subscribers.nil?
  existing = topic.subscriptions.map { |s| s.endpoint }
  to_ignore = subscribers & existing
  if to_ignore.length > 0
    puts "Subscribers exist for topic #{topic.name}, skipping:\n#{to_ignore}"
  end
  new = subscribers - to_ignore
  new.each do |s|
    puts "Subscribing #{s} to topic #{topic.name}"
    topic.subscribe s
  end
end

#clear_queue(queue) ⇒ Object



52
53
54
55
# File 'lib/easy_aws/sqs.rb', line 52

def clear_queue(queue)
  puts "Clearing queue items #{queue.arn}"
  queue.poll(:idle_timeout => 10) { |msg| puts msg.delete }
end

#create_or_replace_cname(domain, target, ttl = 300) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/easy_aws/route53.rb', line 8

def create_or_replace_cname(domain, target, ttl=300)
  domain = qualify_domain domain
  target = qualify_domain target
  zone = zone_for domain
  raise "No existing zone found for #{domain}" if zone.nil?

  puts "Creating CNAME #{domain} for #{target} in zone #{zone.name}"
  record_sets = zone.resource_record_sets
  cname = record_sets[domain, CNAME_RRS_TYPE]

  if !cname.nil? && cname.exists?
    puts "CNAME already exists, recreating"
    cname.delete
  end
  record_sets.create(domain, CNAME_RRS_TYPE, ttl:ttl, resource_records:[value:target])
end

#create_or_retrieve_queue(name, clear_policies = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/easy_aws/sqs.rb', line 25

def create_or_retrieve_queue(name, clear_policies=false)
  puts "Verifying queue exists #{name}"
  queue = get_queue(name)
  if queue.nil?
    queue = create_queue(name)
  else
    puts "Queue already exists, skipping creation"
    queue.policy = nil if clear_policies
  end
  queue
end

#create_or_retrieve_topic(name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/easy_aws/sns.rb', line 11

def create_or_retrieve_topic(name)
  puts "Creating topic #{name}"
  topic = sns.topics.select { |t| t.name == name }.first
  if topic.nil?
    execute { topic = sns.topics.create name }
  else
    puts "Topic already exists, skipping"
  end
  topic
end

#create_queue(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/easy_aws/sqs.rb', line 15

def create_queue(name, options={})
  options[:message_retention_period] ||= 1209600
  puts "Creating queue #{name}"
  queue = nil
  execute {queue = sqs.queues.create name, options}
  puts "Verifying queue created"
  retry_until_success { !get_queue(name).nil? }
  queue
end

#delete_cname(domain) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/easy_aws/route53.rb', line 25

def delete_cname(domain)
  domain = qualify_domain domain
  zone = zone_for domain
  return if zone.nil?

  puts "Deleting CNAME #{domain} from zone #{zone.name}"
  cname = zone.resource_record_sets[domain, CNAME_RRS_TYPE]
  cname.delete if !cname.nil? && cname.exists?
end

#delete_queues_with(prefix) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/easy_aws/sqs.rb', line 37

def delete_queues_with(prefix)
  puts "Deleting queues starting with #{prefix}"
  queues = sqs.queues.with_prefix(prefix)
  queues.each do |q|
    puts "Deleting queue #{q.arn}"
    q.delete
  end
  puts "Verifying all queues have been deleted"
  verify_queues_deleted prefix
end

#delete_topics(topics) ⇒ Object



47
48
49
# File 'lib/easy_aws/sns.rb', line 47

def delete_topics(topics)
  topics.each { |t| t.delete }
end

#delete_topics_with(prefix) ⇒ Object



41
42
43
44
45
# File 'lib/easy_aws/sns.rb', line 41

def delete_topics_with(prefix)
  puts "Deleting sns topics starting with #{prefix}"
  topics = sns.topics.select { |t| t.name.start_with? prefix }
  delete_topics topics
end

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/easy_aws/helpers.rb', line 10

def execute
  waited = false
  begin
    yield
  rescue Exception => error
    wait_error = /.*you must wait (\d*) seconds.*/i.match(error.to_s)
    raise error if !wait_error || waited
    secs_to_wait = wait_error[1].to_i
    puts "Told to re-try after #{secs_to_wait} seconds, so will try again"
    sleep secs_to_wait
    waited = true
    retry
  end
end

#get_next_msg_from(queue) ⇒ Object



57
58
59
60
61
# File 'lib/easy_aws/sqs.rb', line 57

def get_next_msg_from(queue)
  value = nil
  queue.receive_message(wait_time_seconds:10) { |msg| value = msg }
  value
end

#get_queue(name) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/easy_aws/sqs.rb', line 7

def get_queue(name)
  begin
    sqs.queues.named(name)
  rescue Exception => error
    raise error unless error.to_s =~ /.*queue does not exist.*/i
  end
end

#get_topic(arn) ⇒ Object



7
8
9
# File 'lib/easy_aws/sns.rb', line 7

def get_topic(arn)
  sns.topics[arn]
end

#retry_until_success(timeout = 60, retry_interval = 5) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easy_aws/helpers.rb', line 25

def retry_until_success(timeout=60, retry_interval=5)
  time_elapsed = 0
  success = false
  start_time = Time.now

  while time_elapsed < timeout
    success = yield
    break if success
    sleep retry_interval
    time_elapsed = Time.now - start_time
  end
  raise "Operation still not successful after #{timeout} secs" unless success
end

#setup(access_key, secret_key, region = 'ap-southeast-2') ⇒ Object



6
7
8
# File 'lib/easy_aws/helpers.rb', line 6

def setup(access_key, secret_key, region='ap-southeast-2')
  AWS::config access_key_id:access_key, secret_access_key:secret_key, region:region
end

#verify_queues_deleted(prefix) ⇒ Object



48
49
50
# File 'lib/easy_aws/sqs.rb', line 48

def verify_queues_deleted(prefix)
  retry_until_success { sqs.queues.with_prefix(prefix).count == 0 }
end