From d27e5cb1fdd3fb3aa94fbccab3bd55a841287501 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:09:34 -0500 Subject: [PATCH] add upgrade sentinel to try to restar ui --- Installer/ZitiDesktopEdge.aip | 6 +- UpgradeSentinel/App.config | 6 + UpgradeSentinel/Program.cs | 126 +++++++++++++++++++++ UpgradeSentinel/Properties/AssemblyInfo.cs | 36 ++++++ UpgradeSentinel/UpgradeSentinel.csproj | 53 +++++++++ ZitiDesktopEdge.sln | 42 +++++++ 6 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 UpgradeSentinel/App.config create mode 100644 UpgradeSentinel/Program.cs create mode 100644 UpgradeSentinel/Properties/AssemblyInfo.cs create mode 100644 UpgradeSentinel/UpgradeSentinel.csproj diff --git a/Installer/ZitiDesktopEdge.aip b/Installer/ZitiDesktopEdge.aip index 3f903116..d6a1a618 100644 --- a/Installer/ZitiDesktopEdge.aip +++ b/Installer/ZitiDesktopEdge.aip @@ -1,5 +1,5 @@ - + @@ -91,6 +91,7 @@ + @@ -121,6 +122,7 @@ + @@ -138,6 +140,7 @@ + @@ -346,6 +349,7 @@ + diff --git a/UpgradeSentinel/App.config b/UpgradeSentinel/App.config new file mode 100644 index 00000000..57547288 --- /dev/null +++ b/UpgradeSentinel/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/UpgradeSentinel/Program.cs b/UpgradeSentinel/Program.cs new file mode 100644 index 00000000..9c210d99 --- /dev/null +++ b/UpgradeSentinel/Program.cs @@ -0,0 +1,126 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Text.RegularExpressions; +using System.Threading; + +class FileWatcher { + static SemaphoreSlim semaphore = new SemaphoreSlim(0, 1); + static string lockFilePath = Path.Combine(Path.GetTempPath(), "ZitiDesktopEdgeUpdateSentinelLock.txt"); + static string pathToWatch = @"C:\Program Files (x86)\NetFoundry, Inc\Ziti Desktop Edge\ZitiDesktopEdge.exe"; + + static void Main() { + Console.CancelKeyPress += OnCancelKeyPress; + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; + // Check if the lock file exists + if (File.Exists(lockFilePath)) { + + try { + File.Delete(lockFilePath); + Console.WriteLine("Found stale lock file and removed it."); + } catch { + AppDomain.CurrentDomain.ProcessExit -= OnProcessExit; + Console.WriteLine("The program is already running. Exiting."); + return; + } + } + + Console.WriteLine($"{lockFilePath} does not exist"); + var f = File.Create(lockFilePath); + try { + // Create or overwrite the log file + string logFilePath = Path.Combine(Path.GetTempPath(), "ZitiDesktopEdgeUpdateSentinel.log"); + using (StreamWriter logWriter = new StreamWriter(logFilePath, false)) { + // Redirect console output to the log file + Console.WriteLine($"Upgrade started. Waiting for the upgrade to complete."); + Console.WriteLine($"Log file created at: {logFilePath}"); + var outwriter = Console.Out; + Console.SetOut(logWriter); + if (File.Exists(pathToWatch)) { + Console.WriteLine($"File exists: {pathToWatch}"); + } + WatchForFile(pathToWatch); + + Thread.Sleep(1000); + // Start the process + Console.WriteLine($"Starting {pathToWatch}"); + using (Process process = new Process()) { + process.StartInfo.FileName = pathToWatch; + process.StartInfo.Arguments = "version"; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + process.Start(); + Console.WriteLine($"Started {pathToWatch}"); + } + logWriter.Flush(); + Console.SetOut(outwriter); + } + } finally { + // Delete the lock file from the user's temporary folder to allow the program to run next time + f.Close(); + File.Delete(lockFilePath); + } + } + + + static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e) { + Console.WriteLine("Ctrl+C detected. Cleaning up..."); + try { + // Delete the lock file upon termination + if (File.Exists(lockFilePath)) { + File.Delete(lockFilePath); + Console.WriteLine("Lock file deleted."); + } + } catch { } + } + + static void OnProcessExit(object sender, EventArgs e) { + Console.WriteLine("Process is exiting. Cleaning up..."); + OnCancelKeyPress(sender, null); + } + + static void WatchForFile(string filePath) { + // Extract the directory path from the file path + string directoryPath = Path.GetDirectoryName(filePath); + + // Create a new FileSystemWatcher instance + FileSystemWatcher watcher = new FileSystemWatcher(); + + // Set the path to watch (the directory containing the file) + watcher.Path = directoryPath; + + // Watch only the specified file + watcher.Filter = Path.GetFileName(filePath); + + // Subscribe to events + watcher.Created += OnFileCreated; + watcher.Deleted += OnFileDeleted; + watcher.Renamed += OnFileRenamed; + + // Enable the FileSystemWatcher + watcher.EnableRaisingEvents = true; + + Console.WriteLine($"Watching for changes to file '{filePath}'..."); + semaphore.Wait(); + } + + static void OnFileCreated(object sender, FileSystemEventArgs e) { + Console.WriteLine($"File created: {e.FullPath}"); + semaphore.Release(); + } + + static void OnFileDeleted(object sender, FileSystemEventArgs e) { + Console.WriteLine($"File deleted: {e.FullPath}"); + + // Uncomment the line below if you want to stop watching after the file is deleted + // ((FileSystemWatcher)sender).EnableRaisingEvents = false; + } + + static void OnFileRenamed(object sender, FileSystemEventArgs e) { + Console.WriteLine($"File renamed: {e.FullPath}"); + + // Uncomment the line below if you want to stop watching after the file is deleted + // ((FileSystemWatcher)sender).EnableRaisingEvents = false; + } +} \ No newline at end of file diff --git a/UpgradeSentinel/Properties/AssemblyInfo.cs b/UpgradeSentinel/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..56ab3134 --- /dev/null +++ b/UpgradeSentinel/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("UpgradeSentinel")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UpgradeSentinel")] +[assembly: AssemblyCopyright("Copyright © 2023")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3904dec1-ed7a-49d8-ba4d-d88f3b419248")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UpgradeSentinel/UpgradeSentinel.csproj b/UpgradeSentinel/UpgradeSentinel.csproj new file mode 100644 index 00000000..49466dc2 --- /dev/null +++ b/UpgradeSentinel/UpgradeSentinel.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248} + Exe + UpgradeSentinel + UpgradeSentinel + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ZitiDesktopEdge.sln b/ZitiDesktopEdge.sln index 25015012..ede4fd6b 100644 --- a/ZitiDesktopEdge.sln +++ b/ZitiDesktopEdge.sln @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZitiDesktopEdge.Client", "Z EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZitiUpdateService", "ZitiUpdateService\ZitiUpdateService.csproj", "{9AB81609-F5DD-40A6-87B5-F3279D48514D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UpgradeSentinel", "UpgradeSentinel\UpgradeSentinel.csproj", "{3904DEC1-ED7A-49D8-BA4D-D88F3B419248}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -165,6 +167,46 @@ Global {9AB81609-F5DD-40A6-87B5-F3279D48514D}.Releases|x64.Build.0 = Releases|Any CPU {9AB81609-F5DD-40A6-87B5-F3279D48514D}.Releases|x86.ActiveCfg = Releases|Any CPU {9AB81609-F5DD-40A6-87B5-F3279D48514D}.Releases|x86.Build.0 = Releases|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|ARM.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|ARM.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|ARM64.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|x64.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|x64.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|x86.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Debug|x86.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|Any CPU.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|Any CPU.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|ARM.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|ARM.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|ARM64.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|ARM64.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|x64.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|x64.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|x86.ActiveCfg = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.DebugUpdates|x86.Build.0 = Debug|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|Any CPU.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|ARM.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|ARM.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|ARM64.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|ARM64.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|x64.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|x64.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|x86.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Release|x86.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|Any CPU.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|Any CPU.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|ARM.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|ARM.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|ARM64.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|ARM64.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|x64.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|x64.Build.0 = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|x86.ActiveCfg = Release|Any CPU + {3904DEC1-ED7A-49D8-BA4D-D88F3B419248}.Releases|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE