Skip to content

Commit

Permalink
Add health/unhealthy marking
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Chesler committed Jul 17, 2017
1 parent 97bee20 commit dd5c7c0
Showing 1 changed file with 67 additions and 39 deletions.
106 changes: 67 additions & 39 deletions lib/movable_ink/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def sns
@sns_client ||= Aws::SNS::Client.new(region: region)
end

def autoscaling
@autoscaling_client ||= Aws::Autoscaling::Client.new(region: region)
end

def sns_slack_topic_arn
sns.list_topics.topics.select {|topic| topic.topic_arn.include? "slack-aws-alerts"}
.first.topic_arn
Expand All @@ -69,15 +73,15 @@ def mi_env
def load_mi_env
run_with_backoff do
ec2.describe_tags({:filters =>
[{:name =>'resource-id',
:values => [instance_id]
}]
})
.tags
.detect { |tag| tag.key == 'mi:env' }
.value
end
[{:name =>'resource-id',
:values => [instance_id]
}]
})
.tags
.detect { |tag| tag.key == 'mi:env' }
.value
end
end

def thopter_instance
@thopter_instance ||= load_thopter_instance
Expand All @@ -86,23 +90,23 @@ def thopter_instance
def load_thopter_instance
run_with_backoff do
Aws::EC2::Client.new(region: 'us-east-1')
.describe_instances(filters: [
{
name: 'tag:mi:roles',
values: ['*thopter*']
},
{
name: 'tag:mi:env',
values: [mi_env]
},
{
name: 'instance-state-name',
values: ['running']
}
])
.reservations
.map { |r| r.instances }
.flatten
.describe_instances(filters: [
{
name: 'tag:mi:roles',
values: ['*thopter*']
},
{
name: 'tag:mi:env',
values: [mi_env]
},
{
name: 'instance-state-name',
values: ['running']
}
])
.reservations
.map { |r| r.instances }
.flatten
end
end

Expand All @@ -112,20 +116,20 @@ def all_instances

def load_all_instances
run_with_backoff do
resp = ec2.describe_instances(filters: [{
name: 'instance-state-name',
values: ['running']
},
{
name: 'tag:mi:env',
values: [mi_env]
}
])
reservations = resp.reservations
while (!resp.last_page?) do
resp = resp.next_page
reservations += resp.reservations
end
resp = ec2.describe_instances(filters: [{
name: 'instance-state-name',
values: ['running']
},
{
name: 'tag:mi:env',
values: [mi_env]
}
])
reservations = resp.reservations
while (!resp.last_page?) do
resp = resp.next_page
reservations += resp.reservations
end
reservations.map { |r| r.instances }.flatten
end
end
Expand Down Expand Up @@ -178,5 +182,29 @@ def redis_by_role(role, port)
redii.push({"host" => instance, "port" => port})
}
end

def mark_me_as_unhealthy
run_with_backoff do
autoscaling.set_instance_health({
health_status: "Unhealthy",
instance_id: instance_id,
should_respect_grace_period: false
})
end
end

def mark_me_as_healthy(role:)
run_with_backoff do
ec2.create_tags({
resources: [instance_id],
tags: [
{
key: "mi:roles",
value: role
}
]
})
end
end
end
end

0 comments on commit dd5c7c0

Please sign in to comment.