`download` operation
This commit is contained in:
parent
f8ab516082
commit
64ece507ad
|
@ -17,6 +17,13 @@ module Vagrant
|
||||||
def ready?
|
def ready?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Download a file from the virtual machine to the local machine.
|
||||||
|
#
|
||||||
|
# @param [String] from Path of the file on the virtual machine.
|
||||||
|
# @param [String] to Path to where to save the remote file.
|
||||||
|
def download(from, to)
|
||||||
|
end
|
||||||
|
|
||||||
# Upload a file to the virtual machine.
|
# Upload a file to the virtual machine.
|
||||||
#
|
#
|
||||||
# @param [String] from Path to a file to upload.
|
# @param [String] from Path to a file to upload.
|
||||||
|
|
|
@ -71,20 +71,20 @@ module Vagrant
|
||||||
execute(command, opts, &block)
|
execute(command, opts, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def download(from, to=nil)
|
||||||
|
@logger.debug("Downloading: #{from} to #{to}")
|
||||||
|
|
||||||
|
scp_connect do |scp|
|
||||||
|
scp.download!(from, to)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def upload(from, to)
|
def upload(from, to)
|
||||||
@logger.debug("Uploading: #{from} to #{to}")
|
@logger.debug("Uploading: #{from} to #{to}")
|
||||||
|
|
||||||
# Do an SCP-based upload...
|
scp_connect do |scp|
|
||||||
connect do |connection|
|
|
||||||
scp = Net::SCP.new(connection)
|
|
||||||
scp.upload!(from, to)
|
scp.upload!(from, to)
|
||||||
end
|
end
|
||||||
rescue Net::SCP::Error => e
|
|
||||||
# If we get the exit code of 127, then this means SCP is unavailable.
|
|
||||||
raise Errors::SCPUnavailable if e.message =~ /\(127\)/
|
|
||||||
|
|
||||||
# Otherwise, just raise the error up
|
|
||||||
raise
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
@ -219,6 +219,22 @@ module Vagrant
|
||||||
# Return the final exit status
|
# Return the final exit status
|
||||||
return exit_status
|
return exit_status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Opens an SCP connection and yields it so that you can download
|
||||||
|
# and upload files.
|
||||||
|
def scp_connect
|
||||||
|
# Connect to SCP and yield the SCP object
|
||||||
|
connect do |connection|
|
||||||
|
scp = Net::SCP.new(connection)
|
||||||
|
return yield scp
|
||||||
|
end
|
||||||
|
rescue Net::SCP::Error => e
|
||||||
|
# If we get the exit code of 127, then this means SCP is unavailable.
|
||||||
|
raise Errors::SCPUnavailable if e.message =~ /\(127\)/
|
||||||
|
|
||||||
|
# Otherwise, just raise the error up
|
||||||
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,19 @@ module Vagrant
|
||||||
@vm = vm
|
@vm = vm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Download a file from the remote virtual machine. If `to` is nil, then
|
||||||
|
# the contents are returned as a string.
|
||||||
|
#
|
||||||
|
# @param [String] from Path on the remote VM to download.
|
||||||
|
# @param [String] to Path to a local file to save to.
|
||||||
|
# @return [String] File contents if `to` is nil.
|
||||||
|
def download(from, to=nil)
|
||||||
|
raise Errors::VMNotRunningError if @vm.state != :running
|
||||||
|
|
||||||
|
@logger.info("download: #{from} => #{to}")
|
||||||
|
@vm.channel.download(from, to)
|
||||||
|
end
|
||||||
|
|
||||||
# Runs a command on the local machine. This will return an object where
|
# Runs a command on the local machine. This will return an object where
|
||||||
# you can access the `exit_code`, `stdout`, and `stderr` easiy:
|
# you can access the `exit_code`, `stdout`, and `stderr` easiy:
|
||||||
#
|
#
|
||||||
|
@ -69,6 +82,8 @@ module Vagrant
|
||||||
# @param [String] from Path to a local file or an IO object.
|
# @param [String] from Path to a local file or an IO object.
|
||||||
# @param [String] to Path where to upload to.
|
# @param [String] to Path where to upload to.
|
||||||
def upload(from, to)
|
def upload(from, to)
|
||||||
|
raise Errors::VMNotRunningError if @vm.state != :running
|
||||||
|
|
||||||
# If we're dealing with an IO object, then save it to a temporary
|
# If we're dealing with an IO object, then save it to a temporary
|
||||||
# file and upload that. We define `temp = nil` here so that it
|
# file and upload that. We define `temp = nil` here so that it
|
||||||
# doesn't go out of scope and get GC'd until after the method
|
# doesn't go out of scope and get GC'd until after the method
|
||||||
|
|
Loading…
Reference in New Issue