public override void Start() { logger("Start to linux Service Start :" + this.args.AppName); LinuxServiceHelper.ServiceRun(this.args.AppName, this.args.TempPhysicalPath, this.args.UseOfflineHtm, this.args.ApplicationPoolName, this.logger); var runSuccess = false; logger("Wait 5Senconds to Check service state :" + retryTimes); Thread.Sleep(5000); CopyHelper.RunCommand($"sudo systemctl status {this.args.AppName}", null, (msg) => { if (!string.IsNullOrEmpty(msg)) { this.logger.Invoke(msg); var msg1 = msg.ToLower(); if (msg1.Contains("activating (start)")) { runSuccess = true; } else if (msg1.Contains("active:") && (msg1.Contains("running"))) { runSuccess = true; } } }); if (!runSuccess) { throw new Exception("Start service Fail "); } }
public override void Stop() { logger("Start to Linux Service Stop :" + this.args.AppName); logger($"【Command】sudo systemctl stop {this.args.AppName}"); var result = CopyHelper.RunCommand($"sudo systemctl stop {this.args.AppName}", null, null); if (!result) { logger?.Invoke("【Command】" + $"sudo systemctl stop {this.args.AppName}" + "--->Fail"); } }
public override string DeployExcutor(FormHandler.FormItem fileItem) { var projectPath = Path.Combine(Setting.PublishLinuxPathFolder, _serviceName); _projectPublishFolder = Path.Combine(projectPath, !string.IsNullOrEmpty(_dateTimeFolderName) ? _dateTimeFolderName : DateTime.Now.ToString("yyyyMMddHHmmss")); EnsureProjectFolder(projectPath); EnsureProjectFolder(_projectPublishFolder); try { 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 if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Log("linux agent version ==>" + AntDeployAgentWindows.Version.VERSION); } else { 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); } //查找 Service 里面是否存在该服务 var service = LinuxServiceHelper.GetLinuxService(this._serviceName); if (!string.IsNullOrEmpty(service.Item1)) { //运行命令出错了 return(service.Item1); } if (string.IsNullOrEmpty(service.Item2))//没有找到该服务的workingFolder 可能是service描述文件内容不对,可能是服务不存在 { Log($"systemctlService : {_serviceName} 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!"); } //复制文件到发布目录 CopyHelper.ProcessXcopy(deployFolder, firstDeployFolder, Log); Log($"copy files success from [{deployFolder}] to [{firstDeployFolder}]"); //linux service var execFullPath = Path.Combine(firstDeployFolder, _serviceExecName); if (!File.Exists(execFullPath)) { //try { Directory.Delete(firstDeployFolder, true); } catch (Exception) { } return($"systemctl service exec file not found : {execFullPath}"); } //安装服务 Log($"start to install systemctl service"); Log($"service name:{_serviceName}"); Log($"service path:{execFullPath}"); Log($"service description:{_serviceDescription ?? string.Empty}"); try { var err = LinuxServiceHelper.CreateServiceFileAndRun(this._serviceName, firstDeployFolder, _serviceExecName, (_serviceDescription ?? string.Empty), _env, execFullPath, string.IsNullOrEmpty(_serviceStartType) || _serviceStartType.Equals("Auto"), _notify, Log); if (!string.IsNullOrEmpty(err)) { return(err); } //检查是否成功了? var runSuccess = false; Thread.Sleep(5000); CopyHelper.RunCommand($"sudo systemctl status {this._serviceName}", null, (msg) => { if (!string.IsNullOrEmpty(msg)) { this.Log(msg); var msg1 = msg.ToLower(); if (msg1.Contains("activating (start)")) { runSuccess = true; } else if (msg1.Contains("active:") && msg1.Contains("running")) { runSuccess = true; } } }); if (!runSuccess) { return($"install linux service fail"); } Log($"install systemctl service success"); return(string.Empty); } catch (Exception e2) { return($"install linux service fail:" + e2.Message); } } var projectLocationFolder = service.Item2; try { if (!Directory.Exists(projectLocationFolder)) { //如果目录不存在 那么就重新建立 EnsureProjectFolder(projectLocationFolder); } } catch (Exception) { return("ServiceFolder is not correct ===> " + projectLocationFolder); } if (string.IsNullOrEmpty(projectLocationFolder)) { return("ServiceFolder is not correct ===> " + projectLocationFolder); } var fullExcutePath = Path.Combine(projectLocationFolder, _serviceExecName); if (!File.Exists(fullExcutePath)) { return($"systemctl service exec file not found : {fullExcutePath}"); } //保证有service描述文件 等后面实际要用到 LinuxServiceHelper.CreateServiceFile(this._serviceName, projectLocationFolder, _serviceExecName, (_serviceDescription ?? string.Empty), _env, this._notify, Log); Arguments args = new Arguments { DeployType = "Linux", BackupFolder = Setting.BackUpLinuxPathFolder, AppName = _serviceName, AppFolder = projectLocationFolder, TempPhysicalPath = Path.Combine(projectLocationFolder, $"{_serviceName}.service"),//服务文件描述 DeployFolder = deployFolder, ApplicationPoolName = fullExcutePath, BackUpIgnoreList = this._backUpIgnoreList, UseOfflineHtm = string.IsNullOrEmpty(_serviceStartType) || _serviceStartType.Equals("Auto"), NoBackup = !Setting.NeedBackUp, Site1 = _env }; Log("Start to deploy linux Service:"); Log("ServiceName ===>" + _serviceName); Log("ServiceFolder ===> " + projectLocationFolder); if (_isNoStopWebSite) { args.NoStop = true; args.NoStart = true; } var ops = new OperationsLinux(args, Log); try { ops.Execute(); try { //如果是增量的话 要把复制过来 if (_isIncrement) { Log("Increment deploy start to backup..."); //projectLocation.Item1 转到 increment 的目录 var incrementFolder = Path.Combine(_projectPublishFolder, "increment"); EnsureProjectFolder(incrementFolder); if (this._backUpIgnoreList != null && this._backUpIgnoreList.Any()) { var excludFrom = Path.Combine(_projectPublishFolder, "exclude.log"); File.WriteAllLines(excludFrom, this._backUpIgnoreList); //存到文件里面去 要指定排除 CopyHelper.ProcessXcopy(projectLocationFolder, incrementFolder, excludFrom, Log); } else { CopyHelper.ProcessXcopy(projectLocationFolder, incrementFolder, Log); } Log("Increment deploy backup success..."); } } catch (Exception ex3) { Log("Increment deploy folder backup fail:" + ex3.Message); } Log("Deploy linux service Execute Success"); } catch (Exception ex) { try { //ops.Rollback(); return($"publish to linux service err:{ex.Message}"); } catch (Exception ex2) { return($"publish to linux service err:{ex.Message},rollback fail:{ex2.Message}"); } } return(string.Empty); } catch (Exception ex) { return(ex.Message); } }