From ea5114dc93ed44d4884c15969e0c43fa9b31adc5 Mon Sep 17 00:00:00 2001 From: Stefan Bourlon Date: Fri, 7 Jul 2017 14:15:58 -0700 Subject: [PATCH 1/3] Backend vCenter: Upload ISO only with linuxkit push Signed-off-by: Stefan Bourlon --- src/cmd/linuxkit/push_vcenter.go | 29 +++++++++++++++++++++++++++++ src/cmd/linuxkit/run_vcenter.go | 31 ------------------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/cmd/linuxkit/push_vcenter.go b/src/cmd/linuxkit/push_vcenter.go index 2113a14d2..8c1e633b5 100644 --- a/src/cmd/linuxkit/push_vcenter.go +++ b/src/cmd/linuxkit/push_vcenter.go @@ -10,6 +10,9 @@ import ( "strings" log "github.com/Sirupsen/logrus" + "github.com/vmware/govmomi" + "github.com/vmware/govmomi/object" + "github.com/vmware/govmomi/vim25/soap" ) // Process the push arguments and execute push @@ -67,3 +70,29 @@ func pushVCenter(args []string) { // The CreateFolder method isn't necessary as the *newVM.vmname will be created automatically uploadFile(c, newVM, dss) } + +func checkFile(file string) { + if _, err := os.Stat(file); err != nil { + if os.IsPermission(err) { + log.Fatalf("Unable to read file [%s], please check permissions", file) + } else if os.IsNotExist(err) { + log.Fatalf("File [%s], does not exist", file) + } else { + log.Fatalf("Unable to stat file [%s]: %v", file, err) + } + } +} + +func uploadFile(c *govmomi.Client, newVM vmConfig, dss *object.Datastore) { + _, fileName := path.Split(*newVM.path) + log.Infof("Uploading LinuxKit file [%s]", *newVM.path) + if *newVM.path == "" { + log.Fatalf("No file specified") + } + dsurl := dss.NewURL(fmt.Sprintf("%s/%s", *newVM.vmFolder, fileName)) + + p := soap.DefaultUpload + if err := c.Client.UploadFile(*newVM.path, dsurl, &p); err != nil { + log.Fatalf("Unable to upload file to vCenter Datastore\n%v", err) + } +} diff --git a/src/cmd/linuxkit/run_vcenter.go b/src/cmd/linuxkit/run_vcenter.go index 9c06e4617..78051567b 100644 --- a/src/cmd/linuxkit/run_vcenter.go +++ b/src/cmd/linuxkit/run_vcenter.go @@ -13,7 +13,6 @@ import ( "github.com/vmware/govmomi" "github.com/vmware/govmomi/find" "github.com/vmware/govmomi/object" - "github.com/vmware/govmomi/vim25/soap" "github.com/vmware/govmomi/vim25/types" log "github.com/Sirupsen/logrus" @@ -89,9 +88,6 @@ func runVcenter(args []string) { log.Fatalln("Please pass an \".iso\" file as the path") } - // Test any passed in files before creating a new VM - checkFile(*newVM.path) - // Connect to VMware vCenter and return the default and found values needed for a new VM c, dss, folders, hs, net, rp := vCenterConnect(ctx, newVM) @@ -127,7 +123,6 @@ func runVcenter(args []string) { // Retrieve the new VM vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference)) - uploadFile(c, newVM, dss) addISO(ctx, newVM, vm, dss) if *newVM.persistent != "" { @@ -237,20 +232,6 @@ func powerOnVM(ctx context.Context, vm *object.VirtualMachine) { } } -func uploadFile(c *govmomi.Client, newVM vmConfig, dss *object.Datastore) { - _, fileName := path.Split(*newVM.path) - log.Infof("Uploading LinuxKit file [%s]", *newVM.path) - if *newVM.path == "" { - log.Fatalf("No file specified") - } - dsurl := dss.NewURL(fmt.Sprintf("%s/%s", *newVM.vmFolder, fileName)) - - p := soap.DefaultUpload - if err := c.Client.UploadFile(*newVM.path, dsurl, &p); err != nil { - log.Fatalf("Unable to upload file to vCenter Datastore\n%v", err) - } -} - func addNIC(ctx context.Context, vm *object.VirtualMachine, net object.NetworkReference) { backing, err := net.EthernetCardBackingInfo(ctx) if err != nil { @@ -321,15 +302,3 @@ func addISO(ctx context.Context, newVM vmConfig, vm *object.VirtualMachine, dss log.Fatalf("Unable to add new CD-ROM device to VM configuration\n%v", err) } } - -func checkFile(file string) { - if _, err := os.Stat(file); err != nil { - if os.IsPermission(err) { - log.Fatalf("Unable to read file [%s], please check permissions", file) - } else if os.IsNotExist(err) { - log.Fatalf("File [%s], does not exist", file) - } else { - log.Fatalf("Unable to stat file [%s]: %v", file, err) - } - } -} From ffef020a09f002fd89fd6aec62d374cc2849f7ec Mon Sep 17 00:00:00 2001 From: Stefan Bourlon Date: Fri, 7 Jul 2017 14:32:14 -0700 Subject: [PATCH 2/3] Backend vCenter: add the datacenter parameter Signed-off-by: Stefan Bourlon --- src/cmd/linuxkit/push_vcenter.go | 1 + src/cmd/linuxkit/run_vcenter.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cmd/linuxkit/push_vcenter.go b/src/cmd/linuxkit/push_vcenter.go index 8c1e633b5..3b58598f8 100644 --- a/src/cmd/linuxkit/push_vcenter.go +++ b/src/cmd/linuxkit/push_vcenter.go @@ -32,6 +32,7 @@ func pushVCenter(args []string) { } newVM.vCenterURL = flags.String("url", os.Getenv("VCURL"), "URL of VMware vCenter in the format of https://username:password@VCaddress/sdk") + newVM.dcName = flags.String("datacenter", os.Getenv("VCDATACENTER"), "The name of the DataCenter to host the image") newVM.dsName = flags.String("datastore", os.Getenv("VCDATASTORE"), "The name of the DataStore to host the image") newVM.networkName = flags.String("network", os.Getenv("VCNETWORK"), "The network label the VM will use") newVM.vSphereHost = flags.String("hostname", os.Getenv("VCHOST"), "The server that will host the image") diff --git a/src/cmd/linuxkit/run_vcenter.go b/src/cmd/linuxkit/run_vcenter.go index 78051567b..c44a08362 100644 --- a/src/cmd/linuxkit/run_vcenter.go +++ b/src/cmd/linuxkit/run_vcenter.go @@ -20,6 +20,7 @@ import ( type vmConfig struct { vCenterURL *string + dcName *string dsName *string networkName *string vSphereHost *string @@ -44,6 +45,7 @@ func runVcenter(args []string) { invoked := filepath.Base(os.Args[0]) newVM.vCenterURL = flags.String("url", os.Getenv("VCURL"), "URL of VMware vCenter in the format of https://username:password@VCaddress/sdk") + newVM.dcName = flags.String("datacenter", os.Getenv("VCDATACENTER"), "The name of the Datacenter to host the VM") newVM.dsName = flags.String("datastore", os.Getenv("VCDATASTORE"), "The name of the DataStore to host the VM") newVM.networkName = flags.String("network", os.Getenv("VCNETWORK"), "The network label the VM will use") newVM.vSphereHost = flags.String("hostname", os.Getenv("VCHOST"), "The server that will run the VM") @@ -178,7 +180,7 @@ func vCenterConnect(ctx context.Context, newVM vmConfig) (*govmomi.Client, *obje f := find.NewFinder(c.Client, true) // Find one and only datacenter, not sure how VMware linked mode will work - dc, err := f.DefaultDatacenter(ctx) + dc, err := f.DatacenterOrDefault(ctx, *newVM.dcName) if err != nil { log.Fatalf("No Datacenter instance could be found inside of vCenter %v", err) } From 846e814a7fa464c017c1baaafadaf2d891b939ea Mon Sep 17 00:00:00 2001 From: Stefan Bourlon Date: Mon, 10 Jul 2017 14:03:55 -0700 Subject: [PATCH 3/3] Backend vCenter: Remove network param for push cmd The network parameter is not used in the command "linuxkit push vcenter ..." Signed-off-by: Stefan Bourlon --- src/cmd/linuxkit/push_vcenter.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/linuxkit/push_vcenter.go b/src/cmd/linuxkit/push_vcenter.go index 3b58598f8..fddef1f04 100644 --- a/src/cmd/linuxkit/push_vcenter.go +++ b/src/cmd/linuxkit/push_vcenter.go @@ -34,7 +34,6 @@ func pushVCenter(args []string) { newVM.vCenterURL = flags.String("url", os.Getenv("VCURL"), "URL of VMware vCenter in the format of https://username:password@VCaddress/sdk") newVM.dcName = flags.String("datacenter", os.Getenv("VCDATACENTER"), "The name of the DataCenter to host the image") newVM.dsName = flags.String("datastore", os.Getenv("VCDATASTORE"), "The name of the DataStore to host the image") - newVM.networkName = flags.String("network", os.Getenv("VCNETWORK"), "The network label the VM will use") newVM.vSphereHost = flags.String("hostname", os.Getenv("VCHOST"), "The server that will host the image") newVM.path = flags.String("path", "", "Path to a specific image")