ec2ssh 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +4 -0
- data/Gemfile.lock +4 -4
- data/lib/ec2ssh/cli.rb +31 -5
- data/lib/ec2ssh/ssh_config.rb +55 -3
- data/lib/ec2ssh/version.rb +1 -1
- data/spec/lib/ec2ssh/cli_spec.rb +43 -4
- data/spec/lib/ec2ssh/ssh_config_spec.rb +125 -0
- metadata +4 -2
data/ChangeLog.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ec2ssh (2.0.
|
4
|
+
ec2ssh (2.0.3)
|
5
5
|
aws-sdk (~> 1.8)
|
6
6
|
highline (~> 1.6)
|
7
7
|
thor (~> 0.14.6)
|
@@ -10,7 +10,7 @@ GEM
|
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
12
|
addressable (2.3.2)
|
13
|
-
aws-sdk (1.8.
|
13
|
+
aws-sdk (1.8.5)
|
14
14
|
json (~> 1.4)
|
15
15
|
nokogiri (>= 1.4.4)
|
16
16
|
uuidtools (~> 2.1)
|
@@ -26,12 +26,12 @@ GEM
|
|
26
26
|
guard-rspec (2.4.0)
|
27
27
|
guard (>= 1.1)
|
28
28
|
rspec (~> 2.11)
|
29
|
-
highline (1.6.
|
29
|
+
highline (1.6.17)
|
30
30
|
json (1.7.7)
|
31
31
|
listen (0.7.2)
|
32
32
|
lumberjack (1.0.2)
|
33
33
|
method_source (0.8.1)
|
34
|
-
nokogiri (1.5.
|
34
|
+
nokogiri (1.5.9)
|
35
35
|
pry (0.9.12)
|
36
36
|
coderay (~> 1.0.5)
|
37
37
|
method_source (~> 0.8)
|
data/lib/ec2ssh/cli.rb
CHANGED
@@ -25,16 +25,16 @@ module Ec2ssh
|
|
25
25
|
desc "update", "Update ec2 hosts list in ssh_config"
|
26
26
|
method_option :aws_key, :banner => 'aws key name', :default => 'default'
|
27
27
|
def update
|
28
|
-
config = SshConfig.new(config_path)
|
28
|
+
config = SshConfig.new(config_path, options.aws_key)
|
29
29
|
unless config.mark_exist?
|
30
30
|
red "Marker not found on #{config_path}"
|
31
31
|
red "Execute '#{$0} init --path=/path/to/ssh_config' first!"
|
32
32
|
return
|
33
33
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
|
35
|
+
config.parse!
|
36
|
+
sections = merge_sections(config)
|
37
|
+
config_str = config.wrap(sections.join("\n"))
|
38
38
|
config.replace! config_str
|
39
39
|
yellow config_str
|
40
40
|
green "Updated #{hosts.size} hosts on #{config_path}"
|
@@ -81,6 +81,32 @@ Host #{h[:host]}
|
|
81
81
|
puts hl.color(str, col)
|
82
82
|
end
|
83
83
|
end
|
84
|
+
|
85
|
+
def merge_sections(config)
|
86
|
+
section_str = hosts.map { |h| <<-END }.join
|
87
|
+
Host #{h[:host]}
|
88
|
+
HostName #{h[:dns_name]}
|
89
|
+
END
|
90
|
+
config.sections[options.aws_key] ||= SshConfig::Section.new(
|
91
|
+
options.aws_key,
|
92
|
+
section_str
|
93
|
+
)
|
94
|
+
|
95
|
+
sections = config.sections.values.map do |section|
|
96
|
+
if (
|
97
|
+
# section is matched
|
98
|
+
(section.name == options.aws_key) ||
|
99
|
+
|
100
|
+
# for backward compatibility
|
101
|
+
(config.sections.size == 1 && options.aws_key != 'default')
|
102
|
+
)
|
103
|
+
section.name = options.aws_key
|
104
|
+
section.replace!(section_str)
|
105
|
+
end
|
106
|
+
|
107
|
+
section.to_s
|
108
|
+
end
|
109
|
+
end
|
84
110
|
end
|
85
111
|
end
|
86
112
|
end
|
data/lib/ec2ssh/ssh_config.rb
CHANGED
@@ -6,10 +6,31 @@ module Ec2ssh
|
|
6
6
|
HEADER = "### EC2SSH BEGIN ###"
|
7
7
|
FOOTER = "### EC2SSH END ###"
|
8
8
|
|
9
|
-
attr_reader :path
|
9
|
+
attr_reader :path, :sections
|
10
10
|
|
11
|
-
def initialize(path=nil)
|
12
|
-
@path
|
11
|
+
def initialize(path=nil, aws_key='default')
|
12
|
+
@path = Pathname(path || "#{ENV['HOME']}/.ssh/config")
|
13
|
+
@aws_key = aws_key
|
14
|
+
@sections = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse!
|
18
|
+
return unless mark_exist?
|
19
|
+
ec2_config = config_src.match(/#{HEADER}\n(.*)#{FOOTER}/m).to_s
|
20
|
+
|
21
|
+
current_section = 'default'
|
22
|
+
@sections[current_section] = Section.new('default')
|
23
|
+
|
24
|
+
ec2_config.split(/\n+/).each do |line|
|
25
|
+
if line =~ /#{Section::HEADER} (.+)/
|
26
|
+
current_section = $1
|
27
|
+
@sections[current_section] ||= Section.new(current_section)
|
28
|
+
elsif line =~ /^#/ # ignore
|
29
|
+
elsif line =~ /^$/ # ignore
|
30
|
+
else
|
31
|
+
@sections[current_section].append("#{line}\n")
|
32
|
+
end
|
33
|
+
end
|
13
34
|
end
|
14
35
|
|
15
36
|
def append_mark!
|
@@ -49,5 +70,36 @@ module Ec2ssh
|
|
49
70
|
#{FOOTER}
|
50
71
|
END
|
51
72
|
end
|
73
|
+
|
74
|
+
class Section
|
75
|
+
HEADER = "# section:"
|
76
|
+
|
77
|
+
attr_accessor :name
|
78
|
+
attr_reader :text
|
79
|
+
|
80
|
+
def initialize(name, text = '')
|
81
|
+
@name = name
|
82
|
+
@text = text
|
83
|
+
end
|
84
|
+
|
85
|
+
def append(text)
|
86
|
+
@text << text
|
87
|
+
end
|
88
|
+
|
89
|
+
def replace!(text)
|
90
|
+
@text = text
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
if text.empty?
|
95
|
+
""
|
96
|
+
else
|
97
|
+
<<-EOS
|
98
|
+
#{HEADER} #{@name}
|
99
|
+
#{@text}
|
100
|
+
EOS
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
52
104
|
end
|
53
105
|
end
|
data/lib/ec2ssh/version.rb
CHANGED
data/spec/lib/ec2ssh/cli_spec.rb
CHANGED
@@ -8,8 +8,8 @@ describe Ec2ssh::CLI do
|
|
8
8
|
cls.class_eval do
|
9
9
|
def all
|
10
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'},
|
11
|
+
{:host => 'db-01.ap-northeast-1', :dns_name => 'ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com'},
|
12
|
+
{:host => 'db-02.ap-northeast-1', :dns_name => 'ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com'},
|
13
13
|
]
|
14
14
|
end
|
15
15
|
end
|
@@ -73,11 +73,13 @@ Host foo.bar.com
|
|
73
73
|
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
74
74
|
# DO NOT edit this block!
|
75
75
|
# Updated 2013-01-01T00:00:00+00:00
|
76
|
-
|
76
|
+
# section: default
|
77
|
+
Host db-01.ap-northeast-1
|
77
78
|
HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
|
78
|
-
Host db-02
|
79
|
+
Host db-02.ap-northeast-1
|
79
80
|
HostName ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com
|
80
81
|
|
82
|
+
|
81
83
|
### EC2SSH END ###
|
82
84
|
END
|
83
85
|
end
|
@@ -102,11 +104,13 @@ END
|
|
102
104
|
context do
|
103
105
|
let(:keyname) { 'default' }
|
104
106
|
it { should =~ /Updated 2 hosts/ }
|
107
|
+
it { should =~ /# section: default/ }
|
105
108
|
end
|
106
109
|
|
107
110
|
context do
|
108
111
|
let(:keyname) { 'key1' }
|
109
112
|
it { should =~ /Updated 2 hosts/ }
|
113
|
+
it { should =~ /# section: key1/ }
|
110
114
|
end
|
111
115
|
|
112
116
|
context do
|
@@ -115,6 +119,41 @@ END
|
|
115
119
|
end
|
116
120
|
end
|
117
121
|
|
122
|
+
describe '#update with aws-keys option in multiple times' do
|
123
|
+
before do
|
124
|
+
silence(:stdout) do
|
125
|
+
cli.start %W[init --path #{ssh_config_path} --dotfile #{dotfile_path}]
|
126
|
+
end
|
127
|
+
dotfile = Ec2ssh::Dotfile.load(dotfile_path)
|
128
|
+
dotfile['aws_keys']['key1'] = {
|
129
|
+
'access_key_id' => 'ACCESS_KEY_ID',
|
130
|
+
'secret_access_key' => 'SECRET_ACCESS_KEY'
|
131
|
+
}
|
132
|
+
dotfile['aws_keys']['key2'] = {
|
133
|
+
'access_key_id' => 'ACCESS_KEY_ID',
|
134
|
+
'secret_access_key' => 'SECRET_ACCESS_KEY'
|
135
|
+
}
|
136
|
+
@output = capture(:stdout) do
|
137
|
+
cli.start %W[update --path #{ssh_config_path} --dotfile #{dotfile_path} --aws-key key1]
|
138
|
+
cli.start %W[update --path #{ssh_config_path} --dotfile #{dotfile_path} --aws-key #{keyname}]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
subject { @output }
|
143
|
+
|
144
|
+
context 'when updated by the same key' do
|
145
|
+
let(:keyname) { 'key1' }
|
146
|
+
it { subject.should =~ /# section: key1/ }
|
147
|
+
it { subject.should_not =~ /# section: key2/ }
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'when updated by different key' do
|
151
|
+
let(:keyname) { 'key2' }
|
152
|
+
it { subject.should =~ /# section: key1/ }
|
153
|
+
it { subject.should =~ /# section: key2/ }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
118
157
|
describe '#remove' do
|
119
158
|
before do
|
120
159
|
silence(:stdout) do
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ec2ssh/ssh_config'
|
3
|
+
|
4
|
+
describe Ec2ssh::SshConfig do
|
5
|
+
describe '#parse!' do
|
6
|
+
context 'when no section exists' do
|
7
|
+
let(:path) { path = tmp_dir.join('ssh_config') }
|
8
|
+
subject { described_class.new(path) }
|
9
|
+
|
10
|
+
before do
|
11
|
+
path.open('w') {|f| f.write <<-END }
|
12
|
+
### EC2SSH BEGIN ###
|
13
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
14
|
+
# DO NOT edit this block!
|
15
|
+
# Updated 2013-01-01T00:00:00+00:00
|
16
|
+
Host db-01.ap-northeast-1
|
17
|
+
HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
|
18
|
+
|
19
|
+
### EC2SSH END ###
|
20
|
+
END
|
21
|
+
subject.parse!
|
22
|
+
end
|
23
|
+
|
24
|
+
it { expect(subject.sections.size).to be == 1 }
|
25
|
+
it { expect(subject.sections['default']).to be_an_instance_of Ec2ssh::SshConfig::Section }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when a section exists' do
|
29
|
+
let(:path) { path = tmp_dir.join('ssh_config') }
|
30
|
+
subject { described_class.new(path) }
|
31
|
+
|
32
|
+
before do
|
33
|
+
path = tmp_dir.join('ssh_config')
|
34
|
+
path.open('w') {|f| f.write <<-END }
|
35
|
+
Host foo.bar.com
|
36
|
+
HostName 1.2.3.4
|
37
|
+
### EC2SSH BEGIN ###
|
38
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
39
|
+
# DO NOT edit this block!
|
40
|
+
# Updated 2013-01-01T00:00:00+00:00
|
41
|
+
# section: foo
|
42
|
+
Host db-01.ap-northeast-1
|
43
|
+
HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
|
44
|
+
|
45
|
+
### EC2SSH END ###
|
46
|
+
END
|
47
|
+
subject.parse!
|
48
|
+
end
|
49
|
+
|
50
|
+
it { expect(subject.sections.size).to be == 2 }
|
51
|
+
it { expect(subject.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when multiple sections exist' do
|
55
|
+
let(:path) { path = tmp_dir.join('ssh_config') }
|
56
|
+
subject { described_class.new(path) }
|
57
|
+
|
58
|
+
before do
|
59
|
+
path = tmp_dir.join('ssh_config')
|
60
|
+
path.open('w') {|f| f.write <<-END }
|
61
|
+
Host foo.bar.com
|
62
|
+
HostName 1.2.3.4
|
63
|
+
### EC2SSH BEGIN ###
|
64
|
+
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
|
65
|
+
# DO NOT edit this block!
|
66
|
+
# Updated 2013-01-01T00:00:00+00:00
|
67
|
+
# section: foo
|
68
|
+
Host db-01.ap-northeast-1
|
69
|
+
HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
|
70
|
+
# section: bar
|
71
|
+
Host db-02.ap-northeast-1
|
72
|
+
HostName ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com
|
73
|
+
|
74
|
+
### EC2SSH END ###
|
75
|
+
END
|
76
|
+
subject.parse!
|
77
|
+
end
|
78
|
+
|
79
|
+
it { expect(subject.sections.size).to be == 3 }
|
80
|
+
it { expect(subject.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
|
81
|
+
it { expect(subject.sections['bar']).to be_an_instance_of Ec2ssh::SshConfig::Section }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe Ec2ssh::SshConfig::Section do
|
86
|
+
describe '#append' do
|
87
|
+
let(:section) { Ec2ssh::SshConfig::Section.new('test') }
|
88
|
+
before { section.append('foo') }
|
89
|
+
|
90
|
+
it { expect(section.text).to eq 'foo' }
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#replace' do
|
94
|
+
let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
|
95
|
+
before { section.replace!('bar') }
|
96
|
+
|
97
|
+
it { expect(section.text).to eq 'bar' }
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#to_s' do
|
101
|
+
context 'when no text given' do
|
102
|
+
let(:section) { Ec2ssh::SshConfig::Section.new('test') }
|
103
|
+
|
104
|
+
it { expect(section.to_s).to eq '' }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when empty text given' do
|
108
|
+
let(:section) { Ec2ssh::SshConfig::Section.new('test', '') }
|
109
|
+
|
110
|
+
it { expect(section.to_s).to eq '' }
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when some text given' do
|
114
|
+
let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
|
115
|
+
|
116
|
+
it {
|
117
|
+
expect(section.to_s).to eq <<EOS
|
118
|
+
# section: test
|
119
|
+
foo
|
120
|
+
EOS
|
121
|
+
}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2ssh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: thor
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/ec2ssh/ssh_config.rb
|
87
87
|
- lib/ec2ssh/version.rb
|
88
88
|
- spec/lib/ec2ssh/cli_spec.rb
|
89
|
+
- spec/lib/ec2ssh/ssh_config_spec.rb
|
89
90
|
- spec/spec_helper.rb
|
90
91
|
homepage: http://github.com/mirakui/ec2ssh
|
91
92
|
licenses: []
|
@@ -113,5 +114,6 @@ specification_version: 3
|
|
113
114
|
summary: A ssh_config manager for AWS EC2
|
114
115
|
test_files:
|
115
116
|
- spec/lib/ec2ssh/cli_spec.rb
|
117
|
+
- spec/lib/ec2ssh/ssh_config_spec.rb
|
116
118
|
- spec/spec_helper.rb
|
117
119
|
has_rdoc:
|