vos 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,147 +0,0 @@
1
- module Rsh
2
- module Drivers
3
- class Ssh < Abstract
4
- def initialize options = {}
5
- super
6
- raise "ssh options not provided!" unless options[:ssh]
7
- raise "invalid ssh options!" unless options[:ssh].is_a?(Hash)
8
- end
9
-
10
- def upload_file from_local_path, to_remote_path
11
- sftp.upload! from_local_path, fix_path(to_remote_path)
12
- end
13
-
14
- def download_file from_remote_path, to_local_path
15
- File.open to_local_path, "w" do |out|
16
- sftp.download! fix_path(from_remote_path), out #, :recursive => true
17
- end
18
- end
19
-
20
- def exist? remote_file_path
21
- begin
22
- fattrs = sftp.stat! fix_path(remote_file_path)
23
- fattrs.directory? or fattrs.file? or fattrs.symlink?
24
- rescue Net::SFTP::StatusException
25
- false
26
- end
27
- end
28
- alias_method :directory_exist?, :exist?
29
- alias_method :file_exist?, :exist?
30
-
31
- def remove_file remote_file_path
32
- sftp.remove! fix_path(remote_file_path)
33
- end
34
-
35
- def exec command
36
- remote do |ssh, sftp|
37
- # somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
38
- # command = ". ~/.profile && #{command}"
39
-
40
- stdout, stderr, code, signal = hacked_exec! ssh, command
41
-
42
- return code, stdout, stderr
43
- end
44
- end
45
-
46
- def open
47
- ssh_options = self.options[:ssh].clone
48
- host = options[:host] || raise('host not provided!')
49
- user = ssh_options.delete(:user) || raise('user not provied!')
50
- @ssh = Net::SSH.start(host, user, ssh_options)
51
- @sftp = @ssh.sftp.connect
52
- end
53
-
54
- def close
55
- @ssh.close
56
- # @sftp.close not needed
57
- @ssh, @sftp = nil
58
- end
59
-
60
- def create_directory path
61
- remote do |ssh, sftp|
62
- sftp.mkdir! path
63
- # exec "mkdir #{path}"
64
- end
65
- end
66
-
67
- def remove_directory path
68
- exec "rm -r #{path}"
69
- end
70
-
71
- def upload_directory from_local_path, to_remote_path
72
- remote do |ssh, sftp|
73
- sftp.upload! from_local_path, fix_path(to_remote_path)
74
- end
75
- end
76
-
77
- def download_directory from_remote_path, to_local_path
78
- remote do |ssh, sftp|
79
- sftp.download! fix_path(from_remote_path), to_local_path, :recursive => true
80
- end
81
- end
82
-
83
- protected
84
- def fix_path path
85
- path.sub(/^\~/, home)
86
- end
87
-
88
- def home
89
- unless @home
90
- command = 'cd ~; pwd'
91
- code, stdout, stderr = exec command
92
- raise "can't execute '#{command}'!" unless code == 0
93
- @home = stdout.gsub("\n", '')
94
- end
95
- @home
96
- end
97
-
98
- # taken from here http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library/3386375#3386375
99
- def hacked_exec!(ssh, command, &block)
100
- stdout_data = ""
101
- stderr_data = ""
102
- exit_code = nil
103
- exit_signal = nil
104
-
105
- channel = ssh.open_channel do |channel|
106
- channel.exec(command) do |ch, success|
107
- raise "could not execute command: #{command.inspect}" unless success
108
-
109
- channel.on_data{|ch2, data| stdout_data << data}
110
- channel.on_extended_data{|ch2, type, data| stderr_data << data}
111
- channel.on_request("exit-status"){|ch,data| exit_code = data.read_long}
112
- channel.on_request("exit-signal"){|ch, data| exit_signal = data.read_long}
113
- end
114
- end
115
-
116
- channel.wait
117
-
118
- [stdout_data, stderr_data, exit_code, exit_signal]
119
- end
120
-
121
- def remote(&block)
122
- if @ssh
123
- block.call @ssh, @sftp
124
- else
125
- # Rails.logger.info "Connecting to remote Hadoop #{options[:user]}@#{options[:host]}"
126
- begin
127
- open
128
- block.call @ssh, @sftp
129
- ensure
130
- close
131
- end
132
-
133
- # Net::SSH.start options[:host], options[:user], :config => true do |ssh|
134
- # ssh.sftp.connect do |sftp|
135
- # begin
136
- # @ssh, @sftp = ssh, sftp
137
- # block.call @ssh, @sftp
138
- # ensure
139
- # @ssh, @sftp = nil
140
- # end
141
- # end
142
- # end
143
- end
144
- end
145
- end
146
- end
147
- end
data/lib/rsh/gems.rb DELETED
@@ -1,2 +0,0 @@
1
- gem 'net-ssh'
2
- gem 'net-sftp'
File without changes
@@ -1 +0,0 @@
1
- some content
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- shared_examples_for 'abstract driver' do
4
- dir = "#{File.dirname __FILE__}/abstract_driver"
5
- with_tmp_spec_dir dir, before: :each
6
-
7
- describe "io" do
8
- before :each do
9
- @local_dir = spec_dir
10
- @remote_dir = @driver.generate_tmp_dir_name
11
-
12
- @driver.remove_directory @remote_dir if @driver.directory_exist? @remote_dir
13
- @driver.create_directory @remote_dir
14
- end
15
-
16
- after :each do
17
- @driver.remove_directory @remote_dir if @driver.directory_exist? @remote_dir
18
- end
19
-
20
- describe "files" do
21
- before :each do
22
- @local_file = "#{@local_dir}/local_file"
23
- @check_file = "#{@local_dir}/check_file"
24
- @remote_file = "#{@remote_dir}/remote_file"
25
- end
26
-
27
- it "file_exist?" do
28
- @driver.file_exist?(@remote_file).should be_false
29
- @driver.upload_file(@local_file, @remote_file)
30
- @driver.file_exist?(@remote_file).should be_true
31
- end
32
-
33
- it "upload & download file" do
34
- @driver.upload_file(@local_file, @remote_file)
35
- @driver.file_exist?(@remote_file).should be_true
36
-
37
- @driver.download_file(@remote_file, @check_file)
38
- File.read(@local_file).should == File.read(@check_file)
39
- end
40
-
41
- it "remove_file" do
42
- @driver.upload_file(@local_file, @remote_file)
43
- @driver.file_exist?(@remote_file).should be_true
44
- @driver.remove_file(@remote_file)
45
- @driver.file_exist?(@remote_file).should be_false
46
- end
47
- end
48
-
49
- describe 'directories' do
50
- before :each do
51
- @from_local, @remote_path, @to_local = "#{@local_dir}/dir", "#{@remote_dir}/upload", "#{@local_dir}/download"
52
- end
53
-
54
- it "directory_exist?, create_directory, remove_directory" do
55
- dir = "#{@remote_dir}/some_dir"
56
- @driver.directory_exist?(dir).should be_false
57
- @driver.create_directory(dir)
58
- @driver.directory_exist?(dir).should be_true
59
- @driver.remove_directory(dir)
60
- @driver.directory_exist?(dir).should be_false
61
- end
62
-
63
- it "upload_directory & download_directory" do
64
- upload_path_check = "#{@remote_path}/dir2/file"
65
- @driver.file_exist?(upload_path_check).should be_false
66
- @driver.upload_directory(@from_local, @remote_path)
67
- @driver.file_exist?(upload_path_check).should be_true
68
-
69
- download_path_check = "#{@to_local}/dir2/file"
70
- File.exist?(download_path_check).should be_false
71
- @driver.download_directory(@remote_path, @to_local)
72
- File.exist?(download_path_check).should be_true
73
- end
74
- end
75
- end
76
-
77
- describe "shell" do
78
- it 'exec' do
79
- @driver.exec("echo 'ok'").should == [0, "ok\n", ""]
80
- end
81
- end
82
- end
File without changes
@@ -1 +0,0 @@
1
- some content
@@ -1,9 +0,0 @@
1
- require 'abstract_driver'
2
-
3
- describe Rsh::Drivers::Local do
4
- it_should_behave_like "abstract driver"
5
-
6
- before :each do
7
- @driver = Rsh::Drivers::Local.new
8
- end
9
- end
@@ -1,15 +0,0 @@
1
- require 'abstract_driver'
2
- require 'yaml'
3
-
4
- describe Rsh::Drivers::Ssh do
5
- it_should_behave_like "abstract driver"
6
-
7
- before :each do
8
- @driver = Rsh::Drivers::Ssh.new config[:remote_driver]
9
- @driver.open
10
- end
11
-
12
- after :each do
13
- @driver.close
14
- end
15
- end