awful 0.0.2 → 0.0.3

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: 8279d47c43bd2ad05a4091376089c74750afcc79
4
- data.tar.gz: ada93e597d39fdbc85294f9eeb2ca72a11dfcf9b
3
+ metadata.gz: f2edeb3d038db1c9063d5808e2475b027a057dce
4
+ data.tar.gz: 6d04587e11d128aa9363ef8cf332886a26e80b3e
5
5
  SHA512:
6
- metadata.gz: 486a2e6c76c70e4f24bfb0e02da5beadf868f84fd6d5825eec0640ceb08e077371877289ca9baec0ac8b3a85346c8046eec1a61e69e126698a788c55615b3f6a
7
- data.tar.gz: 47ac174a20005af8e1028168b7fb4ac602c1ef1220e7d1610232d4357ad4f87c2e3e1e72e586c8677445a0bd4521050c2c720741b273acd68765cfaadd30959d
6
+ metadata.gz: dc25885681695b9eb9e15a55847ac46e978c7ba91457bc4bce2562b847d736ba9eaa4bf1d58fbc371032c0bea5f971d1c1009f2193c234a5b4f65d97989cef0b
7
+ data.tar.gz: af16e76f55bc4895fba349b7c551a1cd35157b1aa5b420087dfbb59734d27544b59c1392c334672aa5289b59f43938b1556c6990dae86a2b7ebd6a575cd95cdf
data/bin/sg ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/ruby
2
+ #-*- mode: ruby; -*-
3
+
4
+ require 'awful'
5
+ require 'awful/security_group'
6
+
7
+ Awful::SecurityGroup.start(ARGV)
@@ -92,9 +92,10 @@ module Awful
92
92
  end
93
93
 
94
94
  desc 'update NAME', 'update existing auto-scaling group'
95
- method_option :desired_capacity, aliases: '-d', default: nil, desc: 'Set desired capacity'
96
- method_option :min_size, aliases: '-m', default: nil, desc: 'Set minimum capacity'
97
- method_option :max_size, aliases: '-M', default: nil, desc: 'Set maximum capacity'
95
+ method_option :desired_capacity, aliases: '-d', default: nil, desc: 'Set desired capacity'
96
+ method_option :min_size, aliases: '-m', default: nil, desc: 'Set minimum capacity'
97
+ method_option :max_size, aliases: '-M', default: nil, desc: 'Set maximum capacity'
98
+ method_option :launch_configuration_name, aliases: '-l', default: nil, desc: 'Launch config name'
98
99
  def update(name)
99
100
  opt = load_cfg(options)
100
101
  whitelist = %i[auto_scaling_group_name launch_configuration_name min_size max_size desired_capacity default_cooldown availability_zones
@@ -114,16 +115,24 @@ module Awful
114
115
  end
115
116
  end
116
117
 
118
+ no_commands do
119
+ def oldest(name)
120
+ instance_ids = autoscaling.describe_auto_scaling_instances.map(&:auto_scaling_instances).flatten.select do |instance|
121
+ instance.auto_scaling_group_name == name
122
+ end.map(&:instance_id)
123
+ ec2.describe_instances(instance_ids: instance_ids).map(&:reservations).flatten.map(&:instances).flatten.sort_by(&:launch_time)
124
+ end
125
+
126
+ def newest(name)
127
+ oldest(name).reverse
128
+ end
129
+ end
130
+
117
131
  desc 'terminate NAME [NUMBER]', 'terminate NUMBER instances in group NAME'
118
132
  method_option :decrement, aliases: '-d', default: false, type: :boolean, desc: 'Decrement desired capacity for each terminated instance'
119
133
  method_option :newest, aliases: '-n', default: false, type: :boolean, desc: 'Delete newest instances instead of oldest'
120
134
  def terminate(name, num = 1)
121
- instance_ids = autoscaling.describe_auto_scaling_instances.map(&:auto_scaling_instances).flatten.select do |instance|
122
- instance.auto_scaling_group_name == name
123
- end.map(&:instance_id)
124
-
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]
135
+ ins = options[:newest] ? newest(name) : oldest(name)
127
136
 
128
137
  ins.first(num.to_i).map(&:instance_id).tap do |ids|
129
138
  if yes? "Really terminate #{num} instances: #{ids.join(',')}?", :yellow
@@ -132,7 +141,17 @@ module Awful
132
141
  end
133
142
  end
134
143
  end
144
+ end
135
145
 
146
+ desc 'stop NAME [NUMBER]', 'stop NUMBER instances in group NAME'
147
+ method_option :newest, aliases: '-n', default: false, type: :boolean, desc: 'Stop newest instances instead of oldest'
148
+ def stop(name, num = 1)
149
+ ins = options[:newest] ? newest(name) : oldest(name)
150
+ ins.first(num.to_i).map(&:instance_id).tap do |ids|
151
+ if yes? "Really stop #{num} instances: #{ids.join(',')}?", :yellow
152
+ ec2.stop_instances(instance_ids: ids)
153
+ end
154
+ end
136
155
  end
137
156
 
138
157
  end
@@ -44,13 +44,36 @@ module Awful
44
44
  puts YAML.dump(stringify_keys(lc))
45
45
  end
46
46
 
47
+ desc 'latest', 'latest'
48
+ def latest(name)
49
+ autoscaling.describe_launch_configurations.map(&:launch_configurations).flatten.select do |lc|
50
+ lc.launch_configuration_name.match(/^#{name}/)
51
+ end.sort_by(&:created_time).last.launch_configuration_name.tap do |latest|
52
+ puts latest
53
+ end
54
+ end
55
+
47
56
  desc 'create [NAME]', 'create a new launch configuration'
57
+ method_option :timestamp, aliases: '-t', default: false, desc: 'Add timestamp to launch config name'
58
+ method_option :patch, aliases: '-p', default: false, desc: 'Add release to launch config name'
48
59
  def create(name)
49
- opt = load_cfg
60
+ opt = load_cfg(options)
61
+
50
62
  whitelist = %i[launch_configuration_name image_id key_name security_groups classic_link_vpc_id classic_link_vpc_security_groups user_data
51
63
  instance_id instance_type kernel_id ramdisk_id block_device_mappings instance_monitoring spot_price iam_instance_profile
52
64
  ebs_optimized associate_public_ip_address placement_tenancy]
53
- opt[:launch_configuration_name] = "#{name}-#{Time.now.utc.strftime('%Y%m%d%H%M%S')}"
65
+
66
+ if options[:timestamp]
67
+ opt[:launch_configuration_name] = "#{name}-#{Time.now.utc.strftime('%Y%m%d%H%M%S')}"
68
+ elsif opt[:patch]
69
+ print "bumping version from latest: "
70
+ m = latest(name).match(/^#{name}-(\w+)\.(\w+)\.(\w+)$/)
71
+ raise "latest launch config does not match format #{name}-x.x.x" unless m
72
+ opt[:launch_configuration_name] = "#{name}-#{[m[1], m[2], m[3].to_i + 1].join('.')}"
73
+ else
74
+ opt[:launch_configuration_name] = name
75
+ end
76
+
54
77
  opt[:user_data] = Base64.encode64(opt[:user_data]) # encode user data
55
78
  opt = remove_empty_strings(opt)
56
79
  opt = only_keys_matching(opt, whitelist)
@@ -0,0 +1,33 @@
1
+ module Awful
2
+
3
+ class SecurityGroup < Thor
4
+ include Awful
5
+
6
+ desc 'ls [NAME]', 'list security groups [matching NAME]'
7
+ method_option :long, aliases: '-l', default: false, desc: 'Long listing'
8
+ def ls(name = /./)
9
+ fields = options[:long] ?
10
+ ->(s) { [tag_name(s), s.group_id, s.group_name, s.vpc_id, s.description] } :
11
+ ->(s) { [s.group_name] }
12
+
13
+ ec2.describe_security_groups.map(&:security_groups).flatten.select do |sg|
14
+ sg.group_name.match(name) or sg.group_id.match(name)
15
+ end.map do |sg|
16
+ fields.call(sg)
17
+ end.tap do |list|
18
+ print_table list
19
+ end
20
+ end
21
+
22
+ desc 'dump NAME', 'dump security group with NAME as yaml'
23
+ def dump(name)
24
+ ec2.describe_security_groups.map(&:security_groups).flatten.find do |sg|
25
+ sg.group_name == name
26
+ end.tap do |sg|
27
+ puts YAML.dump(stringify_keys(sg.to_hash))
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Awful
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
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-06 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ executables:
75
75
  - elb
76
76
  - lc
77
77
  - rds
78
+ - sg
78
79
  - subnet
79
80
  - vpc
80
81
  extensions: []
@@ -91,6 +92,7 @@ files:
91
92
  - bin/elb
92
93
  - bin/lc
93
94
  - bin/rds
95
+ - bin/sg
94
96
  - bin/subnet
95
97
  - bin/vpc
96
98
  - lib/awful.rb
@@ -99,6 +101,7 @@ files:
99
101
  - lib/awful/elb.rb
100
102
  - lib/awful/launch_config.rb
101
103
  - lib/awful/rds.rb
104
+ - lib/awful/security_group.rb
102
105
  - lib/awful/subnet.rb
103
106
  - lib/awful/version.rb
104
107
  - lib/awful/vpc.rb