Skip to content
This repository was archived by the owner on Nov 9, 2020. It is now read-only.

Writing New Commands

rlane edited this page Mar 30, 2011 · 5 revisions

RVC is designed to make adding your own commands easy. Most commands will need to use the RbVmomi library; see its documentation.

Command Modules

Every command exists in a module, which is just a Ruby file in a special directory. The default directories searched are lib/rvc/modules in the source tree and ~/.rvc. You can add other directories by including them in the environment variable RVC_MODULE_PATH, separated by colons. The module will be named the same as the Ruby file, minus the .rb extension. If you have files with the same name in different module directories they will be combined into the same module.

For rapid development, you can reload command modules while RVC is running. Just use the reload command. It will also tell you the location of each command module it loads.

Example

This example is taken from the vm module. Copy it into ~/.rvc/test.rb (you may need to create the directory first).

opts :remove_device do
  summary "Remove a virtual device"
  arg :vm, nil, :lookup => VIM::VirtualMachine
  arg :label, "Device label"
end

def remove_device vm, label
  dev = vm.config.hardware.device.find { |x| x.deviceInfo.label == label }
  err "no such device" unless dev
  spec = {
    :deviceChange => [
      { :operation => :remove, :device => dev },
    ]
  }
  vm.ReconfigVM_Task(:spec => spec).wait_for_completion
end

Now restart RVC or use the reload command. You should be able to use the test.remove_device command.

Option parsing

Every command has its own command line option parser, defined by the opts method. This parser is based on Trollop with a few additions.

The summary method is used to define the text to show in the help listing.

opt has a new option: :lookup. If this is set to a class then RVC will treat the value given by the user as a path and look it up in the virtual filesystem. If it isn't found, or if it does not match the class given to :lookup, then RVC will display an error message and not execute the command. If it was found, the object will be given to the command instead of the path string.

The biggest addition is the arg method. This defines a positional argument. The first parameter is the name, which is used only for documentation, and the second parameter is an optional description. arg supports the :default, :multi, :required, and :lookup options just like opt.

Utility methods

See the implementations in lib/rvc/util.rb for more information.

  • lookup
  • lookup!
  • err
  • progress
  • tasks
  • system_fg

Clone this wiki locally