awful 0.0.1 → 0.0.2

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: 7bedcb9861ea2075c3ea043bd651ebf56edd2668
4
- data.tar.gz: aa9e8729e5772fef666e9334ad6405bf8977c816
3
+ metadata.gz: 8279d47c43bd2ad05a4091376089c74750afcc79
4
+ data.tar.gz: ada93e597d39fdbc85294f9eeb2ca72a11dfcf9b
5
5
  SHA512:
6
- metadata.gz: b9af606fdddd26cb7455a485690da1e62b7bc02efdc5d2d2ee494c092957cfdd47b8adb5cae71e98188a0d3f16ecedb26fd44420734de7b4bc128f2fe4e0eec3
7
- data.tar.gz: 6c6218d4692d44c435aad729be0346d166e6421cc4cf9f6d2ec686b6866179b6f11fbf4ccb2315c391db7740480b545533ddf78155bcf7aff5abc53b36f68ee3
6
+ metadata.gz: 486a2e6c76c70e4f24bfb0e02da5beadf868f84fd6d5825eec0640ceb08e077371877289ca9baec0ac8b3a85346c8046eec1a61e69e126698a788c55615b3f6a
7
+ data.tar.gz: 47ac174a20005af8e1028168b7fb4ac602c1ef1220e7d1610232d4357ad4f87c2e3e1e72e586c8677445a0bd4521050c2c720741b273acd68765cfaadd30959d
data/bin/rds ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/ruby
2
+ #-*- mode: ruby; -*-
3
+
4
+ require 'awful'
5
+ require 'awful/rds'
6
+
7
+ Awful::Rds.start(ARGV)
@@ -18,6 +18,10 @@ module Awful
18
18
  @elb ||= Aws::ElasticLoadBalancing::Client.new
19
19
  end
20
20
 
21
+ def rds
22
+ @rds || Aws::RDS::Client.new
23
+ end
24
+
21
25
  def symbolize_keys(thing)
22
26
  if thing.is_a?(Hash)
23
27
  Hash[ thing.map { |k,v| [ k.to_sym, symbolize_keys(v) ] } ]
@@ -53,15 +53,17 @@ module Awful
53
53
  desc 'ips NAME', 'list IPs for instances in groups matching NAME'
54
54
  method_option :long, aliases: '-l', default: false, desc: 'Long listing'
55
55
  def ips(name)
56
- fields = options[:long] ? %i[public_ip_address private_ip_address instance_id image_id instance_type launch_time] : %i[ public_ip_address ]
56
+ fields = options[:long] ?
57
+ ->(i) { [ i.public_ip_address, i.private_ip_address, i.instance_id, i.image_id, i.instance_type, i.placement.availability_zone, i.state.name, i.launch_time ] } :
58
+ ->(i) { [ i.public_ip_address ] }
57
59
 
58
60
  instance_ids = autoscaling.describe_auto_scaling_instances.map(&:auto_scaling_instances).flatten.select do |instance|
59
61
  instance.auto_scaling_group_name.match(name)
60
62
  end.map(&:instance_id)
61
63
 
62
64
  ec2 = Aws::EC2::Client.new
63
- ec2.describe_instances(instance_ids: instance_ids).map(&:reservations).flatten.map(&:instances).flatten.map do |instance|
64
- fields.map { |field| instance.send(field) }
65
+ ec2.describe_instances(instance_ids: instance_ids).map(&:reservations).flatten.map(&:instances).flatten.sort_by(&:launch_time).map do |instance|
66
+ fields.call(instance)
65
67
  end.tap do |list|
66
68
  print_table list
67
69
  end
@@ -112,15 +114,18 @@ module Awful
112
114
  end
113
115
  end
114
116
 
115
- desc 'terminate NAME', 'terminate instances in group NAME'
116
- method_option :decrement, aliases: '-d', default: false, desc: 'Decrement desired capacity for each terminated instance'
117
+ desc 'terminate NAME [NUMBER]', 'terminate NUMBER instances in group NAME'
118
+ method_option :decrement, aliases: '-d', default: false, type: :boolean, desc: 'Decrement desired capacity for each terminated instance'
119
+ method_option :newest, aliases: '-n', default: false, type: :boolean, desc: 'Delete newest instances instead of oldest'
117
120
  def terminate(name, num = 1)
118
121
  instance_ids = autoscaling.describe_auto_scaling_instances.map(&:auto_scaling_instances).flatten.select do |instance|
119
122
  instance.auto_scaling_group_name == name
120
123
  end.map(&:instance_id)
121
124
 
122
- instances = ec2.describe_instances(instance_ids: instance_ids).map(&:reservations).flatten.map(&:instances).flatten.sort_by(&:launch_time)
123
- instances.first(num.to_i).map(&:instance_id).tap do |ids|
125
+ ins = ec2.describe_instances(instance_ids: instance_ids).map(&:reservations).flatten.map(&:instances).flatten.sort_by(&:launch_time)
126
+ ins = ins.reverse if options[:newest]
127
+
128
+ ins.first(num.to_i).map(&:instance_id).tap do |ids|
124
129
  if yes? "Really terminate #{num} instances: #{ids.join(',')}?", :yellow
125
130
  ids.each do |id|
126
131
  autoscaling.terminate_instance_in_auto_scaling_group(instance_id: id, should_decrement_desired_capacity: options[:decrement] && true)
@@ -23,6 +23,20 @@ module Awful
23
23
  autoscaling.delete_launch_configuration(launch_configuration_name: name)
24
24
  end
25
25
 
26
+ desc 'clean NAME [NUM]', 'delete oldest NUM launch configs matching NAME'
27
+ def clean(name, num = 1)
28
+ autoscaling.describe_launch_configurations.map(&:launch_configurations).flatten.select do |lc|
29
+ lc.launch_configuration_name.match(name)
30
+ end.sort_by(&:created_time).first(num.to_i).map(&:launch_configuration_name).tap do |names|
31
+ puts names
32
+ if yes? 'delete these launch configs?', :yellow
33
+ names.each do |name|
34
+ autoscaling.delete_launch_configuration(launch_configuration_name: name)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
26
40
  desc 'dump NAME', 'dump existing launch_configuration as yaml'
27
41
  def dump(name)
28
42
  lc = autoscaling.describe_launch_configurations(launch_configuration_names: Array(name)).map(&:launch_configurations).flatten.first.to_hash
@@ -0,0 +1,43 @@
1
+ module Awful
2
+
3
+ class Rds < Thor
4
+ include Awful
5
+
6
+ desc 'ls [NAME]', 'list DB instances matching NAME'
7
+ method_option :long, aliases: '-l', default: false, desc: 'Long listing'
8
+ def ls(name = /./)
9
+ fields = options[:long] ?
10
+ ->(d) { [d.db_instance_identifier, d.availability_zone, d.db_instance_class, d.db_instance_status, d.preferred_maintenance_window, d.storage_type, d.allocated_storage,
11
+ d.engine, d.engine_version] } :
12
+ ->(d) { [d.db_instance_identifier] }
13
+
14
+ rds.describe_db_instances.map(&:db_instances).flatten.select do |db|
15
+ db.db_instance_identifier.match(name)
16
+ end.map do |db|
17
+ fields.call(db)
18
+ end.tap do |list|
19
+ print_table list
20
+ end
21
+ end
22
+
23
+ desc 'dump NAME', 'dump DB instance matching NAME'
24
+ def dump(name)
25
+ rds.describe_db_instances.map(&:db_instances).flatten.find do |db|
26
+ db.db_instance_identifier == name
27
+ end.tap do |db|
28
+ puts YAML.dump(stringify_keys(db.to_hash))
29
+ end
30
+ end
31
+
32
+ desc 'dns NAME', 'show DNS name and port for DB instance NAME'
33
+ def dns(name)
34
+ rds.describe_db_instances.map(&:db_instances).flatten.find do |db|
35
+ db.db_instance_identifier == name
36
+ end.tap do |db|
37
+ puts "#{db.endpoint.address}:#{db.endpoint.port}"
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Awful
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ric Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,7 @@ executables:
74
74
  - ec2
75
75
  - elb
76
76
  - lc
77
+ - rds
77
78
  - subnet
78
79
  - vpc
79
80
  extensions: []
@@ -89,6 +90,7 @@ files:
89
90
  - bin/ec2
90
91
  - bin/elb
91
92
  - bin/lc
93
+ - bin/rds
92
94
  - bin/subnet
93
95
  - bin/vpc
94
96
  - lib/awful.rb
@@ -96,6 +98,7 @@ files:
96
98
  - lib/awful/ec2.rb
97
99
  - lib/awful/elb.rb
98
100
  - lib/awful/launch_config.rb
101
+ - lib/awful/rds.rb
99
102
  - lib/awful/subnet.rb
100
103
  - lib/awful/version.rb
101
104
  - lib/awful/vpc.rb