public static void NewTemplate(string hostName, string name, Guid baseUid, string switchName, JToken config, PsStreamEventHandlers handlers = null) { var baseVm = SessionManager.GetDatabase().GetVm(baseUid); var srcHost = baseVm.Host; var dstHost = hostName; string dstDir = Path.Combine(Settings.Default.vhdPath, srcHost); string srcPath = GetTopMostParent(baseVm.Host, baseVm.VhdPath[0]); string dstPath = Path.Combine(dstDir, baseUid.ToString() + ".vhdx"); string vhdPath = Path.Combine(Settings.Default.vhdPath, name + ".vhdx"); if (srcHost != hostName) { if (!PathExists(dstHost, dstPath)) { NewDirectory(dstHost, dstDir, handlers); CopyFile(srcHost, dstHost, srcPath, dstPath, handlers); } } else { dstPath = srcPath; } HyperV.NewVHD(dstHost, dstPath, vhdPath, handlers); PSWrapper.Execute(dstHost, config, handlers, name, vhdPath, switchName); var vm = HyperV.GetVm(hostName, name, handlers).GetDbObject(); vm.ParentHost = srcHost; vm.ParentUuid = baseUid; vm.VmType = (int)VirtualMachineType.TEMPLATE; SessionManager.GetDatabase().SetVm(vm); }
public static void NewDeployment(string hostName, string name, Guid tmplUid, string switchName, JToken config, PsStreamEventHandlers handlers = null) { var tmplVm = SessionManager.GetDatabase().GetVm(tmplUid); var baseVm = SessionManager.GetDatabase().GetVm(tmplVm.ParentUuid); if (baseVm == null) { throw new Exception("Template VM without parent."); } var bsrcHost = baseVm.Host; string bdstDir = Path.Combine(Settings.Default.vhdPath, bsrcHost); string bdstPath = Path.Combine(bdstDir, baseVm.Uuid.ToString() + ".vhdx"); string bsrcPath = GetTopMostParent(baseVm.Host, baseVm.VhdPath[0]); var tsrcHost = tmplVm.Host; string tsrcPath = tmplVm.VhdPath[0]; string dstHost = hostName; string vhdPath = Path.Combine(Settings.Default.vhdPath, name + ".vhdx"); if (bsrcHost != hostName) { if (!PathExists(dstHost, bdstPath)) { NewDirectory(dstHost, bdstDir, handlers); CopyFile(bsrcHost, dstHost, bsrcPath, bdstPath, handlers); } } else { bdstPath = bsrcPath; } if (!PathExists(dstHost, vhdPath)) { NewDirectory(dstHost, bdstDir, handlers); CopyFile(tsrcHost, dstHost, tsrcPath, vhdPath, handlers); } HyperV.SetVHD(dstHost, bdstPath, vhdPath, true, handlers); PSWrapper.Execute(dstHost, config, handlers, name, vhdPath, switchName); var vm = HyperV.GetVm(hostName, name, handlers).GetDbObject(); vm.ParentHost = bsrcHost; vm.ParentUuid = baseVm.Uuid; vm.VmType = (int)VirtualMachineType.DEPLOY; SessionManager.GetDatabase().SetVm(vm); }
public static string GetTopMostParent(string hostName, string path, PsStreamEventHandlers handlers = null) { while (true) { var pso = HyperV.GetVhd(hostName, path, handlers)[0]; if (pso == null) { break; } if (string.IsNullOrWhiteSpace((string)pso?.Members["ParentPath"].Value)) { break; } path = (string)pso?.Members["ParentPath"].Value; } return(path); }