ec2ssh 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ vendor
3
+ .bundle
4
+ tmp
5
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gemspec
3
+
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ schop (1.0.0)
5
+ highline (~> 1.6)
6
+ ktheory-right_aws (~> 2.0.3)
7
+ thor (~> 0.14.6)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ highline (1.6.1)
13
+ ktheory-right_aws (2.0.3)
14
+ right_http_connection (>= 1.2.1)
15
+ right_http_connection (1.2.4)
16
+ thor (0.14.6)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ highline (~> 1.6)
23
+ ktheory-right_aws (~> 2.0.3)
24
+ schop!
25
+ thor (~> 0.14.6)
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ == Introduction
2
+ ec2ssh is a ssh_config manager for amazonaws ec2.
3
+ <tt>ec2ssh</tt> command adds <tt>Host</tt> descriptions to ssh_config (~/.ssh/config default). 'Name' tag of instances are used as <tt>Host</tt> descriptions.
4
+ == How to use
5
+ 1. Set 'Name' tag to your instances
6
+ eg. Tag 'app-server-1' as 'Name' to an instance i-xxxxx in us-west-1 region.
7
+ 2. install ec2ssh
8
+ gem install ec2ssh
9
+ 3. Set environment variables "AMAZON_ACCESS_KEY_ID" and "AMAZON_SECRET_ACCESS_KEY"
10
+ export AMAZON_ACCESS_KEY_ID="..."
11
+ export AMAZON_SECRET_ACCESS_KEY="..."
12
+ 4. execute <tt>ec2ssh</tt> init and <tt>update</tt>
13
+ ec2ssh init
14
+ ec2ssh update
15
+ Then host-names of your instances are generated and wrote to .ssh/config
16
+ 5. And you can ssh to your instances with your tagged name.
17
+ ssh user@app-server-1.us-west-1
18
+ == Commands
19
+ ec2ssh help [TASK] # Describe available tasks or one specific task
20
+ ec2ssh init # Add ec2ssh mark to ssh_config
21
+ ec2ssh update # Update ec2 hosts list in ssh_config
22
+ ec2ssh remove # Remove ec2ssh mark from ssh_config
23
+ Each command can be used <tt>--path</tt> option to set ssh_config path. The default is "~/.ssh/config"
24
+ ec2ssh init --path=/path/to/ssh_config
25
+ == ssh_config and mark lines
26
+ <tt>ec2ssh init</tt> command inserts mark lines your <tt>.ssh/config</tt> such as:
27
+ ### EC2SSH BEGIN ###
28
+ # Generated by ec2ssh http://github.com/mirakui/ec2ssh
29
+ # DO NOT edit this block!
30
+ # Updated Sun Dec 05 00:00:14 +0900 2010
31
+ ### EC2SSH END ###
32
+ <tt>ec2ssh update</tt> command inserts 'Host' descriptions between 'BEGIN' line and 'END' line.
33
+ ### EC2SSH BEGIN ###
34
+ # Generated by ec2ssh http://github.com/mirakui/ec2ssh
35
+ # DO NOT edit this block!
36
+ # Updated Sun Dec 05 00:00:14 +0900 2010
37
+ Host app-server-1.us-west-1
38
+ HostName ec2-xxx-xxx-xxx-xxx.us-west-1.compute.amazonaws.com
39
+ Host db-server-1.ap-southeast-1
40
+ HostName ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com
41
+ :
42
+ :
43
+ ### EC2SSH END ###
44
+ <tt>ec2ssh remove</tt> command removes the mark lines.
45
+ == Notice
46
+ <tt>ec2ssh</tt> command updates your <tt>.ssh/config</tt> file default. You should make a backup of it.
47
+ == License
48
+ ec2ssh is released under the MIT license.
data/bin/ec2ssh ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ec2ssh'
3
+ Ec2ssh::CLI.start
data/ec2ssh.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ec2ssh/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ec2ssh"
7
+ s.version = Ec2ssh::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["mirakui"]
10
+ s.email = ["mirakui@tsuyabu.in"]
11
+ s.homepage = "http://github.com/mirakui/ec2ssh"
12
+ s.summary = %q{A ssh_config manager for AWS EC2}
13
+ s.description = %q{A ssh_config manager for AWS EC2}
14
+
15
+ s.rubyforge_project = "ec2ssh"
16
+ s.add_dependency "thor", "~> 0.14.6"
17
+ s.add_dependency "highline", "~> 1.6"
18
+ s.add_dependency "ktheory-right_aws", "~> 2.0.3"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
data/lib/ec2ssh/cli.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'thor'
2
+ require 'right_aws'
3
+ require 'highline'
4
+
5
+ module Ec2ssh
6
+ class CLI < Thor
7
+ path_option = [:path, {:banner => "/path/to/ssh_config", :default=>"#{ENV['HOME']}/.ssh/config"}]
8
+
9
+ desc "init", "Add ec2ssh mark to ssh_config"
10
+ method_option *path_option
11
+ def init
12
+ config = Config.new(options.path)
13
+ if config.mark_exist?
14
+ red "Marker already exists on #{options.path}"
15
+ return
16
+ end
17
+ config.append_mark!
18
+ green "Added mark to #{options.path}"
19
+ end
20
+
21
+ desc "update", "Update ec2 hosts list in ssh_config"
22
+ method_option *path_option
23
+ def update
24
+ config = Config.new(options.path)
25
+ unless config.mark_exist?
26
+ red "Marker not found on #{options.path}"
27
+ red "Execute '#{$0} init --path=#{options.path}' first!"
28
+ return
29
+ end
30
+ hosts = Hosts.new.all
31
+ config_str = config.wrap(hosts.map{|h|<<-END}.join)
32
+ Host #{h[:host]}
33
+ HostName #{h[:dns_name]}
34
+ END
35
+ config.replace! config_str
36
+ yellow config_str
37
+ green "Updated #{hosts.size} hosts on #{options.path}"
38
+ rescue AwsEnvNotDefined
39
+ red <<-END
40
+ Set environment variables to access AWS such as:
41
+ export AMAZON_ACCESS_KEY_ID="..."
42
+ export AMAZON_SECRET_ACCESS_KEY="..."
43
+ END
44
+ end
45
+
46
+ desc "remove", "Remove ec2ssh mark from ssh_config"
47
+ method_option *path_option
48
+ def remove
49
+ config = Config.new(options.path)
50
+ unless config.mark_exist?
51
+ red "Marker not found on #{options.path}"
52
+ return
53
+ end
54
+ config.replace! ""
55
+ green "Removed mark from #{options.path}"
56
+ end
57
+
58
+ private
59
+ def hl
60
+ @hl ||= HighLine.new
61
+ end
62
+
63
+ [:red,:green,:yellow].each do |col|
64
+ define_method(col) do |str|
65
+ puts hl.color(str, col)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,52 @@
1
+ require 'pathname'
2
+
3
+ module Ec2ssh
4
+ class Config
5
+ HEADER = "### EC2SSH BEGIN ###"
6
+ FOOTER = "### EC2SSH END ###"
7
+
8
+ attr_reader :path
9
+
10
+ def initialize(path=nil)
11
+ @path = Pathname(path || "#{ENV['HOME']}/.ssh/config")
12
+ end
13
+
14
+ def append_mark!
15
+ replace! ""
16
+ open(@path, "a") do |f|
17
+ f.puts wrap("")
18
+ end
19
+ end
20
+
21
+ def mark_exist?
22
+ config_src =~ /#{HEADER}\n.*#{FOOTER}\n/m
23
+ end
24
+
25
+ def replace!(str)
26
+ save! config_src.gsub(/#{HEADER}\n.*#{FOOTER}\n/m, str)
27
+ end
28
+
29
+ def config_src
30
+ @config_src ||= open(@path, "r") do |f|
31
+ f.read
32
+ end
33
+ end
34
+
35
+ def save!(str)
36
+ open(@path, "w") do |f|
37
+ f.puts str
38
+ end
39
+ end
40
+
41
+ def wrap(text)
42
+ return <<-END
43
+ #{HEADER}
44
+ # Generated by ec2ssh http://github.com/mirakui/ec2ssh
45
+ # DO NOT edit this block!
46
+ # Updated #{Time.now}
47
+ #{text}
48
+ #{FOOTER}
49
+ END
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,34 @@
1
+ module Ec2ssh
2
+ class AwsEnvNotDefined < StandardError; end
3
+ class Hosts
4
+ def initialize
5
+ @access_key_id = ENV["AMAZON_ACCESS_KEY_ID"].to_s
6
+ @secret_access_key = ENV["AMAZON_SECRET_ACCESS_KEY"].to_s
7
+ if @access_key_id.length == 0 || @secret_access_key.length == 0
8
+ raise AwsEnvNotDefined
9
+ end
10
+ end
11
+
12
+ def all
13
+ ec2 = RightAws::Ec2.new(@access_key_id, @secret_access_key)
14
+ regions = ec2.describe_regions
15
+ regions.map do |region|
16
+ process_region region
17
+ end.flatten
18
+ end
19
+
20
+ private
21
+ def process_region(region)
22
+ ec2 = RightAws::Ec2.new(@access_key_id, @secret_access_key, :region => region)
23
+ instance_names = {}
24
+ ec2.describe_tags.each do |tag|
25
+ next unless tag[:key]=='Name' && tag[:resource_type]=='instance'
26
+ instance_names[tag[:resource_id]] = tag[:value]
27
+ end
28
+ ec2.describe_instances.map do |instance|
29
+ instance_name = instance_names[instance[:aws_instance_id]]
30
+ instance_name ? {:host => "#{instance_name}.#{region}", :dns_name => instance[:dns_name]} : nil
31
+ end.compact
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Ec2ssh
2
+ VERSION = '1.0.1'
3
+ end
data/lib/ec2ssh.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Ec2ssh
2
+ autoload :CLI, "ec2ssh/cli"
3
+ autoload :Config, "ec2ssh/config"
4
+ autoload :Hosts, "ec2ssh/hosts"
5
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2ssh
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - mirakui
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-05 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thor
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 43
30
+ segments:
31
+ - 0
32
+ - 14
33
+ - 6
34
+ version: 0.14.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: highline
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 1
48
+ - 6
49
+ version: "1.6"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: ktheory-right_aws
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 9
61
+ segments:
62
+ - 2
63
+ - 0
64
+ - 3
65
+ version: 2.0.3
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ description: A ssh_config manager for AWS EC2
69
+ email:
70
+ - mirakui@tsuyabu.in
71
+ executables:
72
+ - ec2ssh
73
+ extensions: []
74
+
75
+ extra_rdoc_files: []
76
+
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.rdoc
82
+ - bin/ec2ssh
83
+ - ec2ssh.gemspec
84
+ - lib/ec2ssh.rb
85
+ - lib/ec2ssh/cli.rb
86
+ - lib/ec2ssh/config.rb
87
+ - lib/ec2ssh/hosts.rb
88
+ - lib/ec2ssh/version.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/mirakui/ec2ssh
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project: ec2ssh
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A ssh_config manager for AWS EC2
123
+ test_files: []
124
+