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}") Dir.mktmpdir { |dir| machine.ui.info("Removing remote #{guestpath}") machine.communicate.execute("sudo rm -rf #{guestpath}; sudo mkdir -p #{guestpath}; sudo chown #{username}:$(id -gn) #{guestpath}") machine.ui.info("Compressing #{hostpath} into #{dir}/temp.tar.gz") `tar -czLf #{dir}/temp.tar.gz -C #{hostpath} #{included_files}` machine.ui.info("Copying #{dir}/temp.tar.gz to #{guestpath}") command = [ "scp", '-o StrictHostKeyChecking=no', '-o UserKnownHostsFile=/dev/null', "-o port=#{ssh_info[:port]}", "-i '#{ssh_info[:private_key_path][0]}'", "#{dir}/temp.tar.gz", "#{username}@#{host}:#{guestpath}" ].join(' ') system(command) machine.ui.info("Extracting remotely #{guestpath}/temp.tar.gz") machine.communicate.execute("tar -xzf #{guestpath}/temp.tar.gz -C #{guestpath}; rm #{guestpath}/temp.tar.gz") } machine.ui.info("#{guestpath} synchronised") end end end end