Move all config to flags;DRY;fixed vApptransport case
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Danny Bessems 2022-12-22 16:00:22 +01:00
parent b85fd3946e
commit 15105ee1ea
7 changed files with 25 additions and 77 deletions

View File

@ -3,7 +3,7 @@ type: kubernetes
name: 'Golang Build'
environment:
SKOPEO_VERSION: v1.10.0
BINARY_VERSION: v0.2.0
clone:
disable: true
@ -27,7 +27,7 @@ steps:
curl \
--header "Authorization: token $GIT_APIKEY" \
--upload-file bin/vappprop-manager \
https://code.spamasaurus.com/api/packages/$GIT_USERNAME/generic/vappprop-manager/v0.1.0/vappprop-manager
https://code.spamasaurus.com/api/packages/$GIT_USERNAME/generic/vappprop-manager/$BINARY_VERSION/vappprop-manager
environment:
GIT_APIKEY:
from_secret: git_apikey

1
.gitignore vendored
View File

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

View File

@ -5,11 +5,15 @@ import (
"flag"
"log"
"spamasaurus.com/m/pkg/config"
"spamasaurus.com/m/pkg/hypervisor"
)
type Input struct {
FQDN string
Username string
Password string
Datacenter string
VirtualMachine string
Network string
}
@ -17,24 +21,23 @@ type Input struct {
func main() {
var input Input
flag.StringVar(&input.VirtualMachine, "vm", "", "name of VM")
flag.StringVar(&input.Network, "network", "", "name of network portgroup")
flag.StringVar(&input.FQDN, "server", "", "FQDN of the vCenter appliance")
flag.StringVar(&input.Username, "username", "", "Username to authenticate with")
flag.StringVar(&input.Password, "password", "", "Password to authenticate with")
flag.StringVar(&input.Datacenter, "dc", "", "Name of datacenter")
flag.StringVar(&input.VirtualMachine, "vm", "", "Name of VM")
flag.StringVar(&input.Network, "network", "", "Name of network portgroup")
flag.Parse()
cfg, err := config.NewConfig()
if err != nil {
log.Fatalf("Invalid configuration: %s", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clt, err := hypervisor.NewClient(ctx, cfg.Hypervisor.Url, cfg.Hypervisor.Username, cfg.Hypervisor.Password, true)
clt, err := hypervisor.NewClient(ctx, input.FQDN, input.Username, input.Password, true)
if err != nil {
log.Fatalf("Login failed: %s", err)
}
fnd, err := hypervisor.DatacenterFinder(ctx, clt, cfg.Hypervisor.Datacenter)
fnd, err := hypervisor.DatacenterFinder(ctx, clt, input.Datacenter)
if err != nil {
log.Fatalf("Foo indeed: %s", err)
}

5
go.mod
View File

@ -4,7 +4,4 @@ go 1.19
require github.com/vmware/govmomi v0.30.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
require github.com/davecgh/go-spew v1.1.1 // indirect

2
go.sum
View File

@ -4,5 +4,3 @@ 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=

View File

@ -1,51 +0,0 @@
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
}

View File

@ -22,28 +22,28 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualm
vappproperties := []vAPPProperty{
{
Key: "guestinfo.dns.domains",
Value: fmt.Sprintf("${searchPath:%s}", network),
Value: "${searchPath:%s}",
},
{
Key: "guestinfo.dns.servers",
Value: fmt.Sprintf("${dns:%s}", network),
Value: "${dns:%s}",
},
{
Key: "guestinfo.interface.0.ip.0.address",
Value: fmt.Sprintf("${autoIp:%s}", network),
Value: "${autoIp:%s}",
},
{
Key: "guestinfo.interface.0.ip.0.netmask",
Value: fmt.Sprintf("${netmask:%s}", network),
Value: "${netmask:%s}",
},
{
Key: "guestinfo.interface.0.route.0.gateway",
Value: fmt.Sprintf("${gateway:%s}", network),
Value: "${gateway:%s}",
},
}
vappconfig := &types.VmConfigSpec{
// OvfEnvironmentTransport: []string{"com.vmware.guestinfo"},
OvfEnvironmentTransport: []string{"com.vmware.guestInfo"},
}
for i, vappproperty := range vappproperties {
@ -54,12 +54,14 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualm
Info: &types.VAppPropertyInfo{
Key: int32(i),
Id: vappproperty.Key,
DefaultValue: vappproperty.Value,
DefaultValue: fmt.Sprintf(vappproperty.Value, network),
Type: "expression",
},
})
}
// spew.Dump(vappconfig)
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
VAppConfig: vappconfig,
})