remote-session 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,7 +17,7 @@ remote-session [![Build Status](https://secure.travis-ci.org/joeyates/remote-ses
17
17
 
18
18
  Add this line to your application's Gemfile:
19
19
 
20
- gem 'remote-session'
20
+ gem 'remote/session'
21
21
 
22
22
  And then execute:
23
23
 
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
  ## Usage
31
31
 
32
32
  ```ruby
33
- require 'remote-session'
33
+ require 'remote/session'
34
34
 
35
35
  r = Remote::Session.new( 'host.example.com' )
36
36
  puts r.run( 'pwd' )
@@ -1,4 +1,5 @@
1
1
  require 'remote/session/version'
2
+ require 'net/sftp'
2
3
  require 'net/ssh'
3
4
 
4
5
  module Remote
@@ -13,13 +14,24 @@ module Remote
13
14
  rs.close
14
15
  end
15
16
 
16
- attr_accessor :host, :user, :password, :options
17
+ attr_accessor :host
18
+ attr_accessor :username
19
+ attr_accessor :password
20
+ attr_accessor :port
21
+ attr_accessor :private_key
22
+ attr_accessor :prompts
23
+ attr_accessor :session
17
24
 
18
25
  def initialize( host, options = {} )
19
- @host = host
20
- @options = options.clone
21
- @user = @options.delete( :user ) || ENV[ 'USER' ]
22
- @password = @options.delete( :password )
26
+ @session = nil
27
+ @host = host
28
+ @username = options[ :username ] || ENV[ 'USER' ]
29
+ @password = options[ :password ]
30
+ @port = options[ :port ]
31
+ @private_key = options[ :private_key ]
32
+ @prompts = options[ :prompts ] || {}
33
+ @sudo_password = options[ :sudo_password ]
34
+
23
35
  connect
24
36
  end
25
37
 
@@ -29,7 +41,7 @@ module Remote
29
41
  puts @session.exec!( command )
30
42
  end
31
43
 
32
- def sudo( command, prompts = {} )
44
+ def sudo( command )
33
45
  raise "Session is closed" if @session.nil?
34
46
 
35
47
  puts "@#{ @host }: sudo #{ command }"
@@ -37,7 +49,7 @@ module Remote
37
49
  ch.request_pty do |ch, success|
38
50
  raise "Could not obtain pty" if ! success
39
51
 
40
- channel_exec ch, command, prompts
52
+ channel_exec ch, command
41
53
  end
42
54
  end
43
55
  @session.loop
@@ -48,28 +60,50 @@ module Remote
48
60
  @session = nil
49
61
  end
50
62
 
63
+ def sudo_put( remote_path, &block )
64
+ temp_path = "/tmp/remote-session.#{ Time.now.to_f }"
65
+ run "mkdir #{ temp_path }"
66
+ run "chmod 0700 #{ temp_path }"
67
+
68
+ temp_file = File.join( temp_path, File.basename( remote_path ) )
69
+ put temp_file, &block
70
+
71
+ sudo "cp -f #{ temp_file } #{ remote_path }"
72
+ run "rm -rf #{ temp_path }"
73
+ end
74
+
75
+ def put( remote_path, &block )
76
+ sftp = Net::SFTP::Session.new( @session ).connect!
77
+ sftp.file.open( remote_path, 'w' ) do |f|
78
+ f.puts block.call
79
+ end
80
+ sftp.close_channel
81
+ end
82
+
51
83
  private
52
84
 
53
85
  def ssh_options
54
86
  s = {}
55
- s[ :password ] = @password if @password
87
+ s[ :password ] = @password if @password
88
+ s[ :keys ] = [ @private_key ] if @private_key
89
+ s[ :port ] = @port if @port
56
90
  s
57
91
  end
58
92
 
59
93
  def connect
60
- @session = Net::SSH.start( @host, @user, ssh_options )
94
+ @session = Net::SSH.start( @host, @username, ssh_options )
61
95
  end
62
96
 
63
- def channel_exec( ch, command, prompts )
97
+ def channel_exec( ch, command )
64
98
  ch.exec "sudo -p '#{ SUDO_PROMPT }' #{ command }" do |ch, success|
65
99
  raise "Could not execute sudo command: #{ command }" if ! success
66
100
 
67
101
  ch.on_data do | ch, data |
68
102
  if data =~ Regexp.new( SUDO_PROMPT )
69
- ch.send_data "#{ @options[ :sudo_password ] }\n"
103
+ ch.send_data "#{ @sudo_password }\n"
70
104
  else
71
105
  prompt_matched = false
72
- prompts.each_pair do | prompt, send |
106
+ @prompts.each_pair do | prompt, send |
73
107
  if data =~ Regexp.new( prompt )
74
108
  ch.send_data "#{ send }\n"
75
109
  prompt_matched = true
@@ -1,5 +1,5 @@
1
1
  module Remote
2
2
  class Session
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -17,6 +17,7 @@ Run commands as the logged on user, or via sudo as any permitetd user (defaults
17
17
  gem.version = Remote::Session::VERSION
18
18
 
19
19
  gem.add_runtime_dependency 'rake', '>= 0.8.7'
20
+ gem.add_runtime_dependency 'net-sftp'
20
21
  gem.add_runtime_dependency 'net-ssh'
21
22
 
22
23
  gem.add_development_dependency 'rspec', '>= 2.3.0'
@@ -9,12 +9,12 @@ describe Remote::Session do
9
9
 
10
10
  before :each do
11
11
  Net::SSH.stub!( :start )
12
- @user = ENV[ 'USER' ]
12
+ @username = ENV[ 'USER' ]
13
13
  ENV[ 'USER' ] = 'the_user'
14
14
  end
15
15
 
16
16
  after :each do
17
- ENV[ 'USER' ] = @user
17
+ ENV[ 'USER' ] = @username
18
18
  end
19
19
 
20
20
  it 'should require a hostname parameter' do
@@ -29,22 +29,22 @@ describe Remote::Session do
29
29
  Remote::Session.new( TEST_HOST )
30
30
  end
31
31
 
32
- it 'user should default to the current user' do
32
+ it 'username should default to the current user' do
33
33
  Net::SSH.should_receive( :start ).with( TEST_HOST, 'the_user', {} )
34
34
 
35
35
  Remote::Session.new( TEST_HOST )
36
36
  end
37
37
 
38
- it 'should accept user option' do
38
+ it 'should accept username option' do
39
39
  Net::SSH.should_receive( :start ).with( TEST_HOST, 'another_user', {} )
40
40
 
41
- Remote::Session.new( TEST_HOST, :user => 'another_user' )
41
+ Remote::Session.new( TEST_HOST, :username => 'another_user' )
42
42
  end
43
43
 
44
44
  it 'should use any supplied password' do
45
45
  Net::SSH.should_receive( :start ).with( TEST_HOST, 'another_user', { :password => 'secret' } )
46
46
 
47
- Remote::Session.new( TEST_HOST, :user => 'another_user', :password => 'secret' )
47
+ Remote::Session.new( TEST_HOST, :username => 'another_user', :password => 'secret' )
48
48
  end
49
49
 
50
50
  end
@@ -62,7 +62,7 @@ describe Remote::Session do
62
62
  @ssh.stub!( :close )
63
63
 
64
64
  called = false
65
- Remote::Session.open( TEST_HOST, :user => 'another_user' ) do | rs |
65
+ Remote::Session.open( TEST_HOST, :username => 'another_user' ) do | rs |
66
66
  called = true
67
67
  end
68
68
 
@@ -74,19 +74,19 @@ describe Remote::Session do
74
74
 
75
75
  Net::SSH.should_receive( :start ).with( 'host.example.com', 'another_user', {} )
76
76
 
77
- Remote::Session.open( TEST_HOST, :user => 'another_user' ) {}
77
+ Remote::Session.open( TEST_HOST, :username => 'another_user' ) {}
78
78
  end
79
79
 
80
80
  it 'should require a block' do
81
81
  expect do
82
- Remote::Session.open( TEST_HOST, :user => 'another_user' )
82
+ Remote::Session.open( TEST_HOST, :username => 'another_user' )
83
83
  end. to raise_error( NoMethodError, "undefined method `call' for nil:NilClass" )
84
84
  end
85
85
 
86
86
  it 'should close the connection' do
87
87
  @ssh.should_receive( :close )
88
88
 
89
- Remote::Session.open( TEST_HOST, :user => 'another_user' ) {}
89
+ Remote::Session.open( TEST_HOST, :username => 'another_user' ) {}
90
90
  end
91
91
 
92
92
  end
@@ -258,8 +258,9 @@ describe Remote::Session do
258
258
 
259
259
  it 'should send the supplied data' do
260
260
  @ch.should_receive( :send_data ).with( "this data\n" )
261
+ @rs.prompts[ 'my prompt' ] = 'this data'
261
262
 
262
- @rs.sudo( 'pwd', 'my prompt' => 'this data' )
263
+ @rs.sudo( 'pwd' )
263
264
  end
264
265
 
265
266
  it 'should not echo the prompt' do
@@ -267,8 +268,9 @@ describe Remote::Session do
267
268
  @rs.stub!( :puts ) do | s |
268
269
  output << s
269
270
  end
271
+ @rs.prompts[ 'my prompt' ] = 'this data'
270
272
 
271
- @rs.sudo( 'pwd', 'my prompt' => 'this data' )
273
+ @rs.sudo( 'pwd' )
272
274
 
273
275
  output.should == ["@#{TEST_HOST}: sudo pwd"]
274
276
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote-session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000 Z
12
+ date: 2012-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.8.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: net-sftp
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: net-ssh
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -112,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
128
  version: '0'
113
129
  segments:
114
130
  - 0
115
- hash: -78875980215889898
131
+ hash: 2675303135346864190
116
132
  required_rubygems_version: !ruby/object:Gem::Requirement
117
133
  none: false
118
134
  requirements:
@@ -121,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
137
  version: '0'
122
138
  segments:
123
139
  - 0
124
- hash: -78875980215889898
140
+ hash: 2675303135346864190
125
141
  requirements: []
126
142
  rubyforge_project: nowarning
127
143
  rubygems_version: 1.8.24