Skip to content

Commit

Permalink
Merge pull request #8 from NickLaMuro/use-net-ssh
Browse files Browse the repository at this point in the history
Refactor and use net-ssh
  • Loading branch information
invernizzi committed Sep 14, 2015
2 parents 81e4c45 + af057c6 commit 41e508c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
.ruby-version
.ruby-gemset
73 changes: 42 additions & 31 deletions lib/vagrant/scp/commands/scp.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'net/scp'

module VagrantPlugins
module Scp
module Command
Expand All @@ -9,45 +11,28 @@ def self.synopsis
end

def execute
# Parse the arguments
file_1, file_2 = parse_args()
return if file_2.nil?
# Extract host info
# We want to get the name of the vm, from a [host_1]:file_1 [host_2]:file_2 description
host = [file_1, file_2].map{|file_spec| file_spec.match(/^([^:]*):/)[1] rescue nil}.compact.first
# The default machine name for Vagrant is 'default'
host = 'default' if (host.nil? || host == 0 )
@file_1, @file_2 = parse_args()
return if @file_2.nil?

# Get the info about the target VM
with_target_vms(host) do |machine|
ssh_info = machine.ssh_info
raise Vagrant::Errors::SSHNotReady if ssh_info.nil?
if file_1.include? ':'
source_files = "#{ssh_info[:username]}@#{ssh_info[:host]}:#{file_1.split(':').last}"
target_files = file_2
else
source_files = file_1
target_files = "#{ssh_info[:username]}@#{ssh_info[:host]}:#{file_2.split(':').last}"
end
@ssh_info = machine.ssh_info
raise Vagrant::Errors::SSHNotReady if @ssh_info.nil?

# Run the SCP
command = [
"scp",
'-r',
'-o StrictHostKeyChecking=no',
'-o UserKnownHostsFile=/dev/null',
"-o port=#{ssh_info[:port]}",
"-i '#{ssh_info[:private_key_path][0]}'",
source_files,
target_files
].join(' ')
system(command)
Net::SCP.send net_ssh_command,
@ssh_info[:host],
@ssh_info[:username],
source_files,
target_files,
:recursive => true,
:ssh => {:port => @ssh_info[:port], :keys => @ssh_info[:private_key_path]}
end
end

private

def parse_args
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant scp <local_path> [vm_name]:<remote_path> \n"
o.banner = "Usage: vagrant scp <local_path> [vm_name]:<remote_path> \n"
o.banner += " vagrant scp [vm_name]:<remote_path> <local_path>"
o.separator ""
o.separator "Options:"
Expand All @@ -59,6 +44,32 @@ def parse_args
return nil, nil
end

def host
host = [@file_1, @file_2].map{|file_spec| file_spec.match(/^([^:]*):/)[1] rescue nil}.compact.first
host = 'default' if (host.nil? || host == 0 )
host
end

def net_ssh_command
@file_1.include?(':') ? :download! : :upload!
end

def source_files
format_file_path(@file_1)
end

def target_files
format_file_path(@file_2)
end

def format_file_path(filepath)
if @file_1.include?(':')
filepath.split(':').last.gsub("~", "/home/#{@ssh_info[:username]}")
else
filepath
end
end

end

end
Expand Down

0 comments on commit 41e508c

Please sign in to comment.