Initial commit/push

This commit is contained in:
Danny Bessems 2022-12-22 13:07:21 +01:00
commit c8483de7a9
7 changed files with 217 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.vscode
bin**
config.yml

View File

@ -0,0 +1,43 @@
package main
import (
"context"
"flag"
"log"
"spamasaurus.com/m/pkg/config"
"spamasaurus.com/m/pkg/hypervisor"
)
func main() {
var virtualmachine string
flag.StringVar(&virtualmachine, "vm", "", "name of VM")
flag.Parse()
if virtualmachine == "" {
log.Fatalf("Name of vm is required")
}
cfg, err := config.NewConfig()
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clt, err := hypervisor.NewClient(ctx, cfg.Hypervisor.Url, cfg.Hypervisor.Username, cfg.Hypervisor.Password, true)
if err != nil {
log.Fatalf("Login failed: %s", err)
}
fnd, err := hypervisor.DatacenterFinder(ctx, clt, cfg.Hypervisor.Datacenter)
if err != nil {
log.Fatalf("Foo indeed: %s", err)
}
if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, virtualmachine); err != nil {
log.Fatalf("Could not apply vApp properties: %s", err)
}
}

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module spamasaurus.com/m
go 1.19
require github.com/vmware/govmomi v0.30.0
require gopkg.in/yaml.v2 v2.4.0 // indirect

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/vmware/govmomi v0.30.0 h1:Fm8ugPnnlMSTSceDKY9goGvjmqc6eQLPUSUeNXdpeXA=
github.com/vmware/govmomi v0.30.0/go.mod h1:F7adsVewLNHsW/IIm7ziFURaXDaHEwcc+ym4r3INMdY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

51
pkg/config/config.go Normal file
View File

@ -0,0 +1,51 @@
package config
import (
"flag"
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
Hypervisor struct {
Url string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Datacenter string `yaml:"datacenter"`
} `yaml:"hypervisor"`
}
func NewConfig() (*Config, error) {
var configPath string
flag.StringVar(&configPath, "config", "./config.yml", "path to config file")
flag.Parse()
s, err := os.Stat(configPath)
if err != nil {
return nil, err
}
if s.IsDir() {
return nil, fmt.Errorf("'%s' is a directory, not a regular file", configPath)
}
config := &Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}

44
pkg/hypervisor/client.go Normal file
View File

@ -0,0 +1,44 @@
package hypervisor
import (
"context"
neturl "net/url"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/session/cache"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/soap"
)
func NewClient(ctx context.Context, host, username, password string, insecure bool) (*vim25.Client, error) {
url, err := soap.ParseURL(host)
if err != nil {
return nil, err
}
url.User = neturl.UserPassword(username, password)
session := &cache.Session{
URL: url,
Insecure: insecure,
}
clt := new(vim25.Client)
err = session.Login(ctx, clt, nil)
if err != nil {
return nil, err
}
return clt, err
}
func DatacenterFinder(ctx context.Context, clt *vim25.Client, datacenter string) (*find.Finder, error) {
fnd := find.NewFinder(clt)
founddc, err := fnd.DatacenterOrDefault(ctx, datacenter)
if err != nil {
return nil, err
}
return fnd.SetDatacenter(founddc), nil
}

62
pkg/hypervisor/vm.go Normal file
View File

@ -0,0 +1,62 @@
package hypervisor
import (
"context"
"fmt"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/types"
)
type vAPPProperty struct {
Key string
Value string
}
func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, name string) error {
vm, err := fnd.VirtualMachine(ctx, name)
if err != nil {
return err
}
vappproperties := []vAPPProperty{
{
Key: "foo",
Value: "bar",
},
{
Key: "woot",
Value: "dude",
},
}
vappconfig := &types.VmConfigSpec{
// OvfEnvironmentTransport: []string{"com.vmware.guestinfo"},
}
for i, vappproperty := range vappproperties {
vappconfig.Property = append(vappconfig.Property, types.VAppPropertySpec{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationAdd,
},
Info: &types.VAppPropertyInfo{
Key: int32(i),
Id: vappproperty.Key,
DefaultValue: fmt.Sprintf("${%v}", vappproperty.Value),
Type: "expression",
},
})
}
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
VAppConfig: vappconfig,
})
if err != nil {
return err
}
if err := task.Wait(ctx); err != nil {
return err
}
return nil
}