ec2-host 0.4.2 → 0.5.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/CHANGELOG.md +6 -0
- data/README.md +4 -3
- data/example/example.conf +1 -0
- data/lib/ec2/host/cli.rb +9 -10
- data/lib/ec2/host/config.rb +4 -0
- data/lib/ec2/host/ec2_client.rb +7 -9
- data/lib/ec2/host/host_data.rb +10 -7
- data/lib/ec2/host/role_data.rb +23 -20
- data/lib/ec2/host/version.rb +1 -1
- data/spec/host_spec.rb +2 -1
- data/spec/role_data_spec.rb +40 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc628fa546f382774069143d6c6e5469091985f6
|
4
|
+
data.tar.gz: ab4e0f46e19adf37a1cdd7079818f3e6f78c556c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5cdc6c9ac53423f828ccc5e0e62049b291e1506ae39e1761943839a8aa911d17cb282314b6d58e0372d577e63eea823279d3b498d48707076d1a4ef6e88de71
|
7
|
+
data.tar.gz: 6fc6ff602bee4177f7843b308b7b32b00c526c098496576b1c1a2e8ebcc98d829598a58cb6685f525c17aea8dffcbb0dadc5acd800ef131615d2b9a271903011
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -38,6 +38,7 @@ ec2-host parameters:
|
|
38
38
|
* **OPTIONAL_STRING_TAGS (optional)**: You may add optional non-array tags. You can specify multiple tags like `Service,Status`.
|
39
39
|
* **OPTIONAL_ARRAY_TAGS (optional)**: You may add optional array tags. Array tags allows multiple values delimited by `ARRAY_TAG_DELIMITER` (default: `,`)
|
40
40
|
* **ARRAY_TAG_DELIMITER (optional)**: A delimiter to express array. Default is `,`
|
41
|
+
* **ROLE_MAX_DEPTH (optional)**: The maximum depth of levels of roles. Default is 3.
|
41
42
|
* **LOG_LEVEL (optional)**: Log level such as `info`, `debug`, `error`. The default is `info`.
|
42
43
|
|
43
44
|
See [example.conf](./example/example.conf)
|
@@ -115,9 +116,9 @@ $ bin/ec2-host --help
|
|
115
116
|
Usage: ec2-host [options]
|
116
117
|
--hostname one,two,three name or private_dns_name
|
117
118
|
-r, --role one,two,three role
|
118
|
-
--r1, --role1 one,two,three role1,
|
119
|
-
--r2, --role2 one,two,three role2,
|
120
|
-
--r3, --role3 one,two,three role3,
|
119
|
+
--r1, --role1 one,two,three role1, 1th part of role delimited by :
|
120
|
+
--r2, --role2 one,two,three role2, 2th part of role delimited by :
|
121
|
+
--r3, --role3 one,two,three role3, 3th part of role delimited by :
|
121
122
|
--instance-id one,two,three instance_id
|
122
123
|
--state one,two,three filter with instance state (default: running)
|
123
124
|
--monitoring one,two,three filter with instance monitoring
|
data/example/example.conf
CHANGED
data/lib/ec2/host/cli.rb
CHANGED
@@ -31,15 +31,11 @@ class EC2
|
|
31
31
|
op.on('-r', '--role one,two,three', Array, "role") {|v|
|
32
32
|
opts[:role] = v
|
33
33
|
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
}
|
40
|
-
op.on('--r3', '--role3 one,two,three', Array, "role3, the 3st part of role delimited by #{Config.role_tag_delimiter}") {|v|
|
41
|
-
opts[:role3] = v
|
42
|
-
}
|
34
|
+
1.upto(Config.role_max_depth).each do |i|
|
35
|
+
op.on("--r#{i}", "--role#{i} one,two,three", Array, "role#{i}, #{i}th part of role delimited by #{Config.role_tag_delimiter}") {|v|
|
36
|
+
opts["role#{i}".to_sym] = v
|
37
|
+
}
|
38
|
+
end
|
43
39
|
op.on('--instance-id one,two,three', Array, "instance_id") {|v|
|
44
40
|
opts[:instance_id] = v
|
45
41
|
}
|
@@ -55,9 +51,12 @@ class EC2
|
|
55
51
|
}
|
56
52
|
end
|
57
53
|
op.on('-a', '--all', "list all hosts (remove default filter)") {|v|
|
58
|
-
[:hostname, :role, :
|
54
|
+
[:hostname, :role, :instance_id, :state, :monitoring].each do |key|
|
59
55
|
opts.delete(key)
|
60
56
|
end
|
57
|
+
1.upto(Config.role_max_depth).each do |i|
|
58
|
+
opts.delete("role#{i}".to_sym)
|
59
|
+
end
|
61
60
|
Config.optional_options.each do |opt, tag|
|
62
61
|
opts.delete(opt.to_sym)
|
63
62
|
end
|
data/lib/ec2/host/config.rb
CHANGED
@@ -104,6 +104,10 @@ class EC2
|
|
104
104
|
@array_tag_delimiter ||= ENV['ARRAY_TAG_DELIMITER'] || config.fetch('ARRAY_TAG_DELIMITER', ',')
|
105
105
|
end
|
106
106
|
|
107
|
+
def self.role_max_depth
|
108
|
+
@role_max_depth ||= Integer(ENV['ROLE_MAX_DEPTH'] || config.fetch('ROLE_MAX_DEPTH', 3))
|
109
|
+
end
|
110
|
+
|
107
111
|
# private
|
108
112
|
|
109
113
|
def self.optional_array_options
|
data/lib/ec2/host/ec2_client.rb
CHANGED
@@ -44,16 +44,14 @@ class EC2
|
|
44
44
|
|
45
45
|
def build_filters(condition)
|
46
46
|
if role = (condition[:role] || condition[:usage]) and role.size == 1
|
47
|
-
[{name: "tag:#{Config.roles_tag}", values: ["*#{role.first}*"]}]
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
[{name: "tag:#{Config.roles_tag}", values: ["*#{role3.first}*"]}]
|
54
|
-
else
|
55
|
-
nil
|
47
|
+
return [{name: "tag:#{Config.roles_tag}", values: ["*#{role.first}*"]}]
|
48
|
+
end
|
49
|
+
1.upto(Config.role_max_depth).each do |i|
|
50
|
+
if role = (condition["role#{i}".to_sym] || condition["usage#{i}".to_sym]) and role.size == 1
|
51
|
+
return [{name: "tag:#{Config.roles_tag}", values: ["*#{role.first}*"]}]
|
52
|
+
end
|
56
53
|
end
|
54
|
+
nil
|
57
55
|
end
|
58
56
|
end
|
59
57
|
end
|
data/lib/ec2/host/host_data.rb
CHANGED
@@ -180,20 +180,23 @@ class EC2
|
|
180
180
|
def role_match?(condition)
|
181
181
|
# usage is an alias of role
|
182
182
|
if role = (condition[:role] || condition[:usage])
|
183
|
-
|
183
|
+
parts = role.first.split(Config.role_tag_delimiter, Config.role_max_depth)
|
184
184
|
else
|
185
|
-
|
186
|
-
|
187
|
-
|
185
|
+
parts = 1.upto(Config.role_max_depth).map do |i|
|
186
|
+
(condition["role#{i}".to_sym] || condition["usage#{i}".to_sym] || []).first
|
187
|
+
end
|
188
188
|
end
|
189
|
-
if
|
190
|
-
return false unless roles.find {|role| role.match?(
|
189
|
+
if parts.first
|
190
|
+
return false unless roles.find {|role| role.match?(*parts) }
|
191
191
|
end
|
192
192
|
true
|
193
193
|
end
|
194
194
|
|
195
195
|
def instance_match?(condition)
|
196
|
-
|
196
|
+
excepts = [:role, :usage]
|
197
|
+
1.upto(Config.role_max_depth).each {|i| excepts << "role#{i}".to_sym }
|
198
|
+
1.upto(Config.role_max_depth).each {|i| excepts << "usage#{i}".to_sym }
|
199
|
+
condition = HashUtil.except(condition, *excepts)
|
197
200
|
condition.each do |key, values|
|
198
201
|
v = instance_variable_recursive_get(key)
|
199
202
|
if v.is_a?(Array)
|
data/lib/ec2/host/role_data.rb
CHANGED
@@ -2,40 +2,43 @@ class EC2
|
|
2
2
|
class Host
|
3
3
|
# Represents each role
|
4
4
|
class RoleData
|
5
|
-
|
6
|
-
|
7
|
-
def initialize(role1, role2 = nil, role3 = nil)
|
8
|
-
@role1 = role1
|
9
|
-
@role2 = role2
|
10
|
-
@role3 = role3
|
5
|
+
def initialize(*parts)
|
6
|
+
@parts = parts
|
11
7
|
end
|
12
8
|
|
13
9
|
def self.build(role)
|
14
|
-
|
15
|
-
new(
|
10
|
+
parts = role.split(Config.role_tag_delimiter, Config.role_max_depth)
|
11
|
+
new(*parts)
|
16
12
|
end
|
17
13
|
|
18
14
|
# @return [String] something like "admin:jenkins:slave"
|
19
15
|
def role
|
20
|
-
@role ||=
|
16
|
+
@role ||= @parts.compact.reject(&:empty?).join(Config.role_tag_delimiter)
|
21
17
|
end
|
22
18
|
alias :to_s :role
|
23
19
|
|
20
|
+
1.upto(Config.role_max_depth).each do |i|
|
21
|
+
define_method("role#{i}") do
|
22
|
+
@parts[i-1]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
24
26
|
# @return [Array] something like ["admin", "admin:jenkins", "admin:jenkins:slave"]
|
25
27
|
def uppers
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
parts = @parts.dup
|
29
|
+
upper_parts = []
|
30
|
+
upper_parts << [parts.shift]
|
31
|
+
parts.each do |part|
|
32
|
+
break if part.nil? or part.empty?
|
33
|
+
upper_parts << [*(upper_parts.last), part]
|
34
|
+
end
|
35
|
+
upper_parts.map {|parts| RoleData.new(*parts) }
|
30
36
|
end
|
31
37
|
|
32
|
-
def match?(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
role1 == self.role1 and role2 == self.role2
|
37
|
-
else
|
38
|
-
role1 == self.role1
|
38
|
+
def match?(*parts)
|
39
|
+
(Config.role_max_depth-1).downto(0).each do |i|
|
40
|
+
next unless parts[i]
|
41
|
+
return @parts[0..i] == parts[0..i]
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
data/lib/ec2/host/version.rb
CHANGED
data/spec/host_spec.rb
CHANGED
@@ -46,11 +46,12 @@ describe EC2::Host do
|
|
46
46
|
expect(subject.keys).to eq([
|
47
47
|
'hostname',
|
48
48
|
'roles',
|
49
|
-
'region',
|
50
49
|
'service',
|
51
50
|
'status',
|
52
51
|
'tags',
|
52
|
+
'region',
|
53
53
|
'instance_id',
|
54
|
+
'instance_type',
|
54
55
|
'private_ip_address',
|
55
56
|
'public_ip_address',
|
56
57
|
'launch_time',
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EC2::Host::RoleData do
|
4
|
+
describe 'initialize' do
|
5
|
+
let(:subject) { EC2::Host::RoleData.new('web', 'test') }
|
6
|
+
it do
|
7
|
+
expect(subject.role1).to eq('web')
|
8
|
+
expect(subject.role2).to eq('test')
|
9
|
+
expect(subject.role3).to be_nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#build' do
|
14
|
+
let(:subject) { EC2::Host::RoleData.build('web:test') }
|
15
|
+
it do
|
16
|
+
expect(subject.role1).to eq('web')
|
17
|
+
expect(subject.role2).to eq('test')
|
18
|
+
expect(subject.role3).to be_nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#uppers' do
|
23
|
+
let(:subject) { EC2::Host::RoleData.build('web:test').uppers }
|
24
|
+
it do
|
25
|
+
expect(subject[0]).to eq('web')
|
26
|
+
expect(subject[1]).to eq('web:test')
|
27
|
+
expect(subject[2]).to be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#match?' do
|
32
|
+
let(:subject) { EC2::Host::RoleData.build('web:test') }
|
33
|
+
it do
|
34
|
+
expect(subject.match?('web')).to be_truthy
|
35
|
+
expect(subject.match?('web', 'test')).to be_truthy
|
36
|
+
expect(subject.match?('web', 'test', 'wrong')).to be_falsey
|
37
|
+
expect(subject.match?('web', 'wrong')).to be_falsey
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2-host
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
@@ -219,6 +219,7 @@ files:
|
|
219
219
|
- lib/ec2/host/string_util.rb
|
220
220
|
- lib/ec2/host/version.rb
|
221
221
|
- spec/host_spec.rb
|
222
|
+
- spec/role_data_spec.rb
|
222
223
|
- spec/spec_helper.rb
|
223
224
|
- terraform.tf
|
224
225
|
homepage: https://github.com/sonots/ec2-host
|
@@ -247,5 +248,6 @@ specification_version: 4
|
|
247
248
|
summary: Search hosts on AWS EC2
|
248
249
|
test_files:
|
249
250
|
- spec/host_spec.rb
|
251
|
+
- spec/role_data_spec.rb
|
250
252
|
- spec/spec_helper.rb
|
251
253
|
has_rdoc:
|