Skip to content

Commit

Permalink
Update to work outside of AWS (if environment is provided), and suppo…
Browse files Browse the repository at this point in the history
…rt multiple regions simultaneously
  • Loading branch information
Matt Chesler committed Jul 21, 2017
1 parent 4196cc7 commit 0a530b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
72 changes: 41 additions & 31 deletions lib/movable_ink/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module MovableInk
class AWS
def initialize
raise "Can only be used within EC2" unless instance_id
def initialize(environment: nil)
@mi_env = environment
end

def run_with_backoff
Expand All @@ -26,32 +26,39 @@ def regions
}
end

def ec2_required
raise("Can only be used in EC2")
end

def availability_zone
@availability_zone ||= `ec2metadata --availability-zone`.chomp rescue nil
@availability_zone ||= `ec2metadata --availability-zone`.chomp rescue ec2_required
end

def region
@region ||= availability_zone.chop
def my_region
@my_region ||= availability_zone.chop
end

def instance_id
@instance_id ||= `ec2metadata --instance-id`.chomp rescue nil
@instance_id ||= `ec2metadata --instance-id`.chomp rescue ec2_required
end

def datacenter
def datacenter(region: my_region)
regions.key(region)
end

def ec2
@ec2_client ||= Aws::EC2::Client.new(region: region)
def ec2(region: my_region)
@ec2_client ||= {}
@ec2_client[region] ||= Aws::EC2::Client.new(region: region)
end

def sns
@sns_client ||= Aws::SNS::Client.new(region: region)
def sns(region: my_region)
@sns_client ||= {}
@sns_client[region] ||= Aws::SNS::Client.new(region: region)
end

def autoscaling
@autoscaling_client ||= Aws::Autoscaling::Client.new(region: region)
def autoscaling(region: my_region)
@autoscaling_client ||= {}
@autoscaling_client[region] ||= Aws::Autoscaling::Client.new(region: region)
end

def s3
Expand All @@ -78,7 +85,7 @@ def notify_nsq_can_not_be_drained

def notify_slack(subject:, message:)
sns.publish(topic_arn: sns_slack_topic_arn,
subject: "#{subject} (#{instance_id}, #{region})"
subject: "#{subject} (#{instance_id}, #{my_region})",
message: message)
end

Expand All @@ -105,8 +112,7 @@ def thopter_instance

def load_thopter_instance
run_with_backoff do
Aws::EC2::Client.new(region: 'us-east-1')
.describe_instances(filters: [
ec2(region: 'us-east-1').describe_instances(filters: [
{
name: 'tag:mi:roles',
values: ['*thopter*']
Expand All @@ -126,22 +132,26 @@ def load_thopter_instance
end
end

def all_instances
@all_instances ||= load_all_instances
def all_instances(region: my_region, no_filter: false)
@all_instances ||= {}
@all_instances[region] ||= load_all_instances(region, no_filter: no_filter)
end

def load_all_instances
def load_all_instances(region, no_filter: no_filter)
filters = if no_filter
[{
name: 'instance-state-name',
values: ['running']
},
{
name: 'tag:mi:env',
values: [mi_env]
}]
else
nil
end
run_with_backoff do
resp = ec2.describe_instances(filters: [
{
name: 'instance-state-name',
values: ['running']
},
{
name: 'tag:mi:env',
values: [mi_env]
}
])
resp = ec2(region: region).describe_instances(filters: filters)
reservations = resp.reservations
while (!resp.last_page?) do
resp = resp.next_page
Expand All @@ -151,10 +161,10 @@ def load_all_instances
end
end

def instances(role:, availability_zone: nil)
def instances(role:, region: my_region, availability_zone: nil)
role_pattern = mi_env == 'production' ? "^#{role}$" : "^*#{role}*$"
role_pattern = role_pattern.gsub('**','*').gsub('*','.*')
instances = all_instances.select { |instance|
instances = all_instances(region: region).select { |instance|
instance.tags.detect { |tag|
tag.key == 'mi:roles'&&
Regexp.new(role_pattern).match(tag.value) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/movable_ink/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module MovableInk
module AWS
VERSION = '0.0.4'
VERSION = '0.0.5'
end
end

0 comments on commit 0a530b3

Please sign in to comment.