Govmomi a VMware toolbox
What is Govmomi
Govmomi is an open-source library for managing VMware vSphere. It allows developers to create applications to manage VMware vSphere resources, such as virtual machines, networks and storage.
The Govmomi library is developed in Go lang, which makes it easy to use and integrate with Go applications. But, you can also use it with other programming languages. There are already Python and Ruby ports.
Govmomi offers a wide range of features for managing VMware vSphere. Developers can use Govmomi to create scripts to automate common tasks, such as scaling virtual machines, backing up and restoring virtual machines, and managing networks and storage.
It also allows you to create monitoring applications to track the performance of virtual machines, networks and storage, as well as receive alerts when problems occur.
Using Govmomi, developers can also access detailed information about VMware vSphere resources, such as virtual machine configurations, resource utilization and event logs.
All in all, Govmomi is a powerful tool for developers to create applications to manage VMware vSphere resources. It offers a variety of features to automate common tasks, monitor performance and access detailed information about VMware vSphere resources.
Some usages examples
Here are some examples of how Govmomi is used:
Automating common tasks : Developers can use Govmomi to create scripts to automate common tasks, such as scaling virtual machines based on load, updating virtual machines, and backing up virtual machines.
Performance Monitoring: Developers can use Govmomi to create monitoring applications to track the performance of virtual machines, networks and storage. These applications can receive alerts when problems occur, allowing administrators to react quickly to resolve issues.
Resource Management: Developers can use Govmomi to access detailed information about VMware vSphere resources, such as virtual machine configurations, resource utilization and event logs. This allows administrators to better understand how resources are used and plan upgrades and resource additions accordingly.
Virtual Machine Migration: Developers can use Govmomi to create applications to migrate virtual machines from one vSphere host to another, or from one vSphere cluster to another. This allows administrators to move virtual machines to balance the load on different hosts or clusters.
Integration with other tools - Govmomi can be used to integrate VMware vSphere functionality with other cloud management or virtualization tools, such as Ansible, Terraform or Kubernetes. Developers can use Govmomi to create scripts and applications to automate common tasks and integrate with these tools for comprehensive infrastructure management.
In summary, Govmomi is a tool for managing VMware vSphere infrastructures, while Vagrant is a tool for creating and managing virtual machines in a repeatable manner. Both can be used together to automate the creation of development environments on VMware vSphere.
How to use it ?
The simpliest way to use it, is to write some Go lang program.
A first sample, where we connect to a VCenter, and list some resources…
package main
import (
"flag"
"fmt"
"net/url"
"os"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)
// var vsphereUrl = "https://name:pass@vsphere/sdk"
var insecureFlag = true
func exit(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Parse command-line flags
//https://administrator@vsphere.local:pass1234@vcenter.devlab/sdk
vcenterURL := flag.String("vcurl", "", "URL of vCenter Server instance")
flag.Parse()
// Validate command-line flags
if *vcenterURL == "" {
fmt.Fprintln(os.Stderr, "Error: vcenter-url flags are required")
os.Exit(1)
}
u, err := url.Parse(*vcenterURL)
if err != nil {
fmt.Printf("Error parsing url %s\n", *vcenterURL)
exit(err)
}
c, err := govmomi.NewClient(ctx, u, insecureFlag)
if err != nil {
exit(err)
}
fmt.Println("Log in successful")
f := find.NewFinder(c.Client, true)
dcObj, err := f.DatacenterOrDefault(ctx, "")
if err != nil {
exit(err)
}
f.SetDatacenter(dcObj)
vms, err := f.VirtualMachineList(ctx, "*")
if err != nil {
exit(err)
}
pc := property.DefaultCollector(c.Client)
var refs []types.ManagedObjectReference
for _, vm := range vms {
refs = append(refs, vm.Reference())
}
var vmt []mo.VirtualMachine
err = pc.Retrieve(ctx, refs, []string{"name", "resourcePool"}, &vmt)
if err != nil {
exit(err)
}
for _, vm := range vmt {
fmt.Println(vm.Name, vm.ResourcePool)
}
fmt.Println("Done.")
c.Logout(ctx)
}
When run, it will output something like this :
./govls --vcurl https://u:p@127.0.0.1/sdk
Log in successful
DC0_H0_VM0 ResourcePool:resgroup-22
DC0_H0_VM1 ResourcePool:resgroup-22
DC0_C0_RP0_VM0 ResourcePool:resgroup-26
DC0_C0_RP0_VM1 ResourcePool:resgroup-26
Done.
Here is a simple example of a program using Govmomi to create a new virtual machine on a VMware vSphere host:
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
var insecureFlag = true
func exit(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Parse command-line flags
//https://administrator@vsphere.local:pass1234@vcenter.devlab/sdk
vcenterURL := flag.String("vcurl", "https://u:p@127.0.0.1/sdk", "URL of vCenter Server instance")
flag.Parse()
// Validate command-line flags
if *vcenterURL == "" {
fmt.Fprintln(os.Stderr, "Error: vcenter-url flags are required")
os.Exit(1)
}
u, err := url.Parse(*vcenterURL)
if err != nil {
fmt.Printf("Error parsing url %s\n", *vcenterURL)
exit(err)
}
// connect to vSphere
client, err := govmomi.NewClient(ctx, u, insecureFlag)
if err != nil {
exit(err)
}
// search target host
finder := find.NewFinder(client.Client, true)
host, err := finder.HostSystem(ctx, "/DC0/host/DC0_C0/DC0_C0_H0")
if err != nil {
log.Fatal(err)
}
// get host information
var hostProperties mo.HostSystem
err = host.Properties(context.Background(), host.Reference(), []string{"config.network"}, &hostProperties)
if err != nil {
log.Fatal(err)
}
// get folder store for VM
var vmFolder *object.Folder
if hostProperties.Parent != nil {
vmFolder, err = finder.Folder(ctx, "")
if err != nil {
log.Fatal(err)
}
}
// create new vm config
var config types.VirtualMachineConfigSpec
config.Name = "test-vm"
config.GuestId = "other3xLinux64Guest"
config.Files = &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", hostProperties.Config.Network.Vswitch[0].Name)}
config.NumCPUs = 2
config.MemoryMB = 4096
// create ew vm with previous parameters
_, err = vmFolder.CreateVM(ctx, config, nil, nil)
if err != nil {
log.Fatal(err)
}
log.Printf("the vm has been successfully created", config.Name)
}
This program uses Govmomi to connect to a VMware vSphere vCenter, search for a target host, retrieve the host information, search for the location where the new virtual machine will be created, configure the settings of the new virtual machine and finally create it.
Govc
govc is an open-source command-line tool (CLI) for managing and automating VMware vSphere environments. It’s build upon Govmomi and so uses the vSphere (or vCenter) API to perform tasks such as creating, deleting and configuring virtual machines, resource pools, networks and storage. It also allows to manage ESXi hosts and clusters. it can be use directly to write some shell tools.
It is easy to use as it has a simple and intuitive syntax to perform common tasks, it is also easy to automate using automation scripts.
It allows you to do the following things:
- Create, delete, start and stop virtual machines
- Clone virtual machines
- Import and export virtual machines
- Manage resource pools, networks and storage
- Manage ESXi hosts and clusters
- Generate configuration information and reports
- Apply security policies and network rules
It is often used for automating vSphere management tasks, virtual machine creation, backups and cluster upgrades. It can also be used to perform routine maintenance tasks, such as updating firmware on ESXi hosts.
govc ls
/DC-SCL-SBX/vm
/DC-SCL-SBX/network
/DC-SCL-SBX/host
/DC-SCL-SBX/datastore
govc ls /DC-SCL-SBX/datastore
/DC-SCL-SBX/datastore/wl-baseboxes
/DC-SCL-SBX/datastore/sDRS-SAN-SANDBOX-01
/DC-SCL-SBX/datastore/Logs
Some known project using it
- Kubernetes vSphere Provider
- Packer plugin for vSphere
- Terraform Provider for vSphere
To go further with Govmomi: govmomi Github
In addition to the vSphere API client, this repository includes:
* govc - vSphere CLI
* vcsim - vSphere API mock framework
* toolbox - VM guest tools framework