From 4c066cbdc861618b4dad1137b1f34e3a1791bfb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=C5=A0taleker?= Date: Wed, 26 Feb 2025 10:24:36 +0100 Subject: [PATCH] update na novi server --- InfosysPublisher/WinMain.xaml.cs | 268 ++++++++++++++++++------------- 1 file changed, 157 insertions(+), 111 deletions(-) diff --git a/InfosysPublisher/WinMain.xaml.cs b/InfosysPublisher/WinMain.xaml.cs index 7e21578..f0923b8 100644 --- a/InfosysPublisher/WinMain.xaml.cs +++ b/InfosysPublisher/WinMain.xaml.cs @@ -39,6 +39,22 @@ namespace InfosysPublisher public string Path { get; set; } } + public class SftpServerData( + string sftpServerAddress, + int sftpPort, + string sftpUsername, + string sftpPassword, + string path, + string sftpArchivePath) + { + internal string SftpServerAddress { get; } = sftpServerAddress; + internal int SftpPort { get; } = sftpPort; + internal string SftpUsername { get; } = sftpUsername; + internal string SftpPassword { get; } = sftpPassword; + internal string Path { get; } = path; + internal string SftpArchivePath { get; } = sftpArchivePath; + } + #endregion private string _projectPath; @@ -49,12 +65,18 @@ namespace InfosysPublisher private string _selectedProjectVersion = ""; private string _selectedProjectGuid = ""; - private static readonly string SftpServerAddress = "192.168.111.75"; - private static readonly int SftpPort = 300; - private static readonly string SftpUsername = "InfosysUpdate"; - private static readonly string SftpPassword = "v&H6c$wTbTkgSgdWvL*8k$st3#z5X"; + //private static readonly string SftpServerAddress = "192.168.111.75"; + //private static readonly int SftpPort = 300; + //private static readonly string SftpUsername = "InfosysUpdate"; + //private static readonly string SftpPassword = "v&H6c$wTbTkgSgdWvL*8k$st3#z5X"; - private const string SftpArchivePath = "Archive"; + //private const string SftpArchivePath = "Archive"; + + private static readonly List _sftpServers = + [ + new ("sftp2.infosys.si", 22, "is-admin", "4&Xt6x7hX3f#yZ", "", "archive"), + new ("192.168.111.75", 300, "InfosysUpdate", "v&H6c$wTbTkgSgdWvL*8k$st3#z5X", "", "Archive"), + ]; public WinMain() { InitializeComponent(); @@ -261,15 +283,15 @@ namespace InfosysPublisher return; } - TbOutput.Text = $"Publish start {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + TbOutput.Text = $"{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Publish start"; - TbOutput.Text += $"\nClean {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Clean"; GridProgress.Visibility = Visibility.Visible; LblLoading.Content = $"Čiščenje mape {releaseFolder}"; await Task.Run(() => { var diReleaseFolder = new DirectoryInfo(releaseFolder); - + foreach (var file in diReleaseFolder.GetFiles()) { file.Delete(); @@ -281,7 +303,7 @@ namespace InfosysPublisher }); LblLoading.Content = $"Rebuild {_selectedProject.Path}"; - TbOutput.Text += $"\nRebuild {_selectedProject.Path} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Rebuild {_selectedProject.Path}"; var output = ""; await Task.Run(() => { @@ -309,7 +331,7 @@ namespace InfosysPublisher var zipDirectory = new DirectoryInfo(releaseFolder).Parent?.FullName ?? ""; var zipPath = Path.Combine(zipDirectory, "Package.zip"); LblLoading.Content = $"Zip {zipPath}"; - TbOutput.Text += $"\nZip {zipPath} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Zip {zipPath}"; if (File.Exists(zipPath)) File.Delete(zipPath); @@ -318,18 +340,20 @@ namespace InfosysPublisher ZipFile.CreateFromDirectory(releaseFolder, zipPath, CompressionLevel.Optimal, false); }); - UploadSftp(zipDirectory, zipPath); + LblLoading.Content = $"Upload to SFTP"; + await UploadSftp(zipDirectory, zipPath); - TbOutput.Text += $"\nVersion {_selectedProjectVersion} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + LblLoading.Content = $"Version"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Version {_selectedProjectVersion}"; WriteVersion(); GridProgress.Visibility = Visibility.Hidden; LblLoading.Content = ""; - TbOutput.Text += $"\nEnd {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> End"; } - private async void UploadSftp(string zipDirectory, string zipPath) + private async Task UploadSftp(string zipDirectory, string zipPath) { if (ChbCreateOnlyZip.IsChecked != null && (bool)ChbCreateOnlyZip.IsChecked) { @@ -339,124 +363,146 @@ namespace InfosysPublisher //InfosysUpdate //v&H6c$wTbTkgSgdWvL*8k$st3#z5X - LblLoading.Content = $"Upload to SFTP"; - TbOutput.Text += $"\nUpload to SFTP {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; var error = ""; - await Task.Run(() => + foreach(var sftpServer in _sftpServers) { - try + LblLoading.Content = $"Upload to SFTP: {sftpServer.SftpServerAddress}"; + TbOutput.Text += $"\n{DateTime.Now:dd.MM.yyyy HH:mm:ss}> Upload to SFTP {sftpServer.SftpServerAddress}"; + await Task.Run(() => { - using var sftpClient = new SftpClient(SftpServerAddress, SftpPort, SftpUsername, SftpPassword); - sftpClient.Connect(); - - if (!sftpClient.Exists(_selectedProjectPublishLocation)) - sftpClient.CreateDirectory(_selectedProjectPublishLocation); - - if (!sftpClient.Exists(SftpArchivePath)) - sftpClient.CreateDirectory(SftpArchivePath); - - var files = sftpClient.ListDirectory(_selectedProjectPublishLocation).ToList(); - var xmlVersion = ""; - //arhiv - foreach (var file in files) + try { - if (!file.Name.ToLower().EndsWith(".xml")) - continue; + using var sftpClient = new SftpClient(sftpServer.SftpServerAddress, sftpServer.SftpPort, sftpServer.SftpUsername, sftpServer.SftpPassword); + sftpClient.Connect(); - var tmpXmlFile = Path.GetTempFileName(); + if (!sftpClient.Exists(_selectedProjectPublishLocation)) + sftpClient.CreateDirectory(_selectedProjectPublishLocation); - using (var tmpFile = File.OpenWrite(tmpXmlFile)) - { - sftpClient.DownloadFile(file.FullName, tmpFile); - } - - try - { - var xmlContent = File.ReadAllText(tmpXmlFile); - if (!string.IsNullOrEmpty(xmlContent)) - { - //Oddaljena verzije - var xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(xmlContent); - var xmlNode = xmlDocument.SelectSingleNode("/Update"); - xmlVersion = xmlNode["Version"].InnerText; - } - } - catch (Exception exception) - { - error += "\n\n"; - error += exception.ToString(); - } - - File.Delete(tmpXmlFile); - } - - if (xmlVersion != "") - { - var projectArchive = SftpArchivePath + "/" + _selectedProjectPublishLocation; - if (!sftpClient.Exists(projectArchive)) - sftpClient.CreateDirectory(projectArchive); - - var archiveFolderWithoutIndex = projectArchive + "/" + xmlVersion.Replace(".", "_"); - var archiveFolder = archiveFolderWithoutIndex; - var index = 1; - while (sftpClient.Exists(archiveFolder)) - { - archiveFolder = archiveFolderWithoutIndex + "_" + index; - index++; - } - - sftpClient.CreateDirectory(archiveFolder); + if (!sftpClient.Exists(sftpServer.SftpArchivePath)) + sftpClient.CreateDirectory(sftpServer.SftpArchivePath); + var files = sftpClient.ListDirectory(_selectedProjectPublishLocation).ToList(); + var xmlVersion = ""; + //arhiv foreach (var file in files) { - if (!file.IsRegularFile) + if (!file.Name.ToLower().EndsWith(".xml")) continue; - sftpClient.Get(file.FullName).MoveTo(archiveFolder + "/" + file.Name); + var tmpXmlFile = Path.GetTempFileName(); + using (var tmpFile = File.OpenWrite(tmpXmlFile)) + { + sftpClient.DownloadFile(file.FullName, tmpFile); + } + + try + { + var xmlContent = File.ReadAllText(tmpXmlFile); + if (!string.IsNullOrEmpty(xmlContent)) + { + //Oddaljena verzije + var xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(xmlContent); + var xmlNode = xmlDocument.SelectSingleNode("/Update"); + xmlVersion = xmlNode["Version"].InnerText; + } + } + catch (Exception exception) + { + error += "\n\n"; + error += exception.ToString(); + } + + File.Delete(tmpXmlFile); } - } - //Upload - var tmpXmlFileUpload = Path.GetTempFileName(); - string xml = $@" + if (xmlVersion != "") + { + var projectArchive = sftpServer.SftpArchivePath + "/" + _selectedProjectPublishLocation; + if (!sftpClient.Exists(projectArchive)) + sftpClient.CreateDirectory(projectArchive); + + var archiveFolderWithoutIndex = projectArchive + "/" + xmlVersion.Replace(".", "_"); + var archiveFolder = archiveFolderWithoutIndex; + var index = 1; + while (sftpClient.Exists(archiveFolder)) + { + archiveFolder = archiveFolderWithoutIndex + "_" + index; + index++; + } + + sftpClient.CreateDirectory(archiveFolder); + + foreach (var file in files) + { + if (!file.IsRegularFile) + continue; + + sftpClient.Get(file.FullName).MoveTo(archiveFolder + "/" + file.Name); + + } + } + + //Upload + var tmpXmlFileUpload = Path.GetTempFileName(); + string xml = $@" {_selectedProjectVersion} "; - File.WriteAllText(tmpXmlFileUpload, xml); - using (var fileStream = new FileStream(tmpXmlFileUpload, FileMode.Open)) - { - sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Update.xml", - true); + File.WriteAllText(tmpXmlFileUpload, xml); + + var genPublishLocation = _selectedProjectPublishLocation + "/" + "Package.zip"; + var genPublishLocationTmp = _selectedProjectPublishLocation + "/" + "Package_tmp.zip"; + + using (var fileStream = new FileStream(zipPath, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, genPublishLocationTmp, + true); + } + + var sftpDirVersion = _selectedProjectPublishLocation + "/" + _selectedProjectVersion; + + if (!sftpClient.Exists(sftpDirVersion)) + sftpClient.CreateDirectory(sftpDirVersion); + + var versionPublishLocation = sftpDirVersion + "/" + "Package.zip"; + var versionPublishLocationTmp = sftpDirVersion + "/" + "Package_tmp.zip"; + + using (var fileStream = new FileStream(zipPath, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, versionPublishLocationTmp, + true); + } + + if (sftpClient.Exists(genPublishLocation)) + sftpClient.DeleteFile(genPublishLocation); + + if (sftpClient.Exists(versionPublishLocation)) + sftpClient.DeleteFile(versionPublishLocation); + + sftpClient.RenameFile(genPublishLocationTmp, genPublishLocation); + sftpClient.RenameFile(versionPublishLocationTmp, versionPublishLocation); + + //XML na koncu + using (var fileStream = new FileStream(tmpXmlFileUpload, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Update.xml", + true); + } + + File.Delete(tmpXmlFileUpload); + sftpClient.Disconnect(); } - - using (var fileStream = new FileStream(zipPath, FileMode.Open)) + catch (Exception ex) { - sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Package.zip", - true); + error += "\n\n"; + error += ex.ToString(); } + }); + } - var sftpDirVersion = _selectedProjectPublishLocation + "/" + _selectedProjectVersion; - - if (!sftpClient.Exists(sftpDirVersion)) - sftpClient.CreateDirectory(sftpDirVersion); - - using (var fileStream = new FileStream(zipPath, FileMode.Open)) - { - sftpClient.UploadFile(fileStream, sftpDirVersion + "/" + "Package.zip", - true); - } - - File.Delete(tmpXmlFileUpload); - sftpClient.Disconnect(); - } - catch (Exception ex) - { - error += "\n\n"; - error += ex.ToString(); - } - }); + if (error != "") { MessageBox.Show(error);