module Vagrant module Zscp # This is a helper that abstracts out the functionality of scping # folders so that it can be called from anywhere. class ZscpHelper def self.scp(machine, ssh_info, opts) guestpath = opts[:guestpath] hostpath = opts[:hostpath] hostpath = File.expand_path(hostpath, machine.env.root_path) hostpath = Vagrant::Util::Platform.fs_real_path(hostpath).to_s if (opts[:zscp_include]) included_files = opts[:zscp_include].join(' ') else included_files = '.' end username = ssh_info[:username] host = ssh_info[:host] if !hostpath.end_with?("/") hostpath += "/" end machine.ui.info("Sending #{hostpath} to #{guestpath}") machine.ui.info("Removing remote #{guestpath}") remotely_execute(ssh_info, "sudo rm -rf #{guestpath}; sudo mkdir -p #{guestpath}; sudo chown #{username}:$(id -gn) #{guestpath}") temp_file = `mktemp -t 'temp.XXXX.tar.gz'`.strip machine.ui.info("Compressing #{hostpath} into #{temp_file}") system("tar -czLf #{temp_file} -C #{hostpath} #{included_files}") machine.ui.info("Copying #{temp_file} to #{guestpath}") command = [ "scp", '-o StrictHostKeyChecking=no', '-o UserKnownHostsFile=/dev/null', "-o port=#{ssh_info[:port]}", "-i '#{ssh_info[:private_key_path][0]}'", "#{temp_file}", "#{username}@#{host}:#{guestpath}" ].join(' ') system(command) machine.ui.info("Extracting remotely #{guestpath}/temp.*.tar.gz") remotely_execute(ssh_info, "tar -xzf #{guestpath}/temp.*.tar.gz -C #{guestpath}; rm #{guestpath}/temp.*.tar.gz") machine.ui.info("#{guestpath} synchronised") end def self.remotely_execute(ssh_info, command) ssh_command = [ 'ssh', '-o StrictHostKeyChecking=no', '-o UserKnownHostsFile=/dev/null', "-o port=#{ssh_info[:port]}", "-i '#{ssh_info[:private_key_path][0]}'", "#{ssh_info[:username]}@#{ssh_info[:host]}", "'#{command}'" ].join(' ') system(ssh_command) end end end end