diff --git a/lib/claws/command/ec2.rb b/lib/claws/command/ec2.rb index 3e006a0..09d0bbc 100644 --- a/lib/claws/command/ec2.rb +++ b/lib/claws/command/ec2.rb @@ -33,13 +33,21 @@ def self.exec(options) puts 'connecting to server...' + identity = config.ssh.identity.nil? ? '' : "-i #{config.ssh.identity}" + current_instance = instances[selection.to_i] + ssh_opts = {:identity => identity, :ssh_user => config.ssh.user} + if instances[selection.to_i].vpc? - system "ssh #{config.ssh.user}@#{instances[selection.to_i].private_ip_address}" + ssh(ssh_opts.merge(:host => current_instance.private_ip_address)) else - system "ssh #{config.ssh.user}@#{instances[selection.to_i].dns_name}" + ssh(ssh_opts.merge(:host => current_instance.dns_name)) end end end + + def self.ssh(opts={}) + system "ssh #{opts[:identity]} #{opts[:ssh_user]}@#{opts[:host]}" + end end end end diff --git a/lib/claws/command/initialize.rb b/lib/claws/command/initialize.rb index 7fce034..3326a36 100644 --- a/lib/claws/command/initialize.rb +++ b/lib/claws/command/initialize.rb @@ -10,6 +10,7 @@ def self.exec }, 'ssh' => { 'user' => nil, + 'identity' => nil, }, 'aws' => { 'access_key_id' => nil, diff --git a/spec/command/ec2_spec.rb b/spec/command/ec2_spec.rb index 6f4d688..4f20b4f 100644 --- a/spec/command/ec2_spec.rb +++ b/spec/command/ec2_spec.rb @@ -102,7 +102,9 @@ OpenStruct.new( { :ssh => OpenStruct.new( - { :user => 'test' } + { :user => 'test', + :identity => 'my_id' + } ), :ec2 => OpenStruct.new( :fields => { @@ -130,7 +132,7 @@ ) subject.should_receive(:puts).twice - subject.should_receive(:system).with('ssh test@secret.com').and_return(0) + subject.should_receive(:system).with('ssh -i my_id test@secret.com').and_return(0) capture_stdout { subject.exec options @@ -152,7 +154,7 @@ ) subject.should_receive(:puts).twice - subject.should_receive(:system).with('ssh test@test.com').and_return(0) + subject.should_receive(:system).with('ssh -i my_id test@test.com').and_return(0) capture_stdout { subject.exec options @@ -175,7 +177,7 @@ ) subject.should_receive(:puts).twice - subject.should_receive(:system).with('ssh test@test2.com').and_return(0) + subject.should_receive(:system).with('ssh -i my_id test@test2.com').and_return(0) capture_stdout { subject.exec OpenStruct.new( {:selection => 1, :config_file => nil, :connect => true} ) @@ -190,7 +192,7 @@ subject.should_receive(:gets).and_return('1\n') subject.should_receive(:puts).once - subject.should_receive(:system).with('ssh test@test2.com').and_return(0) + subject.should_receive(:system).with('ssh -i my_id test@test2.com').and_return(0) capture_stdout { subject.exec options