-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1929 from thebsdbox/push_vcenter
vCenter push capability
- Loading branch information
Showing
3 changed files
with
150 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
) | ||
|
||
// Process the push arguments and execute push | ||
func pushVCenter(args []string) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
var newVM vmConfig | ||
|
||
flags := flag.NewFlagSet("vCenter", flag.ExitOnError) | ||
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.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") | ||
|
||
newVM.vmFolder = flags.String("folder", "", "A folder on the datastore to push the image too") | ||
|
||
flags.Usage = func() { | ||
fmt.Printf("USAGE: %s push vcenter [options] path \n\n", invoked) | ||
fmt.Printf("'path' specifies the full path of an image that will be pushed\n") | ||
fmt.Printf("Options:\n\n") | ||
flags.PrintDefaults() | ||
} | ||
|
||
if err := flags.Parse(args); err != nil { | ||
log.Fatalln("Unable to parse args") | ||
} | ||
|
||
remArgs := flags.Args() | ||
if len(remArgs) == 0 { | ||
fmt.Printf("Please specify the path to the image to push\n") | ||
flags.Usage() | ||
os.Exit(1) | ||
} | ||
*newVM.path = remArgs[0] | ||
|
||
// Ensure an iso has been passed to the vCenter push Command | ||
if !strings.HasSuffix(*newVM.path, ".iso") { | ||
log.Fatalln("Please pass an \".iso\" file as the path") | ||
} | ||
|
||
// Test any passed in files before uploading image | ||
checkFile(*newVM.path) | ||
|
||
// Connect to VMware vCenter and return the values needed to upload image | ||
c, dss, _, _, _, _ := vCenterConnect(ctx, newVM) | ||
|
||
// Create a folder from the uploaded image name if needed | ||
if *newVM.vmFolder == "" { | ||
*newVM.vmFolder = strings.TrimSuffix(path.Base(*newVM.path), ".iso") | ||
} | ||
|
||
// The CreateFolder method isn't necessary as the *newVM.vmname will be created automatically | ||
uploadFile(c, newVM, dss) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters