/// <summary> /// 启动部署 /// </summary> /// <returns>部署结果</returns> internal async Task <DeployResult> DeployAsync() { #if NoAutoCSer throw new Exception(); #else if (TcpClient.IsClient) { AutoCSer.Net.TcpServer.ReturnValue <int> indexReuslt = await TcpClient.TcpInternalClient.createAwaiter(); if (indexReuslt.Type != AutoCSer.Net.TcpServer.ReturnType.Success) { return(new DeployResult { Index = -1, State = DeployState.CreateError, ReturnType = indexReuslt.Type }); } try { Client client = TcpClient.Client; Dictionary <HashString, FileSource> fileSources = DictionaryCreator.CreateHashString <FileSource>(); ClientTaskInfo[] tasks = new ClientTaskInfo[Tasks.Length]; AutoCSer.Net.TcpServer.ReturnValue <AutoCSer.Deploy.Directory> getFileDifferentReuslt; for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { switch (Tasks[taskIndex].Type) { case TaskType.AssemblyFile: appendSource(fileSources, (ClientTask.WebFile)Tasks[taskIndex], ref tasks[taskIndex]); break; case TaskType.File: ClientTask.File file = (ClientTask.File)Tasks[taskIndex]; DirectoryInfo clientDirectory = new DirectoryInfo(file.ClientPath); Directory directory = Directory.Create(clientDirectory, client.Config.FileLastWriteTime, file.SearchPatterns); getFileDifferentReuslt = await TcpClient.TcpInternalClient.getFileDifferentAwaiter(directory, file.ServerPath); if (getFileDifferentReuslt.Type != AutoCSer.Net.TcpServer.ReturnType.Success) { return(new DeployResult { Index = -1, State = DeployState.GetFileDifferentError, ReturnType = getFileDifferentReuslt.Type }); } tasks[taskIndex].Directory = getFileDifferentReuslt.Value; tasks[taskIndex].Directory.Load(clientDirectory); break; case TaskType.WebFile: ClientTask.WebFile webFile = (ClientTask.WebFile)Tasks[taskIndex]; DirectoryInfo webClientDirectory = new DirectoryInfo(webFile.ClientPath); Directory webDirectory = Directory.CreateWeb(webClientDirectory, client.Config.FileLastWriteTime); getFileDifferentReuslt = await TcpClient.TcpInternalClient.getFileDifferentAwaiter(webDirectory, webFile.ServerPath); if (getFileDifferentReuslt.Type != AutoCSer.Net.TcpServer.ReturnType.Success) { return(new DeployResult { Index = -1, State = DeployState.GetFileDifferentError, ReturnType = getFileDifferentReuslt.Type }); } tasks[taskIndex].Directory = getFileDifferentReuslt.Value; tasks[taskIndex].Directory.Load(webClientDirectory); break; } } if (fileSources.Count != 0) { AutoCSer.Net.TcpServer.ReturnValue <bool> result = await TcpClient.TcpInternalClient.setFileSourceAwaiter(fileSources.getArray(value => value.Value.Data)); if (!result.Value) { return new DeployResult { Index = -1, State = DeployState.SetFileSourceError, ReturnType = result.Type } } ; } for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { AutoCSer.Net.TcpServer.ReturnValue <int> result; switch (Tasks[taskIndex].Type) { case TaskType.Run: result = await TcpClient.TcpInternalClient.addRunAwaiter((ClientTask.Run) Tasks[taskIndex]); if (result.Type == AutoCSer.Net.TcpServer.ReturnType.Success) { tasks[taskIndex].TaskIndex = result.Value; } break; case TaskType.WebFile: case TaskType.File: ClientTask.WebFile webFile = (ClientTask.WebFile)Tasks[taskIndex]; webFile.Directory = tasks[taskIndex].Directory; result = await TcpClient.TcpInternalClient.addFilesAwaiter(webFile); break; case TaskType.AssemblyFile: ClientTask.AssemblyFile assemblyFile = (ClientTask.AssemblyFile)Tasks[taskIndex]; assemblyFile.FileIndexs = tasks[taskIndex].FileIndexs.ToArray(); result = await TcpClient.TcpInternalClient.addAssemblyFilesAwaiter(assemblyFile); break; case TaskType.WaitRunSwitch: result = await TcpClient.TcpInternalClient.addWaitRunSwitchAwaiter(tasks[((ClientTask.WaitRunSwitch)Tasks[taskIndex]).TaskIndex].TaskIndex); break; case TaskType.UpdateSwitchFile: result = await TcpClient.TcpInternalClient.addUpdateSwitchFileAwaiter((ClientTask.UpdateSwitchFile) Tasks[taskIndex]); break; case TaskType.Custom: result = await TcpClient.TcpInternalClient.addCustomAwaiter((ClientTask.Custom) Tasks[taskIndex]); break; default: return(new DeployResult { Index = -1, State = DeployState.UnknownTaskType, ReturnType = AutoCSer.Net.TcpServer.ReturnType.Unknown }); } if (result.Type != AutoCSer.Net.TcpServer.ReturnType.Success || result.Value == -1) { return(new DeployResult { Index = -1, State = getErrorState(Tasks[taskIndex].Type), ReturnType = result.Type }); } } AutoCSer.Net.TcpServer.ReturnValue <AutoCSer.Deploy.DeployState> startResult = await TcpClient.TcpInternalClient.startAwaiter(DateTime.MinValue); if (startResult.Type == AutoCSer.Net.TcpServer.ReturnType.Success) { return(new DeployResult { Index = indexReuslt.Value, State = startResult.Value, ReturnType = AutoCSer.Net.TcpServer.ReturnType.Success }); } return(new DeployResult { Index = -1, State = DeployState.StartError, ReturnType = startResult.Type }); } finally { await TcpClient.TcpInternalClient.cancelAwaiter();// && TcpClient.TcpInternalClient.clear(identity).Type != AutoCSer.Net.TcpServer.ReturnType.Success } } #endif return(new DeployResult { Index = -1, State = DeployState.NoClient }); }
/// <summary> /// 添加文件数据源 /// </summary> /// <param name="fileSources"></param> /// <param name="path"></param> /// <param name="fileName"></param> /// <param name="serverTask"></param> private void appendSource(Dictionary <HashString, FileSource> fileSources, string path, string fileName, ref ClientTaskInfo serverTask) { FileSource fileSource; if (!fileSources.TryGetValue(fileName, out fileSource)) { foreach (string runFilePath in TcpClient.Client.Config.RunFilePaths.notNull()) { string runFileName = runFilePath + fileName; if (File.Exists(runFileName)) { fileSources.Add(fileName, fileSource = new FileSource { Data = File.ReadAllBytes(runFileName), Index = fileSources.Count }); break; } } if (fileSource.Data == null) { fileSources.Add(fileName, fileSource = new FileSource { Data = File.ReadAllBytes(path + fileName), Index = fileSources.Count }); } } serverTask.FileIndexs.Add(new KeyValue <string, int>(fileName, fileSource.Index)); }
///// <summary> ///// 添加文件数据源 ///// </summary> ///// <param name="fileSources"></param> ///// <param name="run"></param> ///// <param name="serverTask"></param> //private void appendSource(Dictionary<HashString, FileSource> fileSources, ClientTask.Run run, ref ClientTaskInfo serverTask) //{ // appendSource(fileSources, run, ref serverTask, run.FileName); //} /// <summary> /// 添加文件数据源 /// </summary> /// <param name="fileSources"></param> /// <param name="webFile"></param> /// <param name="serverTask"></param> /// <param name="runFileName"></param> private void appendSource(Dictionary <HashString, FileSource> fileSources, ClientTask.WebFile webFile, ref ClientTaskInfo serverTask, string runFileName = null) { Client client = TcpClient.Client; if (webFile.ClientPath != null) { DirectoryInfo directory = new DirectoryInfo(webFile.ClientPath); string directoryName = directory.fullName(); if (runFileName != null) { appendSource(fileSources, directoryName, runFileName, ref serverTask); } foreach (FileInfo file in directory.GetFiles("*.exe")) { if (file.LastWriteTimeUtc > client.Config.FileLastWriteTime && file.Name != runFileName && !client.IgnoreFileNames.Contains(file.Name) && !file.Name.EndsWith(".vshost.exe", StringComparison.Ordinal)) { appendSource(fileSources, directoryName, file.Name, ref serverTask); } } foreach (FileInfo file in directory.GetFiles("*.dll")) { if (file.LastWriteTimeUtc > client.Config.FileLastWriteTime && !client.IgnoreFileNames.Contains(file.Name)) { appendSource(fileSources, directoryName, file.Name, ref serverTask); } } foreach (FileInfo file in directory.GetFiles("*.pdb")) { if (file.LastWriteTimeUtc > client.Config.FileLastWriteTime && !client.IgnoreFileNames.Contains(file.Name)) { appendSource(fileSources, directoryName, file.Name, ref serverTask); } } } }
/// <summary> /// 启动部署 /// </summary> /// <param name="startTime">部署任务启动时间</param> /// <returns>部署结果</returns> internal DeployResult Deploy(DateTime startTime = default(DateTime)) { #if NoAutoCSer throw new Exception(); #else if (TcpClient.IsClient) { //AutoCSer.Net.IndexIdentity identity = TcpClient.TcpInternalClient.create(TcpClient.ClientId); AutoCSer.Net.IndexIdentity identity = TcpClient.TcpInternalClient.create(); bool isClear = false; try { Client client = TcpClient.Client; Dictionary <HashString, FileSource> fileSources = DictionaryCreator.CreateHashString <FileSource>(); ClientTaskInfo[] tasks = new ClientTaskInfo[Tasks.Length]; for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { switch (Tasks[taskIndex].Type) { case TaskType.Run: appendSource(fileSources, (ClientTask.Run)Tasks[taskIndex], ref tasks[taskIndex]); break; case TaskType.AssemblyFile: appendSource(fileSources, (ClientTask.WebFile)Tasks[taskIndex], ref tasks[taskIndex]); break; case TaskType.File: ClientTask.File file = (ClientTask.File)Tasks[taskIndex]; DirectoryInfo clientDirectory = new DirectoryInfo(file.ClientPath); Directory directory = Directory.Create(clientDirectory, client.Config.FileLastWriteTime, file.SearchPatterns); tasks[taskIndex].Directory = TcpClient.TcpInternalClient.getFileDifferent(directory, file.ServerPath); tasks[taskIndex].Directory.Load(clientDirectory); break; case TaskType.WebFile: ClientTask.WebFile webFile = (ClientTask.WebFile)Tasks[taskIndex]; DirectoryInfo webClientDirectory = new DirectoryInfo(webFile.ClientPath); Directory webDirectory = Directory.CreateWeb(webClientDirectory, client.Config.FileLastWriteTime); tasks[taskIndex].Directory = TcpClient.TcpInternalClient.getFileDifferent(webDirectory, webFile.ServerPath); tasks[taskIndex].Directory.Load(webClientDirectory); break; } } if (fileSources.Count != 0 && !TcpClient.TcpInternalClient.setFileSource(identity, fileSources.getArray(value => value.Value.Data))) { return(new DeployResult { Index = -1, State = DeployState.SetFileSourceError }); } for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { switch (Tasks[taskIndex].Type) { case TaskType.Run: if ((tasks[taskIndex].TaskIndex = TcpClient.TcpInternalClient.addRun(identity, tasks[taskIndex].FileIndexs.ToArray(), (ClientTask.Run)Tasks[taskIndex])) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddRunError }); } break; case TaskType.WebFile: case TaskType.File: if (TcpClient.TcpInternalClient.addFiles(identity, tasks[taskIndex].Directory, (ClientTask.WebFile)Tasks[taskIndex], Tasks[taskIndex].Type) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddFileError }); } break; case TaskType.AssemblyFile: if (TcpClient.TcpInternalClient.addAssemblyFiles(identity, tasks[taskIndex].FileIndexs.ToArray(), (ClientTask.AssemblyFile)Tasks[taskIndex]) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddAssemblyFileError }); } break; case TaskType.WaitRunSwitch: if (TcpClient.TcpInternalClient.addWaitRunSwitch(identity, tasks[((ClientTask.WaitRunSwitch)Tasks[taskIndex]).TaskIndex].TaskIndex) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddWaitRunSwitchError }); } break; case TaskType.Custom: if (TcpClient.TcpInternalClient.addCustom(identity, (ClientTask.Custom)Tasks[taskIndex]) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddCustomError }); } break; } } AutoCSer.Net.TcpServer.ReturnValue <AutoCSer.Deploy.DeployState> result = TcpClient.TcpInternalClient.start(identity, startTime); if (result.Type == AutoCSer.Net.TcpServer.ReturnType.Success) { isClear = checkIsClear(result.Value); return(new DeployResult { Index = identity.Index, State = result.Value }); } return(new DeployResult { Index = -1, State = DeployState.StartError }); } finally { if (isClear) { TcpClient.TcpInternalClient.clear(identity); // && TcpClient.TcpInternalClient.clear(identity).Type != AutoCSer.Net.TcpServer.ReturnType.Success } } } #endif return(new DeployResult { Index = -1, State = DeployState.NoClient }); }
/// <summary> /// 添加文件数据源 /// </summary> /// <param name="fileSources"></param> /// <param name="run"></param> /// <param name="serverTask"></param> private void appendSource(Dictionary <HashString, FileSource> fileSources, ClientTask.Run run, ref ClientTaskInfo serverTask) { appendSource(fileSources, run, ref serverTask, run.FileName); }
/// <summary> /// 启动部署 /// </summary> /// <returns>部署结果</returns> internal DeployResult Deploy() { #if NoAutoCSer throw new Exception(); #else if (TcpClient.IsClient) { AutoCSer.Net.IndexIdentity identity = TcpClient.TcpInternalClient.create(TcpClient.ClientId); byte isStart = 0; try { Client client = TcpClient.Client; Dictionary <HashString, FileSource> fileSources = DictionaryCreator.CreateHashString <FileSource>(); ClientTaskInfo[] tasks = new ClientTaskInfo[Tasks.Length]; for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { switch (Tasks[taskIndex].Type) { case TaskType.Run: case TaskType.AssemblyFile: appendSource(fileSources, ref Tasks[taskIndex], ref tasks[taskIndex]); break; case TaskType.File: //appendFileSource(fileSources, ref Tasks[taskIndex], ref tasks[taskIndex]); DirectoryInfo clientDirectory = new DirectoryInfo(Tasks[taskIndex].ClientPath); Directory directory = Directory.Create(clientDirectory, client.Config.FileLastWriteTime, Tasks[taskIndex].FileSearchPatterns); tasks[taskIndex].Directory = TcpClient.TcpInternalClient.getFileDifferent(directory, Tasks[taskIndex].ServerPath); tasks[taskIndex].Directory.Load(clientDirectory); break; case TaskType.WebFile: DirectoryInfo webClientDirectory = new DirectoryInfo(Tasks[taskIndex].ClientPath); Directory webDirectory = Directory.CreateWeb(webClientDirectory, client.Config.FileLastWriteTime); tasks[taskIndex].Directory = TcpClient.TcpInternalClient.getFileDifferent(webDirectory, Tasks[taskIndex].ServerPath); tasks[taskIndex].Directory.Load(webClientDirectory); break; } } if (fileSources.Count != 0 && !TcpClient.TcpInternalClient.setFileSource(identity, fileSources.getArray(value => value.Value.Data))) { return(new DeployResult { Index = -1, State = DeployState.SetFileSourceError }); } for (int taskIndex = 0; taskIndex != Tasks.Length; ++taskIndex) { switch (Tasks[taskIndex].Type) { case TaskType.Run: if ((tasks[taskIndex].TaskIndex = TcpClient.TcpInternalClient.addRun(identity, tasks[taskIndex].FileIndexs.ToArray(), Tasks[taskIndex].ServerPath, Tasks[taskIndex].RunSleep)) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddRunError }); } break; case TaskType.WebFile: case TaskType.File: if (TcpClient.TcpInternalClient.addFiles(identity, tasks[taskIndex].Directory, Tasks[taskIndex].ServerPath, Tasks[taskIndex].Type) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddFileError }); } break; case TaskType.AssemblyFile: if (TcpClient.TcpInternalClient.addAssemblyFiles(identity, tasks[taskIndex].FileIndexs.ToArray(), Tasks[taskIndex].ServerPath) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddAssemblyFileError }); } break; case TaskType.WaitRunSwitch: if (TcpClient.TcpInternalClient.addWaitRunSwitch(identity, tasks[Tasks[taskIndex].TaskIndex].TaskIndex) == -1) { return(new DeployResult { Index = -1, State = DeployState.AddWaitRunSwitchError }); } break; } } if (TcpClient.TcpInternalClient.start(identity, DateTime.MinValue)) { isStart = 1; return(new DeployResult { Index = identity.Index, State = DeployState.Success }); } return(new DeployResult { Index = -1, State = DeployState.StartError }); } finally { if (isStart == 0) { TcpClient.TcpInternalClient.clear(identity); // && TcpClient.TcpInternalClient.clear(identity).Type != AutoCSer.Net.TcpServer.ReturnType.Success } } } #endif return(new DeployResult { Index = -1, State = DeployState.NoClient }); }