Class: Rsh::Drivers::Ssh

Inherits:
Abstract show all
Defined in:
lib/rsh/drivers/ssh.rb

Instance Attribute Summary

Attributes inherited from Abstract

#options

Instance Method Summary collapse

Methods inherited from Abstract

#generate_tmp_dir_name

Constructor Details

#initialize(options = {}) ⇒ Ssh

Returns a new instance of Ssh.



4
5
6
7
8
# File 'lib/rsh/drivers/ssh.rb', line 4

def initialize options = {}
  super
  raise "ssh options not provided!" unless options[:ssh]
  raise "invalid ssh options!" unless options[:ssh].is_a?(Hash)
end

Instance Method Details

#bulk(&block) ⇒ Object



68
69
70
# File 'lib/rsh/drivers/ssh.rb', line 68

def bulk &block
  remote &block
end

#close_connectionObject



62
63
64
65
66
# File 'lib/rsh/drivers/ssh.rb', line 62

def close_connection
  @ssh.close
  # @sftp.close not needed
  @ssh, @sftp = nil
end

#create_directory(path) ⇒ Object



72
73
74
75
76
77
# File 'lib/rsh/drivers/ssh.rb', line 72

def create_directory path
  remote do |ssh, sftp|          
    sftp.mkdir! path
    # exec "mkdir #{path}"
  end        
end

#download_directory(from_remote_path, to_local_path) ⇒ Object



89
90
91
92
93
# File 'lib/rsh/drivers/ssh.rb', line 89

def download_directory from_remote_path, to_local_path
  remote do |ssh, sftp|
    sftp.download! from_remote_path, to_local_path, :recursive => true
  end
end

#download_file(from_remote_path, to_local_path) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rsh/drivers/ssh.rb', line 16

def download_file from_remote_path, to_local_path
  File.open to_local_path, "w" do |out|
    remote do |ssh, sftp|
      sftp.download! from_remote_path, out #, :recursive => true          
    end
  end
end

#exec(command) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rsh/drivers/ssh.rb', line 43

def exec command
  remote do |ssh, sftp|
    # somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
    # command = ". ~/.profile && #{command}"

    stdout, stderr, code, signal = hacked_exec! ssh, command

    return code, stdout, stderr
  end
end

#exist?(remote_file_path) ⇒ Boolean Also known as: directory_exist?, file_exist?

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
# File 'lib/rsh/drivers/ssh.rb', line 24

def exist? remote_file_path
  remote do |ssh, sftp|
    begin
      fattrs = sftp.stat! remote_file_path
      fattrs.directory? or fattrs.file? or fattrs.symlink?
    rescue Net::SFTP::StatusException
      false
    end
  end
end

#open_connectionObject



54
55
56
57
58
59
60
# File 'lib/rsh/drivers/ssh.rb', line 54

def open_connection      
  ssh_options = self.options[:ssh].clone
  host = options[:host] || raise('host not provided!')
  user = ssh_options.delete(:user) || raise('user not provied!')
  @ssh = Net::SSH.start(host, user, ssh_options)
  @sftp = @ssh.sftp.connect
end

#remove_directory(path) ⇒ Object



79
80
81
# File 'lib/rsh/drivers/ssh.rb', line 79

def remove_directory path
  exec "rm -r #{path}"
end

#remove_file(remote_file_path) ⇒ Object



37
38
39
40
41
# File 'lib/rsh/drivers/ssh.rb', line 37

def remove_file remote_file_path
  remote do |ssh, sftp|
    sftp.remove! remote_file_path
  end
end

#upload_directory(from_local_path, to_remote_path) ⇒ Object



83
84
85
86
87
# File 'lib/rsh/drivers/ssh.rb', line 83

def upload_directory from_local_path, to_remote_path
  remote do |ssh, sftp|
    sftp.upload! from_local_path, to_remote_path
  end
end

#upload_file(from_local_path, to_remote_path) ⇒ Object



10
11
12
13
14
# File 'lib/rsh/drivers/ssh.rb', line 10

def upload_file from_local_path, to_remote_path
  remote do |ssh, sftp|
    sftp.upload! from_local_path, to_remote_path
  end
end