public ActionResult Create(VMOutput vmoutput) { if (ModelState.IsValid) { db.VMOutputs.Add(vmoutput); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.VMTemplateId = new SelectList(db.VMTemplates, "Id", "UniqueName", vmoutput.VMTemplateId); ViewBag.VMRequestId = new SelectList(db.VMRequests, "Id", "DisplayName", vmoutput.VMRequestId); return(View(vmoutput)); }
public ActionResult Create(VMTemplate vmtemplate) { try { if (ModelState.IsValid) { db.VMTemplates.Add(vmtemplate); db.SaveChanges(); return(View("Success")); } return(View(vmtemplate)); } catch (Exception e) { Api.Core.Exceptions.ExceptionManager.HandleException(e); return(View("Error")); } }
/// <summary> /// Saves this instance. /// The only properties saved/updated will be the: request status and last udpdated date. /// </summary> /// <returns></returns> public bool Save() { bool result = false; try { using (var db = new VMFSupportContext()) { VMRequest request = (from v in db.VMRequests where v.Id == this.Id select v).First(); if (request == null) { throw new TechnicalException("Virtual machine request not found!"); } if (request.RequestStatus != (int)this.Status) { request.RequestStatus = (int)this.Status; request.LastUpdated = DateTime.UtcNow; } if (request.ProcessLog != this.ProcessLog) { request.ProcessLog = this.ProcessLog; request.LastUpdated = DateTime.UtcNow; } db.SaveChanges(); result = true; } } catch (Exception e) { ExceptionManager.HandleException(e); throw; } return(result); }
public void SetStatuses(int id, VmStatus status) { var vmRequest = (from vms in db.VMRequests where vms.Id == id select vms).Single <VMRequest>(); vmRequest.RequestStatusLogs.Add(new RequestStatusLog() { RequestStatusDetails = status.StatusInformation, InsertedOn = DateTime.Now, }); vmRequest.RequestStatus = status.CurrentState; db.SaveChanges(); }
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 }
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); } }
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)); }