osbuild-worker: add target for upload to vmware

New upload target for VMWare, similar to the ones for AWS and Azure,
allowing users to set credentials for their vSphere instance.
Commit also includes function that performs the actual upload.
This commit is contained in:
Jozef Mikovic 2021-01-11 14:20:09 +01:00 committed by Tom Gundersen
parent d686abfffb
commit 1a81489ef1
4 changed files with 81 additions and 0 deletions

View file

@ -137,6 +137,35 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
r = append(r, err)
continue
}
case *target.VMWareTargetOptions:
// TODO
if !osbuildOutput.Success {
continue
}
var f *os.File
imagePath := path.Join(outputDirectory, options.Filename)
f, err = vmware.OpenAsStreamOptimizedVmdk(imagePath)
if err != nil {
r = append(r, err)
continue
}
// we don't need the file descriptor to be opened b/c import.vmdk operates on the file path
f.Close()
imagePath = f.Name()
credentials := vmware.Credentials{
Username: options.Username,
Password: options.Password,
Host: options.Host,
Cluster: options.Cluster,
Datacenter: options.Datacenter,
Datastore: options.Datastore,
}
err = vmware.UploadImage(credentials, imagePath, t.ImageName)
if err != nil {
r = append(r, err)
continue
}
case *target.AWSTargetOptions:
if !osbuildOutput.Success {

View file

@ -72,6 +72,8 @@ func UnmarshalTargetOptions(targetName string, rawOptions json.RawMessage) (Targ
options = new(LocalTargetOptions)
case "org.osbuild.koji":
options = new(KojiTargetOptions)
case "org.osbuild.vmware":
options = new(VMWareTargetOptions)
default:
return nil, errors.New("unexpected target name")
}

View file

@ -0,0 +1,17 @@
package target
type VMWareTargetOptions struct {
Filename string `json:"filename"`
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
Datacenter string `json:"datacenter"`
Cluster string `json:"cluster"`
Datastore string `json:"datastore"`
}
func (VMWareTargetOptions) isTargetOptions() {}
func NewVMWareTarget(options *VMWareTargetOptions) *Target{
return newTarget("org.osbuild.vmware", options)
}

View file

@ -1,11 +1,25 @@
package vmware
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/vmware/govmomi/govc/cli"
_ "github.com/vmware/govmomi/govc/importx"
)
type Credentials struct {
Host string
Username string
Password string
Datacenter string
Cluster string
Datastore string
}
func OpenAsStreamOptimizedVmdk(imagePath string) (*os.File, error) {
newPath := strings.TrimSuffix(imagePath, ".vmdk") + "-stream.vmdk"
cmd := exec.Command(
@ -21,3 +35,22 @@ func OpenAsStreamOptimizedVmdk(imagePath string) (*os.File, error) {
}
return f, err
}
func UploadImage(creds Credentials, imagePath, imageName string) error {
args := []string{
"import.vmdk",
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),
imagePath,
imageName,
}
retcode := cli.Run(args)
if retcode != 0 {
return errors.New("importing vmdk failed")
}
return nil
}