awsm 0.1.2 → 0.2.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: 3ad7bcebefe552317579e717e09fce0b4f7ff68d
4
- data.tar.gz: ad146920d05f8b9b2cf39c59d6dad2c62a50f69e
3
+ metadata.gz: b2a776b8b7a7da2f58130c059143503dd7ef9c91
4
+ data.tar.gz: fce86df5336b0ee0db8fa886de9d568856b3b224
5
5
  SHA512:
6
- metadata.gz: b9623ec29662f0738616d4126b3b909fab771e8e4cfb2e371014b6ab52a1cd598573003e10366131ce534f90eb0d08e30acdee4fe689eb4f265a17a0f655a7ca
7
- data.tar.gz: e81cb3864051bf78f65661371fe377941ebce0722ead5f393c33576252b285785776deacc94ca10f058d645b37c1063a95eb31a24fc6cf9ae34b7e9fb19fa0b4
6
+ metadata.gz: 9059fd4236a6833e02183dd59de47ea9ef892136beb5c2b81c5cd9261e2456c1979b8c03e95428fa94514fb2174107a5f05620d6cfe3859d51a0083887db5b1f
7
+ data.tar.gz: 64113ba02491f7f85ecacecffc3313d8e9d77907eae9215c1e8c74e8f0faa1203f17397ed8e7d1a665b00dae406e142243583e1251490da58d8aeb92d2b95186
@@ -11,7 +11,10 @@ require 'awsm/autoscalinggroups'
11
11
  require 'awsm/dns'
12
12
  require 'awsm/instances'
13
13
 
14
+ require 'awsm/table/instance'
15
+
14
16
  require 'awsm/clibase'
17
+ require 'awsm/cli/tag'
15
18
  require 'awsm/cli/unused'
16
19
  require 'awsm/cli/spin'
17
20
  require 'awsm/cli/main'
@@ -16,6 +16,9 @@ module Awsm
16
16
  desc 'unused', 'Find unused resources'
17
17
  subcommand 'unused', Awsm::CLI::Unused
18
18
 
19
+ desc 'tag', 'Search by tags'
20
+ subcommand 'tag', Awsm::CLI::Tag
21
+
19
22
  desc "specific <comma-separated-elb-names>",
20
23
  "Only find specific ELBs named in a comma-separated list."
21
24
  def specific( elb_names )
@@ -38,21 +38,14 @@ module Awsm
38
38
  "Spin down the specified instance"
39
39
  def down( instance_id )
40
40
  response = ec2.describe_instances(
41
- instance_ids: [ instance_id ]
41
+ filters: [
42
+ { name: 'instance-id', values: [ instance_id ] },
43
+ { name: 'tag:awsm:owner', values: [ whoami ] }
44
+ ]
42
45
  )
43
46
 
44
- if response.reservations.first.instances.length == 0
45
- say "Instance #{instance_id} not found."
46
- return
47
- end
48
-
49
- instance = response.reservations.first.instances.first
50
- owner_tag = instance.tags.select do |t|
51
- t.key == 'awsm:owner' && t.value == whoami
52
- end
53
-
54
- if owner_tag.length == 0
55
- say "Instance #{instance_id} doesn't belong to you, leave it alone."
47
+ if response.reservations.length == 0
48
+ say "Instance #{instance_id} not spinning."
56
49
  return
57
50
  end
58
51
 
@@ -67,47 +60,15 @@ module Awsm
67
60
  option :simple, :type => :boolean, :default => false, :aliases => '-s',
68
61
  :desc => "Display list without prettiness - good for sedding"
69
62
  def list
70
- response = ec2.describe_instances(
71
- filters: [
72
- { name: 'tag-key', values: [ "awsm:owner" ] }
73
- ]
74
- )
75
- spinning = []
76
- response.reservations.each do |r|
77
- r.instances.each do |i|
78
- owner = i.tags.select { |t| t.key == 'awsm:owner' }.first.value
79
-
80
- if owner == whoami
81
- fields = [ i.instance_id, i.state.name, i.image_id, owner, i.launch_time ]
82
-
83
- if i.state.name == 'running'
84
- fields << i.private_ip_address
85
- else
86
- fields << 'N/A'
87
- end
88
-
89
- spinning << fields
90
- end
91
- end
92
- end
93
-
94
- if options[:simple]
95
- spinning.each do |row|
96
- say row.join(' ')
97
- end
98
- else
99
- puts_table(
100
- headings: [ 'Instance ID', 'State', 'AMI ID', 'Owner', 'Launched Time', 'Private IP' ],
101
- rows: spinning
102
- )
103
- end
63
+ instances = filter_instances( [
64
+ { name: 'tag:awsm:owner', values: [ whoami ] }
65
+ ] )
66
+ Table::Instance.new( instances, [
67
+ :instance_id, :state, :image_id, :awsm_owner, :launch_time, :private_ip
68
+ ] ).print
104
69
  end
105
70
 
106
71
  no_commands do
107
- def ec2
108
- Aws::EC2::Client.new
109
- end
110
-
111
72
  def whoami
112
73
  me_host = `hostname -f`.strip
113
74
  me_user = `whoami`.strip
@@ -0,0 +1,44 @@
1
+ module Awsm
2
+ module CLI
3
+ class Tag < Clibase
4
+
5
+ desc 'find tag=value [tag2=value2] [...]',
6
+ "Find resources by tags. Resources must have all tag key value pairs specified."
7
+ def find( *args )
8
+ Table::Instance.new( filter_instances( argsToFilters( args ) ) ).print
9
+ end
10
+
11
+ no_commands do
12
+ def argsToFilters( args )
13
+ tags = {}
14
+ filters = args.map do |arg|
15
+ key, value = arg.split( '=' )
16
+
17
+ if tags[ key ].nil?
18
+ tags[ key ] = []
19
+ end
20
+
21
+ tags[ key ] << value
22
+ end
23
+
24
+ filters = []
25
+ tags.each do |k, v|
26
+ v = v.select { |e| !e.nil? }.uniq
27
+ if k == ""
28
+ filters << { name: 'tag-value', values: v }
29
+ else
30
+ if v == []
31
+ filters << { name: 'tag-key', values: [ k ] }
32
+ else
33
+ filters << { name: "tag:#{k}", values: v }
34
+ end
35
+ end
36
+ end
37
+
38
+ filters
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -1,6 +1,7 @@
1
1
  module Awsm
2
2
  class Clibase < Thor
3
3
  no_commands do
4
+
4
5
  def puts_table( table )
5
6
  if options[:tables] == true
6
7
  puts Terminal::Table.new( table )
@@ -10,6 +11,21 @@ module Awsm
10
11
  end
11
12
  end
12
13
  end
14
+
15
+ def ec2
16
+ Aws::EC2::Client.new
17
+ end
18
+
19
+ def filter_instances( filters )
20
+ instances = []
21
+ ec2.describe_instances( filters: filters ).reservations.each do |r|
22
+ r.instances.each do |i|
23
+ instances << i
24
+ end
25
+ end
26
+ instances
27
+ end
28
+
13
29
  end
14
30
  end
15
31
  end
@@ -0,0 +1,70 @@
1
+ module Awsm
2
+ module Table
3
+ class Instance
4
+
5
+ def initialize( instances, fields = [ :instance_id, :name, :state, :image_id, :launch_time, :private_ip ] )
6
+ @fields = fields
7
+ @rows = instances.map do |i|
8
+ row = []
9
+ @fields.each do |f|
10
+ row << extract_field( i, f )
11
+ end
12
+ row
13
+ end
14
+ end
15
+
16
+ def print
17
+ puts Terminal::Table.new(
18
+ headings: @fields.map { |f| heading( f ) },
19
+ rows: @rows
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ 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 ]
61
+ end
62
+
63
+ def tag( key, tags )
64
+ tags.select { |t| t.key == key }
65
+ .map { |t| t.value }
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module Awsm
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Kendell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -102,12 +102,14 @@ files:
102
102
  - lib/awsm/autoscalinggroups.rb
103
103
  - lib/awsm/cli/main.rb
104
104
  - lib/awsm/cli/spin.rb
105
+ - lib/awsm/cli/tag.rb
105
106
  - lib/awsm/cli/unused.rb
106
107
  - lib/awsm/clibase.rb
107
108
  - lib/awsm/configure.rb
108
109
  - lib/awsm/dns.rb
109
110
  - lib/awsm/instances.rb
110
111
  - lib/awsm/loadbalancers.rb
112
+ - lib/awsm/table/instance.rb
111
113
  - lib/awsm/version.rb
112
114
  homepage: http://github.com/mduk/awsm
113
115
  licenses: