-
Notifications
You must be signed in to change notification settings - Fork 49
Writing New Commands
RVC is designed to make adding your own commands easy. Most commands will need to use the RbVmomi library; see its documentation.
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.
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.
Every command has its own command line option parser, defined by the opts method. This parser is based on Trollop with a few additions.
See the implementations in lib/rvc/util.rb for more information.
- lookup
- lookup!
- err
- progress
- tasks
- system_fg