ec2-host 0.4.2 → 0.5.0

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: 39e4a2fac919c9e5cf6b23be541b7da53956f896
4
- data.tar.gz: 263d3173a1b304d5485fbff857d82be36568f2b3
3
+ metadata.gz: dc628fa546f382774069143d6c6e5469091985f6
4
+ data.tar.gz: ab4e0f46e19adf37a1cdd7079818f3e6f78c556c
5
5
  SHA512:
6
- metadata.gz: 0600f9fe0186ca52f37895f19cdeb9847bcaca97d9248e19fc67c60ef909c651feed5beb34ea4065c6d20f718d4de494d13315d330c98dd0a8b0ff1ce82678d8
7
- data.tar.gz: 493a74c50c86e98bf2ce0cd601f677e4ca0b83729b6ba2f9dd74982a9822bca9c95deb0e095f9fcb5e628fec4d06088281cca590bd9389b8490cd10ecf78356f
6
+ metadata.gz: e5cdc6c9ac53423f828ccc5e0e62049b291e1506ae39e1761943839a8aa911d17cb282314b6d58e0372d577e63eea823279d3b498d48707076d1a4ef6e88de71
7
+ data.tar.gz: 6fc6ff602bee4177f7843b308b7b32b00c526c098496576b1c1a2e8ebcc98d829598a58cb6685f525c17aea8dffcbb0dadc5acd800ef131615d2b9a271903011
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.5.0 (2016/12/01)
2
+
3
+ Enhancements:
4
+
5
+ * Support role levels more than 3 by ROLE_MAX_DEPTH configuration
6
+
1
7
  # 0.4.2 (2016/11/28)
2
8
 
3
9
  Enhancements:
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, the 1st part of role delimited by :
119
- --r2, --role2 one,two,three role2, the 2st part of role delimited by :
120
- --r3, --role3 one,two,three role3, the 3st part of role delimited by :
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
@@ -7,4 +7,5 @@ ROLE_TAG_DELIMITER=:
7
7
  OPTIONAL_STRING_TAGS=Service,Status
8
8
  OPTIONAL_ARRAY_TAGS=Tags
9
9
  ARRAY_TAG_DELIMITER=,
10
+ ROLE_MAX_DEPTH=5
10
11
  LOG_LEVEL=info
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
- op.on('--r1', '--role1 one,two,three', Array, "role1, the 1st part of role delimited by #{Config.role_tag_delimiter}") {|v|
35
- opts[:role1] = v
36
- }
37
- op.on('--r2', '--role2 one,two,three', Array, "role2, the 2st part of role delimited by #{Config.role_tag_delimiter}") {|v|
38
- opts[:role2] = v
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, :role1, :role2, :role3, :instance_id, :state, :monitoring].each do |key|
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
@@ -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
@@ -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
- elsif role1 = (condition[:role1] || condition[:usage1]) and role1.size == 1
49
- [{name: "tag:#{Config.roles_tag}", values: ["*#{role1.first}*"]}]
50
- elsif role2 = (condition[:role2] || condition[:usage2]) and role2.size == 1
51
- [{name: "tag:#{Config.roles_tag}", values: ["*#{role2.first}*"]}]
52
- elsif role3 = (condition[:role3] || condition[:usage3]) and role3.size == 1
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
@@ -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
- role1, role2, role3 = role.first.split(':')
183
+ parts = role.first.split(Config.role_tag_delimiter, Config.role_max_depth)
184
184
  else
185
- role1 = (condition[:role1] || condition[:usage1] || []).first
186
- role2 = (condition[:role2] || condition[:usage2] || []).first
187
- role3 = (condition[:role3] || condition[:usage3] || []).first
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 role1
190
- return false unless roles.find {|role| role.match?(role1, role2, role3) }
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
- condition = HashUtil.except(condition, :role, :role1, :role2, :role3, :usage, :usage1, :usage2, :usage3)
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)
@@ -2,40 +2,43 @@ class EC2
2
2
  class Host
3
3
  # Represents each role
4
4
  class RoleData
5
- attr_reader :role1, :role2, :role3
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
- role1, role2, role3 = role.split(Config.role_tag_delimiter, 3)
15
- new(role1, role2, role3)
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 ||= [role1, role2, role3].compact.reject(&:empty?).join(Config.role_tag_delimiter)
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
- uppers = [RoleData.new(role1)]
27
- uppers << RoleData.new(role1, role2) if role2 and !role2.empty?
28
- uppers << RoleData.new(role1, role2, role3) if role3 and !role3.empty?
29
- uppers
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?(role1, role2 = nil, role3 = nil)
33
- if role3
34
- role1 == self.role1 and role2 == self.role2 and role3 == self.role3
35
- elsif role2
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
 
@@ -1,5 +1,5 @@
1
1
  class EC2
2
2
  class Host
3
- VERSION = '0.4.2'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
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.2
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-28 00:00:00.000000000 Z
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: