Skip to content

Commit

Permalink
Merge pull request #17 from envato/poho-add-autoscaling-groups
Browse files Browse the repository at this point in the history
add ability to connect to ASG
  • Loading branch information
envatopoho authored Sep 25, 2017
2 parents 7958911 + 0b1eea6 commit 7d18ca5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Have you ever wanted to shuck away the hard, rough exterior of an ECS cluster and get to the soft, chewy innards? Sounds like you need KnuckleCluster!
This tool provides scripts, invoked via cli or rakefile, to list, connect to and/or run commands on ecs agents and containers via ssh. This makes it very easy to interrogate ECS agents and containers without having to go digging for IP addresses and things.
Primarily created as a tool to connect to instances in an ECS cluster and see what is running on them, it has evolved slightly to include the ability to list instances in spot requests and auto-scaling groups.

## Features
* See what agents in your ECS cluster are doing
Expand All @@ -11,7 +12,7 @@ This tool provides scripts, invoked via cli or rakefile, to list, connect to and
* Optionally integrates with [aws-vault](https://github.com/99designs/aws-vault) for AWS authentication

## Development Status
Is being used in production for various projects and is considered stable. Any new features/bug fixes etc are most welcome! Test coverage is minimal...
Is being used in production for various projects and is considered stable. Any new features/bug fixes etc are most welcome!

## Installation

Expand Down Expand Up @@ -185,8 +186,9 @@ Possible options are below. If left blank, they will be ignored and defaults use

Argument | Description
-------- | -----------
cluster_name | The name of the cluster (not the ARN). eg 'my-super-cluster'. Required (unless using spot_request_id)
spot_request_id | The spot request ID you are connecting to. eg 'sfr-abcdef'. Required (unless using cluster_name)
cluster_name | The name of the cluster (not the ARN). eg 'my-super-cluster'. One of `cluster_name`,`spot_request_id` or `asg_name` is required.
spot_request_id | The spot request ID you are connecting to. eg 'sfr-abcdef'. One of `cluster_name`,`spot_request_id` or `asg_name` is required.
asg_name | The spot request ID you are connecting to. eg 'sfr-abcdef'. One of `cluster_name`,`spot_request_id` or `asg_name` is required.
region | The AWS region you would like to use. Defaults to `us-east-1`
bastion | if you have a bastion to proxy to your ecs cluster via ssh, put the name of it here as defined in your `~/.ssh/config` file.
rsa_key_location | The RSA key needed to connect to an ecs agent eg `~/.ssh/id_rsa`.
Expand All @@ -196,7 +198,10 @@ aws_vault_profile | If you use the `aws-vault` tool to manage your AWS credentia
profile | Another profile to inherit settings from. Settings from lower profiles can be overridden in higher ones.

## Spot Fleets
If you wish to see what instances are running within a spot fleet, KnuckleCluster can do that too!. In your config, use `spot_request_id` instead of `cluster_name`. Note that the `containers` command will not work when invoking.
If you wish to see what instances are running within a spot fleet, KnuckleCluster can do that too!. In your config, use `spot_request_id` instead of `cluster_name`. Note that the `containers` command will not work when invoking (use `agents` instead).

## AutoScaling Groups
If you wish to see what instances are running within an ASG, KnuckleCluster can do that too!. In your config, use `asg_name` instead of `cluster_name`. Note that the `containers` command will not work when invoking (use `agents` instead).

## Maintainer
[Envato](https://github.com/envato)
Expand Down
15 changes: 12 additions & 3 deletions lib/knuckle_cluster.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'knuckle_cluster/asg_instance_registry'
require 'knuckle_cluster/ecs_agent_registry'
require 'knuckle_cluster/spot_request_instance_registry'
require "knuckle_cluster/version"
Expand All @@ -14,6 +15,7 @@ class << self
def new(
cluster_name: nil,
spot_request_id: nil,
asg_name: nil,
region: 'us-east-1',
bastion: nil,
rsa_key_location: nil,
Expand All @@ -24,6 +26,7 @@ def new(
tunnels: {})
@cluster_name = cluster_name
@spot_request_id = spot_request_id
@asg_name = asg_name
@region = region
@bastion = bastion
@rsa_key_location = rsa_key_location
Expand All @@ -33,8 +36,8 @@ def new(
@shortcuts = shortcuts
@tunnels = tunnels

if @cluster_name.nil? && @spot_request_id.nil?
raise "Must specify either cluster_name or spot_request_id"
if @cluster_name.nil? && @spot_request_id.nil? && @asg_name.nil?
raise "Must specify either cluster_name, spot_request_id or asg name"
end
self
end
Expand Down Expand Up @@ -78,7 +81,8 @@ def open_tunnel(name:)

private

attr_reader :cluster_name, :spot_request_id, :region, :bastion, :rsa_key_location, :ssh_username,
attr_reader :cluster_name, :spot_request_id, :asg_name,
:region, :bastion, :rsa_key_location, :ssh_username,
:sudo, :aws_vault_profile, :shortcuts, :tunnels

def select_agent(auto:)
Expand Down Expand Up @@ -196,6 +200,11 @@ def agent_registry
aws_client_config: aws_client_config,
spot_request_id: spot_request_id,
)
elsif @asg_name
AsgInstanceRegistry.new(
aws_client_config: aws_client_config,
asg_name: asg_name,
)
end
)
end
Expand Down
58 changes: 58 additions & 0 deletions lib/knuckle_cluster/asg_instance_registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'knuckle_cluster/agent'

require 'forwardable'

module KnuckleCluster
class AsgInstanceRegistry
extend Forwardable

def initialize(aws_client_config:, asg_name:)
@aws_client_config = aws_client_config
@asg_name = asg_name
end

def agents
@agents ||= load_agents
end

def output_agents
tp agents,
:index,
:instance_id,
# :public_ip,
:private_ip,
:availability_zone
end

private

attr_reader :aws_client_config, :asg_name

def load_agents
auto_scaling_instances = autoscaling_client.describe_auto_scaling_groups(auto_scaling_group_names: [@asg_name]).to_h

instance_ids = auto_scaling_instances[:auto_scaling_groups][0][:instances].map{|instance| instance[:instance_id]}

instance_reservations = ec2_client.describe_instances(instance_ids: instance_ids).reservations

instance_reservations.map(&:instances).flatten.map.with_index do |instance, index|
Agent.new(
index: index + 1,
instance_id: instance[:instance_id],
public_ip: instance[:public_ip_address],
private_ip: instance[:private_ip_address],
availability_zone: instance.dig(:placement, :availability_zone),
)
end
end

def ec2_client
@ec2_client ||= Aws::EC2::Client.new(aws_client_config)
end

def autoscaling_client
@autoscaling_client ||= Aws::AutoScaling::Client.new(aws_client_config)
end

end
end
2 changes: 1 addition & 1 deletion lib/knuckle_cluster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module KnuckleCluster
VERSION = '1.1.0'
VERSION = '1.2.0'
end

0 comments on commit 7d18ca5

Please sign in to comment.