/* * private static Boolean AprovaAtualizacaoComRDM(UpdateGA update) * { * * var builder = new ConfigurationBuilder().SetBasePath(System.IO.Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); * IConfigurationRoot configuration = builder.Build(); * * * var traceGP = new Models.TraceGP(); * configuration.GetSection("TraceGP").Bind(traceGP); * * String stringConnection = traceGP.TraceGPConexao; * String select = traceGP.TraceGPSelect; * * * OracleConnection conn; * OracleCommand cmd = null; * conn = new OracleConnection(stringConnection); * * conn.Open(); * cmd = new OracleCommand(select, conn); * cmd.Parameters.Clear(); * cmd.Parameters.Add(":RDM", update.RDM); * * OracleDataReader reader = cmd.ExecuteReader(); * reader.Read(); * * if (!reader.HasRows) * { * return false; * } * * int resultado = Convert.ToInt16(reader.GetValue(0).ToString()); * * conn.Close(); * * if (resultado == 87 || resultado == 60 || resultado == 76 || resultado == 33) * { * return true; * } * * * return false; * }*/ public static void ExecuteProcedure(ProcedureSchedule procedureSchedule) { Models.GAContext context = new Models.GAContext(); GALibrary.Models.Parameter parameter = context.Parameter.FirstOrDefault(); var steps = context.ProcedureSteps.Where(x => x.ProcedureID == procedureSchedule.ProcedureID).ToList().OrderBy(x => x.Order); //Atualiza status para atualizando (3) procedureSchedule.Status = 3; context.Entry(procedureSchedule).State = EntityState.Modified; context.SaveChanges(); try { foreach (ProcedureSteps step in steps) { switch (step.Type) { case 3: GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "Command", "Inicio da execucao do comando", 2, parameter); UpdateCommand(null, procedureSchedule, step.ProcessId); GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "Command", "Fim da execucao do comando", 2, parameter); break; case 4: GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "ServiceStart", "Inicio do start de serviço", 2, parameter); UpdateServiceStart(null, procedureSchedule, step.ProcessId); GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "ServiceStart", "Fim do start de serviço", 2, parameter); break; case 5: GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "ServiceStop", "Inicio da parada de serviço", 2, parameter); UpdateServiceStop(null, procedureSchedule, step.ProcessId); GALibrary.GALogs.SaveLogProcedure(procedureSchedule, "ServiceStop", "Fim da parada de serviço", 2, parameter); break; } } procedureSchedule.Status = 1; context.Entry(procedureSchedule).State = EntityState.Modified; context.SaveChanges(); } catch { procedureSchedule.Status = 2; context.Entry(procedureSchedule).State = EntityState.Modified; context.SaveChanges(); } }
public static UpdateGA UpdateApplication(int updateId) { Models.GAContext context = new Models.GAContext(); GALibrary.Models.Parameter parameter = context.Parameter.FirstOrDefault(); var steps = context.UpdateSteps.Where(x => x.UpdateId == updateId).ToList().OrderBy(x => x.Order); UpdateGA update = context.UpdateGA.First(x => x.Id == updateId); //Atualiza status para atualizando (3) update.Status = 3; context.Entry(update).State = EntityState.Modified; context.SaveChanges(); foreach (UpdateSteps step in steps) { switch (step.Type) { case 1: GALibrary.GALogs.SaveLogUpdate(update, "FilesDelete", "Inicio da remocao de arquivos", 2, parameter); UpdateRemoveFiles(update, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "FilesDelete", "Fim da remocao de arquivos", 2, parameter); break; case 2: GALibrary.GALogs.SaveLogUpdate(update, "Files", "Inicio da copia de arquivos", 2, parameter); UpdateCopyFiles(update, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "Files", "Fim da copia de arquivos", 2, parameter); break; case 3: GALibrary.GALogs.SaveLogUpdate(update, "Command", "Inicio da execucao do comando", 2, parameter); UpdateCommand(update, null, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "Command", "Fim da execucao do comando", 2, parameter); break; case 4: GALibrary.GALogs.SaveLogUpdate(update, "ServiceStart", "Inicio do start de servico", 2, parameter); UpdateServiceStart(update, null, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "ServiceStart", "Fim do start de servico", 2, parameter); break; case 5: GALibrary.GALogs.SaveLogUpdate(update, "ServiceStop", "Inicio da parada de servico", 2, parameter); UpdateServiceStop(update, null, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "ServiceStop", "Fim da parada de servico", 2, parameter); break; case 6: GALibrary.GALogs.SaveLogUpdate(update, "SQL", "Inicio da execucao SQL", 2, parameter); UpdateSQL(update, step.ProcessId); GALibrary.GALogs.SaveLogUpdate(update, "SQL", "Fim da execucao SQL", 2, parameter); break; } } GALibrary.GAFiles.CompressFolder(update, parameter); try { string log; using (System.IO.StreamReader reader = new System.IO.StreamReader(GALibrary.GALogs.GetUpdateLog(update, parameter))) { log = reader.ReadToEnd(); } if (log.Contains(" - Error - ")) { update.Status = 2; } else { update.Status = 1; } } catch (Exception erro) { update.Status = 1; GALibrary.GALogs.SaveLogUpdate(update, "Status", "Erro ao salvar status: " + erro.ToString(), 1, parameter); } context.Entry(update).State = EntityState.Modified; context.SaveChanges(); return(update); }
public static void ApagaArquivosAntigos() { Models.GAContext context = new Models.GAContext(); String folderUpdate = context.Parameter.First().PathUpdate; String folderBackup = context.Parameter.First().PathBackup; int retentionTime = context.Parameter.FirstOrDefault().RetentionTime.Value; DateTime dataExpurgo = DateTime.Now.AddDays(-retentionTime); //Busca os SQLs e Arquivos que são tempaltes para não apaga-los List <int> fileTemplate = context.UpdateSteps.Include(x => x.Update).Where(x => x.Type == 1 && x.Update.Template == true).Select(x => x.ProcessId).ToList(); List <int> sqlTemplate = context.UpdateSteps.Include(x => x.Update).Where(x => x.Type == 2 && x.Update.Template == true).Select(x => x.ProcessId).ToList(); List <UpdateGA> updateList = context.UpdateGA.Where(x => x.Date < dataExpurgo && x.FilesRemoved == false && x.Template == false).ToList(); List <File> fileList = context.File.Where(x => x.Date < dataExpurgo && x.FilesRemoved == false && !fileTemplate.Contains(x.Id)).ToList(); List <SQL> sqlList = context.SQL.Where(x => x.Date < dataExpurgo && x.FilesRemoved == false && !sqlTemplate.Contains(x.Id)).ToList(); //Apaga backups foreach (UpdateGA update in updateList) { String pastaBackup = folderBackup + "\\" + update.Date.Year.ToString().PadLeft(4, '0') + "\\" + update.Date.Month.ToString().PadLeft(2, '0') + "\\" + update.Id; if (System.IO.Directory.Exists(pastaBackup)) { System.IO.Directory.Delete(pastaBackup, true); } update.FilesRemoved = true; context.Entry(update).State = EntityState.Modified; context.SaveChanges(); } //apaga arquivos foreach (File file in fileList) { String pastaFiles = folderUpdate + "\\Files\\" + file.Date.Year.ToString().PadLeft(4, '0') + "\\" + file.Date.Month.ToString().PadLeft(2, '0') + "\\" + file.Id; if (System.IO.Directory.Exists(pastaFiles)) { System.IO.Directory.Delete(pastaFiles, true); } file.FilesRemoved = true; context.Entry(file).State = EntityState.Modified; context.SaveChanges(); } //Apaga arquivos SQL foreach (SQL sql in sqlList) { String pastaSQL = folderUpdate + "\\SQL\\" + sql.Date.Year.ToString().PadLeft(4, '0') + "\\" + sql.Date.Month.ToString().PadLeft(2, '0') + "\\" + sql.Id; if (System.IO.Directory.Exists(pastaSQL)) { System.IO.Directory.Delete(pastaSQL, true); } sql.FilesRemoved = true; context.Entry(sql).State = EntityState.Modified; context.SaveChanges(); } }
public static String UpdateCopyFiles(UpdateGA update, int fileId) { Models.GAContext context = new Models.GAContext(); GALibrary.Models.Parameter parameter = context.Parameter.FirstOrDefault(); File file = context.File.First(x => x.Id == fileId); List <FileFolder> filefolders = context.FileFolder.Where(x => x.FileId == fileId).ToList(); String logCopy = ""; String pathUpdate = context.Parameter.First().PathUpdate; String pathZip = ""; Boolean errorFound = false; // this.SaveLogUpdate(update, "Files", "Inicio da cópia de arquivos", 2); if (!pathUpdate.EndsWith("\\")) { pathUpdate += "\\"; } pathUpdate += "Files" + "\\" + file.Date.Year.ToString().PadLeft(4, '0') + "\\" + file.Date.Month.ToString().PadLeft(2, '0') + "\\" + file.Id + "\\"; pathZip = pathUpdate + "extracted"; try { String[] fileZip = System.IO.Directory.GetFiles(pathUpdate); //Apaga o conteúdo extraido caso já exista: if (System.IO.Directory.Exists(pathZip)) { System.IO.Directory.Delete(pathZip, true); } //Somente busca 1 arquivo da pasta, pois o sistema não permite mais que um ZipFile.ExtractToDirectory(fileZip[0], pathZip); GALibrary.GALogs.SaveLogUpdate(update, "Files", "Procedimento - " + file.Name, 2, parameter); GALibrary.GALogs.SaveLogUpdate(update, "Files", "Arquivos descompactados na pasta " + pathZip, 2, parameter); //Copia arquivos try { foreach (FileFolder filefolder in filefolders) { String pathDestination = context.Folder.First(x => x.Id == filefolder.FolderId).Path; if (!pathDestination.EndsWith("\\")) { pathDestination += "\\"; } foreach (string dirPath in System.IO.Directory.GetDirectories(pathZip, "*", System.IO.SearchOption.AllDirectories)) { System.IO.Directory.CreateDirectory(dirPath.Replace(pathZip, pathDestination)); } String[] allFiles = System.IO.Directory.GetFiles(pathZip, "*.*", System.IO.SearchOption.AllDirectories); //Copy all the files & Replaces any files with the same name foreach (string newPath in allFiles) { try { //Faz backup do arquivo logCopy += UpdateCopyFilesBackup(update, fileId, newPath.Replace(pathZip, pathDestination), pathDestination, filefolder.FolderId); if (logCopy != "") { GALibrary.GALogs.SaveLogUpdate(update, "Files", "Erro ao fazer backup do arquivo " + pathZip + " para " + pathDestination + ": " + logCopy, 1, parameter); if (System.IO.Directory.Exists(pathZip)) { System.IO.Directory.Delete(pathZip, true); } return(logCopy); } //Só copia se fez backup String caminhoCopia = newPath.Replace(pathZip, pathDestination); System.IO.File.Copy(newPath, newPath.Replace(pathZip, pathDestination), true); GALibrary.GALogs.SaveLogUpdate(update, "Files", "Copiado arquivo " + newPath + " para " + caminhoCopia, 2, parameter); //Salva histórico dos arquivos try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(caminhoCopia); FileHistory filehistory = new FileHistory(); filehistory.FileName = fileInfo.Name; filehistory.Folder = fileInfo.FullName; filehistory.FileId = fileId; filehistory.Size = fileInfo.Length; filehistory.Date = fileInfo.LastWriteTime; filehistory.UpdateId = update.Id; context.FileHistory.Add(filehistory); context.SaveChanges(); } catch { } } catch (Exception error) { GALibrary.GALogs.SaveLogUpdate(update, "Files", "Erro ao copiar arquivo " + newPath + " para " + newPath.Replace(pathZip, pathDestination) + ": " + error.ToString(), 1, parameter); logCopy += error.ToString(); errorFound = true; } } } } catch (Exception error) { GALibrary.GALogs.SaveLogUpdate(update, "Files", error.ToString(), 1, parameter); errorFound = true; } } catch (Exception error) { GALibrary.GALogs.SaveLogUpdate(update, "Files", error.ToString(), 1, parameter); errorFound = true; } //Remove pasta descompactada if (System.IO.Directory.Exists(pathZip)) { System.IO.Directory.Delete(pathZip, true); } if (errorFound) { GALibrary.GALogs.SaveLogUpdate(update, "Files", logCopy, 1, parameter); } return(null); }