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

#closeObject



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

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

#create_directory(path) ⇒ Object



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

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



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

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

#download_file(from_remote_path, to_local_path) ⇒ Object



14
15
16
17
18
# File 'lib/rsh/drivers/ssh.rb', line 14

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

#exec(command) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rsh/drivers/ssh.rb', line 35

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)


20
21
22
23
24
25
26
27
# File 'lib/rsh/drivers/ssh.rb', line 20

def exist? remote_file_path
  begin
    fattrs = sftp.stat! fix_path(remote_file_path)
    fattrs.directory? or fattrs.file? or fattrs.symlink?
  rescue Net::SFTP::StatusException
    false
  end
end

#openObject



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

def open      
  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



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

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

#remove_file(remote_file_path) ⇒ Object



31
32
33
# File 'lib/rsh/drivers/ssh.rb', line 31

def remove_file remote_file_path
  sftp.remove! fix_path(remote_file_path)
end

#upload_directory(from_local_path, to_remote_path) ⇒ Object



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

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

#upload_file(from_local_path, to_remote_path) ⇒ Object



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

def upload_file from_local_path, to_remote_path
  sftp.upload! from_local_path, fix_path(to_remote_path)
end