internal/upload: add import.ova support to vmware

This commit is contained in:
Sanne Raymaekers 2023-03-31 14:08:09 +02:00
parent e91dcae110
commit 967306bc47
2 changed files with 47 additions and 16 deletions

View file

@ -452,20 +452,32 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
}
}()
// create a symlink so that uploaded image has the name specified by user
imageName := jobTarget.ImageName + ".vmdk"
imagePath := path.Join(tempDirectory, imageName)
exportedImagePath := path.Join(outputDirectory, jobTarget.OsbuildArtifact.ExportName, jobTarget.OsbuildArtifact.ExportFilename)
err = os.Symlink(exportedImagePath, imagePath)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, err.Error(), nil)
break
}
err = vmware.UploadImage(credentials, imagePath)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil)
if strings.HasSuffix(exportedImagePath, ".vmdk") {
// create a symlink so that uploaded image has the name specified by user
imageName := jobTarget.ImageName + ".vmdk"
imagePath := path.Join(tempDirectory, imageName)
err = os.Symlink(exportedImagePath, imagePath)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, err.Error(), nil)
break
}
err = vmware.ImportVmdk(credentials, imagePath)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil)
break
}
} else if strings.HasSuffix(exportedImagePath, ".ova") {
err = vmware.ImportOva(credentials, exportedImagePath, jobTarget.ImageName)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil)
break
}
} else {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "No vmdk or ova provided", nil)
break
}

View file

@ -1,7 +1,6 @@
package vmware
import (
"errors"
"fmt"
"github.com/vmware/govmomi/govc/cli"
@ -17,9 +16,9 @@ type Credentials struct {
Datastore string
}
// UploadImage is a function that uploads a stream optimized vmdk image to vSphere
// ImportVmdk is a function that uploads a stream optimized vmdk image to vSphere
// uploaded image will be present in a directory of the same name
func UploadImage(creds Credentials, imagePath string) error {
func ImportVmdk(creds Credentials, imagePath string) error {
args := []string{
"import.vmdk",
fmt.Sprintf("-u=%s:%s@%s", creds.Username, creds.Password, creds.Host),
@ -32,7 +31,27 @@ func UploadImage(creds Credentials, imagePath string) error {
retcode := cli.Run(args)
if retcode != 0 {
return errors.New("importing vmdk failed")
return fmt.Errorf("importing %s into vSphere failed", imagePath)
}
return nil
}
func ImportOva(creds Credentials, imagePath, targetName string) error {
args := []string{
"import.ova",
fmt.Sprintf("-u=%s:%s@%s", creds.Username, creds.Password, creds.Host),
"-k=true",
fmt.Sprintf("-pool=%s/Resources", creds.Cluster),
fmt.Sprintf("-dc=%s", creds.Datacenter),
fmt.Sprintf("-ds=%s", creds.Datastore),
fmt.Sprintf("-name=%s", targetName),
imagePath,
}
retcode := cli.Run(args)
if retcode != 0 {
return fmt.Errorf("importing %s into vSphere failed", imagePath)
}
return nil
}