VMware Cloud Community
HilaCA
Contributor
Contributor

deploy OVF by using C#

I'm trying to deploy OVF by using C#, When I'm deploying the OVF with my tool I'm getting and "Operating system not found" error. however when I'm deploying the same OVF manually  the machine is up and running perfectly.

I'm not sure what I'm doing wrong.

        public VsphereResponse CreateOvf(string datacenterName, string path, string teamFolder, WindowsIdentity userIdentity, string rpName,                                                                                                          string teamName, string envName, string machName, string ovfFile)
        {
            VsphereResponse response = new VsphereResponse();

            ManagedObjectReference dataStoreWithMostFreeSpace = GetDataStoreWithMostFreeSpace(datacenterName, userIdentity);
            ManagedObjectReference datacenterRef =                     vsHandler.Connection.Service.FindByInventoryPath(vsHandler.Connection.ServiceContent.searchIndex, datacenterName);
            ManagedObjectReference moRpRef = vsHandler.GetDecendentMoRef(datacenterRef, NodeTypeEnum.ResourcePool.ToString(),                      rpName);
            ManagedObjectReference temp = vsHandler.GetDecendentMoRef(datacenterRef, NodeTypeEnum.Folder.ToString(), teamName);
            ManagedObjectReference vmFolder = vsHandler.GetDecendentMoRef(temp, NodeTypeEnum.Folder.ToString(), envName);
            ManagedObjectReference hostmor = vsHandler.GetFirstDecendentMoRef(null, NodeTypeEnum.HostSystem.ToString());

            OvfCreateImportSpecParams importSpecParams = CreateImportSpecParams(machName, hostmor);
            string ovfDescriptor = getOvfDescriptorFromLocal(ovfFile);
           
            OvfCreateImportSpecResult ovfImportResult =                     vsHandler.Connection.Service.CreateImportSpec(vsHandler.Connection.ServiceContent.ovfManager, ovfDescriptor, moRpRef,                     dataStoreWithMostFreeSpace, importSpecParams);
            OvfFileItem[] fileItemAttr = ovfImportResult.fileItem;
            if (fileItemAttr != null)
            {
                ManagedObjectReference httpNfcLease = vsHandler.Connection.Service.ImportVApp(moRpRef, ovfImportResult.importSpec,                          vmFolder, hostmor);
                HttpNfcLeaseInfo httpNfcLeaseInfo = vsHandler.GetDynamicProperty(httpNfcLease, "info") as HttpNfcLeaseInfo;
                while (httpNfcLeaseInfo == null)
                {
                    httpNfcLeaseInfo = vsHandler.GetDynamicProperty(httpNfcLease, "info") as HttpNfcLeaseInfo;
                }
       
                HttpNfcLeaseDeviceUrl[] deviceUrlArr = httpNfcLeaseInfo.deviceUrl;
                if (deviceUrlArr != null)
                {
                    int step = 100/fileItemAttr.Length;
                    int progress = 0;
                    foreach (HttpNfcLeaseDeviceUrl deviceUrl in deviceUrlArr)
                    {
                        string deviceKey = deviceUrl.importKey;
                        foreach (OvfFileItem ovfFileItem in fileItemAttr)
                        {
                            if (deviceKey.Equals(ovfFileItem.deviceId))
                            {
                                SendVMDKFile(ovfFileItem.create, ovfFile, deviceUrl.url, ovfFileItem.size);

                                progress += step;
                                vsHandler.Connection.Service.HttpNfcLeaseProgress(httpNfcLease, progress);
                                break;
                            }
                        }
                    }
                    vsHandler.Connection.Service.HttpNfcLeaseProgress(httpNfcLease, 100);
                    vsHandler.Connection.Service.HttpNfcLeaseComplete(httpNfcLease);
                }

            }

            return response;
        }

         private void SendVMDKFile(Boolean put, string fileName, string url, long diskCapacity)
        {
            Console.WriteLine("Destination host URL: " + url);
            Uri uri = new Uri(url);
            WebRequest request = HttpWebRequest.Create(uri);
            if (put)
            {
                request.Method = "PUT";
            }
            else
            {
                request.Method = "POST";
            }
            request.ContentLength = diskCapacity;
            request.ContentType = "application/x-vnd.vmware-streamVmdk";

            using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                //// Read the source file into a byte array.
                Stream dataStream = request.GetRequestStream();

                // Write the byte array to the other FileStream.
                int len = 0;
                byte[] buffer = new byte[1024 * 1024];

                while ((len = fileStream.Read(buffer, 0, buffer.Length-len)) > 0)
                {
                    dataStream.Write(buffer, 0, len);
                }
                dataStream.Flush();
                dataStream.Close();
            }
        }

         private string getOvfDescriptorFromLocal(string ovfDescriptorUrl)
        {
            string strContent = "";
            StreamReader sr = new System.IO.StreamReader(ovfDescriptorUrl);
            strContent = sr.ReadToEnd();
            return strContent;
        }

         private OvfCreateImportSpecParams CreateImportSpecParams(string newVmName, ManagedObjectReference hostmor)
        {
            OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
            importSpecParams.hostSystem = hostmor;
            importSpecParams.locale = "";
            importSpecParams.entityName = newVmName;
            importSpecParams.deploymentOption = "";
            return importSpecParams;
        }

Tags (2)
Reply
0 Kudos
0 Replies