awshark 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-rds'
4
+
5
+ require 'awshark/rds/check_reservations'
6
+ require 'awshark/rds/manager'
7
+
8
+ module Awshark
9
+ module Subcommands
10
+ class Rds < Thor
11
+ include Awshark::Subcommands::ClassOptions
12
+
13
+ desc 'check', 'Check RDS reservations'
14
+ long_desc <<-LONGDESC
15
+ List all RDS instances in a region and check reserved instances
16
+
17
+ Example: `awshark rds check`
18
+ LONGDESC
19
+ def check
20
+ process_class_options
21
+
22
+ puts "\n+++ Instances +++"
23
+ list
24
+
25
+ checks = manager.check_reservations
26
+ pattern = "%-15s %-15s %-15s %-15s %s\n"
27
+ puts "\n+++ Check +++"
28
+ printf pattern, 'Reserved', 'Instances', 'Type', 'MultiAZ', 'Comment'
29
+ checks.each do |c|
30
+ printf pattern, c.reserved_count, c.instance_count, c.type, c.multi_az, comment(c)
31
+ end
32
+ end
33
+
34
+ desc 'list', 'List all RDS instances'
35
+ long_desc <<-LONGDESC
36
+ List all RDS instances in a region
37
+
38
+ Example: `awshark rds list`
39
+ LONGDESC
40
+ def list
41
+ process_class_options
42
+
43
+ instances = manager.instances
44
+ instances = instances.sort_by(&:name)
45
+
46
+ pattern = "%-30s %-15s %-20s %-10s %-10s %s\n"
47
+ printf pattern, 'Name', 'Type', 'Engine (Version)', 'MultiAZ', 'Encrypted', 'State'
48
+ instances.each do |i|
49
+ printf pattern, i.name, i.type, "#{i.engine} (#{i.engine_version})", i.multi_az, i.encrypted, i.state
50
+ end
51
+ end
52
+
53
+ desc 'reservations', 'List all RDS reservations'
54
+ long_desc <<-LONGDESC
55
+ List all RDS reservations in a region
56
+
57
+ Example: `awshark rds reservations`
58
+ LONGDESC
59
+ def reservations
60
+ process_class_options
61
+
62
+ reservations = manager.reservations
63
+
64
+ pattern = "%-30s %-26s %-30s %s\n"
65
+ printf pattern, 'Reserved', 'Type', 'MultiAZ', 'Offering'
66
+ reservations.each do |r|
67
+ printf pattern, r.count, r.type, r.multi_az, r.offering_type
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def manager
74
+ @manager ||= Awshark::Rds::Manager.new
75
+ end
76
+
77
+ def comment(check)
78
+ if check.reserved_count > check.instance_count
79
+ "#{check.reserved_count - check.instance_count} instances available"
80
+ elsif check.reserved_count < check.instance_count
81
+ "#{check.instance_count - check.reserved_count} instances too many!!!"
82
+ else
83
+ ''
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-cloudwatch'
4
+ require 'aws-sdk-s3'
5
+ require 'mini_mime'
6
+
7
+ require 'awshark/s3/artifact'
8
+ require 'awshark/s3/bucket'
9
+ require 'awshark/s3/manager'
10
+
11
+ module Awshark
12
+ module Subcommands
13
+ class S3 < Thor
14
+ include Awshark::Subcommands::ClassOptions
15
+ include ActiveSupport::NumberHelper
16
+
17
+ desc 'list', 'List S3 bucket statistics'
18
+ long_desc <<-LONGDESC
19
+ List S3 bucket statistics of all buckets in this region
20
+
21
+ Example: `awshark s3 list`
22
+ LONGDESC
23
+ def list
24
+ process_class_options
25
+
26
+ buckets = manager.list_buckets
27
+ args = {
28
+ name: 'Bucket',
29
+ region: 'Region',
30
+ size: 'Size',
31
+ number_of_objects: 'Number of Objects'
32
+ }
33
+ printf " %-40<name>s %-13<region>s %-10<size>s %<number_of_objects>s\n", args
34
+ buckets.each do |bucket|
35
+ args = {
36
+ name: bucket.name,
37
+ region: bucket.region,
38
+ size: number_to_human_size(bucket.byte_size),
39
+ number_of_objects: bucket.number_of_objects
40
+ }
41
+ printf " %-40<name>s %-13<region>s %-10<size>s %<number_of_objects>i\n", args
42
+ end
43
+ end
44
+
45
+ desc 'objects', 'List objects of an S3 bucket'
46
+ long_desc <<-LONGDESC
47
+ List objects of an S3 bucket
48
+
49
+ Example: `awshark s3 objects BUCKET PREFIX`
50
+ LONGDESC
51
+ def objects(bucket, prefix = '')
52
+ process_class_options
53
+
54
+ objects = manager.list_objects(bucket: bucket, prefix: prefix)
55
+
56
+ objects.each do |o|
57
+ filesize = number_to_human_size(o.size)
58
+ printf " %-10<size>s %<key>s\n", key: o.key, size: filesize
59
+ end
60
+ printf "\n\n Total:\n"
61
+ printf " %<size>s objects\n", size: objects.size
62
+ printf " %<size>s\n", size: number_to_human_size(objects.map(&:size).sum)
63
+ end
64
+
65
+ desc 'update_metadata', 'Update metadata of S3 objects'
66
+ long_desc <<-LONGDESC
67
+ Update metadata of S3 objects
68
+
69
+ Example: `awshark s3 update_metadata BUCKET PREFIX --meta="cache_control:public, max-age=0, s-maxage=120, must-revalidate" acl:public-read`
70
+ LONGDESC
71
+ method_options meta: :hash
72
+ def update_metadata(bucket, prefix)
73
+ process_class_options
74
+
75
+ objects = manager.list_objects(bucket: bucket, prefix: prefix)
76
+ meta = (options[:meta] || {}).symbolize_keys
77
+
78
+ puts "Metadata: #{JSON.pretty_generate(meta)}"
79
+ puts 'Updating ...'
80
+ objects.each do |o|
81
+ manager.update_object_metadata(bucket, o.key, meta)
82
+ printf " %<key>s \n", key: o.key
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def manager
89
+ @manager ||= Awshark::S3::Manager.new
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Awshark
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,277 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awshark
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joergen Dahlke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-cloudformation
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-cloudwatch
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk-ec2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk-ecr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aws-sdk-rds
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: aws-sdk-s3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: diffy
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: mini_mime
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: recursive-open-struct
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: thor
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: bundler
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '2.0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '2.0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 3.9.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 3.9.0
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '='
200
+ - !ruby/object:Gem::Version
201
+ version: 0.93.1
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '='
207
+ - !ruby/object:Gem::Version
208
+ version: 0.93.1
209
+ description: Custom CLI for for AWS to simplify common tasks with EC2, S3 and Cloud
210
+ Formation
211
+ email:
212
+ - joergen.dahlke@infopark.de
213
+ executables:
214
+ - awshark
215
+ extensions: []
216
+ extra_rdoc_files: []
217
+ files:
218
+ - ".gitignore"
219
+ - ".rspec"
220
+ - ".rubocop.yml"
221
+ - CHANGELOG.md
222
+ - README.md
223
+ - awshark.gemspec
224
+ - exe/awshark
225
+ - gems.rb
226
+ - lib/awshark.rb
227
+ - lib/awshark/cli.rb
228
+ - lib/awshark/cloud_formation/file_loading.rb
229
+ - lib/awshark/cloud_formation/inferrer.rb
230
+ - lib/awshark/cloud_formation/manager.rb
231
+ - lib/awshark/cloud_formation/parameters.rb
232
+ - lib/awshark/cloud_formation/stack.rb
233
+ - lib/awshark/cloud_formation/stack_events.rb
234
+ - lib/awshark/cloud_formation/template.rb
235
+ - lib/awshark/configuration.rb
236
+ - lib/awshark/ec2/instance.rb
237
+ - lib/awshark/ec2/manager.rb
238
+ - lib/awshark/profile_resolver.rb
239
+ - lib/awshark/rds/check_reservations.rb
240
+ - lib/awshark/rds/manager.rb
241
+ - lib/awshark/s3/artifact.rb
242
+ - lib/awshark/s3/bucket.rb
243
+ - lib/awshark/s3/manager.rb
244
+ - lib/awshark/subcommands/class_options.rb
245
+ - lib/awshark/subcommands/cloud_formation.rb
246
+ - lib/awshark/subcommands/ec2.rb
247
+ - lib/awshark/subcommands/ecs.rb
248
+ - lib/awshark/subcommands/rds.rb
249
+ - lib/awshark/subcommands/s3.rb
250
+ - lib/awshark/version.rb
251
+ homepage: https://github.com/jdahlke/awshark
252
+ licenses:
253
+ - MIT
254
+ metadata:
255
+ homepage_uri: https://github.com/jdahlke/awshark
256
+ source_code_uri: https://github.com/jdahlke/awshark
257
+ changelog_uri: https://github.com/jdahlke/awshark/blob/develop/CHANGELOG.md
258
+ post_install_message:
259
+ rdoc_options: []
260
+ require_paths:
261
+ - lib
262
+ required_ruby_version: !ruby/object:Gem::Requirement
263
+ requirements:
264
+ - - ">="
265
+ - !ruby/object:Gem::Version
266
+ version: '2.6'
267
+ required_rubygems_version: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - ">="
270
+ - !ruby/object:Gem::Version
271
+ version: '0'
272
+ requirements: []
273
+ rubygems_version: 3.0.3.1
274
+ signing_key:
275
+ specification_version: 4
276
+ summary: Custom CLI for for AWS related tasks
277
+ test_files: []