swa 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53fd1347fd3e98e99e87890483f9f9ef694ee50d
4
- data.tar.gz: ebc63134ad5f40ce5ca77abae5d0edc31c5a53bf
3
+ metadata.gz: 96696427a2dd6945724b752802f7e041023ef335
4
+ data.tar.gz: 099a422f4ebf6102bddf91ad7c39ef46e673fae0
5
5
  SHA512:
6
- metadata.gz: 36450fa49aa02109b6498ad048cea74d8a8b39ae7123e5ce4be31e4155c4e72a59c0bac24466b3131ebca4b9ee9e17eae4fb703d49368fadbd8aee21b52e9917
7
- data.tar.gz: b806497adeb681d53be9a7eaaf6d49b059593bae45c0fd78dbf23b636841eed86374dca59e1c4d8e54cb7b674f27c3f44c01d2bcf46e3d28ef5c5455fabe4015
6
+ metadata.gz: d72ae93fa0a477e5abab21fd018763597ccd1f1042a27c52625f13e6f5d5c1c574181be76251124867181eb5a4c54d2745b0c05d3ec63b3264229f157543427d
7
+ data.tar.gz: a71e19e5f19ab55af892fd48fbb54fab9037962ae4c4351e9606c4392c1b26e46a9450ea62526595687dfbc74d66dd6b2e6ce5ec0650125da07f0869c0ae6f63
@@ -9,7 +9,7 @@ module Swa
9
9
 
10
10
  class BaseCommand < Clamp::Command
11
11
 
12
- option ["--region"], "REGION", "AWS region"
12
+ option "--region", "REGION", "AWS region"
13
13
  option "--access-key", "KEY", "AWS access key",
14
14
  :attribute_name => :access_key_id
15
15
  option "--secret-key", "KEY", "AWS secret key",
@@ -17,12 +17,37 @@ module Swa
17
17
  option "--session-token", "KEY", "AWS security token",
18
18
  :attribute_name => :session_token
19
19
 
20
- option ["-Y", "--yaml"], :flag, "output data in YAML format"
20
+ option "--format", "FORMAT", "format for data output",
21
+ :attribute_name => :output_format,
22
+ :environment_variable => "SWA_OUTPUT_FORMAT",
23
+ :default => "YAML"
24
+
25
+ option ["--json", "-J"], :flag, "output data in JSON format" do
26
+ self.output_format = "JSON"
27
+ end
28
+
29
+ option ["--yaml", "-Y"], :flag, "output data in YAML format" do
30
+ self.output_format = "YAML"
31
+ end
21
32
 
22
33
  option ["--debug"], :flag, "enable debugging"
23
34
 
35
+ def run(arguments)
36
+ super(arguments)
37
+ rescue Aws::Errors::MissingCredentialsError
38
+ signal_error "no credentials provided"
39
+ rescue Aws::Errors::ServiceError => e
40
+ signal_error e.message
41
+ end
42
+
24
43
  protected
25
44
 
45
+ def parse_subcommand
46
+ return false unless self.class.has_subcommands?
47
+ request_help if subcommand_name == "?"
48
+ super
49
+ end
50
+
26
51
  def logger
27
52
  @logger ||= ConsoleLogger.new($stderr, debug?)
28
53
  end
@@ -44,11 +69,22 @@ module Swa
44
69
  super(arguments)
45
70
  end
46
71
 
72
+ def output_format=(arg)
73
+ arg = arg.upcase
74
+ unless %w(JSON YAML).member?(arg)
75
+ raise ArgumentError, "unrecognised data format: #{arg.inspect}"
76
+ end
77
+ @format = arg
78
+ end
79
+
47
80
  def format_data(data)
48
- if yaml?
81
+ case output_format
82
+ when "JSON"
83
+ MultiJson.dump(data, :pretty => true)
84
+ when "YAML"
49
85
  YAML.dump(data)
50
86
  else
51
- MultiJson.dump(data, :pretty => true)
87
+ raise "bad output format: #{output_format}"
52
88
  end
53
89
  end
54
90
 
@@ -57,6 +93,8 @@ module Swa
57
93
  data = JMESPath.search(jmespath_expression, data)
58
94
  end
59
95
  puts format_data(data)
96
+ rescue JMESPath::Errors::SyntaxError => e
97
+ signal_error("invalid JMESPath expression")
60
98
  end
61
99
 
62
100
  end
@@ -7,7 +7,7 @@ module Swa
7
7
 
8
8
  target.default_subcommand = "summary"
9
9
 
10
- target.subcommand ["summary", "s"], "brief summary (one per line)" do
10
+ target.subcommand ["summary", "s"], "One-line summary" do
11
11
  def execute
12
12
  collection.each do |i|
13
13
  puts i.summary
@@ -15,7 +15,7 @@ module Swa
15
15
  end
16
16
  end
17
17
 
18
- target.subcommand ["data", "d"], "full details" do
18
+ target.subcommand ["data", "d"], "Full details" do
19
19
 
20
20
  parameter "[QUERY]", "JMESPath expression"
21
21
 
@@ -7,13 +7,14 @@ require "swa/ec2/key_pair"
7
7
  require "swa/ec2/image"
8
8
  require "swa/ec2/instance"
9
9
  require "swa/ec2/security_group"
10
+ require "swa/ec2/volume"
10
11
 
11
12
  module Swa
12
13
  module CLI
13
14
 
14
15
  class Ec2Command < BaseCommand
15
16
 
16
- subcommand ["key-pair", "kp"], "show key-pair" do
17
+ subcommand ["key-pair", "kp"], "Show key-pair" do
17
18
 
18
19
  parameter "NAME", "key-pair name"
19
20
 
@@ -29,7 +30,7 @@ module Swa
29
30
 
30
31
  end
31
32
 
32
- subcommand ["key-pairs", "kps"], "list key-pairs" do
33
+ subcommand ["key-pairs", "kps"], "List key-pairs" do
33
34
 
34
35
  include CollectionBehaviour
35
36
 
@@ -43,7 +44,7 @@ module Swa
43
44
 
44
45
  end
45
46
 
46
- subcommand ["image", "ami"], "show image" do
47
+ subcommand ["image", "ami"], "Show image" do
47
48
 
48
49
  parameter "IMAGE-ID", "image id"
49
50
 
@@ -59,10 +60,10 @@ module Swa
59
60
 
60
61
  end
61
62
 
62
- subcommand ["images", "amis"], "list images" do
63
+ subcommand ["images", "amis"], "List images" do
63
64
 
64
- option "--owned-by", "OWNER", "limit to those with selected owner", :default => "self"
65
- option "--named", "PATTERN", "limit to those with matching name"
65
+ option "--owned-by", "OWNER", "with specified owner", :default => "self"
66
+ option "--named", "PATTERN", "with matching name"
66
67
 
67
68
  include TagFilterOptions
68
69
  include CollectionBehaviour
@@ -85,12 +86,30 @@ module Swa
85
86
 
86
87
  end
87
88
 
88
- subcommand ["instance", "i"], "show instance" do
89
+ subcommand ["instance", "i"], "Show instance" do
89
90
 
90
91
  parameter "INSTANCE-ID", "instance id"
91
92
 
92
93
  include ItemBehaviour
93
94
 
95
+ subcommand ["console-output", "console"], "Display console output" do
96
+
97
+ def execute
98
+ puts instance.console_output
99
+ end
100
+
101
+ end
102
+
103
+ %w(stop start reboot terminate).each do |action|
104
+ class_eval <<-RUBY
105
+ subcommand "#{action}", "#{action.capitalize} the instance" do
106
+ def execute
107
+ instance.#{action}
108
+ end
109
+ end
110
+ RUBY
111
+ end
112
+
94
113
  private
95
114
 
96
115
  def instance
@@ -101,7 +120,7 @@ module Swa
101
120
 
102
121
  end
103
122
 
104
- subcommand ["instances", "is"], "list instances" do
123
+ subcommand ["instances", "is"], "List instances" do
105
124
 
106
125
  option ["--state"], "STATE", "with specified status",
107
126
  :default => "running"
@@ -125,6 +144,18 @@ module Swa
125
144
  include TagFilterOptions
126
145
  include CollectionBehaviour
127
146
 
147
+ %w(stop start reboot terminate).each do |action|
148
+ class_eval <<-RUBY
149
+ subcommand "#{action}", "#{action.capitalize} all instances" do
150
+ def execute
151
+ instances.each do |i|
152
+ i.#{action}
153
+ end
154
+ end
155
+ end
156
+ RUBY
157
+ end
158
+
128
159
  private
129
160
 
130
161
  def instances
@@ -137,7 +168,7 @@ module Swa
137
168
 
138
169
  end
139
170
 
140
- subcommand ["security-group", "sg"], "show security-group" do
171
+ subcommand ["security-group", "sg"], "Show security-group" do
141
172
 
142
173
  parameter "GROUP-ID", "security-group id"
143
174
 
@@ -153,7 +184,7 @@ module Swa
153
184
 
154
185
  end
155
186
 
156
- subcommand ["security-groups", "sgs"], "list security-groups" do
187
+ subcommand ["security-groups", "sgs"], "List security-groups" do
157
188
 
158
189
  include TagFilterOptions
159
190
  include CollectionBehaviour
@@ -170,6 +201,39 @@ module Swa
170
201
 
171
202
  end
172
203
 
204
+ subcommand ["volume", "vol", "v"], "Show volume" do
205
+
206
+ parameter "VOLUME-ID", "volume id"
207
+
208
+ include ItemBehaviour
209
+
210
+ private
211
+
212
+ def volume
213
+ Swa::EC2::Volume.new(ec2.volume(volume_id))
214
+ end
215
+
216
+ alias_method :item, :volume
217
+
218
+ end
219
+
220
+ subcommand ["volumes", "vs"], "List volumes" do
221
+
222
+ include TagFilterOptions
223
+ include CollectionBehaviour
224
+
225
+ private
226
+
227
+ def volumes
228
+ options = {}
229
+ options[:filters] = filters unless filters.empty?
230
+ Swa::EC2::Volume.list(ec2.volumes(options))
231
+ end
232
+
233
+ alias_method :collection, :volumes
234
+
235
+ end
236
+
173
237
  protected
174
238
 
175
239
  def ec2
@@ -5,15 +5,13 @@ module Swa
5
5
 
6
6
  def self.included(target)
7
7
 
8
- target.default_subcommand = "summary"
9
-
10
- target.subcommand ["summary", "s"], "brief summary (one per line)" do
8
+ target.subcommand ["summary", "s"], "One-line summary" do
11
9
  def execute
12
10
  puts item.summary
13
11
  end
14
12
  end
15
13
 
16
- target.subcommand ["data", "d"], "full details" do
14
+ target.subcommand ["data", "d"], "Full details" do
17
15
 
18
16
  parameter "[QUERY]", "JMESPath expression"
19
17
 
@@ -1,3 +1,4 @@
1
+ require "forwardable"
1
2
  require "swa/resource"
2
3
  require "swa/ec2/tagged_resource"
3
4
 
@@ -24,6 +25,14 @@ module Swa
24
25
  tags["Name"]
25
26
  end
26
27
 
28
+ def console_output
29
+ i.console_output.output
30
+ end
31
+
32
+ extend Forwardable
33
+
34
+ def_delegators :i, :stop, :start, :reboot, :terminate
35
+
27
36
  private
28
37
 
29
38
  alias_method :i, :aws_resource
@@ -0,0 +1,38 @@
1
+ require "forwardable"
2
+ require "swa/resource"
3
+ require "swa/ec2/tagged_resource"
4
+
5
+ module Swa
6
+ module EC2
7
+
8
+ class Volume < Resource
9
+
10
+ include TaggedResource
11
+
12
+ def summary
13
+ [
14
+ pad(v.volume_id, 13),
15
+ pad(v.snapshot_id, 14),
16
+ sprintf("%5d", v.size),
17
+ pad(v.volume_type, 10),
18
+ pad(v.state, 11),
19
+ quoted(name)
20
+ ].join(" ")
21
+ end
22
+
23
+ def name
24
+ tags["Name"]
25
+ end
26
+
27
+ extend Forwardable
28
+
29
+ def_delegators :v, :delete
30
+
31
+ private
32
+
33
+ alias_method :v, :aws_resource
34
+
35
+ end
36
+
37
+ end
38
+ end
data/lib/swa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Swa
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-26 00:00:00.000000000 Z
11
+ date: 2016-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,12 +135,12 @@ files:
135
135
  - lib/swa/cli/main_command.rb
136
136
  - lib/swa/cli/tag_filter_options.rb
137
137
  - lib/swa/data_presentation.rb
138
- - lib/swa/ec2/collection_behaviour.rb
139
138
  - lib/swa/ec2/image.rb
140
139
  - lib/swa/ec2/instance.rb
141
140
  - lib/swa/ec2/key_pair.rb
142
141
  - lib/swa/ec2/security_group.rb
143
142
  - lib/swa/ec2/tagged_resource.rb
143
+ - lib/swa/ec2/volume.rb
144
144
  - lib/swa/resource.rb
145
145
  - lib/swa/version.rb
146
146
  - swa.gemspec
@@ -1,29 +0,0 @@
1
- module Swa
2
- module CLI
3
-
4
- module CollectionBehaviour
5
-
6
- def self.included(target)
7
-
8
- target.default_subcommand = "summary"
9
-
10
- target.subcommand ["summary", "s"], "brief summary (one per line)" do
11
- def execute
12
- collection.each do |i|
13
- puts i.summary
14
- end
15
- end
16
- end
17
-
18
- target.subcommand ["detail", "d"], "full details" do
19
- def execute
20
- display_data(collection.map(&:data).to_a)
21
- end
22
- end
23
-
24
- end
25
-
26
- end
27
-
28
- end
29
- end