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.

Utility methods

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

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

Clone this wiki locally