bscott-ec2ssh 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :runtime do
4
+ gem 'aws', '>= 2.5.6'
5
+ gem 'activesupport', '~> 3.0.0'
6
+ gem 'colorize', '0.5.8'
7
+ gem 'highline', '>= 1.6.8'
8
+ gem 'text-table', '1.2.2'
9
+ end
10
+
11
+ group :test do
12
+ gem 'jeweler'
13
+ gem 'rake'
14
+ end
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.11)
5
+ aws (2.5.6)
6
+ http_connection
7
+ uuidtools
8
+ xml-simple
9
+ colorize (0.5.8)
10
+ git (1.2.5)
11
+ highline (1.6.8)
12
+ http_connection (1.4.1)
13
+ jeweler (1.6.4)
14
+ bundler (~> 1.0)
15
+ git (>= 1.2.5)
16
+ rake
17
+ rake (0.9.2.2)
18
+ text-table (1.2.2)
19
+ uuidtools (2.1.2)
20
+ xml-simple (1.1.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ activesupport (~> 3.0.0)
27
+ aws (>= 2.5.6)
28
+ colorize (= 0.5.8)
29
+ highline (>= 1.6.8)
30
+ jeweler
31
+ rake
32
+ text-table (= 1.2.2)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ramon Salvadó
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ = EC2ssh
2
+
3
+ A script to make it easier to ssh into running amazon EC2 instances.
4
+
5
+ Since ec2 instance public hostnames are dynamic, and not easy to remember or type, this script provides a list all your running instances so you can select the one you want to ssh into easily (without having to pass the aws console ritual each time you need the hostname).
6
+
7
+ You can configure several accounts, customize the action to run on the selected instance and restrict the list to only the regions you want.
8
+
9
+ You can customize *EC2ssh* behaviour using the <tt>~/.ec2ssh</tt> yaml config file. A sample file will be created on first run if it doesn't already exist.
10
+
11
+ Invocation examples:
12
+
13
+ # Using the default account
14
+ ec2ssh
15
+ # To use the second account
16
+ ec2ssh second
17
+
18
+ Example menu:
19
+
20
+ 0. i-5e523428: gandalf webserver sysadmins ec2-79-165-69-177.eu-west-1.compute.amazonaws.com
21
+ 1. i-5e653429: frodo database sysadmins ec2-45-134-69-185.eu-west-1.compute.amazonaws.com
22
+
23
+ Columns in order: <tt>instance id</tt>, <tt>Name Tag</tt>, <tt>security groups</tt>, <tt>ssh key name</tt>, <tt>dns_name</tt>.
24
+
25
+ == Contributing to ec2ssh
26
+
27
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
28
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
29
+ * Fork the project
30
+ * Start a feature/bugfix branch
31
+ * Commit and push until you are happy with your contribution
32
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
33
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2011 Ramon Salvadó. See LICENSE.txt for
38
+ further details.
39
+
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'jeweler'
6
+
7
+ Jeweler::Tasks.new do |gem|
8
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
9
+ gem.name = "anveo-ec2ssh"
10
+ gem.homepage = "http://github.com/anveo/ec2ssh"
11
+ gem.license = "MIT"
12
+ gem.summary = %Q{A script to make it easier to ssh into running amazon EC2 instances.}
13
+ gem.description = %Q{Since ec2 instance public hostnames are dynamic, and not easy to remember or type, this script provides a list all your running instances so you can select the one you want to ssh into easily (without having to pass the aws console ritual each time you need the hostname).}
14
+ gem.email = "bracer@gmail.com"
15
+ gem.authors = ["Ramon Salvadó", "Brian Racer"]
16
+ gem.executables = ["ec2ssh"]
17
+ gem.add_dependency('aws', '>= 2.5.6')
18
+ gem.add_dependency('activesupport', '~> 3.0.0')
19
+ gem.add_dependency('colorize', '0.5.8')
20
+ gem.add_dependency('highline', '>= 1.6.8')
21
+ gem.add_dependency('text-table', '~> 1.2.2')
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+
5
+ begin
6
+ require 'ec2ssh'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'ec2ssh'
10
+ end
11
+
12
+ # Unset EC2_URL it can cause problems with aws gem
13
+ ENV["EC2_URL"] = nil
14
+
15
+ # TODO: Add proper command line arguments handling
16
+ account = ARGV[0] || "default"
17
+
18
+ ec2ssh = Ec2ssh::App.new("~/.ec2ssh", account.to_sym)
19
+ ec2ssh.select_instance
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bscott-ec2ssh}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Ramon Salvadó}, %q{Brian Scott}]
12
+ s.date = %q{2012-01-31}
13
+ s.description = %q{Since ec2 instance public hostnames are dynamic, and not easy to remember or type, this script provides a list all your running instances so you can select the one you want to ssh into easily (without having to pass the aws console ritual each time you need the hostname).}
14
+ s.email = %q{brainscott@gmail.com}
15
+ s.executables = [%q{ec2ssh}]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bscott-ec2ssh.gemspec",
29
+ "bin/ec2ssh",
30
+ "lib/ec2ssh.rb",
31
+ "lib/templates/ec2ssh_config_sample.yaml"
32
+ ]
33
+ s.homepage = %q{http://github.com/bscott/bscott-ec2ssh}
34
+ s.licenses = [%q{MIT}]
35
+ s.require_paths = [%q{lib}]
36
+ s.rubygems_version = %q{1.8.6}
37
+ s.summary = %q{A script to make it easier to ssh into running amazon EC2 instances.}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<aws>, [">= 2.5.6"])
44
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
45
+ s.add_runtime_dependency(%q<colorize>, ["= 0.5.8"])
46
+ s.add_runtime_dependency(%q<highline>, [">= 1.6.8"])
47
+ s.add_runtime_dependency(%q<text-table>, ["= 1.2.2"])
48
+ s.add_runtime_dependency(%q<aws>, [">= 2.5.6"])
49
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
50
+ s.add_runtime_dependency(%q<colorize>, ["= 0.5.8"])
51
+ s.add_runtime_dependency(%q<highline>, [">= 1.6.8"])
52
+ s.add_runtime_dependency(%q<text-table>, ["~> 1.2.2"])
53
+ else
54
+ s.add_dependency(%q<aws>, [">= 2.5.6"])
55
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
56
+ s.add_dependency(%q<colorize>, ["= 0.5.8"])
57
+ s.add_dependency(%q<highline>, [">= 1.6.8"])
58
+ s.add_dependency(%q<text-table>, ["= 1.2.2"])
59
+ s.add_dependency(%q<aws>, [">= 2.5.6"])
60
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
61
+ s.add_dependency(%q<colorize>, ["= 0.5.8"])
62
+ s.add_dependency(%q<highline>, [">= 1.6.8"])
63
+ s.add_dependency(%q<text-table>, ["~> 1.2.2"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<aws>, [">= 2.5.6"])
67
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
68
+ s.add_dependency(%q<colorize>, ["= 0.5.8"])
69
+ s.add_dependency(%q<highline>, [">= 1.6.8"])
70
+ s.add_dependency(%q<text-table>, ["= 1.2.2"])
71
+ s.add_dependency(%q<aws>, [">= 2.5.6"])
72
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
73
+ s.add_dependency(%q<colorize>, ["= 0.5.8"])
74
+ s.add_dependency(%q<highline>, [">= 1.6.8"])
75
+ s.add_dependency(%q<text-table>, ["~> 1.2.2"])
76
+ end
77
+ end
78
+
@@ -0,0 +1,141 @@
1
+ require 'etc'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/kernel/reporting'
4
+ require 'colorize'
5
+ require 'highline/import'
6
+ require 'text-table'
7
+ silence_warnings { require 'aws' }
8
+ require 'yaml'
9
+ require 'fileutils'
10
+
11
+ module Ec2ssh
12
+
13
+ class UnavailableEC2Credentials < StandardError; end
14
+
15
+ class App
16
+
17
+ def initialize(file = "~/.ec2ssh", account=:default)
18
+ @config = read_aws_config(file, account)
19
+ end
20
+
21
+ def select_instance(instances=[])
22
+ # TODO: Ansi colors https://github.com/JEG2/highline/blob/master/examples/ansi_colors.rb
23
+ instances = get_all_ec2_instances
24
+ table_rows = []
25
+
26
+ instances.each do |i|
27
+ if i[:aws_state] == "running"
28
+ table_rows << ['', i[:tags]["Name"].blank? ? '' : i[:tags]["Name"], i[:aws_instance_id], i[:aws_groups].join(','), i[:ssh_key_name], i[:aws_private_ip_address], i[:dns_name], i[:architecture], i[:aws_instance_type]]
29
+ end
30
+ end
31
+
32
+ # sort tables by tag name
33
+ table_rows.sort! do |a, b|
34
+ a[1] <=> b[1]
35
+ end
36
+
37
+ # give them numbers
38
+ table_rows.count.times do |i|
39
+ table_rows[i][0] = i + 1
40
+ end
41
+
42
+ table_header = ['', 'Name', 'Instance ID', 'SecGroup', 'Key', 'Internal IP', 'Public DNS', 'Arch', 'Type']
43
+
44
+ table = Text::Table.new :rows => table_rows, :head => table_header
45
+
46
+ # output table
47
+ puts table
48
+
49
+ input = ask(">> ")
50
+ options = input.split
51
+
52
+ # check if last arg is 'public' or 'private'. If so remove it from the options stack and save the value
53
+ pub_priv_override = ''
54
+ if options[-1] =~ /public|private/
55
+ pub_priv_override = options.pop
56
+ end
57
+
58
+ input_host = options[0]
59
+ input_user = options[1]
60
+ input_key = options[2]
61
+
62
+ if input_host =~ /^\d+$/
63
+ host = table_rows[input_host.to_i - 1]
64
+ else
65
+ host = table_rows.find { |h| h[1] == input_host }
66
+ end
67
+
68
+ host_ssh_key = host[4]
69
+ host_private_ip = host[5]
70
+ host_public_dns = host[6]
71
+
72
+ default_user = @config[:default_user] || Etc.getlogin
73
+
74
+ template = @config[:template] || "ssh #{default_user}@<public_dns>"
75
+
76
+ # if we have a public or private ip override, replace the template variable
77
+ unless pub_priv_override.blank?
78
+ case pub_priv_override
79
+ when 'public'
80
+ template.gsub!("<private_ip>", "<public_dns>")
81
+ when 'private'
82
+ template.gsub!("<public_dns>", "<private_ip>")
83
+ end
84
+ end
85
+
86
+ # <instance> remains for compatibility with upstream
87
+ command = template.gsub("<instance>", host_public_dns).
88
+ gsub("<public_dns>", host_public_dns).
89
+ gsub("<private_ip>", host_private_ip)
90
+
91
+ # interpolate ssh user
92
+ if input_user.blank?
93
+ command.gsub!("<user>", default_user)
94
+ else
95
+ command.gsub!("<user>", input_user)
96
+ end
97
+
98
+ # interpolate ssh key
99
+ if input_key.blank?
100
+ command.gsub!("<key>", "")
101
+ else
102
+ command.gsub!("<key>", "-i ~/.ssh/#{input_key}.pem")
103
+ end
104
+
105
+ puts "!!! #{command}"
106
+ exec(command)
107
+ end
108
+
109
+ private
110
+
111
+ def read_aws_config(file, account=:default)
112
+ file = File.expand_path(file)
113
+ accounts = YAML::load(File.open(file))
114
+ selected_account = accounts[account] || accounts.first[1]
115
+ rescue Errno::ENOENT
116
+ puts "ec2ssh config file doesn't exist. Creating a new ~/.ec2ssh. Please review it customize it."
117
+ sample_config_file = File.join(File.dirname(__FILE__), "templates/ec2ssh_config_sample.yaml")
118
+ FileUtils.cp sample_config_file, File.expand_path("~/.ec2ssh")
119
+ exit
120
+ end
121
+
122
+ def get_all_ec2_regions
123
+ %w(eu-west-1 us-east-1 ap-northeast-1 us-west-1 ap-southeast-1)
124
+ end
125
+
126
+ def get_all_ec2_instances
127
+ id = @config[:id]
128
+ key = @config[:key]
129
+ regions ||= @config[:regions] || get_all_ec2_regions
130
+ instances = regions.map do |region|
131
+ silence_stream STDOUT do
132
+ Aws::Ec2.new(id, key, :region => region).describe_instances
133
+ end
134
+ end.flatten
135
+ rescue Aws::AwsError => e
136
+ abort "AWS Error. #{e.message}"
137
+ end
138
+
139
+ end
140
+
141
+ end
@@ -0,0 +1,18 @@
1
+ # YAML file with your account credentials and configuration for your accounts.
2
+ ---
3
+ # Default account to use
4
+ :default:
5
+ :id: yourid
6
+ :key: yourkey
7
+ # Regions to check for running instances
8
+ :regions:
9
+ - us-east-1
10
+ # Another account, you can ec2ssh second
11
+ :second:
12
+ :id: yourid
13
+ :key: yourkey
14
+ :regions:
15
+ - eu-west-1
16
+ - us-east-1
17
+ # Command to run on selected instance. <instance> will be replaced by the selected instance hostname
18
+ :template: "ssh ec2-user@<instance> -i your-key.pem"
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bscott-ec2ssh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ramon Salvadó
9
+ - Brian Scott
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-31 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: aws
17
+ requirement: &70244891732760 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.5.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70244891732760
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &70244891731580 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70244891731580
37
+ - !ruby/object:Gem::Dependency
38
+ name: colorize
39
+ requirement: &70244891731040 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - =
43
+ - !ruby/object:Gem::Version
44
+ version: 0.5.8
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70244891731040
48
+ - !ruby/object:Gem::Dependency
49
+ name: highline
50
+ requirement: &70244891730560 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.8
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70244891730560
59
+ - !ruby/object:Gem::Dependency
60
+ name: text-table
61
+ requirement: &70244891729800 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - =
65
+ - !ruby/object:Gem::Version
66
+ version: 1.2.2
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *70244891729800
70
+ - !ruby/object:Gem::Dependency
71
+ name: aws
72
+ requirement: &70244891729040 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.5.6
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *70244891729040
81
+ - !ruby/object:Gem::Dependency
82
+ name: activesupport
83
+ requirement: &70244891728260 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 3.0.0
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: *70244891728260
92
+ - !ruby/object:Gem::Dependency
93
+ name: colorize
94
+ requirement: &70244891721860 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - =
98
+ - !ruby/object:Gem::Version
99
+ version: 0.5.8
100
+ type: :runtime
101
+ prerelease: false
102
+ version_requirements: *70244891721860
103
+ - !ruby/object:Gem::Dependency
104
+ name: highline
105
+ requirement: &70244891721300 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.6.8
111
+ type: :runtime
112
+ prerelease: false
113
+ version_requirements: *70244891721300
114
+ - !ruby/object:Gem::Dependency
115
+ name: text-table
116
+ requirement: &70244891720500 !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ~>
120
+ - !ruby/object:Gem::Version
121
+ version: 1.2.2
122
+ type: :runtime
123
+ prerelease: false
124
+ version_requirements: *70244891720500
125
+ description: Since ec2 instance public hostnames are dynamic, and not easy to remember
126
+ or type, this script provides a list all your running instances so you can select
127
+ the one you want to ssh into easily (without having to pass the aws console ritual
128
+ each time you need the hostname).
129
+ email: brainscott@gmail.com
130
+ executables:
131
+ - ec2ssh
132
+ extensions: []
133
+ extra_rdoc_files:
134
+ - LICENSE.txt
135
+ - README.rdoc
136
+ files:
137
+ - .document
138
+ - Gemfile
139
+ - Gemfile.lock
140
+ - LICENSE.txt
141
+ - README.rdoc
142
+ - Rakefile
143
+ - VERSION
144
+ - bscott-ec2ssh.gemspec
145
+ - bin/ec2ssh
146
+ - lib/ec2ssh.rb
147
+ - lib/templates/ec2ssh_config_sample.yaml
148
+ homepage: http://github.com/bscott/bscott-ec2ssh
149
+ licenses:
150
+ - MIT
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 1.8.10
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: A script to make it easier to ssh into running amazon EC2 instances.
173
+ test_files: []