Exemple #1
0
        public static void CreateVirtualMachine(VMRequest vmToProcess, VMFSupportContext db)
        {
            #region Start VM Creation process

            #region Lauch conditions
            // Check for launch conditions
            Console.WriteLine("Checking for launch conditions.");

            // Call the lauunch checker module
            bool readyToLounch = false;
            ValidateLaunch launchValidator = new ValidateLaunch();
            readyToLounch = launchValidator.VerifyLaunchConditions();
            Console.WriteLine("Completed with the result [{0}]", readyToLounch.ToString());

            // dump the screen info
            Console.WriteLine("Checking lounch conditions: {0}", "<TBD> Lounch conditions status");

            #endregion

            if (readyToLounch) {  Console.WriteLine(String.Format("Reserving VM request : {0} ({1})", vmToProcess.MachineName, vmToProcess.Id));  vmToProcess.RequestStatus = (int)RequestStatus.Queued; vmToProcess.ProcessLog = "";  var valErrors = db.GetValidationErrors();  if (valErrors.Count() > 0) { foreach (var valErr in valErrors) { foreach (var valerrCol in valErr.ValidationErrors) { Console.WriteLine(String.Format("Validation errors on update: {0} - {1}", valerrCol.PropertyName, valerrCol.ErrorMessage)); } } } else    db.SaveChanges();  Console.WriteLine(String.Format("Calling the StartProcess for VM : {0} ({1})", vmToProcess.MachineName, vmToProcess.Id)); vmToProcess.RequestStatus = (int)RequestStatus.InProgress; vmToProcess.ProcessLog = "Copying and importing base VM."; db.SaveChanges();   string baseVmSourcePath = DefaultConfigurationStore.Current.BaseVmSourcePath; string destinationFolder = vmToProcess.DestinationFolder;  string baseVmConfigFile = DefaultConfigurationStore.Current.BaseVmConfigFilePath.Replace(baseVmSourcePath, destinationFolder); string newVmName = vmToProcess.DisplayName; string baseVmName = DefaultConfigurationStore.Current.BaseVmName; HyperVHelper.CopyAndImportVM( baseVmName, baseVmSourcePath, destinationFolder, baseVmConfigFile, newVmName);  vmToProcess.ProcessLog = "Preloading boot data."; db.SaveChanges();  string driveLetter = DefaultConfigurationStore.Current.TemporaryDriveLetter; string dismPath = DefaultConfigurationStore.Current.DismPath; string vmId = vmToProcess.Id.ToString(); string vmStatusSvcUrl = DefaultConfigurationStore.Current.VmfStatusServiceUrl; string cfgFileName = DefaultConfigurationStore.Current.VmfConfigFile;  HyperVHelper.PreLoadVHDBootData( driveLetter, dismPath, vmToProcess.DisplayName, vmId, vmStatusSvcUrl, cfgFileName);  vmToProcess.ProcessLog = "Starting VM."; db.SaveChanges();  HyperVHelper.StartVM(vmToProcess.DisplayName);  Console.WriteLine(String.Format("Transferred control for MDT for VM: {0} ({1})", vmToProcess.MachineName, vmToProcess.Id));              }
            else { Console.WriteLine("No resources available at the moment. Skipping cycle");  Thread.Sleep(DefaultConfigurationStore.Current.WaitTimeBetweenCicles * 1000);  }
            #endregion
        }
Exemple #2
0
 public static void DeployVirtualMachineForDownload(VMRequest vmToProcess, VMFSupportContext db)
 {
     Debug.WriteLine("VM [{1}]({0}) Shutting down.", vmToProcess.Id, vmToProcess.DisplayName); if (HyperVHelper.StopVM(vmToProcess.DisplayName)) { Debug.WriteLine("VM [{1}]({0}) Compressing", vmToProcess.Id, vmToProcess.DisplayName);  vmToProcess.RequestStatus = (int)RequestStatus.Packaging; vmToProcess.ProcessLog = "Compressing VHD file."; db.SaveChanges();  VMFinalization finalization = new VMFinalization();  Guid internalId = Guid.NewGuid();  string dropFolder = string.Format( DefaultConfigurationStore.Current.VhdSplitDropLocationTemplate, internalId);  if (finalization.FinalizeVM( vmToProcess.DisplayName, dropFolder, DefaultConfigurationStore.Current.VhdSplitBlockSize, null, null)) { Debug.WriteLine("VM [{1}]({0}) Updating request information", vmToProcess.Id, vmToProcess.DisplayName);  vmToProcess.RequestStatus = (int)RequestStatus.Deploying; vmToProcess.ProcessLog = "Deploying result."; db.SaveChanges(); vmToProcess.VMOutputs.Clear(); DirectoryInfo dirInfo = new DirectoryInfo(dropFolder);                     foreach (FileInfo file in dirInfo.GetFiles()) { VMOutput vmOut = new VMOutput(); vmOut.FileName = file.Name; vmOut.DownloadUrl = string.Concat(DefaultConfigurationStore.Current.BaseVmDownloadUrl, "/", internalId, "/", file.Name);  vmOut.VMRequest = vmToProcess; vmOut.VMRequestId = vmToProcess.Id; vmOut.VMTemplate = vmToProcess.VMTemplate; vmOut.VMTemplateId = vmToProcess.VMTemplate.Id; vmOut.CreatedOn = file.CreationTime.ToUniversalTime(); vmOut.LastModified = file.LastWriteTime;  vmOut.Log += "File added to the VM Output file list.";  vmToProcess.VMOutputs.Add(vmOut); } Debug.WriteLine("VM [{1}]({0}) Sending email to the user", vmToProcess.Id, vmToProcess.DisplayName); try { EmailHelper.SendConfirmationEmail( vmToProcess.DisplayName, vmToProcess.CreatedBy, vmToProcess.VMOutputs ); } catch (Exception ex) { Console.WriteLine("Processing Error (unable to send email alert) \n {0} \n {1}\n\n", ex.Message, ex.StackTrace); } vmToProcess.RequestStatus = (int)RequestStatus.ReadyForPickup; vmToProcess.ProcessLog = "Done.";  var valErrors = db.GetValidationErrors();                     if (valErrors.Count() > 0) { foreach (var valErr in valErrors) { foreach (var valerrCol in valErr.ValidationErrors) { Console.WriteLine("Validation errors on update: {0} - {1}", valerrCol.PropertyName, valerrCol.ErrorMessage); } } } else db.SaveChanges(); if (HyperVHelper.RemoveVM(vmToProcess.DisplayName)) { try { DirectoryInfo dir = new DirectoryInfo(vmToProcess.DestinationFolder); dir.Delete(true); } catch (Exception ex) { Console.WriteLine("Processing Error (unable to delete VM info) \n {0} \n {1}\n\npath: ", ex.Message, ex.StackTrace, vmToProcess.DestinationFolder); } } else { Console.WriteLine("Processing Error (unable to remove VM from Hyper-V)"); } } } else { Console.WriteLine("Unable to stop machine: {0}", vmToProcess.DisplayName); }
 }
 /// <summary>
 /// This constructor translates the data entity into the business entity
 /// </summary>
 /// <param name="dataRequest"></param>
 public VirtualMachineRequest(VMRequest dataRequest)
 {
     Populate(dataRequest);
 }
 private void Populate(VMRequest dataRequest)
 {
     this.IsValid = false;  try {  this.Id = dataRequest.Id;  if (!string.IsNullOrEmpty(dataRequest.MDTConfigurationSettings)) { if (this.MdtConfiguration == null) this.MdtConfiguration = new XmlDataDocument();  this.MdtConfiguration.LoadXml(dataRequest.MDTConfigurationSettings); }  if (!string.IsNullOrEmpty(dataRequest.TargetConfiguration)) { if (this.DeploymentConfiguration == null) this.DeploymentConfiguration = new XmlDataDocument();  this.DeploymentConfiguration.LoadXml(dataRequest.TargetConfiguration); }  this.RequestedBy = dataRequest.CreatedBy;  if (dataRequest.CreatedOn != null) this.RequestedOn = (DateTime) dataRequest.CreatedOn;  if (dataRequest.VMTemplate != null) this.TemplateId = dataRequest.VMTemplate.Id.ToString();  if (dataRequest.RequestStatus != null) this.Status = (RequestStatus)dataRequest.RequestStatus;  this.ProcessLog = dataRequest.ProcessLog;  this.IsValid = true;  } catch (Exception ex) { ExceptionManager.HandleException(ex); }
 }
 public ActionResult Edit(VMRequest vmrequest)
 {
     if (ModelState.IsValid) { db.Entry(vmrequest).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.RequestStatus = new SelectList(db.RequestStatus, "Id", "Name", vmrequest.RequestStatus); ViewBag.TemplateId = new SelectList(db.VMTemplates, "Id", "UniqueName", vmrequest.TemplateId); return View(vmrequest);
 }
 public ActionResult Create(VMRequest vmrequest)
 {
     bool redirectToVMOutputList = false;  if (ModelState.IsValid) { string vmUrl = string.Empty; StringBuilder message = new StringBuilder();  try { vmrequest.RequestStatus = (int)VMFactory.Api.Business.Entity.RequestStatus.None; vmrequest.LastUpdated = DateTime.UtcNow; vmrequest.CreatedOn = vmrequest.LastUpdated; db.VMRequests.Add(vmrequest); db.SaveChanges();  var existingVms = db.VMOutputs.Where( c => c.VMRequest.TemplateId == vmrequest.TemplateId && c.VMRequest.CreatedBy == vmrequest.CreatedBy).OrderByDescending(c => c.VMRequest.CreatedBy); if (existingVms.Count() > 0) { StringBuilder vmList = new StringBuilder("Here is a list of prebuilt VMs for the selected template:"); foreach (VMOutput v in existingVms) vmList.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href=\"{3}\" target=\"_blank\">Download</a></td></tr>\n", v.VMTemplate.DisplayName, v.FileName, v.LastModified, v.DownloadUrl);  message.Append(vmList.ToString());  redirectToVMOutputList = true;  }  SendConfirmationEmail(vmrequest.CreatedBy, message.ToString());  if (redirectToVMOutputList) return RedirectToAction("ListAvailable", "VMOutput", new { templateId = vmrequest.TemplateId }); else return View("Success"); } catch (DbEntityValidationException e) { Api.Core.Exceptions.ExceptionManager.HandleException(e); return View("Error");; } }  ViewBag.RequestStatus = new SelectList(db.RequestStatus, "Id", "Name", vmrequest.RequestStatus); ViewBag.TemplateId = new SelectList(db.VMTemplates, "Id", "UniqueName", vmrequest.TemplateId); return View(vmrequest);
 }
 public ActionResult Create()
 {
     ViewBag.RequestStatus = new SelectList(db.RequestStatus, "Id", "Name"); ViewBag.TemplateId = new SelectList(db.VMTemplates, "Id", "UniqueName");  VMRequest request = new VMRequest(); if (IsRegistered()) { request.CreatedBy = ViewBag.Email;  return View(request); } else return RedirectToAction("Register", "Home");
 }