Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ova deployment feature #1105

Merged
merged 3 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 206 additions & 40 deletions vsphere/internal/helper/ovfdeploy/ovf_helper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ovfdeploy

import (
"archive/tar"
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -11,6 +13,7 @@ import (
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -38,8 +41,8 @@ func (pr *ProgressReader) Read(p []byte) (n int, err error) {
return
}

func DeployOVFAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecResult, resourcePoolObj *object.ResourcePool,
folder *object.Folder, host *object.HostSystem, OvfPath string, ovfFromLocal bool) error {
func DeployOvfAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecResult, resourcePoolObj *object.ResourcePool,
folder *object.Folder, host *object.HostSystem, filePath string, deployOva bool, fromLocal bool, allowUnverifiedSSL bool) error {

var currBytesRead int64 = 0
var totalBytes int64 = 0
Expand Down Expand Up @@ -85,46 +88,26 @@ func DeployOVFAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecR
for _, ovfFileItem := range ovfCreateImportSpecResult.FileItem {
for _, deviceObj := range leaseInfo.DeviceUrl {

if ovfFileItem.DeviceId == deviceObj.ImportKey {
if ovfFromLocal {
absoluteFilePath := ""
if strings.Contains(OvfPath, string(os.PathSeparator)) {
absoluteFilePath = string(OvfPath[0 : strings.LastIndex(OvfPath, string(os.PathSeparator))+1])
}
vmdkFilePath := absoluteFilePath + ovfFileItem.Path
log.Print(" [DEBUG] Absolute VMDK path: " + vmdkFilePath)
file, err := os.Open(vmdkFilePath)
if err != nil {
return err
}
err = Upload(context.Background(), ovfFileItem, file, deviceObj.Url, ovfFileItem.Size, &currBytesRead)
if err != nil {
return fmt.Errorf("error while uploading the file %s %s", vmdkFilePath, err)
}
err = file.Close()
if err != nil {
log.Printf("error while closing the file %s", vmdkFilePath)
}

if ovfFileItem.DeviceId != deviceObj.ImportKey {
continue
}
if !deployOva {
if fromLocal {
err = uploadDisksFromLocal(filePath, ovfFileItem, deviceObj, &currBytesRead)
} else {
err = uploadDisksFromUrl(filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
}
} else {
if fromLocal {
err = uploadOvaDisksFromLocal(filePath, ovfFileItem, deviceObj, &currBytesRead)
} else {
absoluteFilePath := ""
if strings.Contains(OvfPath, "/") {
absoluteFilePath = string(OvfPath[0 : strings.LastIndex(OvfPath, "/")+1])
}
vmdkFilePath := absoluteFilePath + ovfFileItem.Path
resp, err := http.Get(vmdkFilePath)
log.Print("DEBUG Absolute VMDK path: " + vmdkFilePath)
if err != nil {
return err
}
defer resp.Body.Close()
err = Upload(context.Background(), ovfFileItem, resp.Body, deviceObj.Url, ovfFileItem.Size, &currBytesRead)
if err != nil {
return err
}
err = uploadOvaDisksFromUrl(filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
}
log.Print("DEBUG : Completed uploading the VMDK file")
}
if err != nil {
return fmt.Errorf("error while uploading the disk %s %s", ovfFileItem.Path, err)
}
log.Print(" DEBUG : Completed uploading the vmdk file", ovfFileItem.Path)
}
}
statusChannel <- true
Expand All @@ -138,7 +121,7 @@ func DeployOVFAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecR
return nil
}

func Upload(ctx context.Context, item types.OvfFileItem, f io.Reader, url string, size int64, totalBytesRead *int64) error {
func upload(ctx context.Context, item types.OvfFileItem, f io.Reader, url string, size int64, totalBytesRead *int64) error {

u, err := soap.ParseURL(url)
if err != nil {
Expand Down Expand Up @@ -197,6 +180,178 @@ func Upload(ctx context.Context, item types.OvfFileItem, f io.Reader, url string
return err
}

func uploadDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
absoluteFilePath := ""
if strings.Contains(filePath, string(os.PathSeparator)) {
absoluteFilePath = string(filePath[0 : strings.LastIndex(filePath, string(os.PathSeparator))+1])
}
vmdkFilePath := absoluteFilePath + ovfFileItem.Path
log.Print(" [DEBUG] Absolute vmdk path: " + vmdkFilePath)
file, err := os.Open(vmdkFilePath)
if err != nil {
return err
}
err = upload(context.Background(), ovfFileItem, file, deviceObj.Url, ovfFileItem.Size, currBytesRead)
if err != nil {
return fmt.Errorf("error while uploading the file %s %s", vmdkFilePath, err)
}
err = file.Close()
if err != nil {
log.Printf("error while closing the file %s", vmdkFilePath)
}
return nil
}

func uploadDisksFromUrl(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
allowUnverifiedSSL bool) error {
absoluteFilePath := ""
if strings.Contains(filePath, "/") {
absoluteFilePath = string(filePath[0 : strings.LastIndex(filePath, "/")+1])
}
vmdkFilePath := absoluteFilePath + ovfFileItem.Path
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(vmdkFilePath)
log.Print(" [DEBUG] Absolute vmdk path: " + vmdkFilePath)
if err != nil {
return err
}
defer resp.Body.Close()
err = upload(context.Background(), ovfFileItem, resp.Body, deviceObj.Url, ovfFileItem.Size, currBytesRead)
return err
}

func uploadOvaDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
diskName := ovfFileItem.Path
ovaFile, err := os.Open(filePath)
if err != nil {
return err
}
defer ovaFile.Close()

err = findAndUploadDiskFromOva(ovaFile, diskName, ovfFileItem, deviceObj, currBytesRead)
return err
}

func uploadOvaDisksFromUrl(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
allowUnverifiedSSL bool) error {
diskName := ovfFileItem.Path
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(filePath)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
err = findAndUploadDiskFromOva(resp.Body, diskName, ovfFileItem, deviceObj, currBytesRead)
if err != nil {
return err
}
} else {
return fmt.Errorf("got status %d while getting the file from remote url %s ", resp.StatusCode, filePath)
}
return nil
}

func GetOvfDescriptor(filePath string, deployOva bool, fromLocal bool, allowUnverifiedSSL bool) (string, error) {

ovfDescriptor := ""
if !deployOva {
if fromLocal {
fileBuffer, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
ovfDescriptor = string(fileBuffer)
} else {
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(filePath)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
ovfDescriptor = string(bodyBytes)
sumitAgrawal007 marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
if fromLocal {
ovaFile, err := os.Open(filePath)
if err != nil {
return "", err
}
defer ovaFile.Close()
ovfDescriptor, err = getOvfDescriptorFromOva(ovaFile)
if err != nil {
return "", err
}

} else {
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(filePath)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {

ovfDescriptor, err = getOvfDescriptorFromOva(resp.Body)
if err != nil {
return "", err
}
} else {
return "", fmt.Errorf("got status %d while getting the file from remote url %s ", resp.StatusCode, filePath)
}
}
}
return ovfDescriptor, nil
}

func getOvfDescriptorFromOva(ovaFile io.Reader) (string, error) {
ovaReader := tar.NewReader(ovaFile)
for {
fileHdr, err := ovaReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
if strings.HasSuffix(fileHdr.Name, ".ovf") {
content, _ := ioutil.ReadAll(ovaReader)
ovfDescriptor := string(content)
return ovfDescriptor, nil
}
}
return "", fmt.Errorf("ovf file not found inside the ova")
}

func findAndUploadDiskFromOva(ovaFile io.Reader, diskName string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
ovaReader := tar.NewReader(ovaFile)
for {
fileHdr, err := ovaReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if fileHdr.Name == diskName {
err = upload(context.Background(), ovfFileItem, ovaReader, deviceObj.Url, ovfFileItem.Size, currBytesRead)
if err != nil {
return fmt.Errorf("error while uploading the file %s %s", diskName, err)
}
return nil
}
}
return fmt.Errorf("disk %s not found inside ova", diskName)
}

func GetNetworkMapping(client *govmomi.Client, d *schema.ResourceData) ([]types.OvfNetworkMapping, error) {
var ovfNetworkMappings []types.OvfNetworkMapping
m := d.Get("ovf_deploy.0.ovf_network_map").(map[string]interface{})
Expand All @@ -213,3 +368,14 @@ func GetNetworkMapping(client *govmomi.Client, d *schema.ResourceData) ([]types.
}
return ovfNetworkMappings, nil
}

func getClient(allowUnverifiedSSL bool) *http.Client {
if allowUnverifiedSSL {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return &http.Client{Transport: tr}
} else {
return &http.Client{}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func VirtualMachineOVFDeploySchema() map[string]*schema.Schema {
func VirtualMachineOvfDeploySchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"local_ovf_path": {
Type: schema.TypeString,
Optional: true,
Description: "The absolute path to the ovf file in the local system. Make sure the other necessary files like" +
"the .vmdk files are also in the same directory as the given ovf file.",
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Description: "The absolute path to the ovf/ova file in the local system.",
ForceNew: true,
},
"remote_ovf_url": {
Type: schema.TypeString,
Optional: true,
Description: "URL to the remote ovf file to be deployed.",
Description: "URL to the remote ovf/ova file to be deployed.",
ForceNew: true,
},
"ip_allocation_policy": {
Expand All @@ -34,7 +33,7 @@ func VirtualMachineOVFDeploySchema() map[string]*schema.Schema {
"disk_provisioning": {
Type: schema.TypeString,
Optional: true,
Description: "An optional disk provisioning. If set, all the disks in the deployed OVF will have the same specified disk type (e.g., thin provisioned).",
Description: "An optional disk provisioning. If set, all the disks in the deployed ovf will have the same specified disk type (e.g., thin provisioned).",
ForceNew: true,
},
"ovf_network_map": {
Expand All @@ -44,5 +43,11 @@ func VirtualMachineOVFDeploySchema() map[string]*schema.Schema {
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
},
"allow_unverified_ssl_cert": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Allow unverified ssl certificates while deploying ovf/ova from url.",
},
}
}
Loading