EC2ssh 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/EC2ssh.gemspec ADDED
@@ -0,0 +1,50 @@
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{EC2ssh}
8
+ s.version = "0.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ramon Salvad\303\263"]
12
+ s.date = %q{2011-09-21}
13
+ s.default_executable = %q{ec2ssh}
14
+ 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).}
15
+ s.email = %q{rsalvado@gnuine.com}
16
+ s.executables = ["ec2ssh"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "EC2ssh.gemspec",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/ec2ssh",
29
+ "lib/ec2ssh.rb",
30
+ "lib/templates/ec2ssh_config_sample.yaml"
31
+ ]
32
+ s.homepage = %q{http://github.com/rsalvado/ec2ssh}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.6.2}
36
+ s.summary = %q{A script to make it easier to ssh into running amazon EC2 instances.}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<highline>, [">= 1.5.2"])
43
+ else
44
+ s.add_dependency(%q<highline>, [">= 1.5.2"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<highline>, [">= 1.5.2"])
48
+ end
49
+ end
50
+
data/LICENSE.txt ADDED
@@ -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.
data/README.rdoc ADDED
@@ -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
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ require 'rake'
6
+
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
10
+ gem.name = "EC2ssh"
11
+ gem.homepage = "http://github.com/rsalvado/ec2ssh"
12
+ gem.license = "MIT"
13
+ gem.summary = %Q{A script to make it easier to ssh into running amazon EC2 instances.}
14
+ 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).}
15
+ gem.email = "rsalvado@gnuine.com"
16
+ gem.authors = ["Ramon Salvadó"]
17
+ gem.executables = ["ec2ssh"]
18
+ gem.add_dependency(%q<highline>, '>= 1.5.2')
19
+ end
20
+ Jeweler::RubygemsDotOrgTasks.new
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ test.rcov_opts << '--exclude "gems/*"'
35
+ end
36
+
37
+ task :default => :test
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "ec2ssh #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.3
data/bin/ec2ssh ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'ec2ssh'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'ec2ssh'
8
+ end
9
+
10
+ # TODO: Add proper command line arguments handling
11
+ account = ARGV[0] || "default"
12
+
13
+ ec2ssh = Ec2ssh::App.new("~/.ec2ssh", account.to_sym)
14
+ ec2ssh.select_instance
data/lib/ec2ssh.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'etc'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/kernel/reporting'
4
+ require 'highline/import'
5
+ silence_warnings { require 'aws' }
6
+ require 'yaml'
7
+ require 'fileutils'
8
+
9
+ module Ec2ssh
10
+
11
+ class UnavailableEC2Credentials < StandardError; end
12
+
13
+ class App
14
+
15
+ def initialize(file = "~/.ec2ssh", account=:default)
16
+ @config = read_aws_config(file, account)
17
+ end
18
+
19
+ def select_instance(instances=[])
20
+ # TODO: Order by region
21
+ # TODO: Ansi colors https://github.com/JEG2/highline/blob/master/examples/ansi_colors.rb
22
+ instances = get_all_ec2_instances
23
+ n = 0
24
+ hostnames = []
25
+ instances.each do |i|
26
+ if i[:aws_state] == "running"
27
+ puts "#{n}. #{i[:aws_instance_id]}: %-20s\t%-60s\t%-10s\t%s" % [ i[:tags]["Name"], i[:aws_groups].join(','), i[:ssh_key_name], i[:dns_name] ]
28
+ hostnames << i[:dns_name]
29
+ n = n + 1
30
+ end
31
+ end
32
+ template = @config[:template] || "ssh #{Etc.getlogin}@<instance>"
33
+ selected_host = ask("Host? ", Integer) { |q| q.in = 0..hostnames.count }
34
+ command = template.gsub("<instance>",hostnames[selected_host])
35
+ exec(command)
36
+ end
37
+
38
+ private
39
+
40
+ def read_aws_config(file, account=:default)
41
+ file = File.expand_path(file)
42
+ accounts = YAML::load(File.open(file))
43
+ selected_account = accounts[account] || accounts.first[1]
44
+ rescue Errno::ENOENT
45
+ puts "ec2ssh config file doesn't exist. Creating a new ~/.ec2ssh. Please review it customize it."
46
+ sample_config_file = File.join(File.dirname(__FILE__), "templates/ec2ssh_config_sample.yaml")
47
+ FileUtils.cp sample_config_file, File.expand_path("~/.ec2ssh")
48
+ exit
49
+ end
50
+
51
+ def get_all_ec2_regions
52
+ %w(eu-west-1 us-east-1 ap-northeast-1 us-west-1 ap-southeast-1)
53
+ end
54
+
55
+ def get_all_ec2_instances
56
+ id = @config[:id]
57
+ key = @config[:key]
58
+ regions ||= @config[:regions] || get_all_ec2_regions
59
+ instances = regions.map do |region|
60
+ silence_stream STDOUT do
61
+ Aws::Ec2.new(id, key, :region => region).describe_instances
62
+ end
63
+ end.flatten
64
+ rescue Aws::AwsError
65
+ puts "AWS Error. Please review your credentials ~/.ec2ssh"
66
+ exit(false)
67
+ end
68
+
69
+ end
70
+
71
+ 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,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: EC2ssh
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 3
10
+ version: 0.1.3
11
+ platform: ruby
12
+ authors:
13
+ - "Ramon Salvad\xC3\xB3"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-21 00:00:00 +02:00
19
+ default_executable: ec2ssh
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: highline
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 1
32
+ - 5
33
+ - 2
34
+ version: 1.5.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: 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).
38
+ email: rsalvado@gnuine.com
39
+ executables:
40
+ - ec2ssh
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE.txt
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - EC2ssh.gemspec
49
+ - LICENSE.txt
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - bin/ec2ssh
54
+ - lib/ec2ssh.rb
55
+ - lib/templates/ec2ssh_config_sample.yaml
56
+ has_rdoc: true
57
+ homepage: http://github.com/rsalvado/ec2ssh
58
+ licenses:
59
+ - MIT
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.6.2
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A script to make it easier to ssh into running amazon EC2 instances.
90
+ test_files: []
91
+