ec2ssh 3.1.1 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +31 -0
- data/ChangeLog.md +23 -0
- data/Gemfile +7 -5
- data/README.md +27 -24
- data/bash/ec2ssh.bash +4 -11
- data/ec2ssh.gemspec +6 -5
- data/example/example.ec2ssh +2 -2
- data/lib/ec2ssh/builder.rb +13 -5
- data/lib/ec2ssh/cli.rb +2 -22
- data/lib/ec2ssh/command/init.rb +3 -9
- data/lib/ec2ssh/command/update.rb +0 -5
- data/lib/ec2ssh/dsl.rb +15 -0
- data/lib/ec2ssh/ec2_instances.rb +70 -16
- data/lib/ec2ssh/version.rb +1 -1
- data/spec/aws_sdk_compatibility_spec.rb +45 -44
- data/spec/lib/ec2ssh/builder_spec.rb +16 -14
- data/spec/lib/ec2ssh/command/remove_spec.rb +3 -4
- data/spec/lib/ec2ssh/command/update_spec.rb +12 -10
- data/spec/lib/ec2ssh/dsl_spec.rb +44 -11
- data/spec/lib/ec2ssh/ec2_instances_spec.rb +26 -8
- data/zsh/_ec2ssh +8 -19
- metadata +43 -23
- data/.travis.yml +0 -11
- data/lib/ec2ssh/command/migrate.rb +0 -34
- data/lib/ec2ssh/migrator.rb +0 -77
- data/spec/lib/ec2ssh/command/migrate_spec.rb +0 -113
- data/spec/lib/ec2ssh/migrator_spec.rb +0 -64
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '093b50c289c52b6442b616dc9cfb0688a5441f540334d9358c1c6e0bf29c1a43'
|
4
|
+
data.tar.gz: e069b0a512b9130c5ae164a8f11ecdc5c7674940cacc312d8d8eb4c220f60130
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 589991eb9c5dd93c750fec943600f7c4de503e6d944cb8a9ef74b9b5e07b0a0dad666dc1f719b01212820a7b90c66036a141ec68ab4cb612af3e0406a5aabafc
|
7
|
+
data.tar.gz: 6e6bb717dcc3958aa79502356dc1c314d5f0a820ddb4438cc0fa67ea595e5b0d720b809f688ef72fe0ef66cd6eefa7596c7e48dd2af124c6f38d834d26e6546b
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
build:
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
name: Ruby ${{ matrix.ruby }}
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby:
|
17
|
+
- '2.6'
|
18
|
+
- '2.7'
|
19
|
+
- '3.0'
|
20
|
+
- '3.1'
|
21
|
+
- 'head'
|
22
|
+
|
23
|
+
steps:
|
24
|
+
- uses: actions/checkout@v2
|
25
|
+
- name: Set up Ruby
|
26
|
+
uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby }}
|
29
|
+
bundler-cache: true
|
30
|
+
- name: Run the default task
|
31
|
+
run: bundle exec rake
|
data/ChangeLog.md
CHANGED
@@ -1,4 +1,27 @@
|
|
1
1
|
# Change Log
|
2
|
+
|
3
|
+
## 5.1.0
|
4
|
+
* Drop support outdated Ruby 2.4 and 2.5 (#60, #61)
|
5
|
+
* CI against for Ruby 3.0 and 3.1 (#61)
|
6
|
+
* Fix arguments warnings of ERB.new on Ruby 3.1 (#61)
|
7
|
+
* Migrate CI from TravisCI to GitHub Actions (#62)
|
8
|
+
|
9
|
+
## 5.0.0
|
10
|
+
* Remove `--aws-key` option and add `--path` option in shellcomp (#56)
|
11
|
+
* Use aws-sdk v3 and stop using v2 (#54)
|
12
|
+
* Delete `rubyforge_project=` in gemspec (#51)
|
13
|
+
* Relax thor and highline versions (#49)
|
14
|
+
* CI against Ruby 2.5, 2.6 and 2.7 (#45, #55)
|
15
|
+
* Drop support outdated Ruby 2.2 and 2.3 (#59)
|
16
|
+
|
17
|
+
## 4.0.0
|
18
|
+
* Use aws-sdk v2 and stop using v1 (#44)
|
19
|
+
* Support AssumeRole with `~/.aws/credentials` (#44)
|
20
|
+
* `aws_keys` requires region (#44)
|
21
|
+
Thanks to @yujideveloper
|
22
|
+
* Support `filters` for listing ec2 instances (#43)
|
23
|
+
Thanks to @satotakumi
|
24
|
+
|
2
25
|
## 3.1.1
|
3
26
|
* Fix a bug in `--verbose` option (#41)
|
4
27
|
Thanks to @adamlazz
|
data/Gemfile
CHANGED
@@ -5,8 +5,10 @@ gem 'rake', '>= 12.0.0'
|
|
5
5
|
gem 'rspec', '~> 3.0'
|
6
6
|
gem 'rspec-its', '~> 1.0'
|
7
7
|
gem 'guard-rspec', '~> 4.3'
|
8
|
-
gem 'webmock', '~>
|
9
|
-
gem 'rb-fsevent', '~> 0.
|
10
|
-
gem 'timecop', '~> 0.
|
11
|
-
gem 'fakefs', '~>
|
12
|
-
gem 'vcr', '~>
|
8
|
+
gem 'webmock', '~> 3.14'
|
9
|
+
gem 'rb-fsevent', '~> 0.10'
|
10
|
+
gem 'timecop', '~> 0.9'
|
11
|
+
gem 'fakefs', '~> 1.4', require: 'fakefs/safe'
|
12
|
+
gem 'vcr', '~> 6.1'
|
13
|
+
|
14
|
+
gem 'rexml' if RUBY_VERSION >= '3.0.0'
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
[](https://badge.fury.io/rb/ec2ssh)
|
2
|
+
[](https://github.com/mirakui/ec2ssh/actions/workflows/main.yml)
|
2
3
|
|
3
4
|
# Introduction
|
4
5
|
ec2ssh is a ssh_config manager for Amazon EC2.
|
@@ -41,17 +42,22 @@ $ ec2ssh init
|
|
41
42
|
```
|
42
43
|
$ vi ~/.ec2ssh
|
43
44
|
---
|
44
|
-
profiles 'default', 'myprofile'
|
45
|
-
regions 'us-east-1'
|
45
|
+
profiles 'default', 'myprofile', ...
|
46
|
+
regions 'us-east-1', 'ap-northeast-1', ...
|
46
47
|
|
47
48
|
# Ignore unnamed instances
|
48
|
-
reject {|instance| !instance.
|
49
|
+
reject {|instance| !instance.tag('Name') }
|
49
50
|
|
50
|
-
# You can
|
51
|
-
|
51
|
+
# You can specify filters on DescribeInstances (default: lists 'running' instances only)
|
52
|
+
filters([
|
53
|
+
{ name: 'instance-state-name', values: ['running', 'stopped'] }
|
54
|
+
])
|
55
|
+
|
56
|
+
# You can use methods of AWS::EC2::Instance and tag(key) method.
|
57
|
+
# See https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Instance.html
|
52
58
|
host_line <<END
|
53
|
-
Host <%=
|
54
|
-
HostName <%=
|
59
|
+
Host <%= tag('Name') %>.<%= placement.availability_zone %>
|
60
|
+
HostName <%= public_dns_name || private_ip_address %>
|
55
61
|
END
|
56
62
|
```
|
57
63
|
|
@@ -115,26 +121,23 @@ Host db-server-1.ap-southeast-1
|
|
115
121
|
|
116
122
|
`ec2ssh remove` command removes the mark lines.
|
117
123
|
|
118
|
-
# How to upgrade from
|
119
|
-
|
120
|
-
So you need execute `ec2ssh init` once to create `~/.ec2ssh`, and edit it as you like.
|
121
|
-
|
122
|
-
```
|
123
|
-
$ ec2ssh init
|
124
|
-
$ vi ~/.ec2ssh
|
125
|
-
```
|
126
|
-
|
127
|
-
# How to upgrade from 2.x to 3.x
|
128
|
-
Dotfile (`.ec2ssh`) format has been changed from YAML to Ruby DSL.
|
124
|
+
# How to upgrade from 3.x
|
125
|
+
Dotfile (`.ec2ssh`) format has been changed from 3.x.
|
129
126
|
|
130
|
-
|
127
|
+
* A instance tag access I/F has been changed from `tags['Name']` to `tag('Name')`
|
128
|
+
* `Aws::EC2::Instance` methods have been changed to AWS SDK v3
|
129
|
+
* The `aws_keys` structure has been changed
|
130
|
+
* `aws_keys[profile_name][region] # => Aws::Credentials`
|
131
|
+
* For example:
|
131
132
|
|
132
133
|
```
|
133
|
-
|
134
|
+
aws_keys(
|
135
|
+
my_prof1: {
|
136
|
+
'ap-northeast-1' => Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
|
137
|
+
}
|
138
|
+
)
|
134
139
|
```
|
135
140
|
|
136
|
-
This command converts your existing `.ec2ssh` file into 3.x style.
|
137
|
-
|
138
141
|
# Notice
|
139
142
|
`ec2ssh` command updates your `.ssh/config` file default. You should make a backup of it.
|
140
143
|
|
@@ -142,4 +145,4 @@ This command converts your existing `.ec2ssh` file into 3.x style.
|
|
142
145
|
Use `zsh/_ec2ssh`.
|
143
146
|
|
144
147
|
# License
|
145
|
-
Copyright (c)
|
148
|
+
Copyright (c) 2022 Issei Naruta. ec2ssh is released under the MIT license.
|
data/bash/ec2ssh.bash
CHANGED
@@ -6,8 +6,8 @@ _ec2ssh() {
|
|
6
6
|
cur=$2
|
7
7
|
prev=$3
|
8
8
|
|
9
|
-
subcmds="help init
|
10
|
-
common_opts="--dotfile --verbose"
|
9
|
+
subcmds="help init remove update version"
|
10
|
+
common_opts="--path --dotfile --verbose"
|
11
11
|
|
12
12
|
# contextual completion
|
13
13
|
case $prev in
|
@@ -21,11 +21,7 @@ _ec2ssh() {
|
|
21
21
|
esac
|
22
22
|
return 0
|
23
23
|
;;
|
24
|
-
--
|
25
|
-
COMPREPLY=()
|
26
|
-
return 0;
|
27
|
-
;;
|
28
|
-
--dotfile)
|
24
|
+
--path | --dotfile)
|
29
25
|
COMPREPLY=( $(compgen -o default -- "$cur"))
|
30
26
|
return 0;
|
31
27
|
;;
|
@@ -35,9 +31,6 @@ _ec2ssh() {
|
|
35
31
|
subcmd=${COMP_WORDS[1]}
|
36
32
|
|
37
33
|
case $subcmd in
|
38
|
-
update)
|
39
|
-
COMPREPLY=( $(compgen -W "--aws-key $common_opts" -- "$cur") )
|
40
|
-
;;
|
41
34
|
help)
|
42
35
|
COMPREPLY=( $(compgen -W "$subcmds" $cur) )
|
43
36
|
;;
|
@@ -50,4 +43,4 @@ _ec2ssh() {
|
|
50
43
|
|
51
44
|
}
|
52
45
|
|
53
|
-
complete -F _ec2ssh ec2ssh
|
46
|
+
complete -F _ec2ssh ec2ssh
|
data/ec2ssh.gemspec
CHANGED
@@ -9,14 +9,15 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Issei Naruta"]
|
10
10
|
s.email = ["mimitako@gmail.com"]
|
11
11
|
s.homepage = "http://github.com/mirakui/ec2ssh"
|
12
|
+
s.license = "MIT"
|
12
13
|
s.summary = %q{A ssh_config manager for AWS EC2}
|
13
14
|
s.description = %q{ec2ssh is a ssh_config manager for AWS EC2}
|
14
|
-
s.required_ruby_version = ">= 2.
|
15
|
+
s.required_ruby_version = ">= 2.6.0"
|
15
16
|
|
16
|
-
s.
|
17
|
-
s.add_dependency "
|
18
|
-
s.add_dependency "
|
19
|
-
s.add_dependency
|
17
|
+
s.add_dependency "thor", ">= 0.14", "< 2.0"
|
18
|
+
s.add_dependency "highline", ">= 1.6", "< 3.0"
|
19
|
+
s.add_dependency "aws-sdk-core", "~> 3"
|
20
|
+
s.add_dependency "aws-sdk-ec2", "~> 1"
|
20
21
|
|
21
22
|
s.files = `git ls-files`.split("\n")
|
22
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/example/example.ec2ssh
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
path "#{ENV['HOME']}/.ssh/config"
|
2
2
|
profiles 'default', 'myprofile'
|
3
3
|
regions 'ap-northeast-1', 'us-east-1'
|
4
|
-
reject {|instance| instance.
|
4
|
+
reject {|instance| instance.tag('Name') =~ /.../ }
|
5
5
|
|
6
6
|
host_line <<END
|
7
|
-
Host <%=
|
7
|
+
Host <%= tag('Name') %>
|
8
8
|
HostName <%= private_ip_address %>
|
9
9
|
END
|
data/lib/ec2ssh/builder.rb
CHANGED
@@ -6,9 +6,7 @@ module Ec2ssh
|
|
6
6
|
class Builder
|
7
7
|
def initialize(container)
|
8
8
|
@container = container
|
9
|
-
|
10
|
-
erb_trim_mode = '%-'
|
11
|
-
@host_lines_erb = ERB.new @container.host_line, safe_level, erb_trim_mode
|
9
|
+
@host_lines_erb = ERB.new @container.host_line, trim_mode: '%-'
|
12
10
|
end
|
13
11
|
|
14
12
|
def build_host_lines
|
@@ -26,19 +24,29 @@ module Ec2ssh
|
|
26
24
|
end
|
27
25
|
|
28
26
|
def ec2s
|
29
|
-
@ec2s ||= Ec2Instances.new aws_keys,
|
27
|
+
@ec2s ||= Ec2Instances.new aws_keys, filters
|
30
28
|
end
|
31
29
|
|
32
30
|
def aws_keys
|
33
31
|
@aws_keys ||= if @container.profiles
|
34
32
|
keys = {}
|
35
33
|
@container.profiles.each do |profile_name|
|
36
|
-
keys[profile_name] =
|
34
|
+
keys[profile_name] = {}
|
35
|
+
@container.regions.each do |region|
|
36
|
+
keys[profile_name][region] = Ec2Instances.expand_profile_name_to_credential profile_name, region
|
37
|
+
end
|
37
38
|
end
|
38
39
|
keys
|
39
40
|
else
|
40
41
|
@container.aws_keys
|
41
42
|
end
|
42
43
|
end
|
44
|
+
|
45
|
+
def filters
|
46
|
+
@filters = @container.filters || [{
|
47
|
+
name: 'instance-state-name',
|
48
|
+
values: ['running']
|
49
|
+
}]
|
50
|
+
end
|
43
51
|
end
|
44
52
|
end
|
data/lib/ec2ssh/cli.rb
CHANGED
@@ -2,7 +2,6 @@ require 'thor'
|
|
2
2
|
require 'highline'
|
3
3
|
require 'ec2ssh/ssh_config'
|
4
4
|
require 'ec2ssh/exceptions'
|
5
|
-
require 'ec2ssh/migrator'
|
6
5
|
|
7
6
|
module Ec2ssh
|
8
7
|
class CLI < Thor
|
@@ -12,7 +11,6 @@ module Ec2ssh
|
|
12
11
|
|
13
12
|
desc 'init', 'Add ec2ssh mark to ssh_config'
|
14
13
|
def init
|
15
|
-
check_dotfile_version
|
16
14
|
command = make_command :init
|
17
15
|
command.run
|
18
16
|
rescue MarkAlreadyExists
|
@@ -22,7 +20,6 @@ module Ec2ssh
|
|
22
20
|
desc 'update', 'Update ec2 hosts list in ssh_config'
|
23
21
|
def update
|
24
22
|
check_dotfile_existence
|
25
|
-
check_dotfile_version
|
26
23
|
set_aws_logging
|
27
24
|
command = make_command :update
|
28
25
|
command.run
|
@@ -37,7 +34,6 @@ module Ec2ssh
|
|
37
34
|
desc 'remove', 'Remove ec2ssh mark from ssh_config'
|
38
35
|
def remove
|
39
36
|
check_dotfile_existence
|
40
|
-
check_dotfile_version
|
41
37
|
command = make_command :remove
|
42
38
|
command.run
|
43
39
|
green "Removed mark from #{command.ssh_config_path}"
|
@@ -45,12 +41,6 @@ module Ec2ssh
|
|
45
41
|
red "Marker not found in #{command.ssh_config_path}"
|
46
42
|
end
|
47
43
|
|
48
|
-
desc 'migrate', 'Migrate dotfile from old versions'
|
49
|
-
def migrate
|
50
|
-
command = make_command :migrate
|
51
|
-
command.run
|
52
|
-
end
|
53
|
-
|
54
44
|
desc 'shellcomp [-]', 'Initialize shell completion for bash/zsh'
|
55
45
|
def shellcomp(_ = false)
|
56
46
|
if args.include?("-")
|
@@ -90,16 +80,6 @@ EOS
|
|
90
80
|
end
|
91
81
|
|
92
82
|
no_tasks do
|
93
|
-
def check_dotfile_version
|
94
|
-
return unless File.exist?(options.dotfile)
|
95
|
-
migrator = Migrator.new options.dotfile
|
96
|
-
if migrator.check_version < '3'
|
97
|
-
red "#{options.dotfile} is old style."
|
98
|
-
red "Try '#{$0} migrate' to migrate to version 3"
|
99
|
-
abort
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
83
|
def check_dotfile_existence
|
104
84
|
unless File.exist?(options.dotfile)
|
105
85
|
red "#{options.dotfile} doesn't exist."
|
@@ -117,10 +97,10 @@ EOS
|
|
117
97
|
def set_aws_logging
|
118
98
|
if options.verbose
|
119
99
|
require 'logger'
|
120
|
-
require 'aws-sdk'
|
100
|
+
require 'aws-sdk-core'
|
121
101
|
logger = ::Logger.new($stdout)
|
122
102
|
logger.level = ::Logger::DEBUG
|
123
|
-
::
|
103
|
+
::Aws.config.update logger: logger
|
124
104
|
end
|
125
105
|
end
|
126
106
|
|
data/lib/ec2ssh/command/init.rb
CHANGED
@@ -33,13 +33,7 @@ module Ec2ssh
|
|
33
33
|
def write_dotfile_example
|
34
34
|
example = <<-DOTFILE
|
35
35
|
path '#{ENV['HOME']}/.ssh/config'
|
36
|
-
|
37
|
-
default: {
|
38
|
-
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
|
39
|
-
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
|
40
|
-
},
|
41
|
-
# my_key1: { access_key_id: '...', secret_access_key: '...' }, ...
|
42
|
-
)
|
36
|
+
profiles 'default', 'myprofile'
|
43
37
|
regions ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
|
44
38
|
# Enable regions as you like
|
45
39
|
# regions *%w(ap-northeast-1 ap-southeast-1 ap-southeast-2 eu-west-1 sa-east-1 us-east-1 us-west-1 us-west-2)
|
@@ -47,8 +41,8 @@ regions ENV['AWS_REGION'] || ENV['AMAZON_REGION'] || ENV['AWS_DEFAULT_REGION'] |
|
|
47
41
|
# You can use methods of AWS::EC2::Instance.
|
48
42
|
# See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
|
49
43
|
host_line <<END
|
50
|
-
Host <%=
|
51
|
-
HostName <%=
|
44
|
+
Host <%= tag('Name') %>.<%= placement.availability_zone %>
|
45
|
+
HostName <%= public_dns_name || private_ip_address %>
|
52
46
|
END
|
53
47
|
DOTFILE
|
54
48
|
|
@@ -3,7 +3,6 @@ require 'ec2ssh/command'
|
|
3
3
|
require 'ec2ssh/ssh_config'
|
4
4
|
require 'ec2ssh/builder'
|
5
5
|
require 'ec2ssh/dsl'
|
6
|
-
require 'ec2ssh/migrator'
|
7
6
|
|
8
7
|
module Ec2ssh
|
9
8
|
module Command
|
@@ -30,10 +29,6 @@ module Ec2ssh
|
|
30
29
|
def dsl
|
31
30
|
@dsl ||= Ec2ssh::Dsl::Parser.parse File.read(dotfile_path)
|
32
31
|
end
|
33
|
-
|
34
|
-
def migrator
|
35
|
-
@migrator ||= Migrator.new dotfile_path
|
36
|
-
end
|
37
32
|
end
|
38
33
|
end
|
39
34
|
end
|
data/lib/ec2ssh/dsl.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
1
|
require 'ec2ssh/exceptions'
|
2
|
+
require 'aws-sdk-core'
|
2
3
|
|
3
4
|
module Ec2ssh
|
4
5
|
class Dsl
|
5
6
|
attr_reader :_result
|
6
7
|
|
8
|
+
CREDENTIAL_CLASSES = [Aws::Credentials, Aws::SharedCredentials, Aws::InstanceProfileCredentials, Aws::AssumeRoleCredentials].freeze
|
9
|
+
private_constant :CREDENTIAL_CLASSES
|
10
|
+
|
7
11
|
def initialize
|
8
12
|
@_result = Container.new
|
9
13
|
end
|
10
14
|
|
11
15
|
def aws_keys(keys)
|
16
|
+
unless keys.all? {|_, v| v.is_a?(Hash) && v.each_value.all? {|c| CREDENTIAL_CLASSES.any?(&c.method(:is_a?)) } }
|
17
|
+
raise DotfileValidationError, <<-MSG
|
18
|
+
Since v4.0, `aws_keys` in the dotfile must be specified regions as a hash key.
|
19
|
+
See: https://github.com/mirakui/ec2ssh#how-to-upgrade-from-3x
|
20
|
+
MSG
|
21
|
+
end
|
12
22
|
@_result.aws_keys = keys
|
13
23
|
end
|
14
24
|
|
@@ -28,6 +38,10 @@ module Ec2ssh
|
|
28
38
|
@_result.reject = block
|
29
39
|
end
|
30
40
|
|
41
|
+
def filters(filters)
|
42
|
+
@_result.filters = filters
|
43
|
+
end
|
44
|
+
|
31
45
|
def path(str)
|
32
46
|
@_result.path = str
|
33
47
|
end
|
@@ -38,6 +52,7 @@ module Ec2ssh
|
|
38
52
|
regions
|
39
53
|
host_line
|
40
54
|
reject
|
55
|
+
filters
|
41
56
|
path
|
42
57
|
])
|
43
58
|
end
|
data/lib/ec2ssh/ec2_instances.rb
CHANGED
@@ -1,22 +1,75 @@
|
|
1
|
-
require 'aws-sdk-
|
1
|
+
require 'aws-sdk-core'
|
2
|
+
require 'aws-sdk-ec2'
|
3
|
+
require 'ec2ssh/exceptions'
|
2
4
|
|
3
5
|
module Ec2ssh
|
4
6
|
class Ec2Instances
|
5
7
|
attr_reader :ec2s, :aws_keys
|
6
8
|
|
7
|
-
|
9
|
+
class InstanceWrapper
|
10
|
+
class TagsWrapper
|
11
|
+
def initialize(tags)
|
12
|
+
@tags = tags
|
13
|
+
end
|
14
|
+
|
15
|
+
# simulate
|
16
|
+
def [](key)
|
17
|
+
if key.is_a? ::String
|
18
|
+
raise DotfileValidationError, <<-MSG
|
19
|
+
`tags[String]` syntax in the dotfile has been deleted since v4.0. Use `tag(String)` instead.
|
20
|
+
See: https://github.com/mirakui/ec2ssh#how-to-upgrade-from-3x
|
21
|
+
MSG
|
22
|
+
end
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def method_missing(name, *args, &block)
|
29
|
+
@tags.public_send(name, *args, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def respond_to_missing?(symbol, include_private)
|
33
|
+
@tags.respond_to?(symbol, include_private)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(ec2_instance)
|
38
|
+
@ec2_instance = ec2_instance
|
39
|
+
@_tags ||= @ec2_instance.tags.each_with_object({}) {|t, h| h[t.key] = t.value }
|
40
|
+
end
|
41
|
+
|
42
|
+
def tag(key)
|
43
|
+
@_tags[key]
|
44
|
+
end
|
45
|
+
|
46
|
+
def tags
|
47
|
+
TagsWrapper.new(super)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def method_missing(name, *args, &block)
|
53
|
+
@ec2_instance.public_send(name, *args, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
def respond_to_missing?(symbol, include_private)
|
57
|
+
@ec2_instance.respond_to?(symbol, include_private)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize(aws_keys, filters)
|
8
62
|
@aws_keys = aws_keys
|
9
|
-
@
|
63
|
+
@filters = filters
|
10
64
|
end
|
11
65
|
|
12
66
|
def make_ec2s
|
13
|
-
AWS.start_memoizing
|
14
67
|
_ec2s = {}
|
15
|
-
aws_keys.
|
68
|
+
aws_keys.each_pair do |name, keys|
|
16
69
|
_ec2s[name] = {}
|
17
|
-
|
18
|
-
|
19
|
-
_ec2s[name][region] =
|
70
|
+
keys.each_pair do |region, key|
|
71
|
+
client = Aws::EC2::Client.new region: region, credentials: key
|
72
|
+
_ec2s[name][region] = Aws::EC2::Resource.new client: client
|
20
73
|
end
|
21
74
|
end
|
22
75
|
_ec2s
|
@@ -27,17 +80,18 @@ module Ec2ssh
|
|
27
80
|
end
|
28
81
|
|
29
82
|
def instances(key_name)
|
30
|
-
|
31
|
-
ec2s[key_name][region].instances
|
32
|
-
|
33
|
-
|
34
|
-
|
83
|
+
aws_keys[key_name].each_key.map {|region|
|
84
|
+
ec2s[key_name][region].instances(
|
85
|
+
filters: @filters
|
86
|
+
).
|
87
|
+
map {|ins| InstanceWrapper.new(ins) }.
|
88
|
+
sort_by {|ins| ins.tag('Name').to_s }
|
35
89
|
}.flatten
|
36
90
|
end
|
37
91
|
|
38
|
-
def self.expand_profile_name_to_credential(profile_name)
|
39
|
-
|
40
|
-
|
92
|
+
def self.expand_profile_name_to_credential(profile_name, region)
|
93
|
+
client = Aws::STS::Client.new(profile: profile_name, region: region)
|
94
|
+
client.config.credentials
|
41
95
|
end
|
42
96
|
end
|
43
97
|
end
|
data/lib/ec2ssh/version.rb
CHANGED