evrone-common-spawn 0.0.3 → 0.0.4

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: 186b7bf9721b478698a2b3c77f9b119fe4b02176
4
- data.tar.gz: e79c0efd2b8f572299f088779a871843e7c8fa82
3
+ metadata.gz: 0ba5ac5008b3ed08ab6ab11244721aa25b5cf1de
4
+ data.tar.gz: fa1a01ee4855ba9a26c2384eef00fa5e7a5e2645
5
5
  SHA512:
6
- metadata.gz: 234dcc6871880e9f2eda18613d30ff59270def67cf1afa4f2cde93fd8cd90b004877f374ba8dacdf5e87e179e7690c1f48473cb0954296318e1d62ec59de671d
7
- data.tar.gz: 3b6cf25ec501c7b7c2302334061805ab5c6d6129522a672c56379eeb82691dfc4ead3250a31936a31bd345aaf507a7aea3bd92f1d8932edeac6b4cf27fa412e3
6
+ metadata.gz: 2bf4150280e499ca33df726d41d13a0d992d6067fb3530ec32756319c27a3123d93fd180af85430a15afb1ac15fe5c1cac5a562c8cadae72811ad201f2467ea7
7
+ data.tar.gz: c803c1f155e42c91d92fe1dce23ea1a7da6cc755a418197a2573a5eb2b5111af0837ba2fe61085ba02ddb45b68a8db60baa98b305b069eb7141f3c3bccdb300d
data/.travis.yml CHANGED
@@ -9,4 +9,5 @@ before_script:
9
9
  - export SSH_USER=travis
10
10
  - export SSH_PASS=travis
11
11
  - export SSH_HOST=localhost
12
+ - export SSH_PORT=22
12
13
  script: bundle exec rake SPEC_OPTS='-fd --color --order=rand'
data/Rakefile CHANGED
@@ -4,4 +4,3 @@ require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :default => :spec
7
-
@@ -9,7 +9,6 @@ module Evrone
9
9
  class << self
10
10
  def open(host, user, options = {}, &block)
11
11
  ::Net::SSH.start(host, user, {
12
- forward_agent: true,
13
12
  paranoid: false
14
13
  }.merge(options)) do |ssh|
15
14
  yield new(ssh)
@@ -17,10 +16,10 @@ module Evrone
17
16
  end
18
17
  end
19
18
 
20
- attr_reader :host, :user, :options
19
+ attr_reader :host, :user, :options, :connection
21
20
 
22
21
  def initialize(ssh)
23
- @ssh = ssh
22
+ @connection = ssh
24
23
  end
25
24
 
26
25
  def spawn(*args, &block)
@@ -32,7 +31,8 @@ module Evrone
32
31
  timeout = Spawn::Timeout.new options.delete(:timeout)
33
32
  read_timeout = Spawn::ReadTimeout.new options.delete(:read_timeout)
34
33
 
35
- channel = spawn_channel env, command, read_timeout, &block
34
+ command = build_command(env, command, options)
35
+ channel = spawn_channel command, read_timeout, &block
36
36
 
37
37
  channel.on_request("exit-status") do |_,data|
38
38
  exit_code = data.read_long
@@ -45,8 +45,21 @@ module Evrone
45
45
 
46
46
  private
47
47
 
48
+ def build_command(env, command, options)
49
+ cmd = command
50
+ unless env.empty?
51
+ e = env.map{|k,v| "#{k}=#{v}" }.join(" ")
52
+ cmd = "env #{e} #{cmd}"
53
+ end
54
+ if options.key?(:chdir)
55
+ e = "cd #{options[:chdir]}"
56
+ cmd = "#{e} ; #{cmd}"
57
+ end
58
+ cmd
59
+ end
60
+
48
61
  def pool(channel, timeout, read_timeout)
49
- @ssh.loop Spawn.pool_interval do
62
+ @connection.loop Spawn.pool_interval do
50
63
  if read_timeout.happened? || timeout.happened?
51
64
  false
52
65
  else
@@ -66,16 +79,16 @@ module Evrone
66
79
  end
67
80
  end
68
81
 
69
- def spawn_channel(env, command, read_timeout, &block)
82
+ def spawn_channel(command, read_timeout, &block)
70
83
 
71
- @ssh.open_channel do |channel|
84
+ @connection.open_channel do |channel|
72
85
  read_timeout.reset
73
86
 
74
- env.each do |k, v|
75
- channel.env k, v do |_, success|
76
- yield "FAILED: couldn't execute command (ssh.channel.env)\n" if block_given?
77
- end
78
- end
87
+ #env.each do |k, v|
88
+ # channel.env k, v do |_, success|
89
+ # yield "FAILED: couldn't execute command (ssh.channel.env)\n" if block_given?
90
+ # end
91
+ #end
79
92
 
80
93
  channel.exec command do |_, success|
81
94
 
@@ -1,7 +1,7 @@
1
1
  module Evrone
2
2
  module Common
3
3
  module Spawn
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
6
6
  end
7
7
  end
@@ -3,9 +3,10 @@ require 'timeout'
3
3
 
4
4
  describe Evrone::Common::Spawn::SSH, ssh: true do
5
5
 
6
- let(:user) { ENV['SSH_USER'] }
7
- let(:host) { ENV['SSH_HOST'] }
8
- let(:pass) { ENV['SSH_PASS'] }
6
+ let(:user) { ENV['SSH_USER'] || 'vagrant' }
7
+ let(:host) { ENV['SSH_HOST'] || 'localhost' }
8
+ let(:pass) { ENV['SSH_PASS'] || 'vagrant' }
9
+ let(:port) { ENV['SSH_PORT'] || 2222 }
9
10
  let(:collected) { '' }
10
11
 
11
12
  it "run command successfuly" do
@@ -21,8 +22,24 @@ describe Evrone::Common::Spawn::SSH, ssh: true do
21
22
  end
22
23
 
23
24
  it "run command with env successfuly" do
24
- code = run_ssh({'FOO' => "BAR"}, 'echo $FOO')
25
- expect(collected).to match(re "FAILED: couldn't execute command (ssh.channel.env)")
25
+ code = run_ssh({'FOO' => "BAR"}, "sh -c 'echo $FOO'")
26
+ expect(collected).to eq "BAR\n"
27
+ expect(code).to eq 0
28
+ end
29
+
30
+ it "run command with chdir successfuly" do
31
+ code = run_ssh("echo $(pwd)", chdir: "/tmp")
32
+ expect(collected).to eq "/tmp\n"
33
+ expect(code).to eq 0
34
+ end
35
+
36
+ it "run command with chdir and env successfuly" do
37
+ code = run_ssh(
38
+ {'FOO' => "BAR"},
39
+ "sh -c 'echo $FOO' ; echo $(pwd)",
40
+ chdir: '/tmp'
41
+ )
42
+ expect(collected).to eq "BAR\n/tmp\n"
26
43
  expect(code).to eq 0
27
44
  end
28
45
 
@@ -81,7 +98,7 @@ describe Evrone::Common::Spawn::SSH, ssh: true do
81
98
  end
82
99
 
83
100
  def open_ssh(&block)
84
- described_class.open(host, user, password: pass, verbose: 2, &block)
101
+ described_class.open(host, user, password: pass, paranoid: false, verbose: 2, port: port, &block)
85
102
  end
86
103
 
87
104
  def re(s)
@@ -13,9 +13,14 @@ describe Evrone::Common::Spawn do
13
13
  end
14
14
 
15
15
  context "open_ssh" do
16
+ let(:user) { ENV['SSH_USER'] || 'vagrant' }
17
+ let(:host) { ENV['SSH_HOST'] || 'localhost' }
18
+ let(:pass) { ENV['SSH_PASS'] || 'vagrant' }
19
+ let(:port) { ENV['SSH_PORT'] || 2222 }
16
20
  let(:ssh) { nil }
21
+
17
22
  it "should be" do
18
- subject.open_ssh(ENV['SSH_HOST'], ENV['SSH_USER'], password: ENV['SSH_PASS']) do |ssh|
23
+ subject.open_ssh(host, user, password: pass, port: port) do |ssh|
19
24
  expect(ssh.spawn 'true').to eq 0
20
25
  end
21
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evrone-common-spawn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-24 00:00:00.000000000 Z
11
+ date: 2013-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -139,3 +139,4 @@ test_files:
139
139
  - spec/lib/spawn/ssh_spec.rb
140
140
  - spec/lib/spawn_spec.rb
141
141
  - spec/spec_helper.rb
142
+ has_rdoc: