Class: ThorSsh::RemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/thor-ssh/remote_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, connection) ⇒ RemoteFile

Returns a new instance of RemoteFile.



10
11
12
13
# File 'lib/thor-ssh/remote_file.rb', line 10

def initialize(base, connection)
  @base = base
  @connection = connection
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



8
9
10
# File 'lib/thor-ssh/remote_file.rb', line 8

def base
  @base
end

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/thor-ssh/remote_file.rb', line 7

def connection
  @connection
end

Instance Method Details

#binread(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/thor-ssh/remote_file.rb', line 57

def binread(path)
  # If we first logged in as the running user
  if base.destination_server.running_as_current_user?
    data = nil
    connection.sftp.file.open(path, "rb") do |f|
      data = f.read
    end
    close_sftp!
  else
    # We just run this as root, when reading we don't need to go back
    # down to the user
    data = @base.destination_server.run("cat \"#{path}\"")
  end

  return data
end

#binwrite(path, data) ⇒ Object

TODO: we should just move this to a more standard thing



75
76
77
78
79
# File 'lib/thor-ssh/remote_file.rb', line 75

def binwrite(path, data)
  io = StringIO.new(data)
  connection.sftp.upload!(io, path)
  close_sftp!
end

#chmod(mode, file_name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/thor-ssh/remote_file.rb', line 81

def chmod(mode, file_name)
  if mode.is_a?(Integer)
    # Mode is an integer, convert to octal
    mode = '%04d' % mode.to_s(8)
  end
  
  return run("chmod #{mode} \"#{file_name}\"")
end

#close_sftp!Object

This is a workaround for bug: github.com/net-ssh/net-sftp/issues/13



17
18
19
20
# File 'lib/thor-ssh/remote_file.rb', line 17

def close_sftp!
  connection.sftp.close_channel()
  connection.instance_variable_set('@sftp', nil)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/thor-ssh/remote_file.rb', line 22

def exists?(path)
  begin
    res = connection.sftp.stat!(path)
  rescue Net::SFTP::StatusException
    close_sftp!
    return false
  end
  
  close_sftp!
  return true
end

#identical?(file1, file2) ⇒ Boolean

See if these paths point to the same inode

Returns:

  • (Boolean)


95
96
97
# File 'lib/thor-ssh/remote_file.rb', line 95

def identical?(file1, file2)
  inode(file1) == inode(file2)
end

#inode(file_name) ⇒ Object



90
91
92
# File 'lib/thor-ssh/remote_file.rb', line 90

def inode(file_name)
  return run("ls -i \"#{file_name}\"").strip.split(/ /).first
end


53
54
55
# File 'lib/thor-ssh/remote_file.rb', line 53

def link(old_name, new_name)
  run("ln \"#{old_name}\" \"#{new_name}\"")
end

#mkdir_p(path) ⇒ Object

Creates the directory at the path on the remote server



39
40
41
# File 'lib/thor-ssh/remote_file.rb', line 39

def mkdir_p(path)
  run "mkdir -p \"#{path}\""
end

#rm_rf(path) ⇒ Object Also known as: unlink

Remote the file/folder on the remote server



44
45
46
# File 'lib/thor-ssh/remote_file.rb', line 44

def rm_rf(path)
  run "rm -rf \"#{path}\""
end

#run(command) ⇒ Object



34
35
36
# File 'lib/thor-ssh/remote_file.rb', line 34

def run(command)
  return base.exec(command)
end


49
50
51
# File 'lib/thor-ssh/remote_file.rb', line 49

def symlink(old_name, new_name)
  run("ln -s \"#{old_name}\" \"#{new_name}\"")
end