Class: Vagrant::Zscp::ZscpHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/zscp/helper.rb

Overview

This is a helper that abstracts out the functionality of scping folders so that it can be called from anywhere.

Class Method Summary collapse

Class Method Details

.cleanup(machine, ssh_info, opts) ⇒ Object



6
7
8
9
10
# File 'lib/vagrant/zscp/helper.rb', line 6

def self.cleanup(machine, ssh_info, opts)
  guestpath = opts[:guestpath]
  machine.ui.info("Removing remote #{guestpath}")
  remotely_execute(ssh_info, "sudo rm -rf #{guestpath};")
end

.remotely_execute(ssh_info, command) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vagrant/zscp/helper.rb', line 53

def self.remotely_execute(ssh_info, command)
  ssh_command = [
    'ssh',
    '-o StrictHostKeyChecking=no',
    '-o LogLevel=quiet',
    '-o UserKnownHostsFile=/dev/null',
    "-o port=#{ssh_info[:port]}",
    "-i '#{ssh_info[:private_key_path][0]}'",
    "#{ssh_info[:username]}@#{ssh_info[:host]}",
    "'#{command}'"
  ].join(' ')
  `#{ssh_command}`
end

.scp(machine, ssh_info, opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant/zscp/helper.rb', line 12

def self.scp(machine, ssh_info, opts)
  guestpath = opts[:guestpath]
  guestpath += "/" if !guestpath.end_with?("/")
  hostpath  = File.expand_path(opts[:hostpath], machine.env.root_path)
  hostpath  = Vagrant::Util::Platform.fs_real_path(hostpath).to_s
  hostpath += "/" if !hostpath.end_with?("/")

  if (opts[:zscp_include])
    included_files = opts[:zscp_include].join(' ')
  else
    included_files = '.'
  end

  username = ssh_info[:username]
  host     = ssh_info[:host]

  machine.ui.info("Sending #{hostpath} to #{guestpath}")
  remotely_execute(ssh_info, "sudo mkdir -p #{guestpath} 2> /dev/null; sudo chown #{username}:$(id -gn #{username}) #{guestpath}")

  temp_file = `mktemp -t 'temp.tar.gz' 2> /dev/null || mktemp -t 'temp.tar.gz.XXXX'`.strip
  temp_file_name = Pathname.new(temp_file).basename
  machine.ui.info("Compressing #{hostpath} into #{temp_file}")
  `tar -czhf #{temp_file} -C #{hostpath} #{included_files}`

  machine.ui.info("Copying #{temp_file} to #{guestpath}")
  command = [
    "scp",
    '-o StrictHostKeyChecking=no',
    '-o LogLevel=quiet',
    '-o UserKnownHostsFile=/dev/null',
    "-o port=#{ssh_info[:port]}",
    "-i '#{ssh_info[:private_key_path][0]}'",
    "#{temp_file}",
    "#{username}@#{host}:#{guestpath}"
  ].join(' ')
  `#{command}`

  remotely_execute(ssh_info, "tar -xzf #{guestpath}#{temp_file_name} -C #{guestpath}; rm #{guestpath}#{temp_file_name}")
  machine.ui.info("#{guestpath} synchronised")
end