awsutils 2.2.1 → 2.2.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
  SHA256:
3
- metadata.gz: aade832f94a08899c4528262137fad5b5afdf141e1d7b427ccf7d38d05741326
4
- data.tar.gz: 5d9e8063ab2851a637c664a7728230b6e4f3289d3567d8b7659f693c2bbd5b6f
3
+ metadata.gz: a6f564f3cde800c7bfba2e62dc78787b57f668dc03645ea7ff011f96012c96a4
4
+ data.tar.gz: f217eb9001d9b6e95c9d13b5bb30b1119451c73791926b2631beb0171d2b28bc
5
5
  SHA512:
6
- metadata.gz: c0dd07b2e0918f1c0feb0e66b569c3f3161897c0fba73836f23907a5f09b585cae59801a1755bab9e81abd156f513caeff48001e462a22e58f90fa58b44ad73c
7
- data.tar.gz: 7b649d4a58f98b8205dd94f7de44cf24e8b08021cf48b3e350ae048225d4f6a8cf43f653714cb9d4016e3c5a82d4f577107b12aafb572514d1d63e5b1ecb32ac
6
+ metadata.gz: 570c344850a9a697e6cae3ad60f8e7d80cb0c03250dca2c308dd2690b18f5e58e49339a94ec29b8caf918e4ebca2416bc3e0849919aeac2ffdb6f4902ca666db
7
+ data.tar.gz: c5de2f469b296d8ced025a93897a47043ab68d01e5bc659ce5f6ccfb3f9afe4de6b5e533bb75f24699ac7c2d0973cd19b27d7394124bfd75fb86a25c95adb5ae
@@ -1,5 +1,17 @@
1
- LineLength:
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/ClassLength:
5
+ Max: 250
6
+
7
+ Metrics/BlockLength:
8
+ Max: 50
9
+
10
+ Metrics/LineLength:
2
11
  Max: 120
3
12
 
13
+ Metrics/AbcSize:
14
+ Max: 30
15
+
4
16
  MethodLength:
5
17
  Max: 50
data/README.md CHANGED
@@ -24,6 +24,10 @@ awslogs
24
24
  -------
25
25
  Dumps logs from CloudWatch Logs for easy grepping (see `awslogs --help` for details).
26
26
 
27
+ ec2latestimage
28
+ --------------
29
+ Show a list of Ubuntu AMIs owned by your account.
30
+
27
31
  ec2listmachines
28
32
  ---------------
29
33
  Show a list of all EC2 instances in your account.
data/Rakefile CHANGED
@@ -6,4 +6,4 @@ RuboCop::RakeTask.new
6
6
 
7
7
  RSpec::Core::RakeTask.new(:spec)
8
8
 
9
- task default: [:rubocop, :spec]
9
+ task default: %i[rubocop spec]
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'awsutils/version'
5
4
 
@@ -8,33 +7,39 @@ Gem::Specification.new do |spec|
8
7
  spec.version = AwsUtils::VERSION
9
8
  spec.authors = ['Eric Herot']
10
9
  spec.email = ['eric.rubygems@herot.com']
11
- spec.description = %q{A set of tools for interacting with AWS}
12
- spec.summary = %q{A set of tools for interacting with AWS (summary)}
10
+ spec.description = 'A set of tools for interacting with AWS'
11
+ spec.summary = 'A set of tools for interacting with AWS (summary)'
13
12
  spec.homepage = 'http://github.com/evertrue/awsutils'
14
13
  spec.license = 'MIT'
15
14
  spec.metadata = {
16
15
  'bug_tracker_uri' => 'https://github.com/evertrue/awsutils/issues',
17
- 'changelog_uri' => 'https://github.com/evertrue/awsutils/releases',
16
+ 'changelog_uri' => 'https://github.com/evertrue/awsutils/releases',
18
17
  'source_code_uri' => 'https://github.com/evertrue/awsutils'
19
18
  }
20
19
 
21
20
  spec.files = `git ls-files`.split($RS)
22
- spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
23
- spec.test_files = spec.files.grep(/^(test|spec|features)\//)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
23
  spec.require_paths = ['lib']
25
24
 
26
25
  spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'byebug'
27
27
  spec.add_development_dependency 'pry'
28
28
  spec.add_development_dependency 'rake'
29
29
  spec.add_development_dependency 'rspec'
30
30
  spec.add_development_dependency 'rubocop'
31
31
  spec.add_development_dependency 'simplecov'
32
- spec.add_development_dependency 'byebug'
33
32
 
34
33
  spec.add_dependency 'awesome_print', '~> 1'
34
+ spec.add_dependency 'aws-sdk-cloudwatchlogs', '~> 1'
35
+ spec.add_dependency 'aws-sdk-ec2', '~> 1'
36
+ spec.add_dependency 'aws-sdk-elasticloadbalancing', '~> 1'
37
+ spec.add_dependency 'aws-sdk-elasticloadbalancingv2', '~> 1'
38
+ spec.add_dependency 'aws-sdk-route53', '~> 1'
39
+ spec.add_dependency 'colorize', '~> 0.8.1'
35
40
  spec.add_dependency 'facets', '~> 2.9'
36
- spec.add_dependency 'rainbow', '~> 2.0'
37
41
  spec.add_dependency 'fog-aws', '~> 0.11.0'
42
+ spec.add_dependency 'highline', '~> 2.0'
38
43
  spec.add_dependency 'optimist', '~> 3.0'
39
- spec.add_dependency 'aws-sdk'
44
+ spec.add_dependency 'rainbow', '~> 2.0'
40
45
  end
@@ -11,4 +11,4 @@ require 'awsutils/version'
11
11
  require 'awsutils/ec2addsg'
12
12
 
13
13
  sg = AwsUtils::Ec2AddSecurityGroup.new
14
- sg.run
14
+ sg.run
@@ -10,5 +10,5 @@ require 'rubygems'
10
10
  require 'awsutils/version'
11
11
  require 'awsutils/ec2delsg'
12
12
 
13
- sg = AwsUtils::Ec2DeleteSecurityGroup.new( ARGV )
14
- sg.run
13
+ sg = AwsUtils::Ec2DeleteSecurityGroup.new(ARGV)
14
+ sg.run
@@ -10,4 +10,4 @@ require 'rubygems'
10
10
  require 'awsutils/version'
11
11
  require 'awsutils/ec2info'
12
12
 
13
- AwsUtils::Ec2Info.new
13
+ AwsUtils::Ec2Info.new
@@ -10,4 +10,4 @@ require 'rubygems'
10
10
  require 'awsutils/version'
11
11
  require 'awsutils/ec2listmachines'
12
12
 
13
- AwsUtils::Ec2ListMachines.new
13
+ AwsUtils::Ec2ListMachines.new.run
@@ -1,5 +1,5 @@
1
1
  require 'optimist'
2
- require 'aws-sdk'
2
+ require 'aws-sdk-cloudwatchlogs'
3
3
  require 'time'
4
4
 
5
5
  class LogGroupNotFoundError < StandardError; end
@@ -10,9 +10,9 @@ class NoStreamsError < StandardError; end
10
10
 
11
11
  module AwsUtils
12
12
  class AwsLogs
13
- LOG_LEVELS = %w(TRACE DEBUG INFO NOTICE WARNING ERROR FATAL).freeze
14
- MAX_EVENTS = 100000.freeze
15
- MAX_STREAMS = 100.freeze
13
+ LOG_LEVELS = %w[TRACE DEBUG INFO NOTICE WARNING ERROR FATAL].freeze
14
+ MAX_EVENTS = 100_000
15
+ MAX_STREAMS = 100
16
16
 
17
17
  def run
18
18
  print_events
@@ -83,6 +83,7 @@ module AwsUtils
83
83
  r = cloudwatchlogs.describe_log_groups log_group_name_prefix: opts[:group]
84
84
  return r.log_groups.first.log_group_name if r.log_groups.count == 1
85
85
  raise LogGroupNotFoundError if r.log_groups.empty?
86
+
86
87
  err_msg = "Group filter #{opts[:group]} found multiple groups:\n\n"
87
88
  err_msg += r.log_groups.map(&:log_group_name).join("\n")
88
89
  err_msg += "\nMore than 50 log groups returned. Only showed the first 50." if r.next_token
@@ -102,6 +103,7 @@ module AwsUtils
102
103
  collector = response.events
103
104
  collector += chunk_events(streams_chunk, response.next_token) if response.next_token
104
105
  raise TooManyEventsError if collector.count > MAX_EVENTS
106
+
105
107
  collector
106
108
  end
107
109
 
@@ -117,12 +119,14 @@ module AwsUtils
117
119
 
118
120
  def filtered_log_events
119
121
  return log_events unless opts[:request_id]
122
+
120
123
  log_events.select { |event| event.message =~ /\b#{opts[:request_id]}\b/ }
121
124
  end
122
125
 
123
126
  def print_events
124
127
  filtered_log_events.each do |ev|
125
- if ev.message !~ /^\[(INFO|DEBUG|WARNING|ERROR|NOTICE)\]/ # Check if the message is in the standard format
128
+ # Check if the message is in the standard format
129
+ if ev.message !~ /^\[(INFO|DEBUG|WARNING|ERROR|NOTICE)\]/
126
130
  print "#{ev.log_stream_name}: " if opts[:show_stream_name]
127
131
  print Time.at(ev.timestamp / 1e3).iso8601(3) + ' ' if opts[:timestamp]
128
132
  print ev.message
@@ -148,6 +152,7 @@ module AwsUtils
148
152
 
149
153
  def show_logentry?(level)
150
154
  return true unless LOG_LEVELS.include? level
155
+
151
156
  LOG_LEVELS.index(level.upcase) >= LOG_LEVELS.index(opts[:log_level].upcase)
152
157
  end
153
158
 
@@ -163,14 +168,17 @@ module AwsUtils
163
168
  .log_streams
164
169
  .select { |s| s.last_event_timestamp && s.last_event_timestamp > max_age_ts }
165
170
  .map(&:log_stream_name)
166
- fail NoStreamsError if token.nil? && collector.count == 0
171
+ raise NoStreamsError if token.nil? && collector.count.zero?
172
+
167
173
  collector += streams(response.next_token) if response.next_token
168
- fail TooManyStreamsError if collector.count > MAX_STREAMS
174
+ raise TooManyStreamsError if collector.count > MAX_STREAMS
175
+
169
176
  collector
170
177
  end
171
178
 
172
179
  def max_age_ts
173
180
  return 0 unless opts[:age]
181
+
174
182
  (Time.at(Time.now - opts[:age]).to_f * 1_000).to_i
175
183
  end
176
184
 
@@ -9,7 +9,7 @@ module AwsUtils
9
9
  @g_obj ||= begin
10
10
  connection.security_groups.new(
11
11
  name: @opts[:security_group],
12
- description: "#{@opts[:description]}",
12
+ description: @opts[:description].to_s,
13
13
  vpc_id: @opts[:vpc_id]
14
14
  )
15
15
  end
@@ -18,12 +18,12 @@ module AwsUtils
18
18
  def generate_rule_hash(rule)
19
19
  if rule['source']
20
20
  if rule['dest']
21
- fail 'One of the predefined rules has both a source ' \
21
+ raise 'One of the predefined rules has both a source ' \
22
22
  'and a destination already defined: ' + rule.inspect
23
23
  end
24
24
  if rule['source'] !~ /\./ &&
25
25
  !current_groups.include?(rule['source'])
26
- fail "Group #{rule['source']} specified as part of rule: " \
26
+ raise "Group #{rule['source']} specified as part of rule: " \
27
27
  "#{rule.inspect} does not exist"
28
28
  end
29
29
  end
@@ -31,7 +31,7 @@ module AwsUtils
31
31
  if !rule['dest']
32
32
  rule['dest'] = @new_group_id
33
33
  elsif !current_groups.include?(rule['dest'])
34
- fail "Group #{rule['dest']} specified as part of rule: " \
34
+ raise "Group #{rule['dest']} specified as part of rule: " \
35
35
  "#{rule.inspect} does not exist"
36
36
  end
37
37
 
@@ -46,7 +46,7 @@ module AwsUtils
46
46
 
47
47
  if rule['source'] =~ /\./
48
48
  ip_permissions['Groups'] = []
49
- ip_permissions['IpRanges'] = [ 'CidrIp' => rule['source'] ]
49
+ ip_permissions['IpRanges'] = ['CidrIp' => rule['source']]
50
50
  elsif rule['source']
51
51
  ip_permissions['Groups'] = [
52
52
  {
@@ -114,7 +114,7 @@ module AwsUtils
114
114
  rules.select { |rule| rule['dest'] }.each do |rule|
115
115
  add_rule_to_other_group(rule)
116
116
  end
117
- rescue => e
117
+ rescue StandardError => e
118
118
  connection.delete_security_group(nil, g_obj.group_id)
119
119
  raise e
120
120
  end
@@ -133,13 +133,13 @@ module AwsUtils
133
133
 
134
134
  if @opts[:environment]
135
135
  if !rules_data['env']
136
- fail "Environment #{@opts[:environment]} not present in rules file" \
136
+ raise "Environment #{@opts[:environment]} not present in rules file" \
137
137
  " (#{@opts[:base_rules_file]})."
138
138
  else
139
139
  rules_env_data = rules_data['env'][@opts[:environment]]
140
140
  end
141
141
  elsif rules_data.class != Array
142
- fail 'base_rules_file is an environment-keyed file but you did ' \
142
+ raise 'base_rules_file is an environment-keyed file but you did ' \
143
143
  'not specify an environment.'
144
144
  else
145
145
  rules_env_data = rules_data
@@ -165,7 +165,7 @@ module AwsUtils
165
165
  end
166
166
 
167
167
  def parse_opts
168
- fail 'AWS_OWNER_ID is not set!' unless ENV['AWS_OWNER_ID']
168
+ raise 'AWS_OWNER_ID is not set!' unless ENV['AWS_OWNER_ID']
169
169
 
170
170
  @opts = Optimist.options do
171
171
  opt :security_group,
@@ -3,68 +3,58 @@
3
3
  require 'awsutils/ec2sg'
4
4
 
5
5
  module AwsUtils
6
-
7
6
  class Ec2DeleteSecurityGroup < Ec2SecurityGroup
8
-
9
7
  def references
10
-
11
8
  @references ||= begin
12
-
13
9
  references = []
14
10
 
15
11
  connection.security_groups.each do |group|
16
12
  group.ip_permissions.each do |ip_perm|
17
- ip_perm["groups"].each do |src_grp|
18
- if src_grp["groupName"] == @opts[:security_group]
19
-
20
- options = {
21
- "IpPermissions" => [
22
- {
23
- "FromPort" => ip_perm["fromPort"],
24
- "Groups" => [
25
- {
26
- "GroupName" => @opts[:security_group],
27
- "UserId" => @opts[:owner_group_id]
28
- }
29
- ],
30
- "IpProtocol" => ip_perm["ipProtocol"],
31
- "IpRanges" => [],
32
- "ToPort" => ip_perm["toPort"]
33
- }
34
- ]
35
- }
36
-
37
- references << {
38
- "group_name" => group.name,
39
- "options" => options
40
- }
41
-
42
- end # if src_grp["groupName"] == @opts[:security_group]
43
- end # ip_perm["groups"].each do |src_grp|
44
- end # group.ip_permissions.each do |ip_perm|
45
- end # connection.security_groups.each do |group|
13
+ ip_perm['groups'].each do |src_grp|
14
+ next unless src_grp['groupName'] == @opts[:security_group]
15
+
16
+ options = {
17
+ 'IpPermissions' => [
18
+ {
19
+ 'FromPort' => ip_perm['fromPort'],
20
+ 'Groups' => [
21
+ {
22
+ 'GroupName' => @opts[:security_group],
23
+ 'UserId' => @opts[:owner_group_id]
24
+ }
25
+ ],
26
+ 'IpProtocol' => ip_perm['ipProtocol'],
27
+ 'IpRanges' => [],
28
+ 'ToPort' => ip_perm['toPort']
29
+ }
30
+ ]
31
+ }
32
+
33
+ references << {
34
+ 'group_name' => group.name,
35
+ 'options' => options
36
+ }
37
+ end
38
+ end
39
+ end
46
40
 
47
41
  references
48
- end # @references ||= begin
49
- end # def references
42
+ end
43
+ end
50
44
 
51
45
  def delete_group_refs
52
-
53
46
  references.each do |ref|
47
+ puts 'Removing rule: ' + ref.inspect
54
48
 
55
- puts "Removing rule: " + ref.inspect
56
-
57
- connection.revoke_security_group_ingress(
58
- ref["group_name"],
59
- ref["options"]
60
- )
61
-
49
+ connection.revoke_security_group_ingress(
50
+ ref['group_name'],
51
+ ref['options']
52
+ )
62
53
  end
63
-
64
54
  end
65
55
 
66
- def initialize( args )
67
- @opts = Ec2SecurityGroup.parse_opts( args )
56
+ def initialize(args)
57
+ @opts = Ec2SecurityGroup.parse_opts(args)
68
58
  end
69
59
 
70
60
  def name
@@ -72,25 +62,21 @@ module AwsUtils
72
62
  end
73
63
 
74
64
  def run
75
-
76
- if ! exist?
77
- puts "Specified group does not exist."
65
+ unless exist?
66
+ puts 'Specified group does not exist.'
78
67
  exit 1
79
68
  end
80
69
 
81
70
  if assigned?
82
- puts "Group is still assigned to one or more instances."
71
+ puts 'Group is still assigned to one or more instances.'
83
72
  exit 1
84
73
  end
85
74
 
86
75
  delete_group_refs
87
76
 
88
77
  puts "Deleting group #{@opts[:security_group]}."
89
- connection.delete_security_group( nil,
90
- connection.security_groups.get(@opts[:security_group]).group_id )
91
-
78
+ connection.delete_security_group(nil,
79
+ connection.security_groups.get(@opts[:security_group]).group_id)
92
80
  end
93
-
94
81
  end
95
-
96
82
  end
@@ -14,7 +14,7 @@ class String
14
14
  def title_case
15
15
  underscore.split('_').map do |word|
16
16
  # Recognize certain special cases (e.g. acronyms)
17
- if %w(iam ebs id dns vpc).include? word.downcase
17
+ if %w[iam ebs id dns vpc].include? word.downcase
18
18
  word.upcase
19
19
  elsif word.casecmp('ids').zero?
20
20
  'IDs'
@@ -51,7 +51,7 @@ module AwsUtils
51
51
  # TODO: Naming/MemoizedInstanceVariableName: Memoized variable @instance_ids does
52
52
  # not match method name instances.
53
53
  def instances
54
- @instance_ids ||= begin
54
+ @instances ||= begin
55
55
  if $DEBUG
56
56
  puts 'Entering instance_ids'
57
57
  puts "Search Term: #{search_terms.inspect}"
@@ -65,6 +65,7 @@ module AwsUtils
65
65
  next unless server.tags.key?('Name') \
66
66
  && (server.tags['Name'] != '') \
67
67
  && (/#{search_term}/i =~ server.tags['Name'])
68
+
68
69
  m << server.id
69
70
  end
70
71
  end
@@ -75,7 +76,7 @@ module AwsUtils
75
76
  exit 1
76
77
  end
77
78
 
78
- puts "Found instances: #{results.inspect}" if $DEBUG
79
+ puts "Found instances: #{instance_ids.inspect}" if $DEBUG
79
80
 
80
81
  ec2.servers.all 'instance-id' => instance_ids
81
82
  end
@@ -131,17 +132,14 @@ module AwsUtils
131
132
  'ID',
132
133
  'Group',
133
134
  'Flavor',
134
- 'State'
135
- )
136
-
137
- instance_ids.each do |instance_id|
138
- inst = ec2.servers.get(instance_id)
135
+ 'State')
139
136
 
137
+ instances.each do |inst|
140
138
  s_color = get_state_color(inst.state)
141
139
  f_color = get_flavor_color(inst.flavor_id)
142
140
 
143
141
  printf(
144
- "%-50s %-11s %-12s %-13s #{f_color}%-10s#{reset_color} #{s_color}%s#{reset_color}",
142
+ "%-50s %-11s %-12s %-13s #{f_color}%-10s#{reset_color} #{s_color}%s#{reset_color}\n",
145
143
  inst.tags['Name'],
146
144
  inst.availability_zone,
147
145
  inst.id,
@@ -157,16 +155,15 @@ module AwsUtils
157
155
  reset_color = "\033[0m"
158
156
 
159
157
  print "#{key_color}#{key.to_s.title_case}:#{reset_color} "
160
- case
161
- when value.respond_to?(:to_sym)
158
+ if value.respond_to?(:to_sym)
162
159
  puts value
163
- when value.respond_to?(:key?)
160
+ elsif value.respond_to?(:key?)
164
161
  puts
165
162
  value.each { |k, v| printkv " #{k}", v }
166
- when (
167
- value.respond_to?(:join) &&
168
- value.reject { |item| item.respond_to?(:to_sym) }.empty? # If value only contains strings, do this
169
- )
163
+ # If value only contains strings, do this
164
+ elsif value.respond_to?(:join) &&
165
+ value.reject { |item| item.respond_to?(:to_sym) }.empty?
166
+
170
167
  value.join(', ')
171
168
  else
172
169
  puts value.inspect
@@ -246,9 +243,7 @@ module AwsUtils
246
243
 
247
244
  end
248
245
 
249
- if vol != instance.block_device_mapping.last
250
- puts "\t---------------------------------------"
251
- end
246
+ puts "\t---------------------------------------" if vol != instance.block_device_mapping.last
252
247
  end
253
248
 
254
249
  when :client_token
@@ -262,6 +257,7 @@ module AwsUtils
262
257
  group = ec2.security_groups.get(group_id)
263
258
 
264
259
  next unless group
260
+
265
261
  puts "#{key_color}Security Group:#{reset_color} #{group.name}"
266
262
  puts "\t#{key_color}Description:#{reset_color} #{group.description}"
267
263
  puts "\t#{key_color}ID:#{reset_color} #{group.group_id}"
@@ -285,17 +281,11 @@ module AwsUtils
285
281
 
286
282
  puts "\t#{key_color}Name:#{reset_color} #{image_obj.name}" if defined?(image_obj.name)
287
283
 
288
- if defined?(image_obj.description)
289
- puts "\t#{key_color}Description:#{reset_color} #{image_obj.description}"
290
- end
284
+ puts "\t#{key_color}Description:#{reset_color} #{image_obj.description}" if defined?(image_obj.description)
291
285
 
292
- if defined?(image_obj.location)
293
- puts "\t#{key_color}Location:#{reset_color} #{image_obj.location}"
294
- end
286
+ puts "\t#{key_color}Location:#{reset_color} #{image_obj.location}" if defined?(image_obj.location)
295
287
 
296
- if defined?(image_obj.architecture)
297
- puts "\t#{key_color}Arch:#{reset_color} #{image_obj.architecture}"
298
- end
288
+ puts "\t#{key_color}Arch:#{reset_color} #{image_obj.architecture}" if defined?(image_obj.architecture)
299
289
 
300
290
  if defined?(image_obj.tags) && (image_obj.tags != {})
301
291
  puts "\t#{key_color}Tags:#{reset_color}"
@@ -323,7 +313,7 @@ module AwsUtils
323
313
  puts "#{key_color}State:#{reset_color} #{state_color}#{instance.state}#{reset_color}"
324
314
  when :state_reason
325
315
  if instance.state_reason.any?
326
- puts "#{key_color}State Reason Code:#{reset_color} #{instance.state_reason["Code"]}"
316
+ puts "#{key_color}State Reason Code:#{reset_color} #{instance.state_reason['Code']}"
327
317
  elsif $DEBUG
328
318
  puts "#{key_color}State Reason Code:#{reset_color} N/A"
329
319
  end