-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
135 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'shellwords' | ||
|
||
module Moonshot | ||
# Create an ssh command from configuration. | ||
class SSHCommandBuilder | ||
Result = Struct.new(:cmd, :ip) | ||
|
||
def initialize(ssh_config, instance_id) | ||
@config = ssh_config | ||
@instance_id = instance_id | ||
end | ||
|
||
def build(command = nil) | ||
cmd = ['ssh', '-t'] | ||
cmd << "-i #{@config.ssh_identity_file}" if @config.ssh_identity_file | ||
cmd << "-l #{@config.ssh_user}" if @config.ssh_user | ||
cmd << instance_ip | ||
cmd << command if command | ||
Result.new(cmd.join(' '), instance_ip) | ||
end | ||
|
||
private | ||
|
||
def instance_ip | ||
@instance_ip ||= Aws::EC2::Client.new | ||
.describe_instances(instance_ids: [@instance_id]) | ||
.reservations.first.instances.first.public_ip_address | ||
rescue | ||
raise "Failed to determine public IP address for instance #{@instance_id}!" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Moonshot | ||
class SSHConfig # rubocop:disable Documentation | ||
attr_accessor :ssh_identity_file | ||
attr_accessor :ssh_user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Moonshot | ||
# Choose a publically accessible instance to run commands on, given a Moonshot::Stack. | ||
class SSHTargetSelector | ||
def initialize(stack, asg_name: nil) | ||
@asg_name = asg_name | ||
@stack = stack | ||
end | ||
|
||
def choose! # rubocop:disable AbcSize | ||
groups = @stack.resources_of_type('AWS::AutoScaling::AutoScalingGroup') | ||
|
||
asg = if groups.count == 1 | ||
groups.first | ||
elsif asgs.count > 1 | ||
unless @asg_name | ||
raise 'Multiple Auto Scaling Groups found in the stack. Please specify which '\ | ||
'one to SSH into using the --auto-scaling-group (-g) option.' | ||
end | ||
groups.detect { |x| x.logical_resource_id == @config.ssh_auto_scaling_group_name } | ||
end | ||
raise 'Failed to find the Auto Scaling Group.' unless asg | ||
|
||
Aws::AutoScaling::Client.new.describe_auto_scaling_groups( | ||
auto_scaling_group_names: [asg.physical_resource_id] | ||
).auto_scaling_groups.first.instances.map(&:instance_id).first | ||
rescue | ||
raise 'Failed to find instances in the Auto Scaling Group!' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
describe 'Moonshot SSH features' do | ||
subject do | ||
Moonshot::Controller.new do |c| | ||
c.app_name = 'MyApp' | ||
c.environment_name = 'prod' | ||
c.ssh_config.ssh_user = 'joeuser' | ||
c.ssh_config.ssh_identity_file = '/Users/joeuser/.ssh/thegoods.key' | ||
c.ssh_command = 'cat /etc/passwd' | ||
end | ||
end | ||
|
||
describe 'Moonshot::Controller#ssh' do | ||
context 'normally' do | ||
it 'should execute an ssh command with proper parameters' do | ||
ts = instance_double(Moonshot::SSHTargetSelector) | ||
expect(Moonshot::SSHTargetSelector).to receive(:new).and_return(ts) | ||
expect(ts).to receive(:choose!).and_return('i-04683a82f2dddcc04') | ||
|
||
expect_any_instance_of(Moonshot::SSHCommandBuilder).to receive(:instance_ip).exactly(2) | ||
.and_return('123.123.123.123') | ||
expect(STDOUT).to receive(:puts) | ||
.with('Opening SSH connection to i-04683a82f2dddcc04 (123.123.123.123)...') | ||
expect(subject).to receive(:exec) | ||
.with('ssh -t -i /Users/joeuser/.ssh/thegoods.key -l joeuser 123.123.123.123 cat /etc/passwd') # rubocop:disable LineLength | ||
subject.ssh | ||
end | ||
end | ||
|
||
context 'when an instance id is given' do | ||
subject do | ||
c = super() | ||
c.config.ssh_instance = 'i-012012012012012' | ||
c | ||
end | ||
|
||
it 'should execute an ssh command with proper parameters' do | ||
expect_any_instance_of(Moonshot::SSHCommandBuilder).to receive(:instance_ip).exactly(2) | ||
.and_return('123.123.123.123') | ||
expect(STDOUT).to receive(:puts) | ||
.with('Opening SSH connection to i-012012012012012 (123.123.123.123)...') | ||
expect(subject).to receive(:exec) | ||
.with('ssh -t -i /Users/joeuser/.ssh/thegoods.key -l joeuser 123.123.123.123 cat /etc/passwd') # rubocop:disable LineLength | ||
subject.ssh | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters