From 0a005ac6d68da753af21f05fba634364bbd7624e Mon Sep 17 00:00:00 2001 From: Jeremy Tellier - ROG Date: Fri, 19 Jun 2020 18:42:37 +0100 Subject: [PATCH 1/4] no message --- ZitiUpdateService/UpdateService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index fe0c1444..0d362c88 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -23,7 +23,7 @@ namespace ZitiUpdateService { private bool _isNew = true; /// private int _majorVersion = 1; private string _versionUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/maven-metadata.xml"; - private string _serviceUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/0.0.8-SNAPSHOT/ziti-tunnel-win-0.0.8-SNAPSHOT.zip"; + private string _serviceUrl = "https://netfoundry.jfrog.io/netfoundry/ziti-maven-snapshot/ziti-tunnel-win/amd64/windows/ziti-tunnel-win/${version}/ziti-tunnel-win-${version}.zip"; private System.Timers.Timer _checkTimer = new System.Timers.Timer(); private System.Timers.Timer _updateTimer = new System.Timers.Timer(); private string _rootDirectory = ""; From e92af4574004f005492b7ff477fc714308754eb6 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Fri, 19 Jun 2020 14:55:30 -0400 Subject: [PATCH 2/4] updates to the service watcher. don't redownload every time if it's not needed. provide a way for 'debug' to work. cleanup some logic here/there --- ZitiUpdateService/App.config | 21 ++++++ ZitiUpdateService/Program.cs | 8 ++- ZitiUpdateService/UpdateService.cs | 100 ++++++++++++++++------------- 3 files changed, 82 insertions(+), 47 deletions(-) diff --git a/ZitiUpdateService/App.config b/ZitiUpdateService/App.config index bc30bb6b..d5e06cdb 100644 --- a/ZitiUpdateService/App.config +++ b/ZitiUpdateService/App.config @@ -2,7 +2,28 @@ + + + + + + + diff --git a/ZitiUpdateService/Program.cs b/ZitiUpdateService/Program.cs index d9bdd216..449ad561 100644 --- a/ZitiUpdateService/Program.cs +++ b/ZitiUpdateService/Program.cs @@ -11,12 +11,18 @@ namespace ZitiUpdateService { /// The main entry point for the application. /// static void Main() { + UpdateService updateSvc = new UpdateService(); +#if DEBUG + updateSvc.Debug(); + System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); +#else ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { - new UpdateService() + updateSvc }; ServiceBase.Run(ServicesToRun); +#endif } } } diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index 0d362c88..0ef28c7c 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -19,6 +19,9 @@ using System.Collections.Specialized; namespace ZitiUpdateService { public partial class UpdateService : ServiceBase { + private int checkTimerDefault = 60000; + private int updateTimerDefault = 600000; + private string _version = ""; private bool _isNew = true; /// private int _majorVersion = 1; @@ -36,6 +39,11 @@ namespace ZitiUpdateService { InitializeComponent(); } + public void Debug() + { + OnStart(null); + } + protected override void OnStart(string[] args) { try { if (ConfigurationManager.AppSettings.Get("UpdateUrl") != null) _versionUrl = ConfigurationManager.AppSettings.Get("UpdateUrl"); @@ -47,48 +55,46 @@ namespace ZitiUpdateService { if (!Directory.Exists(_rootDirectory)) Directory.CreateDirectory(_rootDirectory); _logDirectory = Path.Combine(_rootDirectory, "Logs"); if (!Directory.Exists(_logDirectory)) Directory.CreateDirectory(_logDirectory); - Log("Setup Watchers"); - SetupServiceWatcher(); + + SetupWatcher(); } protected override void OnStop() { Log("Stopping update Service"); } - private void SetupServiceWatcher() { - controller = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ziti"); - if (controller==null) { - Log("Get Installation"); - UpdateServiceFiles(); - } else { - Log("Check Version"); - _isNew = false; - if (File.Exists(Path.Combine(_rootDirectory, @"Service\Version.txt"))) { - _version = File.ReadAllText(Path.Combine(_rootDirectory, @"Service\Version.txt")); - Log("Version Found: " + _version); - if (controller.Status==ServiceControllerStatus.Stopped) { - StartZiti(); - } else { - SetupWatcher(); - } - } else { - UpdateServiceFiles(); - } - } - } - private void SetupWatcher() { + Log("Setting Up Watchers"); + + var serviceTimerInterval = ConfigurationManager.AppSettings.Get("ServiceTimer"); + var serviceInt = TimeSpan.Zero; + if (!TimeSpan.TryParse(serviceTimerInterval, out serviceInt)) + { + serviceInt = new TimeSpan(0, 1, 0); + } + + var updateTimerInterval = ConfigurationManager.AppSettings.Get("UpdateTimer"); + var upInt = TimeSpan.Zero; + if (!TimeSpan.TryParse(updateTimerInterval, out upInt)) + { + upInt = new TimeSpan(0, 1, 0); + } + Log("Ziti Update Setting Up Watchers"); -; _checkTimer = new System.Timers.Timer(); + + _checkTimer = new System.Timers.Timer(); _checkTimer.Elapsed += CheckService; - _checkTimer.Interval = 60000; + _checkTimer.Interval = serviceInt.TotalMilliseconds; _checkTimer.Enabled = true; _checkTimer.Start(); + Log("Service Checker is running"); + _updateTimer = new System.Timers.Timer(); _updateTimer.Elapsed += CheckUpdate; - _updateTimer.Interval = 600000; + _updateTimer.Interval = upInt.TotalMilliseconds; _updateTimer.Enabled = true; _updateTimer.Start(); + Log("Version Checker is running"); } private void CheckUpdate(object sender, ElapsedEventArgs e) { @@ -101,10 +107,9 @@ namespace ZitiUpdateService { xmlDoc.LoadXml(result); XmlNode node = xmlDoc.SelectSingleNode("metadata/versioning/"+ _versionType); string version = node.InnerText; - Log("Version Checked: " + version+" on "+_version+" from "+_versionType); if (version != _version) { - StopZiti(); - UpdateServiceFiles(); + Log("Version Checked: " + version + " on " + _version + " from " + _versionType); + UpdateServiceFiles(version); } } @@ -114,7 +119,8 @@ namespace ZitiUpdateService { if (_isJustStopped) { _isJustStopped = false; Log("Ziti Service has been stopped, update Network"); - RunScript(); + //should not be necessary in v0.0.10+ RunScript(); + StartZiti(); } } else { if (controller.Status==ServiceControllerStatus.Running) { @@ -123,15 +129,7 @@ namespace ZitiUpdateService { } } - private void UpdateServiceFiles() { - if (controller!=null) { - controller.Stop(); - } else { - GetFiles(); - } - } - - private void GetFiles() { + private void UpdateServiceFiles(string currentVersion) { var request = WebRequest.Create(_versionUrl) as HttpWebRequest; var response = request.GetResponse(); Stream receiveStream = response.GetResponseStream(); @@ -143,6 +141,14 @@ namespace ZitiUpdateService { string version = node.InnerText; Log("Got Version: " + version+" from "+_versionType); + if (version.Trim() == currentVersion.Trim()) + { + _version = currentVersion; + Log("Version local is the same as the version remote: " + version); + return; + } + + StopZiti(); string remoteService = _serviceUrl.Replace("${version}", version); if (!Directory.Exists(Path.Combine(_rootDirectory, "Service"))) Directory.CreateDirectory(Path.Combine(_rootDirectory, "Service")); @@ -177,16 +183,15 @@ namespace ZitiUpdateService { try { Process exeProcess = Process.Start(installService); exeProcess.WaitForExit(); - StartZiti(); Log("Installed " + installService.FileName + " " + installService.Arguments); } catch (Exception e) { Log("Cannot Install Service - " + e.ToString()); } } else { - StartZiti(); + // nothing to do } - + StartZiti(); } private void RunScript() { @@ -207,7 +212,7 @@ namespace ZitiUpdateService { try { Log("Starting Service"); controller.Start(); - controller.WaitForStatus(ServiceControllerStatus.Running); + controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); SetupWatcher(); } catch (Exception e) { Log("Cannot Start Service - " + e.ToString()); @@ -227,9 +232,12 @@ namespace ZitiUpdateService { } } + object logLock = new object(); private void Log(string message) { - File.AppendAllText(Path.Combine(_logDirectory, @"Log.log"), DateTime.Now.ToString()+" "+message); - EventLog.WriteEntry("Ziti Update Service", message, EventLogEntryType.Information); + lock (logLock) { + File.AppendAllText(Path.Combine(_logDirectory, @"Log.log"), DateTime.Now.ToString() + " " + message + '\n'); + EventLog.WriteEntry("Ziti Update Service", message, EventLogEntryType.Information); + } } } } From 41c4f7d5f31ff7e5f07643e659157d3062ec2315 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Fri, 19 Jun 2020 14:57:37 -0400 Subject: [PATCH 3/4] put debug changes back to 'regular' defaults' --- ZitiUpdateService/App.config | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ZitiUpdateService/App.config b/ZitiUpdateService/App.config index d5e06cdb..bcacd1c9 100644 --- a/ZitiUpdateService/App.config +++ b/ZitiUpdateService/App.config @@ -10,7 +10,7 @@ This example shows a value that would check for updates every one hour, two minutes and three seconds --> - + @@ -23,7 +23,7 @@ This operation is fast and should probably remain low such as "0:1:0" (every minute) or "0:0:15" (every 15 seconds) --> - + From 2cf83fa0353fee20a59b5826a312413a1be040bb Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Fri, 19 Jun 2020 15:09:50 -0400 Subject: [PATCH 4/4] bump the version to see if the updater will take it --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index 7c1886bb..2cfabea2 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.0.10 +0.0.11