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.



12
13
14
15
# File 'lib/thor-ssh/remote_file.rb', line 12

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

#binread(path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/thor-ssh/remote_file.rb', line 68

def binread(path)
  # If we first logged in as the running user
  if base.destination_server.running_as_current_user?
    data = nil
    begin
      # Attempt to upload to the path
      data = connection.sftp.upload!(path)
    rescue Net::SFTP::StatusException => e
      close_sftp!
      raise PermissionError, "Unable to download #{path}"
    end
    
    # 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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/thor-ssh/remote_file.rb', line 94

def binwrite(path, data)
  data = data.gsub(/\n\r/, "\n").gsub(/\r\n/, "\n")
  
  io = StringIO.new(data)
  begin
    # Attempt to upload to the path
    connection.sftp.upload!(io, path)
  rescue Net::SFTP::StatusException => e
    # In the event we don't have permission, upload to 
    # a temp dir, then copy with sudo
    close_sftp!
    
    temp_dir = nil
    base.as_user(nil) do
      current_dir = run("pwd").strip
      temp_dir = "#{current_dir}/.thorssh"
      # Make the dir as the default user
      run("mkdir -p \"#{temp_dir}\"")
    end
    
    temp_file = "#{temp_dir}/#{File.basename(path)}"
    
    # io = StringIO.new(data)
    io = StringIO.new(data)
    connection.sftp.upload!(io, temp_file)
    close_sftp!


    user = base.run_as_user

    # Move the file as the user
    base.as_root do
      folder_path = File.dirname(path)
      unless base.destination_files.exists?(folder_path)
        # Create the directory this is supposed to transfer into
        mkdir_p(folder_path)
        
        # And set the correct user/group
        if user
          chown(user, user, folder_path)
        end
      end
      
      # Copy the text from the source file
      run("cat #{temp_file.inspect} > #{path.inspect}")
      run("rm -f #{temp_file.inspect}")
    end
    
    unless base.destination_files.exists?(path)
      # Something went wrong
      raise PermissionError, "#{path} failed to create/update failed" 
    end
    
    if user
      # Set the correct user as if this user had uploaded
      # directly.
      base.as_root do
        chown(user, user, path)
      end
    end

    return
  end
  close_sftp!
end

#chmod(mode, file_name) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/thor-ssh/remote_file.rb', line 160

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

#chown(user, group, list, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/thor-ssh/remote_file.rb', line 169

def chown(user, group, list, options={})
  changed = []
  not_changed = []
  recursive_flag = options[:recursive] ? '-R' : ''
  [list].flatten.each do |file|
    _, _, file_user, file_group = exec("ls -lh #{file.inspect}").split(/\n/).reject {|l| l =~ /^total/ }.last.split(/\s/)
    if user == file_user && group == file_group
      not_changed << file
    else
      changed << file
      base.as_root do
        # We can just always run this as root for the user
        exec("chown #{recursive_flag} #{user}:#{group} #{file.inspect}")
      end
    end
  end
  
  return changed, not_changed
end

#chown_R(user, group, list) ⇒ Object



189
190
191
# File 'lib/thor-ssh/remote_file.rb', line 189

def chown_R(user, group, list)
  return chown(user, group, list, :recursive => true)
end

#close_sftp!Object

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



19
20
21
22
# File 'lib/thor-ssh/remote_file.rb', line 19

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

#exec(command, options = {}) ⇒ Object



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

def exec(command, options={})
  return base.exec(command, options)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


198
199
200
# File 'lib/thor-ssh/remote_file.rb', line 198

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

#inode(file_name) ⇒ Object



193
194
195
# File 'lib/thor-ssh/remote_file.rb', line 193

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


64
65
66
# File 'lib/thor-ssh/remote_file.rb', line 64

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



45
46
47
48
49
50
51
52
# File 'lib/thor-ssh/remote_file.rb', line 45

def mkdir_p(path)
  stdout, stderr, _, _ = exec("mkdir -p #{path.inspect}", :with_codes => true)

  if stderr =~ /Permission denied/
    base.say_status :permission_error, stderr, :red
    raise PermissionError, "unable to create directory #{path}"
  end
end

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

Remote the file/folder on the remote server



55
56
57
# File 'lib/thor-ssh/remote_file.rb', line 55

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

#run(command) ⇒ Object



36
37
38
# File 'lib/thor-ssh/remote_file.rb', line 36

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


60
61
62
# File 'lib/thor-ssh/remote_file.rb', line 60

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