awsutils 2.0.3 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/awsutils.gemspec +2 -1
- data/bin/awslogs +14 -0
- data/lib/awsutils/awslogs.rb +109 -0
- data/lib/awsutils/ec2lsgrp.rb +20 -5
- data/lib/awsutils/ec2sg.rb +28 -32
- data/lib/awsutils/version.rb +1 -1
- metadata +21 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cd2972b98d1b03ecb9704dc58b10836697d9d9e
|
4
|
+
data.tar.gz: 1072bcfb550967e616027ecc9ebad509693c88de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b58da8539855b7a01148655cab7fefad6d4d751bc066682aa85564267e82507f330c086c5033665c2a5ba888c968c117b1ebc9f28a85eb786b029cede28644ce
|
7
|
+
data.tar.gz: c2f60425d9c4f43c888850857604402b799e144d2e7eb163943b6a83b413e664c1498ca31ce713a60f74ac35db5b184142e2cbcfd4aeb29a070f0815fe50cece
|
data/README.md
CHANGED
data/awsutils.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
|
29
29
|
spec.add_dependency 'facets', '~> 2.9'
|
30
30
|
spec.add_dependency 'rainbow', '~> 2.0'
|
31
|
-
spec.add_dependency 'fog-aws', '~> 0.
|
31
|
+
spec.add_dependency 'fog-aws', '~> 0.10.0'
|
32
32
|
spec.add_dependency 'trollop'
|
33
|
+
spec.add_dependency 'aws-sdk'
|
33
34
|
end
|
data/bin/awslogs
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Eric.
|
4
|
+
#
|
5
|
+
# The application 'awslogs' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
# require 'rubygems'
|
10
|
+
require 'awsutils/version'
|
11
|
+
require 'awsutils/awslogs'
|
12
|
+
|
13
|
+
awslogs = AwsUtils::AwsLogs.new
|
14
|
+
awslogs.print_events
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
require 'aws-sdk'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module AwsUtils
|
6
|
+
class AwsLogs
|
7
|
+
LOG_LEVELS = %w(TRACE DEBUG INFO NOTICE WARNING ERROR FATAL).freeze
|
8
|
+
|
9
|
+
def opts
|
10
|
+
@opts ||= Trollop.options do
|
11
|
+
opt :group,
|
12
|
+
'Log group (e.g. /aws/lambda/sf-updater)',
|
13
|
+
required: true,
|
14
|
+
type: :string,
|
15
|
+
short: 'g'
|
16
|
+
opt :filter_pattern,
|
17
|
+
'See: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/FilterAndPatternSyntax.html',
|
18
|
+
short: 'f'
|
19
|
+
opt :streams_prefix,
|
20
|
+
'E.g. 2016/08',
|
21
|
+
default: Time.now.strftime('%Y/%m/%d'),
|
22
|
+
short: 's'
|
23
|
+
opt :timestamp,
|
24
|
+
'Include the timestamp from the event metadata in the output',
|
25
|
+
short: 't',
|
26
|
+
default: false
|
27
|
+
opt :show_request_id,
|
28
|
+
'Show the request ID in every log message',
|
29
|
+
short: 'r',
|
30
|
+
default: false
|
31
|
+
opt :log_level,
|
32
|
+
'Lowest log level to show',
|
33
|
+
default: 'INFO',
|
34
|
+
short: 'l'
|
35
|
+
opt :show_stream_name,
|
36
|
+
'Include the name of the log stream on the output line',
|
37
|
+
default: false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def chunk_events(streams_chunk, token = nil)
|
42
|
+
parameters = {
|
43
|
+
log_group_name: opts[:group],
|
44
|
+
log_stream_names: streams_chunk,
|
45
|
+
next_token: token
|
46
|
+
}
|
47
|
+
parameters[:filter_pattern] = opts[:filter_pattern] if opts[:filter_pattern]
|
48
|
+
response = cloudwatchlogs.filter_log_events parameters
|
49
|
+
collector = response.events
|
50
|
+
collector += chunk_events(streams_chunk, response.next_token) if response.next_token
|
51
|
+
collector
|
52
|
+
end
|
53
|
+
|
54
|
+
def log_events
|
55
|
+
# puts "Filtering from #{streams.count} streams in #{streams.count / 50 + 1} chunks"
|
56
|
+
collector = []
|
57
|
+
streams.each_slice(50) { |streams_chunk| collector += chunk_events(streams_chunk) }
|
58
|
+
|
59
|
+
puts 'No events found' if collector.empty?
|
60
|
+
|
61
|
+
collector.sort_by(&:timestamp)
|
62
|
+
end
|
63
|
+
|
64
|
+
def print_events
|
65
|
+
log_events.each do |ev|
|
66
|
+
if ev.message !~ /^\[(INFO|DEBUG|WARNING|ERROR|NOTICE)\]/ # Check if the message is in the standard format
|
67
|
+
print "#{ev.log_stream_name}: " if opts[:show_stream_name]
|
68
|
+
print Time.at(ev.timestamp / 1e3).iso8601(3) + ' ' if opts[:timestamp]
|
69
|
+
print ev.message
|
70
|
+
next
|
71
|
+
end
|
72
|
+
|
73
|
+
msg_parts = ev.message.split("\t")
|
74
|
+
level = msg_parts[0].scan(/\[(\w*)\]/)[-1][0]
|
75
|
+
timestamp = msg_parts[1]
|
76
|
+
request_id = msg_parts[2]
|
77
|
+
message = msg_parts[3..-1].join("\t")
|
78
|
+
|
79
|
+
next unless show_logentry? level
|
80
|
+
|
81
|
+
print "#{ev.log_stream_name}: " if opts[:show_stream_name]
|
82
|
+
print Time.at(ev.timestamp / 1e3).iso8601(3) if opts[:timestamp]
|
83
|
+
printf('%-24s %-10s', timestamp, "[#{level}]")
|
84
|
+
printf('%-37s', request_id) if opts[:show_request_id]
|
85
|
+
print(message)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def show_logentry?(level)
|
90
|
+
return true unless LOG_LEVELS.include? level
|
91
|
+
LOG_LEVELS.index(level.upcase) >= LOG_LEVELS.index(opts[:log_level].upcase)
|
92
|
+
end
|
93
|
+
|
94
|
+
def streams(token = nil)
|
95
|
+
response = cloudwatchlogs.describe_log_streams(
|
96
|
+
log_group_name: opts[:group],
|
97
|
+
log_stream_name_prefix: opts[:streams_prefix],
|
98
|
+
next_token: token
|
99
|
+
)
|
100
|
+
collector = response.log_streams.map(&:log_stream_name)
|
101
|
+
collector += streams(response.next_token) if response.next_token
|
102
|
+
collector
|
103
|
+
end
|
104
|
+
|
105
|
+
def cloudwatchlogs
|
106
|
+
@cloudwatchlogs ||= Aws::CloudWatchLogs::Client.new
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/lib/awsutils/ec2lsgrp.rb
CHANGED
@@ -40,17 +40,32 @@ module AwsUtils
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def run
|
43
|
-
|
43
|
+
raise ArgumentError, 'Please specify a security group' unless search
|
44
44
|
unless group_o = group(search) # rubocop:disable Lint/AssignmentInCondition
|
45
|
-
|
45
|
+
raise GroupDoesNotExist
|
46
46
|
end
|
47
47
|
return group_details(group_o) unless opts[:list_refs]
|
48
48
|
refs = references(group_o.group_id)
|
49
49
|
if refs.empty?
|
50
50
|
puts 'No references'
|
51
51
|
else
|
52
|
-
|
53
|
-
|
52
|
+
print_refs refs
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def print_refs(refs)
|
57
|
+
refs.each do |grp, data|
|
58
|
+
print "#{grp} (#{data['groupId']}): "
|
59
|
+
perm_strings = data['references'].map do |perm|
|
60
|
+
if perm['ipProtocol'] == '-1'
|
61
|
+
'all'
|
62
|
+
else
|
63
|
+
"#{perm['ipProtocol']}/#{perm['fromPort']}" +
|
64
|
+
(perm['fromPort'] != perm['toPort'] ? "-#{perm['toPort']}" : '')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
print perm_strings.join ', '
|
68
|
+
puts ''
|
54
69
|
end
|
55
70
|
end
|
56
71
|
|
@@ -64,7 +79,7 @@ module AwsUtils
|
|
64
79
|
def group_perm_string(group_perm)
|
65
80
|
group_perm.map do |g|
|
66
81
|
if g['userId'] == owner_id
|
67
|
-
"#{g['groupId']} (#{group(g['groupId']).
|
82
|
+
"#{g['groupId']} (#{group(g['groupId']).group_name})"
|
68
83
|
else
|
69
84
|
"#{g['groupId']} (#{g['groupName']}, owner: #{g['userId']})"
|
70
85
|
end
|
data/lib/awsutils/ec2sg.rb
CHANGED
@@ -1,46 +1,42 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'fog/aws'
|
3
2
|
|
4
3
|
module AwsUtils
|
5
4
|
class Ec2SecurityGroup
|
6
5
|
def connection
|
7
|
-
@connection ||=
|
8
|
-
|
6
|
+
@connection ||= Fog::Compute::AWS.new
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
def references(search_string)
|
10
|
+
search =
|
11
|
+
if search_string =~ /^sg-/
|
12
|
+
{
|
13
|
+
id: search_string,
|
14
|
+
name: groups.find { |gr| gr.group_id == search_string }.name
|
15
|
+
}
|
16
|
+
else
|
17
|
+
{
|
18
|
+
id: groups.find { |gr| gr.name == search_string }.group_id,
|
19
|
+
name: search_string
|
14
20
|
}
|
15
21
|
end
|
16
22
|
|
17
|
-
Fog::Compute::AWS.new options
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def references(search)
|
22
|
-
if search =~ /^sg-/
|
23
|
-
search_id = search
|
24
|
-
else
|
25
|
-
search_id = groups.find { |g| g.name == search }.group_id
|
26
|
-
end
|
27
|
-
|
28
23
|
groups.each_with_object({}) do |grp, m|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
permission_references = grp.ip_permissions.select do |ip_perm|
|
25
|
+
ip_perm['groups'].find do |pair|
|
26
|
+
pair['groupId'] == search[:id] ||
|
27
|
+
pair['groupName'] == search[:name]
|
28
|
+
end
|
34
29
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
|
31
|
+
next if permission_references.empty?
|
32
|
+
|
33
|
+
m[grp.name] = { 'groupId' => grp.group_id }
|
34
|
+
m[grp.name]['references'] = permission_references.map do |pr|
|
35
|
+
{
|
39
36
|
'groupId' => grp.group_id,
|
40
|
-
'
|
41
|
-
|
42
|
-
|
43
|
-
end
|
37
|
+
'ipProtocol' => pr['ipProtocol'],
|
38
|
+
'fromPort' => pr['fromPort'],
|
39
|
+
'toPort' => pr['toPort']
|
44
40
|
}
|
45
41
|
end
|
46
42
|
end
|
@@ -57,7 +53,7 @@ module AwsUtils
|
|
57
53
|
server.tags['Name'] ? server.tags['Name'] : server.id
|
58
54
|
end.compact
|
59
55
|
|
60
|
-
return false unless servers_using_group.
|
56
|
+
return false unless servers_using_group.empty?
|
61
57
|
print 'The following servers are still using this group: '
|
62
58
|
puts servers_using_group.join(',')
|
63
59
|
|
data/lib/awsutils/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awsutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Herot
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.10.0
|
146
146
|
type: :runtime
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.10.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: trollop
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,10 +164,25 @@ dependencies:
|
|
164
164
|
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: aws-sdk
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
167
181
|
description: A set of tools for interacting with AWS
|
168
182
|
email:
|
169
183
|
- eric.rubygems@herot.com
|
170
184
|
executables:
|
185
|
+
- awslogs
|
171
186
|
- ec2addsg
|
172
187
|
- ec2delsg
|
173
188
|
- ec2info
|
@@ -185,6 +200,7 @@ files:
|
|
185
200
|
- README.md
|
186
201
|
- Rakefile
|
187
202
|
- awsutils.gemspec
|
203
|
+
- bin/awslogs
|
188
204
|
- bin/ec2addsg
|
189
205
|
- bin/ec2delsg
|
190
206
|
- bin/ec2info
|
@@ -194,6 +210,7 @@ files:
|
|
194
210
|
- bin/elbls
|
195
211
|
- bin/r53ls
|
196
212
|
- lib/awsutils.rb
|
213
|
+
- lib/awsutils/awslogs.rb
|
197
214
|
- lib/awsutils/ec2addsg.rb
|
198
215
|
- lib/awsutils/ec2delsg.rb
|
199
216
|
- lib/awsutils/ec2info.rb
|