awsm 0.2.0 → 0.2.1

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: b2a776b8b7a7da2f58130c059143503dd7ef9c91
4
- data.tar.gz: fce86df5336b0ee0db8fa886de9d568856b3b224
3
+ metadata.gz: 8a54968644f5d7afd9ae2b0a7162accca5643c6d
4
+ data.tar.gz: a2289e945afeddc75ddd214fa6c031035fa46ab4
5
5
  SHA512:
6
- metadata.gz: 9059fd4236a6833e02183dd59de47ea9ef892136beb5c2b81c5cd9261e2456c1979b8c03e95428fa94514fb2174107a5f05620d6cfe3859d51a0083887db5b1f
7
- data.tar.gz: 64113ba02491f7f85ecacecffc3313d8e9d77907eae9215c1e8c74e8f0faa1203f17397ed8e7d1a665b00dae406e142243583e1251490da58d8aeb92d2b95186
6
+ metadata.gz: 357ad2bf4b9844a660896a65c90e500a4d567209118aee454ee86675453f65f65941dda55d96c39e5ec6ee0af02d837a9c366958c2963e33e87df1f6e9d45ff4
7
+ data.tar.gz: f357d77031f24201e4040a2ff50a6fe4d070cb1f9c663be91ffef94c5667251df5d0e91869336452b1282149baa46420b89db001d34c13cf15e7acff7c3a1b4c
@@ -2,13 +2,27 @@ module Awsm
2
2
  module CLI
3
3
  class Tag < Clibase
4
4
 
5
- desc 'find tag=value [tag2=value2] [...]',
6
- "Find resources by tags. Resources must have all tag key value pairs specified."
5
+ desc 'find tag=value [tag=] [=value] [...]',
6
+ "Find instances by tags."
7
7
  def find( *args )
8
8
  Table::Instance.new( filter_instances( argsToFilters( args ) ) ).print
9
9
  end
10
10
 
11
+ desc 'list [resource_id]',
12
+ "List tags for resource."
13
+ def list( resource_id )
14
+ print_tags( case resource_id
15
+ when /^i-[0-9a-f]+/
16
+ filter_instances( [ { name: 'instance-id', values: [ resource_id ] } ] ).first.tags
17
+ when /^ami-[0-9a-f]+/
18
+ filter_images( [ { name: 'image-id', values: [ resource_id ] } ] ).first.tags
19
+ else
20
+ raise StandardError, "Unknown resource id format: #{resource_id}"
21
+ end )
22
+ end
23
+
11
24
  no_commands do
25
+
12
26
  def argsToFilters( args )
13
27
  tags = {}
14
28
  filters = args.map do |arg|
@@ -37,6 +51,13 @@ module Awsm
37
51
 
38
52
  filters
39
53
  end
54
+
55
+ def print_tags( tags )
56
+ tags.each do |t|
57
+ say "#{t.key} => #{t.value}"
58
+ end
59
+ end
60
+
40
61
  end
41
62
 
42
63
  end
@@ -26,6 +26,14 @@ module Awsm
26
26
  instances
27
27
  end
28
28
 
29
+ def filter_images( filters )
30
+ images = []
31
+ ec2.describe_images( filters: filters ).images.each do |i|
32
+ images << i
33
+ end
34
+ images
35
+ end
36
+
29
37
  end
30
38
  end
31
39
  end
@@ -13,6 +13,38 @@ module Awsm
13
13
  @@c.dns
14
14
  end
15
15
 
16
+ def self.instance_table_config
17
+ @@c.instance_table
18
+ end
19
+
20
+ class InstanceTablutron
21
+
22
+ def initialize
23
+ @use_fields = []
24
+ @fields = {}
25
+ end
26
+
27
+ def use_fields( fields=nil )
28
+ if fields.nil?
29
+ return @use_fields
30
+ end
31
+
32
+ @use_fields = fields
33
+ end
34
+
35
+ def add_field( name, heading, &block )
36
+ @fields[ name ] = {
37
+ heading: heading,
38
+ block: block
39
+ }
40
+ end
41
+
42
+ def fields
43
+ return @fields
44
+ end
45
+
46
+ end
47
+
16
48
  class Configulator
17
49
 
18
50
  def initialize
@@ -20,6 +52,15 @@ module Awsm
20
52
  @dns_block = nil
21
53
  end
22
54
 
55
+ def instance_table
56
+ if block_given?
57
+ @instance_tablutron = InstanceTablutron.new
58
+ yield @instance_tablutron
59
+ else
60
+ @instance_tablutron
61
+ end
62
+ end
63
+
23
64
  def dns( &block )
24
65
  if block_given?
25
66
  @dns_block = block
@@ -2,11 +2,41 @@ module Awsm
2
2
  module Table
3
3
  class Instance
4
4
 
5
- def initialize( instances, fields = [ :instance_id, :name, :state, :image_id, :launch_time, :private_ip ] )
6
- @fields = fields
5
+ def initialize( instances, fields=nil )
6
+ @use_fields = if fields.nil?
7
+ Awsm::instance_table_config.use_fields
8
+ else
9
+ fields
10
+ end
11
+
12
+ @headings = {
13
+ instance_id: 'Instance ID',
14
+ name: 'Name',
15
+ state: 'State',
16
+ image_id: 'Image ID',
17
+ launch_time: 'Launch Time',
18
+ private_ip: 'Private IP',
19
+ awsm_owner: 'Owner'
20
+ }
21
+
22
+ @fields = {
23
+ instance_id: -> (i) { i.instance_id },
24
+ name: -> (i) { tag( 'Name', i.tags ).first },
25
+ state: -> (i) { i.state.name },
26
+ image_id: -> (i) { i.image_id },
27
+ launch_time: -> (i) { i.launch_time },
28
+ private_ip: -> (i) { i.private_ip_address },
29
+ awsm_owner: -> (i) { tag( 'awsm:owner', i.tags ).first }
30
+ }
31
+
32
+ Awsm::instance_table_config.fields.each do |name, field|
33
+ @headings[ name ] = field[:heading]
34
+ @fields[ name ] = field[:block]
35
+ end
36
+
7
37
  @rows = instances.map do |i|
8
38
  row = []
9
- @fields.each do |f|
39
+ @use_fields.each do |f|
10
40
  row << extract_field( i, f )
11
41
  end
12
42
  row
@@ -15,7 +45,7 @@ module Awsm
15
45
 
16
46
  def print
17
47
  puts Terminal::Table.new(
18
- headings: @fields.map { |f| heading( f ) },
48
+ headings: @use_fields.map { |f| @headings[ f ] },
19
49
  rows: @rows
20
50
  )
21
51
  end
@@ -23,41 +53,7 @@ module Awsm
23
53
  private
24
54
 
25
55
  def extract_field( instance, field )
26
- case field
27
- when :instance_id
28
- instance.instance_id
29
- when :name
30
- tag( 'Name', instance.tags ).first
31
- when :state
32
- instance.state.name
33
- when :image_id
34
- instance.image_id
35
- when :launch_time
36
- instance.launch_time
37
- when :private_ip
38
- if instance.state.name == "running"
39
- instance.private_ip_address
40
- else
41
- 'N/A'
42
- end
43
- when :awsm_owner
44
- tag( 'awsm:owner', instance.tags ).first
45
- else
46
- raise StandardError "Unknown field"
47
- end
48
- end
49
-
50
- def heading( field )
51
- headings = {
52
- instance_id: 'Instance ID',
53
- name: 'Name',
54
- state: 'State',
55
- image_id: 'Image ID',
56
- launch_time: 'Launch Time',
57
- private_ip: 'Private IP',
58
- awsm_owner: 'Owner'
59
- }
60
- headings[ field ]
56
+ @fields[ field ].call( instance )
61
57
  end
62
58
 
63
59
  def tag( key, tags )
@@ -1,3 +1,3 @@
1
1
  module Awsm
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Kendell