diff --git a/.drone.yml b/.drone.yml index f48ca8b..77553a6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/.gitignore b/.gitignore index fa0d2d1..930ef56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .vscode bin** -config.yml diff --git a/cmd/vappprop-manager/main.go b/cmd/vappprop-manager/main.go index 06d087e..9f00e5b 100644 --- a/cmd/vappprop-manager/main.go +++ b/cmd/vappprop-manager/main.go @@ -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) } diff --git a/go.mod b/go.mod index ae7ae9f..f463b8c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c075d3e..4d8d494 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/config/config.go b/pkg/config/config.go deleted file mode 100644 index ae5c675..0000000 --- a/pkg/config/config.go +++ /dev/null @@ -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 -} diff --git a/pkg/hypervisor/vm.go b/pkg/hypervisor/vm.go index 93b1378..c018374 100644 --- a/pkg/hypervisor/vm.go +++ b/pkg/hypervisor/vm.go @@ -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, })