ec2ssh 1.0.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +7 -0
- data/ChangeLog.md +21 -0
- data/Gemfile +6 -1
- data/Gemfile.lock +51 -10
- data/Guardfile +9 -0
- data/MIT-LICENSE +20 -0
- data/README.md +93 -0
- data/bin/ec2ssh +2 -1
- data/ec2ssh.gemspec +4 -4
- data/lib/ec2ssh/cli.rb +39 -30
- data/lib/ec2ssh/dotfile.rb +49 -0
- data/lib/ec2ssh/hosts.rb +28 -21
- data/lib/ec2ssh/{config.rb → ssh_config.rb} +2 -2
- data/lib/ec2ssh/version.rb +1 -1
- data/lib/ec2ssh.rb +0 -3
- data/spec/lib/ec2ssh/cli_spec.rb +129 -0
- data/spec/spec_helper.rb +37 -0
- metadata +28 -19
- data/README.rdoc +0 -48
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3
|
data/.travis.yml
ADDED
data/ChangeLog.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Change Log
|
2
|
+
## 2.0.0
|
3
|
+
|
4
|
+
* Add dotfile (.ec2ssh); supports multiple aws keys with `$ ec2ssh update --aws-key keyname` option.
|
5
|
+
Thanks to @kentaro #3
|
6
|
+
|
7
|
+
* Replace a gem `ktheory-right_aws` with `aws-sdk`.
|
8
|
+
|
9
|
+
* Write tests.
|
10
|
+
|
11
|
+
## 1.0.3
|
12
|
+
|
13
|
+
* Surpress thor warnings. Thanks to @mururu #1, @sanemat #2
|
14
|
+
|
15
|
+
## 1.0.2
|
16
|
+
|
17
|
+
* Fix bug: Blank HostName is created if there're some "stopped" instances.
|
18
|
+
|
19
|
+
## 1.0.1
|
20
|
+
|
21
|
+
* First Release.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,25 +1,66 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ec2ssh (
|
4
|
+
ec2ssh (2.0.0)
|
5
|
+
aws-sdk (~> 1.8)
|
5
6
|
highline (~> 1.6)
|
6
|
-
ktheory-right_aws (~> 2.0.3)
|
7
7
|
thor (~> 0.14.6)
|
8
8
|
|
9
9
|
GEM
|
10
|
-
remote:
|
10
|
+
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
addressable (2.3.2)
|
13
|
+
aws-sdk (1.8.1.3)
|
14
|
+
json (~> 1.4)
|
15
|
+
nokogiri (>= 1.4.4)
|
16
|
+
uuidtools (~> 2.1)
|
17
|
+
coderay (1.0.8)
|
18
|
+
crack (0.3.2)
|
19
|
+
diff-lcs (1.1.3)
|
20
|
+
guard (1.6.2)
|
21
|
+
listen (>= 0.6.0)
|
22
|
+
lumberjack (>= 1.0.2)
|
23
|
+
pry (>= 0.9.10)
|
24
|
+
terminal-table (>= 1.4.3)
|
25
|
+
thor (>= 0.14.6)
|
26
|
+
guard-rspec (2.4.0)
|
27
|
+
guard (>= 1.1)
|
28
|
+
rspec (~> 2.11)
|
29
|
+
highline (1.6.15)
|
30
|
+
json (1.7.7)
|
31
|
+
listen (0.7.2)
|
32
|
+
lumberjack (1.0.2)
|
33
|
+
method_source (0.8.1)
|
34
|
+
nokogiri (1.5.6)
|
35
|
+
pry (0.9.12)
|
36
|
+
coderay (~> 1.0.5)
|
37
|
+
method_source (~> 0.8)
|
38
|
+
slop (~> 3.4)
|
39
|
+
rb-fsevent (0.9.2)
|
40
|
+
rspec (2.12.0)
|
41
|
+
rspec-core (~> 2.12.0)
|
42
|
+
rspec-expectations (~> 2.12.0)
|
43
|
+
rspec-mocks (~> 2.12.0)
|
44
|
+
rspec-core (2.12.2)
|
45
|
+
rspec-expectations (2.12.1)
|
46
|
+
diff-lcs (~> 1.1.3)
|
47
|
+
rspec-mocks (2.12.2)
|
48
|
+
slop (3.4.3)
|
49
|
+
terminal-table (1.4.5)
|
16
50
|
thor (0.14.6)
|
51
|
+
timecop (0.5.9.1)
|
52
|
+
uuidtools (2.1.3)
|
53
|
+
webmock (1.9.0)
|
54
|
+
addressable (>= 2.2.7)
|
55
|
+
crack (>= 0.1.7)
|
17
56
|
|
18
57
|
PLATFORMS
|
19
58
|
ruby
|
20
59
|
|
21
60
|
DEPENDENCIES
|
22
61
|
ec2ssh!
|
23
|
-
|
24
|
-
|
25
|
-
|
62
|
+
guard-rspec (~> 2.4)
|
63
|
+
rb-fsevent (~> 0.9.1)
|
64
|
+
rspec (~> 2.12)
|
65
|
+
timecop (~> 0.5)
|
66
|
+
webmock (~> 1.9)
|
data/Guardfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Issei Naruta
|
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.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Introduction
|
2
|
+
ec2ssh is a ssh_config manager for amazonaws ec2.
|
3
|
+
`ec2ssh` command adds `Host` descriptions to ssh_config (~/.ssh/config default). 'Name' tag of instances are used as `Host` descriptions.
|
4
|
+
|
5
|
+
# How to use
|
6
|
+
### 1. Set 'Name' tag to your instances
|
7
|
+
eg. Tag 'app-server-1' as 'Name' to an instance i-xxxxx in us-west-1 region.
|
8
|
+
|
9
|
+
### 2. Install ec2ssh
|
10
|
+
|
11
|
+
```
|
12
|
+
$ gem install ec2ssh
|
13
|
+
```
|
14
|
+
|
15
|
+
### 3. Execute `ec2ssh init`
|
16
|
+
|
17
|
+
```
|
18
|
+
$ ec2ssh init
|
19
|
+
```
|
20
|
+
|
21
|
+
### 4. Edit `.ec2ssh`
|
22
|
+
|
23
|
+
```
|
24
|
+
$ vi ~/.ec2ssh
|
25
|
+
---
|
26
|
+
path: /path/to/ssh_config
|
27
|
+
access_key_id: YOUR_ACCESS_KEY_ID
|
28
|
+
secret_access_key: YOUR_SECRET_ACCESS_KEY
|
29
|
+
regions:
|
30
|
+
- ap-northeast-1
|
31
|
+
```
|
32
|
+
|
33
|
+
### 5. Execute `ec2ssh update`
|
34
|
+
|
35
|
+
```
|
36
|
+
$ ec2ssh update
|
37
|
+
```
|
38
|
+
Then host-names of your instances are generated and wrote to .ssh/config
|
39
|
+
|
40
|
+
### 6. And you can ssh to your instances with your tagged name.
|
41
|
+
|
42
|
+
```
|
43
|
+
$ ssh app-server-1.us-west-1
|
44
|
+
```
|
45
|
+
|
46
|
+
# Commands
|
47
|
+
```
|
48
|
+
$ ec2ssh help [TASK] # Describe available tasks or one specific task
|
49
|
+
$ ec2ssh init # Add ec2ssh mark to ssh_config
|
50
|
+
$ ec2ssh update # Update ec2 hosts list in ssh_config
|
51
|
+
$ ec2ssh remove # Remove ec2ssh mark from ssh_config
|
52
|
+
```
|
53
|
+
|
54
|
+
Each command can use `--path` option to set ssh_config path. The default is "~/.ssh/config"
|
55
|
+
|
56
|
+
```
|
57
|
+
$ ec2ssh init --path=/path/to/ssh_config
|
58
|
+
```
|
59
|
+
|
60
|
+
# ssh_config and mark lines
|
61
|
+
`ec2ssh init` command inserts mark lines your `.ssh/config` such as:
|
62
|
+
|
63
|
+
```
|
64
|
+
### EC2SSH BEGIN ###
|
65
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
66
|
+
# DO NOT edit this block!
|
67
|
+
# Updated Sun Dec 05 00:00:14 +0900 2010
|
68
|
+
### EC2SSH END ###
|
69
|
+
```
|
70
|
+
|
71
|
+
`ec2ssh update` command inserts 'Host' descriptions between 'BEGIN' line and 'END' line.
|
72
|
+
|
73
|
+
```
|
74
|
+
### EC2SSH BEGIN ###
|
75
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
76
|
+
# DO NOT edit this block!
|
77
|
+
# Updated Sun Dec 05 00:00:14 +0900 2010
|
78
|
+
Host app-server-1.us-west-1
|
79
|
+
HostName ec2-xxx-xxx-xxx-xxx.us-west-1.compute.amazonaws.com
|
80
|
+
Host db-server-1.ap-southeast-1
|
81
|
+
HostName ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com
|
82
|
+
:
|
83
|
+
:
|
84
|
+
### EC2SSH END ###
|
85
|
+
```
|
86
|
+
|
87
|
+
`ec2ssh remove` command removes the mark lines.
|
88
|
+
|
89
|
+
# Notice
|
90
|
+
`ec2ssh` command updates your `.ssh/config` file default. You should make a backup of it.
|
91
|
+
|
92
|
+
# License
|
93
|
+
ec2ssh is released under the MIT license.
|
data/bin/ec2ssh
CHANGED
data/ec2ssh.gemspec
CHANGED
@@ -6,16 +6,16 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = "ec2ssh"
|
7
7
|
s.version = Ec2ssh::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["
|
10
|
-
s.email = ["
|
9
|
+
s.authors = ["Issei Naruta"]
|
10
|
+
s.email = ["mimitako@gmail.com"]
|
11
11
|
s.homepage = "http://github.com/mirakui/ec2ssh"
|
12
12
|
s.summary = %q{A ssh_config manager for AWS EC2}
|
13
|
-
s.description = %q{
|
13
|
+
s.description = %q{ec2ssh is a ssh_config manager for AWS EC2}
|
14
14
|
|
15
15
|
s.rubyforge_project = "ec2ssh"
|
16
16
|
s.add_dependency "thor", "~> 0.14.6"
|
17
17
|
s.add_dependency "highline", "~> 1.6"
|
18
|
-
s.add_dependency
|
18
|
+
s.add_dependency 'aws-sdk', '~> 1.8'
|
19
19
|
|
20
20
|
s.files = `git ls-files`.split("\n")
|
21
21
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/ec2ssh/cli.rb
CHANGED
@@ -1,67 +1,76 @@
|
|
1
1
|
require 'thor'
|
2
|
-
require 'right_aws'
|
3
2
|
require 'highline'
|
3
|
+
require 'ec2ssh/hosts'
|
4
|
+
require 'ec2ssh/ssh_config'
|
5
|
+
require 'ec2ssh/dotfile'
|
4
6
|
|
5
7
|
module Ec2ssh
|
6
8
|
class CLI < Thor
|
7
|
-
|
9
|
+
class_option :path, :banner => "/path/to/ssh_config"
|
10
|
+
class_option :dotfile, :banner => '$HOME/.ec2ssh', :default => "#{ENV['HOME']}/.ec2ssh"
|
8
11
|
|
9
12
|
desc "init", "Add ec2ssh mark to ssh_config"
|
10
|
-
method_option *path_option
|
11
13
|
def init
|
12
|
-
config =
|
14
|
+
config = SshConfig.new(config_path)
|
13
15
|
if config.mark_exist?
|
14
|
-
red "Marker already exists on #{
|
15
|
-
|
16
|
+
red "Marker already exists on #{config_path}"
|
17
|
+
else
|
18
|
+
config.append_mark!
|
19
|
+
green "Added mark to #{config_path}"
|
16
20
|
end
|
17
|
-
|
18
|
-
|
21
|
+
dotfile = Dotfile.update_or_create(options.dotfile, 'path' => options.path)
|
22
|
+
yellow "Please check and edit #{options.dotfile} before run `ec2ssh update`"
|
19
23
|
end
|
20
24
|
|
21
25
|
desc "update", "Update ec2 hosts list in ssh_config"
|
22
|
-
method_option
|
26
|
+
method_option :aws_key, :banner => 'aws key name', :default => 'default'
|
23
27
|
def update
|
24
|
-
config =
|
28
|
+
config = SshConfig.new(config_path)
|
25
29
|
unless config.mark_exist?
|
26
|
-
red "Marker not found on #{
|
27
|
-
red "Execute '#{$0} init --path
|
30
|
+
red "Marker not found on #{config_path}"
|
31
|
+
red "Execute '#{$0} init --path=/path/to/ssh_config' first!"
|
28
32
|
return
|
29
33
|
end
|
30
|
-
hosts = Hosts.new.all
|
31
34
|
config_str = config.wrap(hosts.map{|h|<<-END}.join)
|
32
35
|
Host #{h[:host]}
|
33
36
|
HostName #{h[:dns_name]}
|
34
37
|
END
|
35
38
|
config.replace! config_str
|
36
39
|
yellow config_str
|
37
|
-
green "Updated #{hosts.size} hosts on #{
|
38
|
-
rescue AwsEnvNotDefined
|
39
|
-
red
|
40
|
-
Set environment variables to access AWS such as:
|
41
|
-
export AMAZON_ACCESS_KEY_ID="..."
|
42
|
-
export AMAZON_SECRET_ACCESS_KEY="..."
|
43
|
-
END
|
40
|
+
green "Updated #{hosts.size} hosts on #{config_path}"
|
41
|
+
rescue AwsEnvNotDefined, AwsKeyNotFound
|
42
|
+
red "Set aws keys at #{options.dotfile}"
|
44
43
|
end
|
45
44
|
|
46
45
|
desc "remove", "Remove ec2ssh mark from ssh_config"
|
47
|
-
method_option *path_option
|
48
46
|
def remove
|
49
|
-
config =
|
47
|
+
config = SshConfig.new(config_path)
|
50
48
|
unless config.mark_exist?
|
51
|
-
red "Marker not found on #{
|
49
|
+
red "Marker not found on #{config_path}"
|
52
50
|
return
|
53
51
|
end
|
54
52
|
config.replace! ""
|
55
|
-
green "Removed mark from #{
|
53
|
+
green "Removed mark from #{config_path}"
|
56
54
|
end
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
no_tasks do
|
57
|
+
def hl
|
58
|
+
@hl ||= HighLine.new
|
59
|
+
end
|
60
|
+
|
61
|
+
def hosts
|
62
|
+
@hosts ||= Hosts.new(dotfile, options.aws_key).all
|
63
|
+
end
|
64
|
+
|
65
|
+
def dotfile
|
66
|
+
@dotfile ||= Dotfile.load(options.dotfile)
|
67
|
+
end
|
68
|
+
|
69
|
+
def config_path
|
70
|
+
options.path || dotfile['path'] || "#{$ENV['HOME']}/.ssh/config"
|
71
|
+
end
|
62
72
|
|
63
|
-
|
64
|
-
no_tasks do
|
73
|
+
[:red,:green,:yellow].each do |col|
|
65
74
|
define_method(col) do |str|
|
66
75
|
puts hl.color(str, col)
|
67
76
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Ec2ssh
|
4
|
+
class AwsKeyNotFound < StandardError; end
|
5
|
+
class Dotfile
|
6
|
+
def initialize(config={})
|
7
|
+
@config = {
|
8
|
+
'path' => "~/.ssh/config",
|
9
|
+
'aws_keys' => {
|
10
|
+
'default' => {
|
11
|
+
'access_key_id' => ENV['AMAZON_ACCESS_KEY_ID'],
|
12
|
+
'secret_access_key' => ENV['AMAZON_SECRET_ACCESS_KEY']
|
13
|
+
}
|
14
|
+
},
|
15
|
+
'regions' => %w(ap-northeast-1),
|
16
|
+
}.merge(config)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.load(path)
|
20
|
+
new YAML.load(open(path).read)
|
21
|
+
end
|
22
|
+
|
23
|
+
def save(path)
|
24
|
+
open(path, 'w') {|f| f.write @config.to_yaml }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.update_or_create(path, config={})
|
28
|
+
dotfile = if File.exist?(path)
|
29
|
+
Dotfile.load(path)
|
30
|
+
else
|
31
|
+
new
|
32
|
+
end
|
33
|
+
dotfile.update(config)
|
34
|
+
dotfile.save(path)
|
35
|
+
end
|
36
|
+
|
37
|
+
def [](key)
|
38
|
+
@config[key]
|
39
|
+
end
|
40
|
+
|
41
|
+
def aws_key(keyname)
|
42
|
+
self['aws_keys'][keyname] or raise AwsKeyNotFound
|
43
|
+
end
|
44
|
+
|
45
|
+
def update(config)
|
46
|
+
@config = @config.merge config
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/ec2ssh/hosts.rb
CHANGED
@@ -1,35 +1,42 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'ec2ssh/dotfile'
|
3
|
+
|
1
4
|
module Ec2ssh
|
2
5
|
class AwsEnvNotDefined < StandardError; end
|
3
6
|
class Hosts
|
4
|
-
def initialize
|
5
|
-
@
|
6
|
-
@
|
7
|
-
|
8
|
-
raise AwsEnvNotDefined
|
7
|
+
def initialize(dotfile, keyname)
|
8
|
+
@dotfile = dotfile
|
9
|
+
@ec2 = Hash.new do |h,region|
|
10
|
+
key = dotfile.aws_key(keyname)
|
11
|
+
raise AwsEnvNotDefined if key['access_key_id'].empty? || key['secret_access_key'].empty?
|
12
|
+
h[region] = AWS::EC2.new(
|
13
|
+
:ec2_endpoint => "#{region}.ec2.amazonaws.com",
|
14
|
+
:access_key_id => key['access_key_id'],
|
15
|
+
:secret_access_key => key['secret_access_key']
|
16
|
+
)
|
9
17
|
end
|
10
18
|
end
|
11
19
|
|
12
20
|
def all
|
13
|
-
|
14
|
-
regions = ec2.describe_regions
|
15
|
-
regions.map do |region|
|
21
|
+
@dotfile['regions'].map {|region|
|
16
22
|
process_region region
|
17
|
-
|
23
|
+
}.flatten
|
18
24
|
end
|
19
25
|
|
20
26
|
private
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
def process_region(region)
|
28
|
+
instances(region).map {|instance|
|
29
|
+
name_tag = instance[:tag_set].find {|tag| tag[:key] == 'Name' }
|
30
|
+
next nil if name_tag.nil? || name_tag[:value].empty?
|
31
|
+
name = name_tag[:value]
|
32
|
+
dns_name = instance[:dns_name] or next nil
|
33
|
+
{:host => "#{name}.#{region}", :dns_name => dns_name}
|
34
|
+
}.compact.sort {|a,b| a[:host] <=> b[:host] }
|
35
|
+
end
|
36
|
+
|
37
|
+
def instances(region)
|
38
|
+
response = @ec2[region].instances.tagged('Name').filtered_request(:describe_instances)
|
39
|
+
response[:instance_index].values
|
27
40
|
end
|
28
|
-
ec2.describe_instances.map do |instance|
|
29
|
-
instance_name = instance_names[instance[:aws_instance_id]] or next nil
|
30
|
-
dns_name = instance[:dns_name] or next nil
|
31
|
-
instance_name.empty? || dns_name.empty? ? nil : {:host => "#{instance_name}.#{region}", :dns_name => dns_name}
|
32
|
-
end.compact
|
33
|
-
end
|
34
41
|
end
|
35
42
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
|
3
3
|
module Ec2ssh
|
4
|
-
class
|
4
|
+
class SshConfig
|
5
5
|
HEADER = "### EC2SSH BEGIN ###"
|
6
6
|
FOOTER = "### EC2SSH END ###"
|
7
7
|
|
@@ -43,7 +43,7 @@ module Ec2ssh
|
|
43
43
|
#{HEADER}
|
44
44
|
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
45
45
|
# DO NOT edit this block!
|
46
|
-
# Updated #{Time.now}
|
46
|
+
# Updated #{Time.now.iso8601}
|
47
47
|
#{text}
|
48
48
|
#{FOOTER}
|
49
49
|
END
|
data/lib/ec2ssh/version.rb
CHANGED
data/lib/ec2ssh.rb
CHANGED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ec2ssh/cli'
|
3
|
+
require 'ec2ssh/dotfile'
|
4
|
+
|
5
|
+
describe Ec2ssh::CLI do
|
6
|
+
before(:all) do
|
7
|
+
Ec2ssh::Hosts.tap do |cls|
|
8
|
+
cls.class_eval do
|
9
|
+
def all
|
10
|
+
[
|
11
|
+
{:host => 'db-01', :dns_name => 'ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com'},
|
12
|
+
{:host => 'db-02', :dns_name => 'ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com'},
|
13
|
+
]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
let(:cli) { described_class }
|
19
|
+
let(:ssh_config_path) do
|
20
|
+
path = tmp_dir.join('ssh_config')
|
21
|
+
path.open('w') {|f| f.write <<-END }
|
22
|
+
Host foo.bar.com
|
23
|
+
HostName 1.2.3.4
|
24
|
+
END
|
25
|
+
path
|
26
|
+
end
|
27
|
+
let(:ssh_config_string) { ssh_config_path.read }
|
28
|
+
let(:dotfile_path) do
|
29
|
+
tmp_dir.join('dot.ec2ssh')
|
30
|
+
end
|
31
|
+
|
32
|
+
around do |example|
|
33
|
+
tz = ENV['TZ']
|
34
|
+
ENV['TZ'] = 'UTC'
|
35
|
+
Timecop.freeze(Time.local(2013,1,1,0,0,0)) { example.call }
|
36
|
+
ENV['TZ'] = 'tz'
|
37
|
+
end
|
38
|
+
|
39
|
+
subject { ssh_config_string }
|
40
|
+
|
41
|
+
describe '#init' do
|
42
|
+
before do
|
43
|
+
silence(:stdout) { cli.start %W[init --path #{ssh_config_path} --dotfile #{dotfile_path}] }
|
44
|
+
end
|
45
|
+
|
46
|
+
it { should eq(<<-END) }
|
47
|
+
Host foo.bar.com
|
48
|
+
HostName 1.2.3.4
|
49
|
+
### EC2SSH BEGIN ###
|
50
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
51
|
+
# DO NOT edit this block!
|
52
|
+
# Updated 2013-01-01T00:00:00+00:00
|
53
|
+
|
54
|
+
### EC2SSH END ###
|
55
|
+
END
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#update' do
|
59
|
+
before do
|
60
|
+
silence(:stdout) do
|
61
|
+
cli.start %W[init --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
62
|
+
cli.start %W[update --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it { should eq(<<-END) }
|
67
|
+
Host foo.bar.com
|
68
|
+
HostName 1.2.3.4
|
69
|
+
### EC2SSH BEGIN ###
|
70
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
71
|
+
# DO NOT edit this block!
|
72
|
+
# Updated 2013-01-01T00:00:00+00:00
|
73
|
+
Host db-01
|
74
|
+
HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
|
75
|
+
Host db-02
|
76
|
+
HostName ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com
|
77
|
+
|
78
|
+
### EC2SSH END ###
|
79
|
+
END
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#update with aws-keys option' do
|
83
|
+
before do
|
84
|
+
silence(:stdout) do
|
85
|
+
cli.start %W[init --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
86
|
+
end
|
87
|
+
dotfile = Ec2ssh::Dotfile.load(dotfile_path)
|
88
|
+
dotfile['aws_keys']['key1'] = {
|
89
|
+
'access_key_id' => 'ACCESS_KEY_ID',
|
90
|
+
'secret_access_key' => 'SECRET_ACCESS_KEY'
|
91
|
+
}
|
92
|
+
@output = capture(:stdout) do
|
93
|
+
cli.start %W[update --path #{ssh_config_path} --dotfile #{dotfile_path} --aws-key #{keyname}]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
subject { @output }
|
98
|
+
|
99
|
+
context do
|
100
|
+
let(:keyname) { 'default' }
|
101
|
+
it { should =~ /Updated 2 hosts/ }
|
102
|
+
end
|
103
|
+
|
104
|
+
context do
|
105
|
+
let(:keyname) { 'key1' }
|
106
|
+
it { should =~ /Updated 2 hosts/ }
|
107
|
+
end
|
108
|
+
|
109
|
+
context do
|
110
|
+
let(:keyname) { 'key2' }
|
111
|
+
it { should_not =~ /^Updated 2 hosts/ }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '#remove' do
|
116
|
+
before do
|
117
|
+
silence(:stdout) do
|
118
|
+
cli.start %W[init --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
119
|
+
cli.start %W[update --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
120
|
+
cli.start %W[remove --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it { should eq(<<-END) }
|
125
|
+
Host foo.bar.com
|
126
|
+
HostName 1.2.3.4
|
127
|
+
END
|
128
|
+
end
|
129
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require
|
3
|
+
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'pathname'
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
def capture(stream)
|
10
|
+
begin
|
11
|
+
stream = stream.to_s
|
12
|
+
eval "$#{stream} = StringIO.new"
|
13
|
+
yield
|
14
|
+
result = eval("$#{stream}").string
|
15
|
+
ensure
|
16
|
+
eval("$#{stream} = #{stream.upcase}")
|
17
|
+
end
|
18
|
+
|
19
|
+
result
|
20
|
+
end
|
21
|
+
|
22
|
+
def base_dir
|
23
|
+
Pathname('../..').expand_path(__FILE__)
|
24
|
+
end
|
25
|
+
|
26
|
+
def tmp_dir
|
27
|
+
base_dir.join('tmp')
|
28
|
+
end
|
29
|
+
|
30
|
+
alias :silence :capture
|
31
|
+
|
32
|
+
config.before(:all) do
|
33
|
+
unless tmp_dir.directory?
|
34
|
+
tmp_dir.mkdir
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2ssh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version:
|
9
|
+
- 0
|
10
|
+
version: 2.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
-
|
13
|
+
- Issei Naruta
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-03-11 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: thor
|
@@ -49,24 +49,23 @@ dependencies:
|
|
49
49
|
type: :runtime
|
50
50
|
version_requirements: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
name:
|
52
|
+
name: aws-sdk
|
53
53
|
prerelease: false
|
54
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
55
55
|
none: false
|
56
56
|
requirements:
|
57
57
|
- - ~>
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
hash:
|
59
|
+
hash: 31
|
60
60
|
segments:
|
61
|
-
-
|
62
|
-
-
|
63
|
-
|
64
|
-
version: 2.0.3
|
61
|
+
- 1
|
62
|
+
- 8
|
63
|
+
version: "1.8"
|
65
64
|
type: :runtime
|
66
65
|
version_requirements: *id003
|
67
|
-
description:
|
66
|
+
description: ec2ssh is a ssh_config manager for AWS EC2
|
68
67
|
email:
|
69
|
-
-
|
68
|
+
- mimitako@gmail.com
|
70
69
|
executables:
|
71
70
|
- ec2ssh
|
72
71
|
extensions: []
|
@@ -75,16 +74,25 @@ extra_rdoc_files: []
|
|
75
74
|
|
76
75
|
files:
|
77
76
|
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .rvmrc
|
79
|
+
- .travis.yml
|
80
|
+
- ChangeLog.md
|
78
81
|
- Gemfile
|
79
82
|
- Gemfile.lock
|
80
|
-
-
|
83
|
+
- Guardfile
|
84
|
+
- MIT-LICENSE
|
85
|
+
- README.md
|
81
86
|
- bin/ec2ssh
|
82
87
|
- ec2ssh.gemspec
|
83
88
|
- lib/ec2ssh.rb
|
84
89
|
- lib/ec2ssh/cli.rb
|
85
|
-
- lib/ec2ssh/
|
90
|
+
- lib/ec2ssh/dotfile.rb
|
86
91
|
- lib/ec2ssh/hosts.rb
|
92
|
+
- lib/ec2ssh/ssh_config.rb
|
87
93
|
- lib/ec2ssh/version.rb
|
94
|
+
- spec/lib/ec2ssh/cli_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
88
96
|
homepage: http://github.com/mirakui/ec2ssh
|
89
97
|
licenses: []
|
90
98
|
|
@@ -114,9 +122,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
122
|
requirements: []
|
115
123
|
|
116
124
|
rubyforge_project: ec2ssh
|
117
|
-
rubygems_version: 1.8.
|
125
|
+
rubygems_version: 1.8.15
|
118
126
|
signing_key:
|
119
127
|
specification_version: 3
|
120
128
|
summary: A ssh_config manager for AWS EC2
|
121
|
-
test_files:
|
122
|
-
|
129
|
+
test_files:
|
130
|
+
- spec/lib/ec2ssh/cli_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
data/README.rdoc
DELETED
@@ -1,48 +0,0 @@
|
|
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.
|