public static void SetMgeProcessFullName(string name) { try { lock (_objLock) { _setting.MgeProcessFullName = name; SettingView view = _setting; if (File.Exists(settingPath)) { var str = File.ReadAllText(settingPath); view = str.DeserializeObject <SettingView>() ?? new SettingView(); view.MgeProcessFullName = name; } using (StreamWriter writer = File.CreateText(settingPath)) { writer.WriteLine(view.SerializeObject()); writer.Flush(); } } } catch (Exception e) { TxtLogService.WriteLog(e, "保存进程守护路径异常,信息:" + name); } }
/// <summary> /// 获取所有iis应用程序名称 /// </summary> /// <returns>应用程序名称集合</returns> public static List <AppView> GetAllIISAppInfo() { List <AppView> res = new List <AppView>(); try { using (var mgr = new ServerManager(@"C:\Windows\System32\inetsrv\config\applicationHost.config")) { foreach (var site in mgr.Sites) { res.Add(new AppView { AppName = site.Name, AppPhysicalPath = site.Applications["/"]?.VirtualDirectories["/"]?.PhysicalPath ?? string.Empty, AppAlias = site.Name, Id = site.Id.ToString(), Status = site.State == ObjectState.Started ? 0 : 1, }); } } } catch (Exception e) { TxtLogService.WriteLog(e, "获取所有iis应用程序名称异常"); } return(res); }
public static void SetServiceGroups(List <ServiceGroup> data) { try { lock (_objLock) { _setting.ServiceGroups = data; SettingView view = _setting; if (File.Exists(settingPath)) { var str = File.ReadAllText(settingPath); view = str.DeserializeObject <SettingView>() ?? new SettingView(); view.ServiceGroups = data; } using (StreamWriter writer = File.CreateText(settingPath)) { writer.WriteLine(view.SerializeObject()); writer.Flush(); } } } catch (Exception e) { TxtLogService.WriteLog(e, "保存服务器组异常,信息:" + data.SerializeObject()); } }
public static string GetMgeProcessFullName() { string res = null; try { res = _setting.MgeProcessFullName.DeepCopy(); if (string.IsNullOrWhiteSpace(res)) { // 从进程中读取信息 var allProcesses = Process.GetProcesses(); var mgeProcess = allProcesses.FirstOrDefault(n => n.ProcessName == "ProcessManageApplication"); if (mgeProcess == null) { throw new Exception("未找到守护进程"); } res = mgeProcess.MainModule.FileName; SetMgeProcessFullName(res); } } catch (Exception e) { TxtLogService.WriteLog(e, "读取进程守护路径异常"); } return(res); }
/// <summary> /// 压缩文件夹 /// </summary> /// <param name="dir">文件夹</param> /// <param name="zipName">压缩后的文件名称</param> /// <returns>压缩结果</returns> public static bool ZipDirectory(string dir, string zipName) { bool res = false; try { List <string> pathList = new List <string>(); DirectoryInfo fileDire = new DirectoryInfo(dir); foreach (var directory in fileDire.GetDirectories()) { if (directory.Name.ToLower().Contains("log") || directory.Name.ToLower().Contains("wcfconfig")) { continue; } pathList.Add(directory.FullName); } foreach (FileInfo file in fileDire.GetFiles("*.*").Where(n => !n.Name.ToLower().EndsWith("xml") && !n.Name.Equals("TPublish.setting"))) { if (file.Extension.ToLower() == ".config" || file.Extension.ToLower() == ".manifest" || file.Extension.ToLower() == ".asax") { continue; } pathList.Add(file.FullName); } res = ZipManyFilesOrDictorys(pathList, zipName, dir); } catch (Exception e) { TxtLogService.WriteLog(e, "压缩文件夹异常,信息:" + new { dir, zipName }.SerializeObject()); } return(res); }
/// <summary> /// 获取EXE程序信息 /// </summary> /// <param name="appName"></param> /// <returns></returns> public static List <AppView> GetExeAppInfoByName(this string appName) { List <AppView> res = new List <AppView>(); try { string mgeProcessFileName = SettingLogic.GetMgeProcessFullName(); string processMgeXmlFullName = Path.Combine(Directory.GetParent(mgeProcessFileName).FullName, "ProcessInfo.xml"); XElement element = XElement.Load(processMgeXmlFullName); var subElements = element.Elements(); if (!appName.IsNullOrEmpty()) { subElements = subElements.Where(n => n.Attribute("Name").Value.ToLower().StartsWith(appName.ToLower())); } foreach (XElement processElement in subElements) { AppView view = new AppView { AppName = processElement.Attribute("Name")?.Value ?? string.Empty, Id = processElement.Attribute("ID")?.Value ?? string.Empty, AppPhysicalPath = processElement.Attribute("Path")?.Value ?? string.Empty, AppAlias = processElement.Attribute("Desc")?.Value ?? string.Empty, Status = ((processElement.Attribute("IsDie")?.Value ?? "") == "False") ? 0 : 1, }; res.Add(view); } } catch (Exception e) { TxtLogService.WriteLog(e, "获取EXE程序信息异常,信息:" + appName); } return(res); }
/// <summary> /// 获取所有EXE程序信息 /// </summary> /// <returns></returns> public static List <AppView> GetAllExeAppInfo() { List <AppView> res = new List <AppView>(); try { string mgeProcessFileName = SettingLogic.GetMgeProcessFullName(); string processMgeXmlFullName = Path.Combine(Directory.GetParent(mgeProcessFileName).FullName, "ProcessInfo.xml"); XElement element = XElement.Load(processMgeXmlFullName); foreach (XElement processElement in element.Elements()) { AppView view = new AppView { AppName = processElement.Attribute("Name")?.Value ?? string.Empty, Id = processElement.Attribute("ID")?.Value ?? string.Empty, AppPhysicalPath = processElement.Attribute("Path")?.Value ?? string.Empty, AppAlias = processElement.Attribute("Desc")?.Value ?? string.Empty, Status = ((processElement.Attribute("IsDie")?.Value ?? "") == "False") ? 0 : 1, }; res.Add(view); } } catch (Exception e) { TxtLogService.WriteLog(e, "获取所有EXE程序信息异常"); } return(res); }
/// <summary> /// 获取所有iis应用程序名称 /// </summary> /// <returns>应用程序信息</returns> public static AppView GetIISAppInfoById(this string id) { AppView res = new AppView(); try { using (var mgr = new ServerManager(@"C:\Windows\System32\inetsrv\config\applicationHost.config")) { var site = mgr.Sites.FirstOrDefault(n => n.Id.ToString() == id); if (site != null) { res = new AppView { AppName = site.Name, AppPhysicalPath = site.Applications["/"]?.VirtualDirectories["/"]?.PhysicalPath ?? string.Empty, AppAlias = site.Name, Id = site.Id.ToString(), Status = site.State == ObjectState.Started ? 0 : 1, }; } } } catch (Exception e) { TxtLogService.WriteLog(e, "获取指定iis应用程序名称异常,id=" + id); } return(res); }
/// <summary> /// 获取EXE程序信息 /// </summary> /// <param name="id"></param> /// <returns></returns> public static AppView GetExeAppInfoById(this string id) { AppView res = null; try { string mgeProcessFileName = SettingLogic.GetMgeProcessFullName(); string processMgeXmlFullName = Path.Combine(Directory.GetParent(mgeProcessFileName).FullName, "ProcessInfo.xml"); XElement element = XElement.Load(processMgeXmlFullName); XElement processElement = element.Elements().FirstOrDefault(n => n.Attribute("ID")?.Value == id); if (processElement == null) { return(null); } return(new AppView { AppName = processElement.Attribute("Name")?.Value ?? string.Empty, Id = processElement.Attribute("ID")?.Value ?? string.Empty, AppPhysicalPath = processElement.Attribute("Path")?.Value ?? string.Empty, AppAlias = processElement.Attribute("Desc")?.Value ?? string.Empty, Status = ((processElement.Attribute("IsDie")?.Value ?? "") == "False") ? 0 : 1, }); } catch (Exception e) { TxtLogService.WriteLog(e, "获取EXE程序信息异常,信息:" + id); } return(res); }
public static Result ExeAppVersionRollBack(this string appId) { Result res = new Result(); try { var allProcesses = Process.GetProcesses(); // 读取进程守护信息 string mgeProcessFileName = SettingLogic.GetMgeProcessFullName(); string processMgeXmlFullName = Path.Combine(Directory.GetParent(mgeProcessFileName).FullName, "ProcessInfo.xml"); XElement element = XElement.Load(processMgeXmlFullName); var appProcessXml = element.Elements().FirstOrDefault(n => n.Attribute("ID")?.Value == appId); if (appProcessXml == null) { throw new Exception("该进程未纳入到守护进程中,无法自动部署"); } var appProcess = allProcesses.FirstOrDefault(n => n.Id.ToString() == appProcessXml.Attribute("PID").Value); if (appProcess == null) { throw new Exception("未找到该进程"); } string appFullPath = appProcess.MainModule.FileName; string appPath = Directory.GetParent(appFullPath).FullName; string newAppPath = appPath.SubVersion(); // 关闭进程守护 var mgeProcess = allProcesses.FirstOrDefault(n => String.Equals(n.ProcessName, "ProcessManageApplication", StringComparison.CurrentCultureIgnoreCase)); mgeProcess?.Kill(); // 更新版本号 appProcessXml.Attribute("Path").Value = newAppPath; element.Save(processMgeXmlFullName); // 关闭源程序 appProcess.Kill(); // 启动进程守护 Process.Start(mgeProcessFileName); res.IsSucceed = true; } catch (Exception e) { res.Message = e.Message; TxtLogService.WriteLog(e, "Exe程序版本回退异常,信息:" + appId); } return(res); }
public static void SaveSetting() { lock (_objLock) { try { using (StreamWriter writer = File.CreateText(settingPath)) { writer.WriteLine(_setting.SerializeObject()); writer.Flush(); } } catch (Exception e) { TxtLogService.WriteLog(e, "写入配置文件异常,信息:" + _setting.SerializeObject()); } } }
/// <summary> /// 获取远程服务器负载程序信息 /// </summary> /// <param name="remoteAppId">appid</param> /// <param name="serAdress">负载服务器地址</param> /// <returns>程序信息</returns> public static AppView GetRemoteIISAppInfoById(this string remoteAppId, string serAdress) { AppView res = new AppView(); try { string url = $"{serAdress}/GetIISAppViewByAppId?appId={remoteAppId}"; var appInfo = new HttpHelper().HttpGet(url, null, Encoding.UTF8, false, false, 10000); return(appInfo.DeserializeObject <AppView>()); } catch (Exception e) { TxtLogService.WriteLog(e, "查询远程服务器IIS负载程序信息异常,信息:" + new { remoteAppId, serAdress }.SerializeObject()); } return(res); }
/// <summary> /// 获取远程服务器负载EXE程序信息 /// </summary> /// <param name="remoteAppId"></param> /// <param name="serAdress">负载服务器地址</param> /// <returns></returns> public static List <AppView> GetRemoteExeAppInfoByName(this string remoteAppName, string serAdress) { List <AppView> res = new List <AppView>(); try { string url = $"{serAdress}/GetExeAppView?appName={remoteAppName}"; var appInfo = new HttpHelper().HttpGet(url, null, Encoding.UTF8, false, false, 10000); return(appInfo.DeserializeObject <List <AppView> >()); } catch (Exception e) { TxtLogService.WriteLog(e, "查询远程服务器EXE负载程序信息异常,信息:" + new { remoteAppName, serAdress }.SerializeObject()); } return(res); }
/// <summary> /// IIS程序版本回退 /// </summary> /// <param name="appId">appId</param> /// <returns>回退结果</returns> public static Result IISAppVersionRollBack(this string appId) { Result res = new Result(); try { using (var mgr = new ServerManager(@"C:\Windows\System32\inetsrv\config\applicationHost.config")) { var site = mgr.Sites.FirstOrDefault(n => n.Id.ToString() == appId); if (site == null) { throw new Exception("该应用程序不存在"); } var lastVersionPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath; string oldVersion = lastVersionPath.SubVersion(); site.Applications["/"].VirtualDirectories["/"].PhysicalPath = oldVersion; mgr.CommitChanges(); var appPool = mgr.ApplicationPools[site.Name]; if (appPool == null) { throw new Exception("该应用程序池不存在"); } if (appPool.State == ObjectState.Stopped) { appPool.Start(); } else { appPool.Recycle(); } mgr.CommitChanges(); } res.IsSucceed = true; } catch (Exception e) { res.Message = e.Message; TxtLogService.WriteLog(e, "回退iis版本异常,信息:" + appId); } return(res); }
public static void SetAppZipFilePath(string key, string val) { try { lock (_objLock) { if (_setting.AppZipFileMap.ContainsKey(key)) { _setting.AppZipFileMap[key] = val; } else { _setting.AppZipFileMap.Add(key, val); } SettingView view = _setting; if (File.Exists(settingPath)) { var str = File.ReadAllText(settingPath); view = str.DeserializeObject <SettingView>() ?? new SettingView(); if (_setting.AppZipFileMap.ContainsKey(key)) { _setting.AppZipFileMap[key] = val; } else { _setting.AppZipFileMap.Add(key, val); } } using (StreamWriter writer = File.CreateText(settingPath)) { writer.WriteLine(view.SerializeObject()); writer.Flush(); } } } catch (Exception e) { TxtLogService.WriteLog(e, "保存app程序zip文件路径异常,信息:" + new { key, val }.SerializeObject()); } }
/// <summary> /// 获取指定远程服务上的所有iis程序信息 /// </summary> /// <param name="serAdress">远程服务器地址</param> /// <returns>iis程序信息</returns> public static List <AppView> GetAllRemoteIISAppInfo(string serAdress) { List <AppView> res = new List <AppView>(); try { string url = $"{serAdress}/GetAllIISAppView"; List <AppView> appInfos = new HttpHelper().HttpGet(url, null, Encoding.UTF8, false, false, 10000).DeserializeObject <List <AppView> >(); if (appInfos != null && !appInfos.Any()) { res.AddRange(appInfos); } } catch (Exception e) { TxtLogService.WriteLog(e, "获取远程服务器上所有iis程序异常,信息:" + serAdress); } return(res); }
static SettingLogic() { try { if (File.Exists(settingPath)) { var str = File.ReadAllText(settingPath); _setting = str.DeserializeObject <SettingView>() ?? new SettingView(); } else { using (StreamWriter writer = File.CreateText(settingPath)) { writer.WriteLine(_setting.SerializeObject()); writer.Flush(); } } } catch (Exception e) { TxtLogService.WriteLog(e, "读取配置文件异常"); } }