ec2-host 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -350,7 +350,7 @@
350
350
  </div>
351
351
 
352
352
  <div id="footer">
353
- Generated on Thu Nov 24 10:25:36 2016 by
353
+ Generated on Fri Dec 2 11:53:59 2016 by
354
354
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
355
355
  0.8.7.6 (ruby-2.3.2).
356
356
  </div>
data/docs/_index.html CHANGED
@@ -192,7 +192,7 @@
192
192
  </div>
193
193
 
194
194
  <div id="footer">
195
- Generated on Thu Nov 24 10:25:35 2016 by
195
+ Generated on Fri Dec 2 11:53:58 2016 by
196
196
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
197
197
  0.8.7.6 (ruby-2.3.2).
198
198
  </div>
@@ -86,7 +86,7 @@ DEALINGS IN THE SOFTWARE.</p>
86
86
  </div></div>
87
87
 
88
88
  <div id="footer">
89
- Generated on Thu Nov 24 10:25:36 2016 by
89
+ Generated on Fri Dec 2 11:53:59 2016 by
90
90
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
91
91
  0.8.7.6 (ruby-2.3.2).
92
92
  </div>
@@ -85,9 +85,9 @@ using thease tags with this gem.</p>
85
85
  <h2 id="label-Configuration">Configuration</h2>
86
86
 
87
87
  <p>You can write a configuration file located at
88
- <code>/etc/sysconfig/ec2-host</code> (You can configure this path by
89
- <code>EC2_HOST_CONFIG_FILE</code> environment variable), or as environment
90
- variables:</p>
88
+ <code>/etc/sysconfig/ec2-host</code> or <code>/etc/default/ec2-host</code>
89
+ (You can configure this path by <code>EC2_HOST_CONFIG_FILE</code>
90
+ environment variable), or as environment variables:</p>
91
91
 
92
92
  <p>AWS SDK (CLI) parameters:</p>
93
93
  <ul><li>
@@ -98,7 +98,9 @@ loads a credentials file</p>
98
98
  load a credentials file</p>
99
99
  </li><li>
100
100
  <p><strong>AWS_DEFAULT_REGION</strong> (<strong>AWS_REGION</strong>); AWS SDK
101
- (CLI) region such as <code>ap-northeast-1</code>, <code>us-east-1</code>.</p>
101
+ (CLI) region such as <code>ap-northeast-1</code>, <code>us-east-1</code>.
102
+ Default obtains from <code>$HOME/.aws/config</code> with profile
103
+ <code>AWS_DEFAULT_PROFILE</code>.</p>
102
104
  </li><li>
103
105
  <p><strong>AWS_DEFAULT_PROFILE</strong> (<strong>AWS_PROFILE</strong>): The
104
106
  profile key of the AWS SDK (CLI) credentails file. Default is
@@ -143,6 +145,9 @@ tags. Array tags allows multiple values delimited by
143
145
  <p><strong>ARRAY_TAG_DELIMITER (optional)</strong>: A delimiter to express
144
146
  array. Default is <code>,</code></p>
145
147
  </li><li>
148
+ <p><strong>ROLE_MAX_DEPTH (optional)</strong>: The maximum depth of levels of
149
+ roles. Default is 3.</p>
150
+ </li><li>
146
151
  <p><strong>LOG_LEVEL (optional)</strong>: Log level such as <code>info</code>,
147
152
  <code>debug</code>, <code>error</code>. The default is <code>info</code>.</p>
148
153
  </li></ul>
@@ -215,9 +220,9 @@ test</code></pre>
215
220
  Usage: ec2-host [options]
216
221
  --hostname one,two,three name or private_dns_name
217
222
  -r, --role one,two,three role
218
- --r1, --role1 one,two,three role1, the 1st part of role delimited by :
219
- --r2, --role2 one,two,three role2, the 2st part of role delimited by :
220
- --r3, --role3 one,two,three role3, the 3st part of role delimited by :
223
+ --r1, --role1 one,two,three role1, 1th part of role delimited by :
224
+ --r2, --role2 one,two,three role2, 2th part of role delimited by :
225
+ --r3, --role3 one,two,three role3, 3th part of role delimited by :
221
226
  --instance-id one,two,three instance_id
222
227
  --state one,two,three filter with instance state (default: running)
223
228
  --monitoring one,two,three filter with instance monitoring
@@ -314,7 +319,7 @@ $ env ($cat .env) terraform apply</code></pre>
314
319
  </div></div>
315
320
 
316
321
  <div id="footer">
317
- Generated on Thu Nov 24 10:25:36 2016 by
322
+ Generated on Fri Dec 2 11:53:59 2016 by
318
323
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
319
324
  0.8.7.6 (ruby-2.3.2).
320
325
  </div>
data/docs/index.html CHANGED
@@ -85,9 +85,9 @@ using thease tags with this gem.</p>
85
85
  <h2 id="label-Configuration">Configuration</h2>
86
86
 
87
87
  <p>You can write a configuration file located at
88
- <code>/etc/sysconfig/ec2-host</code> (You can configure this path by
89
- <code>EC2_HOST_CONFIG_FILE</code> environment variable), or as environment
90
- variables:</p>
88
+ <code>/etc/sysconfig/ec2-host</code> or <code>/etc/default/ec2-host</code>
89
+ (You can configure this path by <code>EC2_HOST_CONFIG_FILE</code>
90
+ environment variable), or as environment variables:</p>
91
91
 
92
92
  <p>AWS SDK (CLI) parameters:</p>
93
93
  <ul><li>
@@ -98,7 +98,9 @@ loads a credentials file</p>
98
98
  load a credentials file</p>
99
99
  </li><li>
100
100
  <p><strong>AWS_DEFAULT_REGION</strong> (<strong>AWS_REGION</strong>); AWS SDK
101
- (CLI) region such as <code>ap-northeast-1</code>, <code>us-east-1</code>.</p>
101
+ (CLI) region such as <code>ap-northeast-1</code>, <code>us-east-1</code>.
102
+ Default obtains from <code>$HOME/.aws/config</code> with profile
103
+ <code>AWS_DEFAULT_PROFILE</code>.</p>
102
104
  </li><li>
103
105
  <p><strong>AWS_DEFAULT_PROFILE</strong> (<strong>AWS_PROFILE</strong>): The
104
106
  profile key of the AWS SDK (CLI) credentails file. Default is
@@ -143,6 +145,9 @@ tags. Array tags allows multiple values delimited by
143
145
  <p><strong>ARRAY_TAG_DELIMITER (optional)</strong>: A delimiter to express
144
146
  array. Default is <code>,</code></p>
145
147
  </li><li>
148
+ <p><strong>ROLE_MAX_DEPTH (optional)</strong>: The maximum depth of levels of
149
+ roles. Default is 3.</p>
150
+ </li><li>
146
151
  <p><strong>LOG_LEVEL (optional)</strong>: Log level such as <code>info</code>,
147
152
  <code>debug</code>, <code>error</code>. The default is <code>info</code>.</p>
148
153
  </li></ul>
@@ -215,9 +220,9 @@ test</code></pre>
215
220
  Usage: ec2-host [options]
216
221
  --hostname one,two,three name or private_dns_name
217
222
  -r, --role one,two,three role
218
- --r1, --role1 one,two,three role1, the 1st part of role delimited by :
219
- --r2, --role2 one,two,three role2, the 2st part of role delimited by :
220
- --r3, --role3 one,two,three role3, the 3st part of role delimited by :
223
+ --r1, --role1 one,two,three role1, 1th part of role delimited by :
224
+ --r2, --role2 one,two,three role2, 2th part of role delimited by :
225
+ --r3, --role3 one,two,three role3, 3th part of role delimited by :
221
226
  --instance-id one,two,three instance_id
222
227
  --state one,two,three filter with instance state (default: running)
223
228
  --monitoring one,two,three filter with instance monitoring
@@ -314,7 +319,7 @@ $ env ($cat .env) terraform apply</code></pre>
314
319
  </div></div>
315
320
 
316
321
  <div id="footer">
317
- Generated on Thu Nov 24 10:25:35 2016 by
322
+ Generated on Fri Dec 2 11:53:59 2016 by
318
323
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
319
324
  0.8.7.6 (ruby-2.3.2).
320
325
  </div>
@@ -69,6 +69,18 @@
69
69
  </li>
70
70
 
71
71
 
72
+ <li class="r2 ">
73
+ <span class='object_link'><a href="EC2/Host/Config.html#aws_config-class_method" title="EC2::Host::Config.aws_config (method)">aws_config</a></span>
74
+ <small>EC2::Host::Config</small>
75
+ </li>
76
+
77
+
78
+ <li class="r1 ">
79
+ <span class='object_link'><a href="EC2/Host/Config.html#aws_config_file-class_method" title="EC2::Host::Config.aws_config_file (method)">aws_config_file</a></span>
80
+ <small>EC2::Host::Config</small>
81
+ </li>
82
+
83
+
72
84
  <li class="r2 ">
73
85
  <span class='object_link'><a href="EC2/Host/Config.html#aws_credential_file-class_method" title="EC2::Host::Config.aws_credential_file (method)">aws_credential_file</a></span>
74
86
  <small>EC2::Host::Config</small>
@@ -178,38 +190,38 @@
178
190
 
179
191
 
180
192
  <li class="r2 ">
181
- <span class='object_link'><a href="EC2/Host.html#initialize-instance_method" title="EC2::Host#initialize (method)">#initialize</a></span>
182
- <small>EC2::Host</small>
193
+ <span class='object_link'><a href="EC2/Host/HostData.html#initialize-instance_method" title="EC2::Host::HostData#initialize (method)">#initialize</a></span>
194
+ <small>EC2::Host::HostData</small>
183
195
  </li>
184
196
 
185
197
 
186
198
  <li class="r1 ">
187
- <span class='object_link'><a href="EC2/Host/RoleData.html#initialize-instance_method" title="EC2::Host::RoleData#initialize (method)">#initialize</a></span>
188
- <small>EC2::Host::RoleData</small>
199
+ <span class='object_link'><a href="EC2/Host.html#initialize-instance_method" title="EC2::Host#initialize (method)">#initialize</a></span>
200
+ <small>EC2::Host</small>
189
201
  </li>
190
202
 
191
203
 
192
204
  <li class="r2 ">
193
- <span class='object_link'><a href="EC2/Host/HostData.html#initialize-instance_method" title="EC2::Host::HostData#initialize (method)">#initialize</a></span>
194
- <small>EC2::Host::HostData</small>
205
+ <span class='object_link'><a href="EC2/Host/CLI.html#initialize-instance_method" title="EC2::Host::CLI#initialize (method)">#initialize</a></span>
206
+ <small>EC2::Host::CLI</small>
195
207
  </li>
196
208
 
197
209
 
198
210
  <li class="r1 ">
199
- <span class='object_link'><a href="EC2/Host/CLI.html#initialize-instance_method" title="EC2::Host::CLI#initialize (method)">#initialize</a></span>
200
- <small>EC2::Host::CLI</small>
211
+ <span class='object_link'><a href="EC2/Host/RoleData.html#initialize-instance_method" title="EC2::Host::RoleData#initialize (method)">#initialize</a></span>
212
+ <small>EC2::Host::RoleData</small>
201
213
  </li>
202
214
 
203
215
 
204
216
  <li class="r2 ">
205
- <span class='object_link'><a href="EC2/Host/RoleData.html#inspect-instance_method" title="EC2::Host::RoleData#inspect (method)">#inspect</a></span>
206
- <small>EC2::Host::RoleData</small>
217
+ <span class='object_link'><a href="EC2/Host/HostData.html#inspect-instance_method" title="EC2::Host::HostData#inspect (method)">#inspect</a></span>
218
+ <small>EC2::Host::HostData</small>
207
219
  </li>
208
220
 
209
221
 
210
222
  <li class="r1 ">
211
- <span class='object_link'><a href="EC2/Host/HostData.html#inspect-instance_method" title="EC2::Host::HostData#inspect (method)">#inspect</a></span>
212
- <small>EC2::Host::HostData</small>
223
+ <span class='object_link'><a href="EC2/Host/RoleData.html#inspect-instance_method" title="EC2::Host::RoleData#inspect (method)">#inspect</a></span>
224
+ <small>EC2::Host::RoleData</small>
213
225
  </li>
214
226
 
215
227
 
@@ -262,14 +274,14 @@
262
274
 
263
275
 
264
276
  <li class="r2 ">
265
- <span class='object_link'><a href="EC2/Host/RoleData.html#match%3F-instance_method" title="EC2::Host::RoleData#match? (method)">#match?</a></span>
266
- <small>EC2::Host::RoleData</small>
277
+ <span class='object_link'><a href="EC2/Host/HostData.html#match%3F-instance_method" title="EC2::Host::HostData#match? (method)">#match?</a></span>
278
+ <small>EC2::Host::HostData</small>
267
279
  </li>
268
280
 
269
281
 
270
282
  <li class="r1 ">
271
- <span class='object_link'><a href="EC2/Host/HostData.html#match%3F-instance_method" title="EC2::Host::HostData#match? (method)">#match?</a></span>
272
- <small>EC2::Host::HostData</small>
283
+ <span class='object_link'><a href="EC2/Host/RoleData.html#match%3F-instance_method" title="EC2::Host::RoleData#match? (method)">#match?</a></span>
284
+ <small>EC2::Host::RoleData</small>
273
285
  </li>
274
286
 
275
287
 
@@ -316,14 +328,14 @@
316
328
 
317
329
 
318
330
  <li class="r1 ">
319
- <span class='object_link'><a href="EC2/Host.html#options-instance_method" title="EC2::Host#options (method)">#options</a></span>
320
- <small>EC2::Host</small>
331
+ <span class='object_link'><a href="EC2/Host/CLI.html#options-instance_method" title="EC2::Host::CLI#options (method)">#options</a></span>
332
+ <small>EC2::Host::CLI</small>
321
333
  </li>
322
334
 
323
335
 
324
336
  <li class="r2 ">
325
- <span class='object_link'><a href="EC2/Host/CLI.html#options-instance_method" title="EC2::Host::CLI#options (method)">#options</a></span>
326
- <small>EC2::Host::CLI</small>
337
+ <span class='object_link'><a href="EC2/Host.html#options-instance_method" title="EC2::Host#options (method)">#options</a></span>
338
+ <small>EC2::Host</small>
327
339
  </li>
328
340
 
329
341
 
@@ -370,20 +382,8 @@
370
382
 
371
383
 
372
384
  <li class="r2 ">
373
- <span class='object_link'><a href="EC2/Host/RoleData.html#role1-instance_method" title="EC2::Host::RoleData#role1 (method)">#role1</a></span>
374
- <small>EC2::Host::RoleData</small>
375
- </li>
376
-
377
-
378
- <li class="r1 ">
379
- <span class='object_link'><a href="EC2/Host/RoleData.html#role2-instance_method" title="EC2::Host::RoleData#role2 (method)">#role2</a></span>
380
- <small>EC2::Host::RoleData</small>
381
- </li>
382
-
383
-
384
- <li class="r2 ">
385
- <span class='object_link'><a href="EC2/Host/RoleData.html#role3-instance_method" title="EC2::Host::RoleData#role3 (method)">#role3</a></span>
386
- <small>EC2::Host::RoleData</small>
385
+ <span class='object_link'><a href="EC2/Host/Config.html#role_max_depth-class_method" title="EC2::Host::Config.role_max_depth (method)">role_max_depth</a></span>
386
+ <small>EC2::Host::Config</small>
387
387
  </li>
388
388
 
389
389
 
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Thu Nov 24 10:25:36 2016 by
106
+ Generated on Fri Dec 2 11:53:59 2016 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.7.6 (ruby-2.3.2).
109
109
  </div>
@@ -186,10 +186,7 @@ class EC2
186
186
  (condition["role#{i}".to_sym] || condition["usage#{i}".to_sym] || []).first
187
187
  end
188
188
  end
189
- if parts.first
190
- return false unless roles.find {|role| role.match?(*parts) }
191
- end
192
- true
189
+ roles.find {|role| role.match?(*parts) }
193
190
  end
194
191
 
195
192
  def instance_match?(condition)
@@ -2,50 +2,68 @@ class EC2
2
2
  class Host
3
3
  # Represents each role
4
4
  class RoleData
5
- def initialize(*parts)
6
- @parts = parts
5
+ # Initialize role data with role parts
6
+ #
7
+ # RoleData.new('admin', 'jenkins', 'slave')
8
+ #
9
+ # @param [Array] role_parts such as ['admin', 'jenkins', 'slave']
10
+ def initialize(*role_parts)
11
+ @role_parts = role_parts
7
12
  end
8
13
 
14
+ # Create a role data with role delimiter by Config.role_tag_delimiter
15
+ #
16
+ # RoleData.build('admin:jenkins:slave')
17
+ #
18
+ # @param [String] role such as "admin:jenkins:slave"
9
19
  def self.build(role)
10
- parts = role.split(Config.role_tag_delimiter, Config.role_max_depth)
11
- new(*parts)
20
+ role_parts = role.split(Config.role_tag_delimiter, Config.role_max_depth)
21
+ new(*role_parts)
12
22
  end
13
23
 
14
24
  # @return [String] something like "admin:jenkins:slave"
15
25
  def role
16
- @role ||= @parts.compact.reject(&:empty?).join(Config.role_tag_delimiter)
26
+ @role ||= @role_parts.compact.reject(&:empty?).join(Config.role_tag_delimiter)
17
27
  end
18
28
  alias :to_s :role
19
29
 
20
30
  1.upto(Config.role_max_depth).each do |i|
21
31
  define_method("role#{i}") do
22
- @parts[i-1]
32
+ @role_parts[i-1]
23
33
  end
24
34
  end
25
35
 
26
36
  # @return [Array] something like ["admin", "admin:jenkins", "admin:jenkins:slave"]
27
37
  def 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]
38
+ role_parts = @role_parts.dup
39
+ upper_role_parts = []
40
+ upper_role_parts << [role_parts.shift]
41
+ role_parts.each do |role_part|
42
+ break if role_part.nil? or role_part.empty?
43
+ upper_role_parts << [*(upper_role_parts.last), role_part]
34
44
  end
35
- upper_parts.map {|parts| RoleData.new(*parts) }
45
+ upper_role_parts.map {|role_parts| RoleData.new(*role_parts) }
36
46
  end
37
47
 
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]
42
- end
48
+ # Check whether given role parts matches with this role data object
49
+ #
50
+ # RoleData.new('admin', 'jenkins', 'slave').match?('admin') #=> true
51
+ # RoleData.new('admin', 'jenkins', 'slave').match?('admin', 'jenkins') #=> true
52
+ # RoleData.new('admin', 'jenkins', 'slave').match?('admin', 'jenkins', 'slave') #=> true
53
+ # RoleData.new('admin', 'jenkins', 'slave').match?('admin', 'jenkins', 'master') #=> false
54
+ # RoleData.new('admin', 'jenkins', 'slave').match?(nil, 'jenkins') #=> true
55
+ # RoleData.new('admin', 'jenkins', 'slave').match?(nil, nil, 'slave') #=> true
56
+ #
57
+ # @param [Array] role_parts such as ["admin", "jenkins", "slave"]
58
+ def match?(*role_parts)
59
+ indexes = role_parts.map.with_index {|part, i| part ? i : nil }.compact
60
+ @role_parts.values_at(*indexes) == role_parts.values_at(*indexes)
43
61
  end
44
62
 
45
63
  # Equality
46
64
  #
47
- # Role::Data.new('admin') == Role::Data.new('admin') #=> true
48
- # Role::Data.new('admin', 'jenkin') == "admin:jenkins" #=> true
65
+ # RoleData.new('admin') == RoleData.new('admin') #=> true
66
+ # RoleData.new('admin', 'jenkin') == "admin:jenkins" #=> true
49
67
  #
50
68
  # @param [Object] other
51
69
  def ==(other)
@@ -1,5 +1,5 @@
1
1
  class EC2
2
2
  class Host
3
- VERSION = '0.5.0'
3
+ VERSION = '0.5.1'
4
4
  end
5
5
  end
data/spec/host_spec.rb CHANGED
@@ -87,31 +87,82 @@ describe EC2::Host do
87
87
 
88
88
  context 'by role' do
89
89
  context 'by a role' do
90
- let(:subject) { EC2::Host.new(role: 'web:test').first }
91
- it_should_behave_like 'host'
90
+ context 'hit' do
91
+ let(:subject) { EC2::Host.new(role: 'web:test').first }
92
+ it_should_behave_like 'host'
93
+ end
94
+
95
+ context 'miss' do
96
+ let(:subject) { EC2::Host.new(role: 'web:te').first }
97
+ it { expect(subject).to be_nil }
98
+ end
92
99
  end
93
100
 
94
101
  context 'by a role1' do
95
- let(:subject) { EC2::Host.new(role1: 'web').first }
96
- it_should_behave_like 'host'
102
+ context 'hit' do
103
+ let(:subject) { EC2::Host.new(role1: 'web').first }
104
+ it_should_behave_like 'host'
105
+ end
106
+
107
+ context 'miss' do
108
+ let(:subject) { EC2::Host.new(role1: 'w').first }
109
+ it { expect(subject).to be_nil }
110
+ end
111
+ end
112
+
113
+ context 'by a role2' do
114
+ context 'hit' do
115
+ let(:hosts) { EC2::Host.new(role2: 'test').to_a }
116
+ it { expect(hosts.size).to be >= 2 }
117
+ end
118
+
119
+ context 'miss' do
120
+ let(:hosts) { EC2::Host.new(role2: 'te').to_a }
121
+ it { expect(hosts.size).to eq(0) }
122
+ end
123
+ end
124
+
125
+ context 'by a role3' do
126
+ context 'miss' do
127
+ let(:hosts) { EC2::Host.new(role3: 'no_host_has_role3').to_a }
128
+ it { expect(hosts.size).to eq(0) }
129
+ end
97
130
  end
98
131
 
99
132
  context 'by multiple roles (or)' do
100
- let(:hosts) {
101
- EC2::Host.new(
102
- {
103
- role1: 'web',
104
- role2: 'test',
105
- },
106
- {
107
- role1: 'db',
108
- role2: 'test',
109
- },
110
- ).to_a
111
- }
112
- let(:subject) { hosts.first }
113
- it { expect(hosts.size).to be >= 2 }
114
- it_should_behave_like 'host'
133
+ context 'hit' do
134
+ let(:hosts) {
135
+ EC2::Host.new(
136
+ {
137
+ role1: 'web',
138
+ role2: 'test',
139
+ },
140
+ {
141
+ role1: 'db',
142
+ role2: 'test',
143
+ },
144
+ ).to_a
145
+ }
146
+ let(:subject) { hosts.first }
147
+ it { expect(hosts.size).to be >= 2 }
148
+ it_should_behave_like 'host'
149
+ end
150
+
151
+ context 'miss' do
152
+ let(:hosts) {
153
+ EC2::Host.new(
154
+ {
155
+ role1: 'web',
156
+ role2: 'te',
157
+ },
158
+ {
159
+ role1: 'db',
160
+ role2: 'te',
161
+ },
162
+ ).to_a
163
+ }
164
+ it { expect(hosts.size).to eq(0) }
165
+ end
115
166
  end
116
167
  end
117
168