2013-09-03 18:01:12 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module FileUpload
|
|
|
|
class Provisioner < Vagrant.plugin("2", :provisioner)
|
|
|
|
def provision
|
|
|
|
@machine.communicate.tap do |comm|
|
2017-08-10 22:49:47 +00:00
|
|
|
source = File.expand_path(config.source)
|
2014-02-01 04:23:09 +00:00
|
|
|
destination = expand_guest_path(config.destination)
|
|
|
|
|
2017-08-10 22:49:47 +00:00
|
|
|
# if source is a directory, make it then trim destination with dirname
|
2013-09-03 18:01:12 +00:00
|
|
|
# Make sure the remote path exists
|
2017-08-10 22:49:47 +00:00
|
|
|
if File.directory?(source)
|
|
|
|
# We need to make sure the actual destination folder
|
|
|
|
# also exists before uploading, otherwise
|
|
|
|
# you will get nested folders. We also need to append
|
|
|
|
# a './' to the source folder so we copy the contents
|
|
|
|
# rather than the folder itself, in case a users destination
|
|
|
|
# folder differs from its source.
|
|
|
|
#
|
|
|
|
# https://serverfault.com/questions/538368/make-scp-always-overwrite-or-create-directory
|
|
|
|
# https://unix.stackexchange.com/questions/292641/get-scp-path-behave-like-rsync-path/292732
|
2017-08-14 22:34:10 +00:00
|
|
|
command = "mkdir -p \"%s\"" % destination
|
2017-08-10 22:49:47 +00:00
|
|
|
source << "/."
|
|
|
|
else
|
2017-08-14 22:34:10 +00:00
|
|
|
command = "mkdir -p \"%s\"" % File.dirname(destination)
|
2017-08-10 22:49:47 +00:00
|
|
|
end
|
2014-02-01 04:23:09 +00:00
|
|
|
comm.execute(command)
|
2013-09-03 18:01:12 +00:00
|
|
|
|
|
|
|
# now upload the file
|
2017-08-10 22:49:47 +00:00
|
|
|
comm.upload(source, destination)
|
2014-02-01 04:23:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Expand the guest path if the guest has the capability
|
|
|
|
def expand_guest_path(destination)
|
|
|
|
if machine.guest.capability?(:shell_expand_guest_path)
|
|
|
|
machine.guest.capability(:shell_expand_guest_path, destination)
|
|
|
|
else
|
|
|
|
destination
|
2013-09-03 18:01:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|