swa 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +11 -0
  3. data/lib/swa/cli/cloud_formation_command.rb +1 -1
  4. data/lib/swa/cli/collection_behaviour.rb +8 -0
  5. data/lib/swa/cli/ec2_command.rb +50 -1
  6. data/lib/swa/cli/elb_command.rb +53 -0
  7. data/lib/swa/cli/glue_command.rb +192 -0
  8. data/lib/swa/cli/iam_command.rb +92 -4
  9. data/lib/swa/cli/kms_command.rb +1 -1
  10. data/lib/swa/cli/main_command.rb +4 -0
  11. data/lib/swa/cli/s3_command.rb +70 -1
  12. data/lib/swa/cli/selector.rb +4 -0
  13. data/lib/swa/cloud_formation/stack.rb +4 -0
  14. data/lib/swa/ec2/image.rb +4 -0
  15. data/lib/swa/ec2/instance.rb +10 -1
  16. data/lib/swa/ec2/key_pair.rb +4 -0
  17. data/lib/swa/ec2/security_group.rb +6 -0
  18. data/lib/swa/ec2/snapshot.rb +13 -4
  19. data/lib/swa/ec2/subnet.rb +4 -0
  20. data/lib/swa/ec2/volume.rb +4 -0
  21. data/lib/swa/ec2/vpc.rb +4 -0
  22. data/lib/swa/elb/load_balancer.rb +24 -0
  23. data/lib/swa/glue/crawler.rb +28 -0
  24. data/lib/swa/glue/database.rb +17 -0
  25. data/lib/swa/glue/job.rb +21 -0
  26. data/lib/swa/glue/job_bookmark_entry.rb +17 -0
  27. data/lib/swa/glue/job_run.rb +26 -0
  28. data/lib/swa/glue/partition.rb +26 -0
  29. data/lib/swa/glue/table.rb +17 -0
  30. data/lib/swa/iam/credentials.rb +31 -0
  31. data/lib/swa/iam/group.rb +11 -1
  32. data/lib/swa/iam/instance_profile.rb +26 -0
  33. data/lib/swa/iam/policy.rb +4 -0
  34. data/lib/swa/iam/role.rb +9 -0
  35. data/lib/swa/iam/user.rb +4 -0
  36. data/lib/swa/kms/alias.rb +4 -0
  37. data/lib/swa/record.rb +6 -0
  38. data/lib/swa/s3/bucket.rb +13 -1
  39. data/lib/swa/s3/object.rb +5 -0
  40. data/lib/swa/s3/object_version.rb +48 -0
  41. data/lib/swa/version.rb +1 -1
  42. data/swa.gemspec +12 -3
  43. metadata +139 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 38d68962033d474157c7cadb13472a7caa52b276
4
- data.tar.gz: f0b954064b850223718f06d2310269ce81534099
2
+ SHA256:
3
+ metadata.gz: b1dfb1773a649e5d9e24e8ee24739cb6b2df451bb2cd4ffdf6a9b4336c067d87
4
+ data.tar.gz: dc90877d13ad90f7df91dfcec228639de7dd1393b42f2378ee2236421e1d44a4
5
5
  SHA512:
6
- metadata.gz: aac97d88b50058e49ce713e83628986e84e6756b5ca2f4a9804d9ee2fb81bfdb4e5199e3ced6395dbc86fb090bae020577c009f62b9057b6dbe46628f4c9800d
7
- data.tar.gz: b909e0ff69d22933101c0a9e20eb4f580e6589a31992ecfb88e20cc71ccf109321ba2edb292e3da23eb3825494979406cd2f9a85bf20e28405f957389034f3f6
6
+ metadata.gz: 52ee4e00fd3bc9c3001d6a8168a3278f387da0a31732d9dd2bbb4a1971b9066150638ccdf757eac652ff8f6116b58d820262c030d1b018c32605b4475c59feb1
7
+ data.tar.gz: ec4a90e0a5a3e973c051227c33adb9d63048d82a31ca745a9c754593d738c76d0c58f3f6fbdfe75e9beae5c9dd4aa159b38016c2c444ac6ac1453aa5b3b8721f
data/README.md CHANGED
@@ -76,6 +76,17 @@ The "item" sub-command can be ommitted, when it can be inferred from the resourc
76
76
  $ swa ec2 i-bcf48c2b data
77
77
  $ swa ec2 ami-1e73737d data
78
78
 
79
+ ## Installing it
80
+
81
+ SWA is packaged as a Ruby gem, and can be installed with:
82
+
83
+ gem install swa
84
+
85
+ On a Mac, I recommend [brew-gem](https://github.com/sportngin/brew-gem) for easy system-wide installation:
86
+
87
+ brew install brew-gem
88
+ brew gem install swa
89
+
79
90
  ## Contributing
80
91
 
81
92
  Bug reports and pull requests are welcome on GitHub at https://github.com/mdub/swa.
@@ -1,4 +1,4 @@
1
- require "aws-sdk-resources"
1
+ require "aws-sdk-cloudformation"
2
2
  require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
@@ -19,6 +19,14 @@ module Swa
19
19
 
20
20
  end
21
21
 
22
+ target.subcommand ["ids"], "Just print ids" do
23
+ def execute
24
+ collection.each do |i|
25
+ puts i.id
26
+ end
27
+ end
28
+ end
29
+
22
30
  target.subcommand ["data", "d"], "Full details" do
23
31
 
24
32
  parameter "[QUERY]", "JMESPath expression"
@@ -1,4 +1,4 @@
1
- require "aws-sdk-resources"
1
+ require "aws-sdk-ec2"
2
2
  require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
@@ -227,10 +227,25 @@ module Swa
227
227
  List key-pairs.
228
228
  EOF
229
229
 
230
+ option "--named", "PATTERN", "name pattern"
231
+
230
232
  include CollectionBehaviour
231
233
 
234
+ subcommand "delete", "Delete all key-pairs" do
235
+ def execute
236
+ signal_error "no key-pairs specified" unless selector.specified?
237
+ key_pairs.each(&:delete)
238
+ end
239
+ end
240
+
232
241
  private
233
242
 
243
+ def named=(pattern)
244
+ selector.add do |kp|
245
+ File.fnmatch(pattern, kp.name)
246
+ end
247
+ end
248
+
234
249
  def key_pairs
235
250
  query_for(:key_pairs, Swa::EC2::KeyPair)
236
251
  end
@@ -245,6 +260,12 @@ module Swa
245
260
 
246
261
  include ItemBehaviour
247
262
 
263
+ subcommand ["delete"], "Delete group" do
264
+ def execute
265
+ security_group.delete
266
+ end
267
+ end
268
+
248
269
  private
249
270
 
250
271
  def security_group
@@ -260,6 +281,12 @@ module Swa
260
281
  include TagFilterOptions
261
282
  include CollectionBehaviour
262
283
 
284
+ subcommand ["delete"], "Delete group" do
285
+ def execute
286
+ security_groups.each(&:delete)
287
+ end
288
+ end
289
+
263
290
  private
264
291
 
265
292
  def security_groups
@@ -302,12 +329,34 @@ module Swa
302
329
  EOF
303
330
 
304
331
  option "--owned-by", "OWNER", "with specified owner", :default => "self"
332
+ option ["--volume", "--of"], "VOLUME-ID", "for specified volume"
333
+
334
+ option ["--started-after", "--after"], "WHEN", "earliest start-time"
335
+ option ["--started-before", "--before"], "WHEN", "latest start-time"
305
336
 
306
337
  include TagFilterOptions
307
338
  include CollectionBehaviour
308
339
 
309
340
  private
310
341
 
342
+ def volume=(volume_id)
343
+ add_filter("volume-id", volume_id)
344
+ end
345
+
346
+ def started_after=(datetime_string)
347
+ min_start_time = parse_datetime(datetime_string).max
348
+ selector.add do |image|
349
+ image.start_time > min_start_time
350
+ end
351
+ end
352
+
353
+ def started_before=(datetime_string)
354
+ max_start_time = parse_datetime(datetime_string).min
355
+ selector.add do |image|
356
+ image.start_time < max_start_time
357
+ end
358
+ end
359
+
311
360
  def snapshots
312
361
  query_options[:owner_ids] = [owned_by]
313
362
  query_for(:snapshots, Swa::EC2::Snapshot)
@@ -0,0 +1,53 @@
1
+ require "aws-sdk-elasticloadbalancing"
2
+ require "swa/cli/base_command"
3
+ require "swa/cli/collection_behaviour"
4
+ require "swa/cli/item_behaviour"
5
+ require "swa/elb/load_balancer"
6
+
7
+ module Swa
8
+ module CLI
9
+
10
+ class ElbCommand < BaseCommand
11
+
12
+ subcommand ["load-balancer", "lb"], "Show load-balancer" do
13
+
14
+ parameter "NAME", "ELB name"
15
+
16
+ include ItemBehaviour
17
+
18
+ private
19
+
20
+ def item
21
+ results = describe_load_balancers(:load_balancer_names => [name])
22
+ signal_error "No such ELB '#{name}'" unless results.any?
23
+ Swa::ELB::LoadBalancer.new(results.first)
24
+ end
25
+
26
+ end
27
+
28
+ subcommand ["load-balancers", "lbs"], "Show load-balancers" do
29
+
30
+ include CollectionBehaviour
31
+
32
+ private
33
+
34
+ def collection
35
+ Swa::ELB::LoadBalancer.list(describe_load_balancers)
36
+ end
37
+
38
+ end
39
+
40
+ protected
41
+
42
+ def elb_client
43
+ ::Aws::ElasticLoadBalancing::Client.new(aws_config)
44
+ end
45
+
46
+ def describe_load_balancers(options = {})
47
+ elb_client.describe_load_balancers(options).load_balancer_descriptions
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,192 @@
1
+ require "aws-sdk-glue"
2
+ require "swa/cli/base_command"
3
+ require "swa/cli/collection_behaviour"
4
+ require "swa/cli/item_behaviour"
5
+ require "swa/glue/crawler"
6
+ require "swa/glue/database"
7
+ require "swa/glue/job"
8
+ require "swa/glue/job_run"
9
+ require "swa/glue/job_bookmark_entry"
10
+ require "swa/glue/partition"
11
+ require "swa/glue/table"
12
+
13
+ module Swa
14
+ module CLI
15
+
16
+ class GlueCommand < BaseCommand
17
+
18
+ subcommand ["crawler"], "Show crawler" do
19
+
20
+ parameter "NAME", "crawler name"
21
+
22
+ include ItemBehaviour
23
+
24
+ private
25
+
26
+ def item
27
+ Swa::Glue::Crawler.new(glue_client.get_crawler(:name => name).crawler)
28
+ end
29
+
30
+ end
31
+
32
+ subcommand ["crawlers"], "Show crawlers" do
33
+
34
+ include CollectionBehaviour
35
+
36
+ private
37
+
38
+ def collection
39
+ query_for(:get_crawlers, :crawlers, Swa::Glue::Crawler)
40
+ end
41
+
42
+ end
43
+
44
+ subcommand ["database"], "Show database" do
45
+
46
+ parameter "NAME", "database name"
47
+
48
+ include ItemBehaviour
49
+
50
+ private
51
+
52
+ def item
53
+ Swa::Glue::Database.new(glue_client.get_database(:name => name).database)
54
+ end
55
+
56
+ subcommand ["table"], "Show table" do
57
+
58
+ parameter "NAME", "table name", attribute_name: :table_name
59
+
60
+ include ItemBehaviour
61
+
62
+ subcommand ["partitions"], "Show partitions" do
63
+
64
+ include CollectionBehaviour
65
+
66
+ private
67
+
68
+ def collection
69
+ query_for(:get_partitions, :partitions, Swa::Glue::Partition, :database_name => name, :table_name => table_name)
70
+ end
71
+
72
+ end
73
+
74
+ private
75
+
76
+ def item
77
+ Swa::Glue::Table.new(glue_client.get_table(
78
+ :database_name => name, :name => table_name
79
+ ).table)
80
+ end
81
+
82
+ end
83
+
84
+ subcommand ["tables"], "Show tables" do
85
+
86
+ include CollectionBehaviour
87
+
88
+ private
89
+
90
+ def collection
91
+ query_for(:get_tables, :table_list, Swa::Glue::Table, :database_name => name)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ subcommand ["databases"], "Show databases" do
99
+
100
+ include CollectionBehaviour
101
+
102
+ private
103
+
104
+ def collection
105
+ query_for(:get_databases, :database_list, Swa::Glue::Database)
106
+ end
107
+
108
+ end
109
+
110
+ subcommand ["job"], "Show job" do
111
+
112
+ parameter "NAME", "job name"
113
+
114
+ include ItemBehaviour
115
+
116
+ private
117
+
118
+ def item
119
+ Swa::Glue::Job.new(glue_client.get_job(:job_name => name).job)
120
+ end
121
+
122
+ subcommand ["run"], "Show run" do
123
+
124
+ parameter "ID", "run ID", attribute_name: :run_id
125
+
126
+ include ItemBehaviour
127
+
128
+ subcommand ["bookmark"], "Show bookmark" do
129
+
130
+ self.default_subcommand = "summary"
131
+
132
+ include ItemBehaviour
133
+
134
+ private
135
+
136
+ def item
137
+ Swa::Glue::JobBookmarkEntry.new(
138
+ glue_client.get_job_bookmark(:job_name => name, :run_id => run_id).job_bookmark_entry
139
+ )
140
+ end
141
+
142
+ end
143
+
144
+ private
145
+
146
+ def item
147
+ Swa::Glue::JobRun.new(glue_client.get_job_run(:job_name => name, :run_id => run_id).job_run)
148
+ end
149
+
150
+ end
151
+
152
+ subcommand ["runs"], "Show runs" do
153
+
154
+ include CollectionBehaviour
155
+
156
+ private
157
+
158
+ def collection
159
+ query_for(:get_job_runs, :job_runs, Swa::Glue::JobRun, :job_name => name)
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ subcommand ["jobs"], "Show jobs" do
167
+
168
+ include CollectionBehaviour
169
+
170
+ private
171
+
172
+ def collection
173
+ query_for(:get_jobs, :jobs, Swa::Glue::Job)
174
+ end
175
+
176
+ end
177
+
178
+ protected
179
+
180
+ def glue_client
181
+ ::Aws::Glue::Client.new(aws_config)
182
+ end
183
+
184
+ def query_for(query_method, response_key, model, **query_args)
185
+ records = glue_client.public_send(query_method, **query_args).public_send(response_key)
186
+ model.list(records)
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+ end
@@ -1,8 +1,10 @@
1
- require "aws-sdk-resources"
1
+ require "aws-sdk-iam"
2
2
  require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
5
+ require "swa/iam/credentials"
5
6
  require "swa/iam/group"
7
+ require "swa/iam/instance_profile"
6
8
  require "swa/iam/policy"
7
9
  require "swa/iam/role"
8
10
  require "swa/iam/user"
@@ -42,6 +44,36 @@ module Swa
42
44
 
43
45
  end
44
46
 
47
+ subcommand ["instance-profile", "ip"], "Show instance-profile" do
48
+
49
+ parameter "NAME", "name/ARN"
50
+
51
+ include ItemBehaviour
52
+
53
+ private
54
+
55
+ def item
56
+ Swa::IAM::InstanceProfile.new(iam.instance_profile(File.basename(name)))
57
+ end
58
+
59
+ end
60
+
61
+ subcommand ["instance-profiles", "ips"], "Show instance-profiles" do
62
+
63
+ self.description = <<-EOF
64
+ List instance-profiles.
65
+ EOF
66
+
67
+ include CollectionBehaviour
68
+
69
+ private
70
+
71
+ def collection
72
+ query_for(:instance_profiles, Swa::IAM::InstanceProfile)
73
+ end
74
+
75
+ end
76
+
45
77
  subcommand ["policy"], "Show policy" do
46
78
 
47
79
  parameter "ARN", "policy ARN"
@@ -70,12 +102,29 @@ module Swa
70
102
  List policies.
71
103
  EOF
72
104
 
105
+ option "--scope", "SCOPE", "'AWS' or 'Local'" do |arg|
106
+ case arg.downcase
107
+ when "all", "*"
108
+ "All"
109
+ when "local"
110
+ "Local"
111
+ when "aws"
112
+ "AWS"
113
+ else
114
+ raise ArgumentError, "must be one of 'All', 'AWS' or 'Local'"
115
+ end
116
+ end
117
+
73
118
  include CollectionBehaviour
74
119
 
75
120
  private
76
121
 
77
122
  def collection
78
- query_for(:policies, Swa::IAM::Policy)
123
+ query_for(:policies, Swa::IAM::Policy, query_options)
124
+ end
125
+
126
+ def query_options
127
+ { :scope => scope }.reject { |_k,v| v.nil? }
79
128
  end
80
129
 
81
130
  end
@@ -92,6 +141,41 @@ module Swa
92
141
  Swa::IAM::Role.new(iam.role(File.basename(name)))
93
142
  end
94
143
 
144
+ subcommand "assume", "Assume the role" do
145
+
146
+ option "--session-name", "NAME", "STS session-name",
147
+ :environment_variable => "USER",
148
+ :default => "swa"
149
+
150
+ parameter "[COMMAND] ...", "command to execute"
151
+
152
+ def execute
153
+ env = assume.to_env
154
+ if command_list.empty?
155
+ dump_env(env)
156
+ else
157
+ exec(env, *command_list)
158
+ end
159
+ end
160
+
161
+ private
162
+
163
+ def assume
164
+ response = sts_client.assume_role(
165
+ :role_arn => item.arn,
166
+ :role_session_name => session_name
167
+ )
168
+ Swa::IAM::Credentials.new(response.credentials.to_h)
169
+ end
170
+
171
+ def dump_env(env)
172
+ env.each do |k,v|
173
+ puts "#{k}=#{v}"
174
+ end
175
+ end
176
+
177
+ end
178
+
95
179
  end
96
180
 
97
181
  subcommand ["roles"], "Show roles" do
@@ -146,11 +230,15 @@ module Swa
146
230
  ::Aws::IAM::Resource.new(aws_config)
147
231
  end
148
232
 
149
- def query_for(query_method, resource_model)
150
- aws_resources = iam.public_send(query_method)
233
+ def query_for(query_method, resource_model, *query_args)
234
+ aws_resources = iam.public_send(query_method, *query_args)
151
235
  wrapped_resources = resource_model.list(aws_resources)
152
236
  end
153
237
 
238
+ def sts_client
239
+ ::Aws::STS::Client.new(aws_config)
240
+ end
241
+
154
242
  end
155
243
 
156
244
  end
@@ -1,4 +1,4 @@
1
- require "aws-sdk-resources"
1
+ require "aws-sdk-kms"
2
2
  require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
@@ -1,6 +1,8 @@
1
1
  require "swa/cli/base_command"
2
2
  require "swa/cli/cloud_formation_command"
3
3
  require "swa/cli/ec2_command"
4
+ require "swa/cli/elb_command"
5
+ require "swa/cli/glue_command"
4
6
  require "swa/cli/iam_command"
5
7
  require "swa/cli/kms_command"
6
8
  require "swa/cli/s3_command"
@@ -12,6 +14,8 @@ module Swa
12
14
 
13
15
  subcommand ["cf", "cloudformation"], "CloudFormation stuff", CloudFormationCommand
14
16
  subcommand "ec2", "EC2 stuff", Ec2Command
17
+ subcommand "elb", "elb stuff", ElbCommand
18
+ subcommand "glue", "Glue stuff", GlueCommand
15
19
  subcommand "iam", "IAM stuff", IamCommand
16
20
  subcommand "kms", "KMS stuff", KmsCommand
17
21
  subcommand "s3", "S3 stuff", S3Command
@@ -1,4 +1,4 @@
1
- require "aws-sdk-resources"
1
+ require "aws-sdk-s3"
2
2
  require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
@@ -82,6 +82,30 @@ module Swa
82
82
 
83
83
  end
84
84
 
85
+ subcommand ["version", "v"], "Show version" do
86
+
87
+ parameter "ID", "object version ID", :attribute_name => :version_id
88
+
89
+ include ItemBehaviour
90
+
91
+ subcommand "get", "GET object" do
92
+
93
+ def execute
94
+ IO.copy_stream(version.get_body, $stdout)
95
+ end
96
+
97
+ end
98
+
99
+ protected
100
+
101
+ def version
102
+ object.version(version_id)
103
+ end
104
+
105
+ alias_method :item, :version
106
+
107
+ end
108
+
85
109
  protected
86
110
 
87
111
  def object
@@ -137,6 +161,51 @@ module Swa
137
161
 
138
162
  end
139
163
 
164
+ subcommand ["object-versions", "versions", "vs"], "List object-versions" do
165
+
166
+ option "--prefix", "PREFIX", "object prefix"
167
+
168
+ self.default_subcommand = "list"
169
+
170
+ subcommand ["list", "ls"], "One-line summary" do
171
+
172
+ def execute
173
+ versions.each do |i|
174
+ puts i.summary
175
+ end
176
+ end
177
+
178
+ end
179
+
180
+ subcommand ["data", "d"], "Full details" do
181
+
182
+ parameter "[QUERY]", "JMESPath expression"
183
+
184
+ def execute
185
+ display_data(versions.map(&:data).to_a, query)
186
+ end
187
+
188
+ end
189
+
190
+ subcommand "delete-all", "Delete versions" do
191
+
192
+ def execute
193
+ versions.each do |v|
194
+ logger.info "Deleting #{v.uri} #{v.id}"
195
+ v.delete
196
+ end
197
+ end
198
+
199
+ end
200
+
201
+ protected
202
+
203
+ def versions
204
+ bucket.object_versions(:prefix => prefix)
205
+ end
206
+
207
+ end
208
+
140
209
  subcommand "policy", "print bucket policy" do
141
210
 
142
211
  def execute
@@ -21,6 +21,10 @@ module Swa
21
21
  collection.lazy.select(&method(:call))
22
22
  end
23
23
 
24
+ def specified?
25
+ @predicates.any?
26
+ end
27
+
24
28
  end
25
29
 
26
30
  end
@@ -7,6 +7,10 @@ module Swa
7
7
 
8
8
  class Stack < Resource
9
9
 
10
+ def id
11
+ name
12
+ end
13
+
10
14
  def summary
11
15
  [
12
16
  pad(name, 44),
data/lib/swa/ec2/image.rb CHANGED
@@ -8,6 +8,10 @@ module Swa
8
8
 
9
9
  include TaggedResource
10
10
 
11
+ def id
12
+ ami.image_id
13
+ end
14
+
11
15
  def summary
12
16
  [
13
17
  field(ami, :image_id),