push via sftp issue when file parent directory does not exist

Overcomes exception Net::SFTP::StatusException (2, "no such file") when using: "vagrant push" via sftp and a file parent directory does not exist. Function "upload" does not create the directory before uploading a file ('mkdir: true' seems to have no effect as zero directories are created while files are uploaded normally).
This commit is contained in:
Phivos Stylianides 2015-09-24 20:08:31 +03:00 committed by Seth Vargo
parent 197a2d8799
commit a27e7e106a
1 changed files with 16 additions and 1 deletions

View File

@ -106,6 +106,7 @@ module VagrantPlugins
def initialize(*)
require "net/sftp"
super
@dirs = []
end
def default_port
@ -120,7 +121,21 @@ module VagrantPlugins
end
def upload(local, remote)
@server.upload!(local, remote, mkdir: true)
dir = File.dirname(remote)
fullpath = Pathname.new(dir)
fullpath.descend do |path|
if !@dirs.include?(path.to_s)
@dirs.push(path.to_s) # Cache visited directories in a list to avoid duplicate requests
begin
@server.mkdir!(path.to_s)
rescue Net::SFTP::StatusException => e
# Directory exists, skip...
end
end
end
@server.upload!(local, remote)
end
end
end