Class: VagrantPlugins::VBoxManage::Command::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vboxmanage/commands/root.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



6
7
8
9
10
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
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant-vboxmanage/commands/root.rb', line 6

def execute
  options = {}

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant vboxmanage [vm-name] [--] <vboxmanage-cmd> [UUID] [cmd-args]..."
  end

  if split_index = @argv.index('--')
    # of form `vagrant vboxmanage [vm-name] -- showvminfo ...`
    options[:command] = @argv[split_index + 1]
    options[:extra_args] = @argv.drop(split_index + 2) 
    @argv = @argv.take(split_index)
  elsif command_index = @argv.index { |x| vboxmanage_commands_all.include?(x)}
    # of form `vagrant vboxmanage [vm-name] showvminfo...`
    options[:command] = @argv[command_index]
    options[:extra_args] = @argv.drop(command_index + 1)
    @argv = @argv.take(command_index)
  else
    @env.ui.error opts
    @env.ui.error "No valid VBoxManage subcommand detected."
    return 1
  end

  @logger.debug "Parsing options:" + options.merge({"argv" => @argv}).to_yaml

  argv = parse_options(opts)
  return if argv.length > 1

  with_target_vms(argv, single_target: true) do |machine|
    if machine.state.id == :not_created
      machine.env.ui.error("Target machine is not created, unable to run VBoxManage.")
      return 1
    end

    if vboxmanage_commands_standard.include?(options[:command])
      @logger.debug("Performing UUID insertion for standard vboxmanage command: #{options[:command]}")
      args = [options[:command], machine.id] + options[:extra_args]
    else
      @logger.debug("Skipping UUID insertion for special vboxmanage command: #{options[:command]}")
      # TODO: handle substitution for commands like `usbfilter add 0 --target <uuid|name>`
      # [VM_UUID] is an optiona literal token `VM_UUID`, which will be replaced by the UUID of the VM referenced by Vagrant
      args = [options[:command]] + options[:extra_args]
    end

    @logger.debug("Executing: VBoxManage " + args.join(" "))
    machine.provider.driver.execute(*args) do |type, data|
      colors = { :stdout => :green, :stderr => :red }
      machine.env.ui.info(data, :color => colors[type])
    end
  end
end

#vboxmanage_commands_allObject



58
59
60
61
62
63
64
65
66
# File 'lib/vagrant-vboxmanage/commands/root.rb', line 58

def vboxmanage_commands_all 
  # derived as follows: `VBoxManage | sed '1,/Commands/d' | grep '^  [a-z]' | awk '{ print $1; }' | sort -u`
  return %w{adoptstate bandwidthctl clonehd clonevm closemedium controlvm convertfromraw
    createhd createvm debugvm dhcpserver discardstate export extpack
    getextradata guestcontrol guestproperty hostonlyif import list
    metrics modifyhd modifyvm registervm setextradata setproperty
    sharedfolder showhdinfo showvminfo snapshot startvm storageattach
    storagectl unregistervm usbfilter}
end

#vboxmanage_commands_specialObject

not of form ‘VBoxManage showvminfo uuid|name …`



69
70
71
72
73
74
# File 'lib/vagrant-vboxmanage/commands/root.rb', line 69

def vboxmanage_commands_special
  #   derived by manual inspection of `VBoxManage`; see SPECIAL_COMMANDS.md
  return %w{list registervm createvm import export closemedium createhd clonehd
    convertfromraw getextradata setextradata setproperty usbfilter
    sharedfolder guestproperty metrics hostonlyif dhcpserver extpack}
end

#vboxmanage_commands_standardObject

of form ‘VBoxManage showvminfo uuid|name …`



77
78
79
# File 'lib/vagrant-vboxmanage/commands/root.rb', line 77

def vboxmanage_commands_standard
  return vboxmanage_commands_all - vboxmanage_commands_special
end