pec2 0.4.1 → 0.5.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: c423952fa8333500b2fd92609486eee8091bfd3e
4
- data.tar.gz: 7ab971186c28854893605c0f7bb1ee68ad58d433
3
+ metadata.gz: 221b02cfeb12fc1307eba9f1d8d4e267af138a10
4
+ data.tar.gz: 684b5a45a8bd2e3ebb4121230bf7a380622487ac
5
5
  SHA512:
6
- metadata.gz: e65faac1a7c3a5f36a4a49bbbe7f6c9455aafe1ab6f2a4d2cbdb43e4b067b221915174adec28a40a6395561e2b6995011995ad2980f445791be534851daa26e3
7
- data.tar.gz: c6b0bbdd12c15b1b6a98e73c70f383e406ade474727adefab9d285fcdc1b387ab281b157ce57da25ebe2f9ad0522e0bf72b32fef96e8488a7eb0b0b5763d3420
6
+ metadata.gz: ef6ddb6106717e08eff3e48e2675159e922cc60ad4851d286816247d5cc62f6fa08007bc699aaaf817ef413a9aabf880ea6d035af3fa4492cd8b6d8cb4242758
7
+ data.tar.gz: cb6b3ae20f18033ea181a1c0130b957681cb8a9072a56981617d90ce4f13eac716edc35a7a71f9e06d69ccea7cda3699e820fd8dc66aec88090135732c0b1c93
data/lib/pec2.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pec2/version'
2
2
  require 'pec2/constants'
3
- require 'pec2/core'
3
+ require 'pec2/ec2'
4
+ require 'pec2/pssh'
4
5
  require 'pec2/cli'
data/lib/pec2/cli.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require "thor"
2
2
  require "tempfile"
3
3
  require "logger"
4
- require 'shellwords'
5
4
 
6
5
  module Pec2
7
6
  class CLI < Thor
@@ -12,22 +11,28 @@ module Pec2
12
11
  def initialize(args = [], options = {}, config = {})
13
12
  super(args, options, config)
14
13
  @global_options = config[:shell].base.options
15
- @core = Core.new
16
- @pssh_path = File.expand_path('../../../exe/bin/pssh', __FILE__)
14
+ @core = Ec2.new
17
15
  @logger = Logger.new(STDOUT)
18
16
  end
19
17
 
20
18
  desc 'run_command', 'run command'
21
- option :command, aliases: '-c', type: :string, required: true, desc: 'command'
19
+ option :command, aliases: '-c', type: :string, desc: 'command'
22
20
  option :sudo_password, aliases: '-s', type: :string, desc: 'sudo_password'
23
21
  option :tag, aliases: '-t', type: :hash, default: {}, desc: 'tag'
24
22
  option :user, aliases: '-u', type: :string, desc: 'user'
25
23
  option :log, aliases: '-o', type: :string, desc: 'log'
26
24
  option :parallel, aliases: '-p', type: :numeric, desc: 'parallel'
27
25
  option :print, aliases: '-P', type: :boolean, default: false, desc: 'print stdout.'
26
+ option :resolve, aliases: '--resolve', type: :string, default: 'private_ip_address', enum: ['private_ip_address', 'public_ip_address', 'name_tag'], desc: 'resolve'
28
27
  def run_command
29
28
  addresses = @core.instances_hash(options[:tag]).map do |instance|
30
- instance.private_ip_address
29
+ if options[:resolve] == 'private_ip_address'
30
+ instance.private_ip_address
31
+ elsif options[:resolve] == 'public_ip_address'
32
+ instance.public_ip_address
33
+ elsif options[:resolve] == 'name_tag'
34
+ instance.tags.select{|tag| tag["key"] == "Name" }.first["value"]
35
+ end
31
36
  end
32
37
 
33
38
  if addresses.empty?
@@ -35,37 +40,28 @@ module Pec2
35
40
  exit
36
41
  end
37
42
 
43
+ @logger.info(%Q{listing connection to #{addresses.join(', ')}.})
44
+
38
45
  tf = Tempfile.open("pec2") { |fp|
39
46
  fp.puts(addresses.join("\n"))
40
47
  fp
41
48
  }
42
49
 
43
- cmd = "#{@pssh_path} -t 0 -x '-tt' -h #{tf.path} -O StrictHostKeyChecking=no"
44
- if options[:print]
45
- cmd = "#{cmd} -P"
46
- end
50
+ pssh = Pssh.new(options, tf.path)
47
51
 
48
- if options[:user]
49
- cmd = "#{cmd} -l #{options[:user]}"
50
- end
51
-
52
- if options[:log]
53
- cmd = "#{cmd} -o #{options[:log]}"
54
- end
52
+ interactive = options[:command] ? false : true
55
53
 
56
- if options[:parallel]
57
- cmd = "#{cmd} -p #{options[:parallel]}"
58
- end
59
-
60
- if options[:sudo_password]
61
- cmd = %Q{(echo #{options[:sudo_password]}) | #{cmd} -I #{Shellwords.escape(options[:command])}}
54
+ if interactive
55
+ while true
56
+ command = ask(">:")
57
+ pssh.exec_pssh_command(command)
58
+ end
62
59
  else
63
- cmd = %Q{#{cmd} -i #{Shellwords.escape(options[:command])}}
64
- end
65
-
66
- unless system(cmd)
67
- tf.close
68
- exit 1
60
+ ret = pssh.exec_pssh_command(options[:command])
61
+ unless ret
62
+ tf.close
63
+ exit 1
64
+ end
69
65
  end
70
66
  tf.close
71
67
  end
@@ -10,7 +10,7 @@ class Pec2Mash < ::Hashie::Mash
10
10
  end
11
11
 
12
12
  module Pec2
13
- class Core
13
+ class Ec2
14
14
 
15
15
  def initialize
16
16
  @logger = Logger.new(STDOUT)
data/lib/pec2/pssh.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'shellwords'
2
+
3
+ module Pec2
4
+ class Pssh
5
+
6
+ PSSH_PATH = File.expand_path('../../../exe/bin/pssh', __FILE__)
7
+
8
+ def initialize(options, hosts_file)
9
+ @pssh_command = "#{PSSH_PATH} -t 0 -x '-tt' -h #{hosts_file} -O StrictHostKeyChecking=no"
10
+ if options[:print]
11
+ @pssh_command = "#{@pssh_command} -P"
12
+ end
13
+
14
+ if options[:user]
15
+ @pssh_command = "#{@pssh_command} -l #{options[:user]}"
16
+ end
17
+
18
+ if options[:log]
19
+ @pssh_command = "#{@pssh_command} -o #{options[:log]}"
20
+ end
21
+
22
+ if options[:parallel]
23
+ @pssh_command = "#{@pssh_command} -p #{options[:parallel]}"
24
+ end
25
+ @sudo_password = options[:sudo_password]
26
+ end
27
+
28
+ def build_pssh_command(command)
29
+ if @sudo_password
30
+ %Q{(echo #{@sudo_password}) | #{@pssh_command} -I #{Shellwords.escape(command)}}
31
+ else
32
+ %Q{#{@pssh_command} -i #{Shellwords.escape(command)}}
33
+ end
34
+ end
35
+
36
+ def exec_pssh_command(command)
37
+ return false if command.nil? || command.empty?
38
+ build_command = build_pssh_command(command)
39
+ system(build_command)
40
+ end
41
+ end
42
+ end
data/lib/pec2/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Pec2
2
2
  # pec2 version
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
data/spec/cli_spec.rb CHANGED
@@ -3,6 +3,7 @@ require 'pec2'
3
3
 
4
4
  describe Pec2::CLI do
5
5
  before do
6
+ ENV['AWS_REGION'] = 'us-east-1'
6
7
  end
7
8
 
8
9
  it "should stdout sample" do
@@ -1,9 +1,8 @@
1
1
  require 'spec_helper'
2
2
  require 'pec2'
3
3
 
4
- describe Pec2::Core do
4
+ describe Pec2::Ec2 do
5
5
  before do
6
- @core = Core.new
7
6
  end
8
7
 
9
8
  after do
data/spec/pssh_spec.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'pec2'
3
+ require 'tempfile'
4
+
5
+ describe Pec2::Pssh do
6
+ before do
7
+ @tf = Tempfile.open("pec2") { |fp|
8
+ fp.puts("127.0.0.1")
9
+ fp
10
+ }
11
+ @pssh = Pssh.new({}, @tf.path)
12
+ end
13
+
14
+ it "test exec_pssh_command empty" do
15
+ ret = @pssh.exec_pssh_command('')
16
+
17
+ expect(ret).to eq(false)
18
+ end
19
+
20
+ it "test exec_pssh_command nil" do
21
+ ret = @pssh.exec_pssh_command(nil)
22
+
23
+ expect(ret).to eq(false)
24
+ end
25
+
26
+ it "test build_pssh_command empty" do
27
+ pssh_command = @pssh.build_pssh_command('')
28
+
29
+ expect(pssh_command).to include(%Q{-O StrictHostKeyChecking=no})
30
+ expect(pssh_command).to include(%Q{-t 0 -x '-tt'})
31
+ expect(pssh_command).to include(%Q{-i ''})
32
+ end
33
+
34
+ it "test build_pssh_command" do
35
+ pssh_command = @pssh.build_pssh_command('hostname')
36
+
37
+ expect(pssh_command).not_to start_with(%Q{(echo password) |})
38
+ expect(pssh_command).to include(%Q{-O StrictHostKeyChecking=no})
39
+ expect(pssh_command).to include(%Q{-t 0 -x '-tt'})
40
+ expect(pssh_command).to include(%Q{-i hostname})
41
+ end
42
+
43
+ it "test build_pssh_command with user option" do
44
+ pssh = Pssh.new({ user: 'app' }, @tf.path)
45
+ pssh_command = pssh.build_pssh_command('hostname')
46
+ expect(pssh_command).to include(%Q{ -l app})
47
+ end
48
+
49
+ it "test build_pssh_command with log option" do
50
+ pssh = Pssh.new({ log: 'hoge.log' }, @tf.path)
51
+ pssh_command = pssh.build_pssh_command('hostname')
52
+ expect(pssh_command).to include(%Q{ -o hoge.log})
53
+ end
54
+
55
+ it "test build_pssh_command with parallel option" do
56
+ pssh = Pssh.new({ parallel: 10 }, @tf.path)
57
+ pssh_command = pssh.build_pssh_command('hostname')
58
+ expect(pssh_command).to include(%Q{ -p 10})
59
+ end
60
+
61
+ it "test build_pssh_command with print option" do
62
+ pssh = Pssh.new({ print: true }, @tf.path)
63
+ pssh_command = pssh.build_pssh_command('hostname')
64
+ expect(pssh_command).to include(%Q{ -P})
65
+ end
66
+
67
+ it "test build_pssh_command with sudo_password option" do
68
+ pssh = Pssh.new({ sudo_password: 'password' }, @tf.path)
69
+ pssh_command = pssh.build_pssh_command('hostname')
70
+ expect(pssh_command).to start_with(%Q{(echo password) |})
71
+ end
72
+
73
+ after do
74
+ @tf.close
75
+ end
76
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - toyama0919
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-16 00:00:00.000000000 Z
11
+ date: 2017-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -190,12 +190,14 @@ files:
190
190
  - lib/pec2.rb
191
191
  - lib/pec2/cli.rb
192
192
  - lib/pec2/constants.rb
193
- - lib/pec2/core.rb
193
+ - lib/pec2/ec2.rb
194
+ - lib/pec2/pssh.rb
194
195
  - lib/pec2/version.rb
195
196
  - pec2.gemspec
196
197
  - spec/cli_spec.rb
197
- - spec/core_spec.rb
198
+ - spec/ec2_spec.rb
198
199
  - spec/pec2_spec.rb
200
+ - spec/pssh_spec.rb
199
201
  - spec/spec_helper.rb
200
202
  homepage: https://github.com/toyama0919/pec2
201
203
  licenses:
@@ -223,6 +225,7 @@ specification_version: 4
223
225
  summary: run parallel ssh command. ec2 tag base operation.
224
226
  test_files:
225
227
  - spec/cli_spec.rb
226
- - spec/core_spec.rb
228
+ - spec/ec2_spec.rb
227
229
  - spec/pec2_spec.rb
230
+ - spec/pssh_spec.rb
228
231
  - spec/spec_helper.rb