public override string RollBack() { try { //获取到版本 //check版本是会否存在 //如果存在那么执行重新覆盖 //读取该项目是否存在 不存在就报错 //读取该项目的地址 地址不存在就报错 //执行覆盖 var projectPath = Path.Combine(Setting.PublishIIsPathFolder, _projectName); _projectPublishFolder = Path.Combine(projectPath, _dateTimeFolderName); if (!Directory.Exists(_projectPublishFolder)) { return("rollback folder not found:" + _projectPublishFolder); } #if NETCORE Log("netcore agent version ==>" + AntDeployAgentWindows.Version.VERSION); #else Log("netframework agent version ==>" + AntDeployAgentWindows.Version.VERSION); #endif var deployFolder = Path.Combine(_projectPublishFolder, "publish"); if (!Directory.Exists(deployFolder)) { if (Directory.Exists(_projectPublishFolder)) { var temp = new DirectoryInfo(_projectPublishFolder); var tempFolderList = temp.GetDirectories(); if (tempFolderList.Length == 1) { deployFolder = tempFolderList.First().FullName; } } } var isUseTempPhysicalPath = false; var ddeploy = Path.Combine(_projectPublishFolder, "_deploy_"); if (Directory.Exists(ddeploy)) { deployFolder = ddeploy; //这里需要直接修改物理路径就可以了 isUseTempPhysicalPath = true; } else { var incrementFolder = Path.Combine(_projectPublishFolder, "increment"); if (Directory.Exists(incrementFolder)) { deployFolder = incrementFolder; } } if (!Directory.Exists(deployFolder)) { return("rollback folder not found:" + deployFolder); } Log("rollback from folder ==>" + deployFolder); //查找 IIS 里面是否存在 var siteArr = _webSiteName.Split('/'); if (siteArr.Length > 2) { return($"website level limit is 2!"); } var level1 = siteArr[0]; var level2 = siteArr.Length == 2 ? siteArr[1] : string.Empty; var isSiteExistResult = IISHelper.IsSiteExist(level1, level2); if (!string.IsNullOrEmpty(isSiteExistResult.Item3)) { return($"【Error】 : {isSiteExistResult.Item3}"); } if (!isSiteExistResult.Item1)//一级都不存在 { return($"website: ${level1} not found"); } if (!isSiteExistResult.Item2)//一级都不存在 { return($"website:${level2} not found in ${level1}"); } var projectLocation = IISHelper.GetWebSiteLocationInIIS(level1, level2, Log); if (projectLocation == null) { return($"website:${_webSiteName} location not found"); } if (string.IsNullOrEmpty(projectLocation.Item1)) { return($"website : {_webSiteName} not found in iis"); } if (!Directory.Exists(projectLocation.Item1)) { return($"website:${_webSiteName} location not found"); } Log("Start to rollback IIS:"); Log("SiteName ===>" + _webSiteName); Log("SiteFolder ===> " + projectLocation.Item1); Log("SiteApplicationPoolName ===> " + projectLocation.Item3); if (isUseTempPhysicalPath) { var err = IISHelper.ChangePhysicalPath(level1, level2, deployFolder); if (string.IsNullOrEmpty(err)) { Log("Change Site PhysicalPath Success:" + (level1 + "/" + level2)); Log("NewSiteFolder ===> " + deployFolder); } else { Log("Change Site PhysicalPath Fail:" + (level1 + "/" + level2)); } return(err); } Arguments args = new Arguments { DeployType = "IIS", BackupFolder = Setting.BackUpIIsPathFolder, AppName = _projectName, ApplicationPoolName = projectLocation.Item3, AppFolder = projectLocation.Item1, DeployFolder = deployFolder, SiteName = projectLocation.Item2, NoBackup = true }; var ops = new OperationsIIS(args, Log); try { ops.Execute(); Log("Rollback IIS Execute Success"); } catch (Exception ex) { try { //ops.Rollback(); return($"Rollback to iis err:{ex.Message}"); } catch (Exception ex2) { return($"Rollback to iis err:{ex.Message},fail:{ex2.Message}"); } } return(string.Empty); } catch (Exception exx) { return(exx.Message); } }
public override string DeployExcutor(FormHandler.FormItem fileItem) { var projectPath = Path.Combine(Setting.PublishIIsPathFolder, _projectName); _projectPublishFolder = Path.Combine(projectPath, !string.IsNullOrEmpty(_dateTimeFolderName) ? _dateTimeFolderName : DateTime.Now.ToString("yyyyMMddHHmmss")); EnsureProjectFolder(projectPath); EnsureProjectFolder(_projectPublishFolder); try { var isNetcore = _sdkTypeName.ToLower().Equals("netcore"); var filePath = Path.Combine(_projectPublishFolder, fileItem.FileName); using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { fs.Write(fileItem.FileBody, 0, fileItem.FileBody.Length); } if (!File.Exists(filePath)) { return("publish file save fail"); } #if NETCORE Log("netcore agent version ==>" + AntDeployAgentWindows.Version.VERSION); #else Log("netframework agent version ==>" + AntDeployAgentWindows.Version.VERSION); #endif Log("upload success ==>" + filePath); //解压 try { Log("start unzip file"); ZipFile.ExtractToDirectory(filePath, _projectPublishFolder); } catch (Exception ex) { return("unzip publish file error:" + ex.Message); } Log("unzip success ==>" + _projectPublishFolder); var deployFolder = Path.Combine(_projectPublishFolder, "publish"); if (!Directory.Exists(deployFolder)) { if (Directory.Exists(_projectPublishFolder)) { var temp = new DirectoryInfo(_projectPublishFolder); var tempFolderList = temp.GetDirectories(); if (tempFolderList.Length == 1) { deployFolder = tempFolderList.First().FullName; } } } if (!Directory.Exists(deployFolder)) { return("unzip publish file error,Path not found:" + deployFolder); } //查找 IIS 里面是否存在 var siteArr = _webSiteName.Split('/'); if (siteArr.Length > 2) { return($"website level limit is 2!"); } var level1 = siteArr[0]; var level2 = siteArr.Length == 2 ? siteArr[1] : string.Empty; var isSiteExistResult = IISHelper.IsSiteExist(level1, level2); if (!string.IsNullOrEmpty(isSiteExistResult.Item3)) { return($"【Error】 : {isSiteExistResult.Item3}"); } var tempPhysicalPath = string.Empty; if (_useTempPhysicalPath) { tempPhysicalPath = Path.Combine(_projectPublishFolder, "_deploy_"); } var iisVersion = IISHelper.GetIISVersion(); if (iisVersion > 0) { Log($"IIS_Version : {iisVersion} "); } if (!isSiteExistResult.Item1)//一级都不存在 { if (iisVersion <= 6) { return($"website : {_webSiteName} not found,start to create,but iis verison is too low!"); } //准备好目录 if (string.IsNullOrEmpty(_port)) { if (IISHelper.IsDefaultWebSite(level1)) { _port = "80"; } else { return($"website : {_webSiteName} not found,start to create,but port is required!"); } } Log($"website : {_webSiteName} not found,start to create!"); //创建发布目录 var firstDeployFolder = string.IsNullOrEmpty(_physicalPath)? Path.Combine(projectPath, "deploy"):_physicalPath; EnsureProjectFolder(firstDeployFolder); if (Directory.Exists(firstDeployFolder)) { Log($"deploy folder create success : {firstDeployFolder} "); } else { return($"DeployFolder : {firstDeployFolder} create error!"); } var rt = IISHelper.InstallSite(level1, firstDeployFolder, _port, (string.IsNullOrEmpty(_poolName) ? _projectName : _poolName), isNetcore, _poolAlwaysRunning); if (string.IsNullOrEmpty(rt)) { Log($"create website : {level1} success "); } else { return($"create website : {level1} error: {rt} "); } if (!string.IsNullOrEmpty(level2)) { //创建一级 但是一级需要一个空的目录 //创建二级虚拟目录 二级的目录才是正常程序所在目录 var level2Folder = Path.Combine(firstDeployFolder, level2); EnsureProjectFolder(level2Folder); var rt2 = IISHelper.InstallVirtualSite(level1, level2, level2Folder, (string.IsNullOrEmpty(_poolName) ? _projectName : _poolName), isNetcore, _poolAlwaysRunning); if (string.IsNullOrEmpty(rt2)) { Log($"create virtualSite :{level2} Of Website : {level1} success "); } else { return($"create virtualSite :{level2} website : {level1} error: {rt2} "); } //复制文件到发布目录 CopyHelper.ProcessXcopy(deployFolder, level2Folder, Log); if (_useTempPhysicalPath) { EnsureProjectFolder(tempPhysicalPath); CopyHelper.ProcessXcopy(deployFolder, tempPhysicalPath, Log); } Log($"copy files success from [{deployFolder}] to [{level2Folder}]"); return(String.Empty); } else { //只需要一级 就是程序所在目录 //复制文件到发布目录 CopyHelper.ProcessXcopy(deployFolder, firstDeployFolder, Log); if (_useTempPhysicalPath) { EnsureProjectFolder(tempPhysicalPath); CopyHelper.ProcessXcopy(deployFolder, tempPhysicalPath, Log); } Log($"copy files success from [{deployFolder}] to [{firstDeployFolder}]"); return(String.Empty); } } else if (isSiteExistResult.Item1 && !isSiteExistResult.Item2 && !string.IsNullOrEmpty(level2)) { if (IISHelper.GetIISVersion() <= 6) { return($"website : {_webSiteName} not found,start to create,but iis verison is too low!"); } //有一级 但是需要创建二级 Log($"website : {_webSiteName} not found,start to create!"); //创建发布目录 var firstDeployFolder = string.IsNullOrEmpty(_physicalPath)? Path.Combine(projectPath, "deploy"):_physicalPath; EnsureProjectFolder(firstDeployFolder); Log($"deploy folder create success : {firstDeployFolder} "); var level2Folder = Path.Combine(firstDeployFolder, level2); EnsureProjectFolder(level2Folder); var rt2 = IISHelper.InstallVirtualSite(level1, level2, level2Folder, (string.IsNullOrEmpty(_poolName) ? _projectName : _poolName), isNetcore, _poolAlwaysRunning); if (string.IsNullOrEmpty(rt2)) { Log($"create virtualSite :{level2} Of Website : {level1} success "); } else { return($"create virtualSite :{level2} website : {level1} error: {rt2} "); } //复制文件到发布目录 CopyHelper.ProcessXcopy(deployFolder, level2Folder, Log); if (_useTempPhysicalPath) { EnsureProjectFolder(tempPhysicalPath); CopyHelper.ProcessXcopy(deployFolder, tempPhysicalPath, Log); } Log($"copy files success from [{deployFolder}] to [{level2Folder}]"); return(String.Empty); } //下面的逻辑就是网站已经存在了 所以就有冲突的风险 var projectLocation = IISHelper.GetWebSiteLocationInIIS(level1, level2, Log); if (projectLocation == null) { return($"read info from iis error"); } if (string.IsNullOrEmpty(projectLocation.Item1)) { return($"website : {_webSiteName} not found in iis"); } if (!Directory.Exists(projectLocation.Item1)) { //如果目录不存在 那么就重新建立 EnsureProjectFolder(projectLocation.Item1); } Log("Start to deploy IIS:"); Log("SiteName ===>" + _webSiteName + $"[{projectLocation.Item2}]"); Log("SiteFolder ===> " + projectLocation.Item1); Log("SiteApplicationPoolName ===> " + projectLocation.Item3); Arguments args = new Arguments { DeployType = "IIS", BackupFolder = Setting.BackUpIIsPathFolder, AppName = _projectName, ApplicationPoolName = projectLocation.Item3, AppFolder = projectLocation.Item1, DeployFolder = deployFolder, SiteName = projectLocation.Item2, BackUpIgnoreList = this._backUpIgnoreList, NoBackup = !Setting.NeedBackUp }; //是否采用 每次都更换新的物理路径的方式 if (_useTempPhysicalPath) { args.NoStop = true; args.NoStart = true; args.UseTempPhysicalPath = true; args.Site1 = level1; args.Site2 = level2; args.TempPhysicalPath = Path.Combine(_projectPublishFolder, "_deploy_"); EnsureProjectFolder(args.TempPhysicalPath); } else if (_useOfflineHtm) { args.NoStop = true; args.NoStart = true; args.UseOfflineHtm = true; } else if (_isNoStopWebSite) { args.NoStop = true; args.NoStart = true; } var ops = new OperationsIIS(args, Log); try { ops.Execute(); try { //如果是增量的话 要把复制过来 if (!args.UseTempPhysicalPath && _isIncrement) { Log("Increment deploy start to backup..."); //projectLocation.Item1 转到 increment 的目录 var incrementFolder = Path.Combine(_projectPublishFolder, "increment"); EnsureProjectFolder(incrementFolder); DirectoryInfo directoryInfo = new DirectoryInfo(projectLocation.Item1); string fullName = directoryInfo.FullName; if (directoryInfo.Parent != null) { fullName = directoryInfo.Parent.FullName; } new Task(() => { CopyHelper.DirectoryCopy(projectLocation.Item1, incrementFolder, true, fullName, directoryInfo.Name, this._backUpIgnoreList); }).Start(); Log("Increment deploy backup success..."); } } catch (Exception ex3) { Log("Increment deploy folder backup fail:" + ex3.Message); } Log("Deploy IIS Execute Success"); } catch (Exception ex) { try { //ops.Rollback(); return($"publish to iis err:{ex.Message}"); } catch (Exception ex2) { return($"publish to iis err:{ex.Message},rollback fail:{ex2.Message}"); } } return(string.Empty); } catch (Exception ex) { return(ex.Message); } }