aws-ssm-console 0.1.1 → 0.2.0

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
  SHA1:
3
- metadata.gz: cf3706d8206b5c6056c7a0bcb6f108e54ef0cbb0
4
- data.tar.gz: f840803944a85f5a49e6b9e5d5504f3e9e3ecff4
3
+ metadata.gz: d1b7150d2747265daac976d0d9e7eb9817ef9cd4
4
+ data.tar.gz: 0bfe9b384a10b7238ecd397e30b723f8875b0d86
5
5
  SHA512:
6
- metadata.gz: dd58c5938dc01717511f72f5854a619fb10f32e81ab976176fe08ab9680e366a6ef9f522139f0294453e9586029c11a05f03fc45cbb76b3c6e189b64169af33e
7
- data.tar.gz: d5a8debec31d2aee3b757c2ca46b0dabd7194b5d6aa4f6f0980dd8aa841e2b3d40e50456152f8ac476731d910642fdd9c63f8a6f5cd78af6c99d01d292e85815
6
+ metadata.gz: 823b89833f82b17ca5674563540f9a27aee55cea005d2e0d267e8ceb312699b2731ccfe5f04b72842a1f584f0b1e781d55a9121441125aed52434393791e9538
7
+ data.tar.gz: 03a99c3a5782b43e90c9549370ec98fb1ecb32a0fa7c0270216b66ce7579b801347f06c242282985b8054c5956d0d4835f898acb50e54cc87267ac443ad9570d
data/README.md CHANGED
@@ -28,11 +28,20 @@ You should configure some environment variables to use AWS SDK.
28
28
 
29
29
  ### Run Command
30
30
 
31
+ #### Interactive mode
32
+
31
33
  ```
32
34
  $ bin/aws-ssm-console --instance-ids=i-xxxx,i-yyyy,...
33
35
  >>
34
36
  ```
35
37
 
38
+ #### Batch mode
39
+
40
+ ```
41
+ $ bin/aws-ssm-console -c ls --instance-ids=i-xxxx,i-yyyy,...
42
+ ...
43
+ ```
44
+
36
45
  ## Development
37
46
 
38
47
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'aws-ssm-console'
7
- spec.version = '0.1.1'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['koshigoe']
9
9
  spec.email = ['koshigoeb@gmail.com']
10
10
  spec.license = 'MIT'
@@ -2,15 +2,5 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'aws/ssm/console'
5
- require 'optparse'
6
5
 
7
- options = {}
8
- OptionParser.new do |opt|
9
- opt.on('--instance-ids id,id,...', Array) { |v| options[:instance_ids] = v }
10
-
11
- opt.parse!(ARGV)
12
- end
13
-
14
- raise ArgumentError, '--instance-ids is required' unless options[:instance_ids]
15
-
16
- Aws::SSM::Console.run(options[:instance_ids])
6
+ Aws::SSM::Console::CLI.new(ARGV).run
@@ -1,5 +1,7 @@
1
1
  require 'aws-sdk'
2
2
  require 'aws/ssm/console/version'
3
+ require 'aws/ssm/console/cli'
4
+ require 'aws/ssm/console/options'
3
5
  require 'aws/ssm/console/runner'
4
6
  require 'aws/ssm/console/command'
5
7
  require 'aws/ssm/console/printer'
@@ -10,10 +12,6 @@ module Aws
10
12
  def self.client
11
13
  @client ||= Aws::SSM::Client.new
12
14
  end
13
-
14
- def self.run(instance_ids)
15
- Aws::SSM::Console::Runner.new(instance_ids).run
16
- end
17
15
  end
18
16
  end
19
17
  end
@@ -0,0 +1,22 @@
1
+ module Aws
2
+ module SSM
3
+ module Console
4
+ class CLI
5
+ attr_reader :options
6
+
7
+ def initialize(argv)
8
+ @options = Options.new(argv)
9
+ end
10
+
11
+ def run
12
+ runner = Aws::SSM::Console::Runner.new(options.instance_ids)
13
+ if options.command
14
+ runner.invoke(options.command)
15
+ else
16
+ runner.run
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,42 @@
1
+ require 'optparse'
2
+
3
+ module Aws
4
+ module SSM
5
+ module Console
6
+ class Options
7
+ attr_reader :instance_ids,
8
+ :command
9
+
10
+ def initialize(argv)
11
+ @instance_ids = []
12
+ @command = nil
13
+
14
+ parser.parse!(argv)
15
+ validate!
16
+ end
17
+
18
+ private
19
+
20
+ def parser
21
+ @parser = OptionParser.new do |opt|
22
+ opt.on('--instance-ids id,id,...', Array) { |v| @instance_ids += v }
23
+ opt.on('-c command', String) { |v| @command = v }
24
+ opt.on('--tag Name:Value', String) { |v| @instance_ids += collect_instance_ids_by_tag(v) }
25
+ end
26
+ end
27
+
28
+ def validate!
29
+ raise ArgumentError, 'EC2 instance IDs required.' if instance_ids.empty?
30
+ end
31
+
32
+ def collect_instance_ids_by_tag(tag)
33
+ name, value = tag.split(':', 2)
34
+ filters = [{ name: "tag:#{name}", values: [value] }]
35
+ Aws::EC2::Client.new.describe_instances(filters: filters).reservations.each_with_object([]) do |reservation, res|
36
+ reservation.instances.each { |instance| res << instance.instance_id }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -37,7 +37,7 @@ module Aws
37
37
  response = Aws::SSM::Console.client.list_command_invocations(command_id: command_id, instance_id: instance_id, details: true)
38
38
  response.command_invocations.each do |invocation|
39
39
  invocation.command_plugins.map do |command_plugin|
40
- yield(instance_id, command_plugin.status, command_plugin.output.split("\n").map { |x| "\t#{x}" }.join("\n"))
40
+ yield(instance_id, command_plugin.status, command_plugin.output.to_s.split("\n").map { |x| "\t#{x}" }.join("\n"))
41
41
  end
42
42
  end
43
43
  end
@@ -4,6 +4,10 @@ module Aws
4
4
  module SSM
5
5
  module Console
6
6
  class Runner
7
+ extend Forwardable
8
+
9
+ delegate invoke: :@command
10
+
7
11
  attr_reader :command
8
12
 
9
13
  def initialize(instance_ids)
@@ -11,8 +15,13 @@ module Aws
11
15
  end
12
16
 
13
17
  def run
18
+ stty_save = `stty -g`.chomp
19
+ trap('INT') { system 'stty', stty_save; exit }
20
+
14
21
  loop do
15
- command.invoke(Readline.readline('>> ', true))
22
+ cmd = Readline.readline('>> ', true)
23
+ break unless cmd
24
+ command.invoke(cmd)
16
25
  end
17
26
  end
18
27
  end
@@ -1,7 +1,9 @@
1
1
  module Aws
2
2
  module SSM
3
3
  module Console
4
- VERSION = '0.1.1'.freeze
4
+ GEMSPEC_PATH = File.expand_path('../../../../../aws-ssm-console.gemspec', __FILE__)
5
+ GEMSPEC = Gem::Specification.load(GEMSPEC_PATH)
6
+ VERSION = GEMSPEC.version.to_s.freeze
5
7
  end
6
8
  end
7
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-ssm-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - koshigoe
@@ -85,7 +85,9 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - lib/aws/ssm/console.rb
88
+ - lib/aws/ssm/console/cli.rb
88
89
  - lib/aws/ssm/console/command.rb
90
+ - lib/aws/ssm/console/options.rb
89
91
  - lib/aws/ssm/console/printer.rb
90
92
  - lib/aws/ssm/console/runner.rb
91
93
  - lib/aws/ssm/console/version.rb