ec2ssh 2.0.8 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12075149108b54ade580b209a7a4f0967a9e9f0a
4
- data.tar.gz: c6c0a09938f2136ab34e0e93217a57be96716517
3
+ metadata.gz: 4ba8b5318372505cf30fc98a44565ae291d35c70
4
+ data.tar.gz: f2e69c94b92e1f8f795fe894e84ee6779a3906bd
5
5
  SHA512:
6
- metadata.gz: f3b15ed5fec14c3d0b729eaa0ed3a0c5d511fd75ef99785e2d4f0cdb400a8e7d42c42fb6e4d844046635c22d35a795aec62d9a9f627a8c0b16f2cd95bbca0cd8
7
- data.tar.gz: 1e7a933fc01d5d39fbe664801abce52be7a76afaae392b3fcdeb383a76ec5564114ded1b5f3ed97424815bb2eeea3538204b61473098db88b4ffadbba47c258d
6
+ metadata.gz: bc87a5021187606885dbed6ef6e9f297af5544477ce6ce4063e0f7d3b64ee114507e16c721e64d6c29abb5e6c53e82854e826399bd9aabfc50cbf097bb2d66e0
7
+ data.tar.gz: 91b1bedabe6f6409251df5415ff0b53ad9d65afb2a9a13dc5396e5f0ac9af24ab8212dc446630206d192c31a54d9fb4672ebe489e2a1227ced631f4d8f8105b3
data/.gitignore CHANGED
@@ -1,7 +1,17 @@
1
- vendor
2
- .bundle
3
- tmp
4
1
  *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
5
6
  Gemfile.lock
6
- .ruby-version
7
- .rvmrc
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
4
+ - 2.1.3
5
5
  script: 'bundle exec rspec spec'
@@ -1,9 +1,4 @@
1
1
  # Change Log
2
- ## 2.0.8
3
-
4
- * Update EC2 API endpoint (#16, #20)
5
- Thanks to @a2ikm and @wnoguchi
6
-
7
2
  ## 2.0.7
8
3
 
9
4
  * Add ssh_options (#11)
data/Gemfile CHANGED
@@ -1,8 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
- gem 'rspec', '~> 2.12'
5
- gem 'guard-rspec', '~> 2.4'
4
+ gem 'rspec', '~> 3.0'
5
+ gem 'rspec-its', '~> 1.0'
6
+ gem 'guard-rspec', '~> 4.3'
6
7
  gem 'webmock', '~> 1.9'
7
8
  gem 'rb-fsevent', '~> 0.9.1'
8
9
  gem 'timecop', '~> 0.5'
10
+ gem 'fakefs', require: 'fakefs/safe'
data/Guardfile CHANGED
@@ -1,9 +1,5 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'rspec' do
1
+ guard :rspec, cmd: 'bundle exec rspec' do
5
2
  watch(%r{^spec/.+_spec\.rb$})
6
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
4
  watch('spec/spec_helper.rb') { "spec" }
8
5
  end
9
-
data/README.md CHANGED
@@ -26,13 +26,21 @@ $ ec2ssh init
26
26
  ```
27
27
  $ vi ~/.ec2ssh
28
28
  ---
29
- path: /path/to/ssh_config
30
- aws_keys:
31
- default:
32
- access_key_id: YOUR_ACCESS_KEY_ID
33
- secret_access_key: YOUR_SECRET_ACCESS_KEY
34
- regions:
35
- - ap-northeast-1
29
+ aws_keys(
30
+ default: {
31
+ access_key_id: ENV['AWS_ACCESS_KEY_ID'],
32
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
33
+ },
34
+ # my_key1: { access_key_id: '...', secret_access_key: '...' }, ...
35
+ )
36
+ regions 'us-east-1'
37
+
38
+ # You can use methods of AWS::EC2::Instance.
39
+ # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
40
+ host_line <<END
41
+ Host <%= tags['Name'] %>.<%= availability_zone %>
42
+ HostName <%= dns_name || private_ip_address %>
43
+ END
36
44
  ```
37
45
 
38
46
  ### 5. Execute `ec2ssh update`
@@ -45,7 +53,7 @@ Then host-names of your instances are generated and wrote to .ssh/config
45
53
  ### 6. And you can ssh to your instances with your tagged name.
46
54
 
47
55
  ```
48
- $ ssh app-server-1.us-west-1
56
+ $ ssh app-server-1.us-east-1a
49
57
  ```
50
58
 
51
59
  # Commands
@@ -57,13 +65,6 @@ $ ec2ssh remove # Remove ec2ssh mark from ssh_config
57
65
  ```
58
66
 
59
67
  ## Options
60
- ### --path
61
- Each command can use `--path` option to set ssh_config path. `~/.ssh/config` is default.
62
-
63
- ```
64
- $ ec2ssh init --path /path/to/ssh_config
65
- ```
66
-
67
68
  ### --dotfile
68
69
  Each command can use `--dotfile` option to set dotfile (.ec2ssh) path. `~/.ec2ssh` is default.
69
70
 
@@ -71,14 +72,6 @@ Each command can use `--dotfile` option to set dotfile (.ec2ssh) path. `~/.ec2ss
71
72
  $ ec2ssh init --dotfile /path/to/ssh_config
72
73
  ```
73
74
 
74
- ### --aws-key
75
- `ec2ssh update` allows `--aws-key` option. If you have multiple aws keys, you can choose from them as you like using this option. See Dotfile section for details.
76
-
77
- ```
78
- $ ec2ssh update --aws-key my_key1
79
- ```
80
-
81
-
82
75
  # ssh_config and mark lines
83
76
  `ec2ssh init` command inserts mark lines your `.ssh/config` such as:
84
77
 
@@ -110,85 +103,28 @@ Host db-server-1.ap-southeast-1
110
103
 
111
104
  `ec2ssh remove` command removes the mark lines.
112
105
 
113
- # Dotfile (.ec2ssh)
114
- Dotfile (`.ec2ssh`) is a feature which is released at v2.0.0. A template of `.ec2ssh` is created when you execute `ec2ssh init`.
106
+ # How to upgrade from 1.x to 2.x
107
+ If you have used ec2ssh-1.x, it seems that you may not have '~/.ec2ssh'.
108
+ So you need execute `ec2ssh init` once to create `~/.ec2ssh`, and edit it as you like.
115
109
 
116
110
  ```
117
111
  $ ec2ssh init
118
- $ cat ~/.ec2ssh
119
- ---
120
- path: /home/yourname/.ssh/config
121
- aws_keys:
122
- default:
123
- access_key_id: ...(Filled by ENV['AMAZON_ACCESS_KEY_ID']
124
- secret_access_key: ...(Filled by ENV['AMAZON_SECRET_ACCESS_KEY'])
125
- regions:
126
- - ap-northeast-1
127
- ```
128
-
129
- ## multiple aws keys
130
- You can use multiple aws keys at `ec2ssh update` with `--aws-key` option.
131
-
132
- ```
133
- $ cat ~/.ec2ssh
134
- ---
135
- path: /home/yourname/.ssh/config
136
- aws_keys:
137
- default:
138
- access_key_id: ...
139
- secret_access_key: ...
140
- my_key1:
141
- access_key_id: ...
142
- secret_access_key: ...
143
- regions:
144
- - ap-northeast-1
145
- ```
146
-
147
- Updating ssh_config by 'default' aws key:
148
-
149
- ```
150
- $ ec2ssh update
112
+ $ vi ~/.ec2ssh
151
113
  ```
152
114
 
153
- Updates ssh_config by 'my_key1' aws key:
154
-
155
- ```
156
- $ ec2ssh update --aws-key my_key1
157
- ```
115
+ # How to upgrade from 2.x to 3.x
116
+ Dotfile (`.ec2ssh`) format has been changed from YAML to Ruby DSL.
158
117
 
159
- ## ssh options
160
- You can set other ssh options such as IdentityFile or User.
118
+ Don't panic and run `ec2ssh migrate` if you have ec2ssh-2.x styled dotfile.
161
119
 
162
120
  ```
163
- $ cat ~/.ec2ssh
164
- ---
165
- path: /home/yourname/.ssh/config
166
- aws_keys:
167
- default:
168
- access_key_id: ...
169
- secret_access_key: ...
170
- my_key1:
171
- access_key_id: ...
172
- secret_access_key: ...
173
- regions:
174
- - ap-northeast-1
175
- ssh_options:
176
- - "IdentityFile ~/.ssh/ec2.id_rsa"
177
- - "User ec2-user"
178
- - "TCPKeepAlive yes"
121
+ $ ec2ssh migrate
179
122
  ```
180
123
 
181
- # How to upgrade from 1.x to 2.x
182
- If you have used ec2ssh-1.x, it seems that you may not have '~/.ec2ssh'.
183
- So you need execute `ec2ssh init` once to create `~/.ec2ssh`, and edit it as you like.
184
-
185
- ```
186
- $ ec2ssh init
187
- $ vi ~/.ec2ssh
188
- ```
124
+ This command converts your existing `.ec2ssh` file into 3.x style.
189
125
 
190
126
  # Notice
191
127
  `ec2ssh` command updates your `.ssh/config` file default. You should make a backup of it.
192
128
 
193
129
  # License
194
- ec2ssh is released under the MIT license.
130
+ Copyright (c) 2014 Issei Naruta. ec2ssh is released under the MIT license.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ path '~/.ssh/config'
2
+ aws_keys(
3
+ {
4
+ access_key_id: ENV['AWS_ACCESS_KEY_ID'],
5
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
6
+ },
7
+ #{
8
+ # access_key_id: '...'
9
+ # secret_access_key: '...'
10
+ #}
11
+ )
12
+ regions 'ap-northeast-1', 'us-east-1'
13
+ reject {|instance| instance.tags['Name'] =~ /.../ }
14
+ host_lines <<END
15
+ Host <%= tags['Name'] %>
16
+ HostName <%= private_ip_address %>
17
+ END
@@ -0,0 +1,29 @@
1
+ require 'ec2ssh/ec2_instances'
2
+ require 'erb'
3
+ require 'stringio'
4
+
5
+ module Ec2ssh
6
+ class Builder
7
+ def initialize(container)
8
+ @container = container
9
+ @host_lines_erb = ERB.new @container.host_line
10
+ end
11
+
12
+ def build_host_lines
13
+ out = StringIO.new
14
+ @container.aws_keys.each do |name, key|
15
+ out.puts "# section: #{name}"
16
+ ec2s.instances(name).each do |instance|
17
+ bind = instance.instance_eval { binding }
18
+ next if @container.reject && @container.reject.call(instance)
19
+ out.puts @host_lines_erb.result(bind)
20
+ end
21
+ end
22
+ out.string
23
+ end
24
+
25
+ def ec2s
26
+ @ec2s ||= Ec2Instances.new @container.aws_keys, @container.regions
27
+ end
28
+ end
29
+ end
@@ -1,79 +1,93 @@
1
1
  require 'thor'
2
2
  require 'highline'
3
- require 'ec2ssh/hosts'
4
3
  require 'ec2ssh/ssh_config'
5
- require 'ec2ssh/dotfile'
4
+ require 'ec2ssh/exceptions'
5
+ require 'ec2ssh/migrator'
6
6
 
7
7
  module Ec2ssh
8
8
  class CLI < Thor
9
- class_option :path, :banner => "/path/to/ssh_config"
10
- class_option :dotfile, :banner => '$HOME/.ec2ssh', :default => "#{ENV['HOME']}/.ec2ssh"
9
+ class_option :dotfile, banner: '$HOME/.ec2ssh', default: "#{ENV['HOME']}/.ec2ssh"
10
+ class_option :verbose, banner: 'enable debug log', default: false
11
11
 
12
- desc "init", "Add ec2ssh mark to ssh_config"
12
+ desc 'init', 'Add ec2ssh mark to ssh_config'
13
13
  def init
14
- config = SshConfig.new(config_path)
15
- if config.mark_exist?
16
- red "Marker already exists on #{config_path}"
17
- else
18
- config.append_mark!
19
- green "Added mark to #{config_path}"
20
- end
21
- dotfile = Dotfile.update_or_create(options.dotfile, 'path' => config_path)
22
- yellow "Please check and edit #{options.dotfile} before run `ec2ssh update`"
14
+ check_dotfile_version
15
+ command = make_command :init
16
+ command.run
17
+ rescue MarkAlreadyExists
18
+ red "Marker already exists in #{command.ssh_config_path}"
23
19
  end
24
20
 
25
- desc "update", "Update ec2 hosts list in ssh_config"
26
- method_option :aws_key, :banner => 'aws key name', :default => 'default'
21
+ desc 'update', 'Update ec2 hosts list in ssh_config'
22
+ method_option :aws_key, banner: 'aws key name', default: 'default'
27
23
  def update
28
- config = SshConfig.new(config_path, options.aws_key)
29
- unless config.mark_exist?
30
- red "Marker not found on #{config_path}"
31
- red "Execute '#{$0} init --path=/path/to/ssh_config' first!"
32
- return
33
- end
34
-
35
- config.parse!
36
- sections = merge_sections(config)
37
- config_str = config.wrap(sections.join("\n"))
38
- config.replace! config_str
39
- yellow config_str
40
- green "Updated #{hosts.size} hosts on #{config_path}"
41
- rescue AwsEnvNotDefined, AwsKeyNotFound
42
- red "Set aws keys at #{options.dotfile}"
24
+ check_dotfile_existence
25
+ check_dotfile_version
26
+ set_aws_logging
27
+ command = make_command :update
28
+ command.run
29
+ green "Updated #{command.ssh_config_path}"
30
+ rescue AwsKeyNotFound
31
+ red "Set aws keys at #{command.dotfile_path}"
32
+ rescue MarkNotFound
33
+ red "Marker not found in #{command.ssh_config_path}"
34
+ red "Execute '#{$0} init' first!"
43
35
  end
44
36
 
45
- desc "remove", "Remove ec2ssh mark from ssh_config"
37
+ desc 'remove', 'Remove ec2ssh mark from ssh_config'
46
38
  def remove
47
- config = SshConfig.new(config_path)
48
- unless config.mark_exist?
49
- red "Marker not found on #{config_path}"
50
- return
51
- end
52
- config.replace! ""
53
- green "Removed mark from #{config_path}"
39
+ check_dotfile_existence
40
+ check_dotfile_version
41
+ command = make_command :remove
42
+ command.run
43
+ green "Removed mark from #{command.ssh_config_path}"
44
+ rescue MarkNotFound
45
+ red "Marker not found in #{command.ssh_config_path}"
46
+ end
47
+
48
+ desc 'migrate', 'Migrate dotfile from old versions'
49
+ def migrate
50
+ command = make_command :migrate
51
+ command.run
54
52
  end
55
53
 
56
54
  no_tasks do
57
- def hl
58
- @hl ||= HighLine.new
55
+ def check_dotfile_version
56
+ return unless File.exist?(options.dotfile)
57
+ migrator = Migrator.new options.dotfile
58
+ if migrator.check_version < '3'
59
+ red "#{options.dotfile} is old style."
60
+ red "Try '#{$0} migrate' to migrate to version 3"
61
+ abort
62
+ end
63
+ end
64
+
65
+ def check_dotfile_existence
66
+ unless File.exist?(options.dotfile)
67
+ red "#{options.dotfile} doesn't exist."
68
+ red "Try '#{$0} init' to generate it or specify the path with --dotfile option"
69
+ abort
70
+ end
59
71
  end
60
72
 
61
- def hosts
62
- @hosts ||= Hosts.new(dotfile, options.aws_key).all
73
+ def make_command(cmd)
74
+ require "ec2ssh/command/#{cmd}"
75
+ cls = eval "Ec2ssh::Command::#{cmd.capitalize}"
76
+ cls.new(self)
63
77
  end
64
78
 
65
- def dotfile
66
- @dotfile ||= begin
67
- if File.exist?(options.dotfile)
68
- Dotfile.load(options.dotfile)
69
- else
70
- Dotfile.new
71
- end
79
+ def set_aws_logging
80
+ if options.verbose
81
+ require 'logger'
82
+ require 'aws-sdk'
83
+ logger = ::Logger.new($stdout)
84
+ logger.level = ::Logger::DEBUG
85
+ ::AWS.config logger: logger
72
86
  end
73
87
  end
74
88
 
75
- def config_path
76
- options.path || dotfile['path'] || "#{ENV['HOME']}/.ssh/config"
89
+ def hl
90
+ @hl ||= ::HighLine.new
77
91
  end
78
92
 
79
93
  [:red,:green,:yellow].each do |col|
@@ -81,45 +95,6 @@ module Ec2ssh
81
95
  puts hl.color(str, col)
82
96
  end
83
97
  end
84
-
85
- def merge_sections(config)
86
- ssh_options = dotfile['ssh_options']
87
-
88
- section_str = hosts.map { |h|
89
- section = <<-END
90
- Host #{h[:host]}
91
- HostName #{h[:dns_name]}
92
- END
93
-
94
- unless ssh_options.nil?
95
- ssh_options.each {|line|
96
- section << " #{line}\n"
97
- }
98
- end
99
-
100
- section
101
- }.join
102
-
103
- config.sections[options.aws_key] ||= SshConfig::Section.new(
104
- options.aws_key,
105
- section_str
106
- )
107
-
108
- sections = config.sections.values.map do |section|
109
- if (
110
- # section is matched
111
- (section.name == options.aws_key) ||
112
-
113
- # for backward compatibility
114
- (config.sections.size == 1 && options.aws_key != 'default')
115
- )
116
- section.name = options.aws_key
117
- section.replace!(section_str)
118
- end
119
-
120
- section.to_s
121
- end
122
- end
123
98
  end
124
99
  end
125
100
  end