Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Add command to upload to template library
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-pripoae authored and paddatrapper committed Jan 7, 2021
1 parent 2ebe4bd commit d709ebe
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ var (
func init() {
cwd, _ := os.Getwd()
upload.AddCommand(&ova.Ova)
upload.AddCommand(&ova.Template)

convert.Flags().String("name", "", "Name of the template")
convert.Flags().String("format", "ova", "Target format for conversion")
Expand Down
7 changes: 7 additions & 0 deletions pkg/build/ova/ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ func Import(name, ova, network string) error {
return utils.Exec("govc import.ova --name %s --options %s %s", name, tmp.Name(), ova)
}

func ImportContentLibrary(library, name, ova string) error {
if name != "" {
return utils.Exec("govc library.import -n %s %s %s", name, library, ova)
}
return utils.Exec("govc library.import %s %s", library, ova)
}

func getVmx(name, image string, properties map[string]string) string {
vmx := fmt.Sprintf(base, name, image)

Expand Down
40 changes: 40 additions & 0 deletions pkg/build/ova/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,51 @@ var (
}
},
}

Template = cobra.Command{
Use: "template",
Short: "Upload a template to a vSphere content library",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var err error
image, _ := cmd.Flags().GetString("image")
name, _ := cmd.Flags().GetString("name")
if name == "" {
name = utils.GetBaseName(image)
}

library, _ := cmd.Flags().GetString("library")

if library == "" {
log.Fatalf("Library name cannot be empty")
} else if library[0] != '/' {
log.Fatalf("Library name must start with /")
}

ext := path.Ext(image)

if ext != ".ova" {
image, err = Create(name, image, make(map[string]string))
if err != nil {
log.Fatalf("Failed to create OVA %s", err)
}
}

if err := ImportContentLibrary(library, name, image); err != nil {
log.Fatalf("Failed to upload %s: %v", name, err)
}
},
}
)

func init() {
Ova.Flags().String("image", "", "A local or remote path to a disk image")
Ova.Flags().String("name", "", "Name of the template")
Ova.Flags().String("folder", utils.GetEnvOrDefault("GOVC_FOLDER", "vm"), " The folder to upload to")
Ova.Flags().String("network", utils.GetEnvOrDefault("GOVC_NETWORK", "VM Network"), " The VM network")

Template.Flags().String("image", "", "A local or remote path to a disk image")
Template.Flags().String("name", "", "Name of the template")
Template.Flags().String("library", "", "Name of the library")
}
8 changes: 4 additions & 4 deletions resources/static.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d709ebe

Please sign in to comment.