Class: VagrantPlugins::Scp::Command::Scp

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/scp/commands/scp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



7
8
9
# File 'lib/vagrant/scp/commands/scp.rb', line 7

def self.synopsis
  "copies data into a box via SCP"
end

Instance Method Details

#executeObject



11
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
# File 'lib/vagrant/scp/commands/scp.rb', line 11

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

  # 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

    # 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)
  end
end

#parse_argsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant/scp/commands/scp.rb', line 47

def parse_args
  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant scp <files> <target> [vm_name]"
    o.separator ""
    o.separator "Options:"
    o.separator ""
  end
  argv = parse_options(opts)
  return argv if argv and  argv.length == 2
  @env.ui.info(opts.help, prefix: false) if argv
  return nil, nil
end