diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..f48ca8b --- /dev/null +++ b/.drone.yml @@ -0,0 +1,34 @@ +kind: pipeline +type: kubernetes +name: 'Golang Build' + +environment: + SKOPEO_VERSION: v1.10.0 + +clone: + disable: true + +steps: +- name: Clone vappproperty-manager source repository + image: bv11-cr01.bessems.eu/proxy/alpine/git + commands: + - git clone https://code.spamasaurus.com/djpbessems/Go.vAppPropertyManager.git . + # - git checkout tags/$SKOPEO_VERSION + - git checkout HEAD + when: + status: + - success +- name: Build binary + image: bv11-cr01.bessems.eu/proxy/library/golang + commands: + - | + go build -o ./bin/vappprop-manager ./cmd/vappprop-manager/main.go + - | + 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 + environment: + GIT_APIKEY: + from_secret: git_apikey + GIT_USERNAME: djpbessems diff --git a/cmd/vappprop-manager/main.go b/cmd/vappprop-manager/main.go index 67c1b1e..06d087e 100644 --- a/cmd/vappprop-manager/main.go +++ b/cmd/vappprop-manager/main.go @@ -9,19 +9,21 @@ import ( "spamasaurus.com/m/pkg/hypervisor" ) +type Input struct { + VirtualMachine string + Network string +} + func main() { - var virtualmachine string + var input Input - flag.StringVar(&virtualmachine, "vm", "", "name of VM") + flag.StringVar(&input.VirtualMachine, "vm", "", "name of VM") + flag.StringVar(&input.Network, "network", "", "name of network portgroup") flag.Parse() - if virtualmachine == "" { - log.Fatalf("Name of vm is required") - } - cfg, err := config.NewConfig() if err != nil { - log.Fatal(err) + log.Fatalf("Invalid configuration: %s", err) } ctx, cancel := context.WithCancel(context.Background()) @@ -37,7 +39,7 @@ func main() { log.Fatalf("Foo indeed: %s", err) } - if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, virtualmachine); err != nil { + if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, input.VirtualMachine, input.Network); err != nil { log.Fatalf("Could not apply vApp properties: %s", err) } } diff --git a/go.mod b/go.mod index 6b65094..ae7ae9f 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,7 @@ go 1.19 require github.com/vmware/govmomi v0.30.0 -require gopkg.in/yaml.v2 v2.4.0 // indirect +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum index 1f9b21e..c075d3e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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= diff --git a/pkg/hypervisor/vm.go b/pkg/hypervisor/vm.go index eb0e857..93b1378 100644 --- a/pkg/hypervisor/vm.go +++ b/pkg/hypervisor/vm.go @@ -13,20 +13,32 @@ type vAPPProperty struct { Value string } -func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, name string) error { - vm, err := fnd.VirtualMachine(ctx, name) +func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, virtualmachine string, network string) error { + vm, err := fnd.VirtualMachine(ctx, virtualmachine) if err != nil { return err } vappproperties := []vAPPProperty{ { - Key: "foo", - Value: "bar", + Key: "guestinfo.dns.domains", + Value: fmt.Sprintf("${searchPath:%s}", network), }, { - Key: "woot", - Value: "dude", + Key: "guestinfo.dns.servers", + Value: fmt.Sprintf("${dns:%s}", network), + }, + { + Key: "guestinfo.interface.0.ip.0.address", + Value: fmt.Sprintf("${autoIp:%s}", network), + }, + { + Key: "guestinfo.interface.0.ip.0.netmask", + Value: fmt.Sprintf("${netmask:%s}", network), + }, + { + Key: "guestinfo.interface.0.route.0.gateway", + Value: fmt.Sprintf("${gateway:%s}", network), }, } @@ -42,7 +54,7 @@ func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, name str Info: &types.VAppPropertyInfo{ Key: int32(i), Id: vappproperty.Key, - DefaultValue: fmt.Sprintf("${%v}", vappproperty.Value), + DefaultValue: vappproperty.Value, Type: "expression", }, })