From 7f9fff468324b5769c7c3c5e55e0d58fe7efabbd Mon Sep 17 00:00:00 2001 From: flightlevel Date: Sun, 24 Jun 2018 11:31:08 +1000 Subject: [PATCH] Jackett.Updater: Make compatible with both legacy & .NET Core --- src/Jackett.Common/Services/UpdateService.cs | 19 ++- src/Jackett.Updater/App.config | 7 -- src/Jackett.Updater/Jackett.Updater.csproj | 90 ++------------ src/Jackett.Updater/Program.cs | 114 +++++++++++------- .../Properties/AssemblyInfo.cs | 35 ------ src/Jackett.Updater/UpdaterConsoleOptions.cs | 3 + 6 files changed, 95 insertions(+), 173 deletions(-) delete mode 100644 src/Jackett.Updater/App.config delete mode 100644 src/Jackett.Updater/Properties/AssemblyInfo.cs diff --git a/src/Jackett.Common/Services/UpdateService.cs b/src/Jackett.Common/Services/UpdateService.cs index 36b1a0baa..a3c808d1d 100644 --- a/src/Jackett.Common/Services/UpdateService.cs +++ b/src/Jackett.Common/Services/UpdateService.cs @@ -98,6 +98,12 @@ namespace Jackett.Common.Services return; } + bool trayIsRunning = false; + if (isWindows) + { + trayIsRunning = Process.GetProcessesByName("JackettTray").Length > 0; + } + try { @@ -135,7 +141,7 @@ namespace Jackett.Common.Services var installDir = Path.GetDirectoryName(ExePath()); var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe"); if (updaterPath != null) - StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart); + StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart, trayIsRunning); } catch (Exception e) { @@ -212,7 +218,7 @@ namespace Jackett.Common.Services private async Task DownloadRelease(List assets, bool isWindows, string version) { - var targetAsset = assets.Where(a => isWindows ? a.Browser_download_url.ToLowerInvariant().EndsWith(".zip") : a.Browser_download_url.ToLowerInvariant().EndsWith(".gz")).FirstOrDefault(); + var targetAsset = assets.Where(a => isWindows ? a.Browser_download_url.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) : a.Browser_download_url.EndsWith(".gz", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (targetAsset == null) { @@ -262,7 +268,7 @@ namespace Jackett.Common.Services return tempDir; } - private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart) + private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart, bool trayWasRunning) { var exe = Path.GetFileName(ExePath()); var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" +a + "\"" : a )).Replace("\"", "\\\""); @@ -298,7 +304,14 @@ namespace Jackett.Common.Services } if (NoRestart) + { startInfo.Arguments += " --NoRestart"; + } + + if (trayWasRunning) + { + startInfo.Arguments += " --StartTray"; + } logger.Info($"Starting updater: {startInfo.FileName} {startInfo.Arguments}"); var procInfo = Process.Start(startInfo); diff --git a/src/Jackett.Updater/App.config b/src/Jackett.Updater/App.config deleted file mode 100644 index 6d65ca7c1..000000000 --- a/src/Jackett.Updater/App.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/src/Jackett.Updater/Jackett.Updater.csproj b/src/Jackett.Updater/Jackett.Updater.csproj index 221b75bcd..11715844d 100644 --- a/src/Jackett.Updater/Jackett.Updater.csproj +++ b/src/Jackett.Updater/Jackett.Updater.csproj @@ -1,88 +1,14 @@ - - - - - Debug - AnyCPU - {A61E311A-6F8B-4497-B5E4-2EA8994C7BD8} - Exe - Properties - Jackett.Updater - JackettUpdater - v4.5.2 - 512 - true - PackageReference - win - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - + + + net452;net461;netcoreapp2.1 jackett.ico + JackettUpdater + Exe + - - - - - - - - - - - - - - - + - - - - - - - - Designer - - - - - {6B854A1B-9A90-49C0-BC37-9A35C75BCA73} - Jackett.Common - - - {e636d5f8-68b4-4903-b4ed-ccfd9c9e899f} - Jackett - - - - - - - + \ No newline at end of file diff --git a/src/Jackett.Updater/Program.cs b/src/Jackett.Updater/Program.cs index babe31a9d..16a0a8025 100644 --- a/src/Jackett.Updater/Program.cs +++ b/src/Jackett.Updater/Program.cs @@ -1,34 +1,51 @@ using CommandLine; using CommandLine.Text; using Jackett.Common.Models.Config; -using Jackett.Services; +using Jackett.Common.Services; +using Jackett.Common.Services.Interfaces; +using Jackett.Common.Utils; +using NLog; using System; using System.Diagnostics; using System.IO; using System.Linq; +using System.Net; using System.Reflection; -using System.Web; -using Jackett.Common; namespace Jackett.Updater { - class Program + public class Program { - static void Main(string[] args) + private IProcessService processService; + private IServiceConfigService windowsService; + private Logger logger; + + public static void Main(string[] args) { new Program().Run(args); } private void Run(string[] args) { - Engine.BuildContainer(new RuntimeSettings() + RuntimeSettings runtimeSettings = new RuntimeSettings() { CustomLogFileName = "updater.txt" - }); - Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion()); - Engine.Logger.Info("Options \"" + string.Join("\" \"", args) + "\""); - try { - var optionsResult = Parser.Default.ParseArguments(args); + }; + + LogManager.Configuration = LoggingSetup.GetLoggingConfiguration(runtimeSettings); + logger = LogManager.GetCurrentClassLogger(); + + logger.Info("Jackett Updater v" + GetCurrentVersion()); + logger.Info("Options \"" + string.Join("\" \"", args) + "\""); + + processService = new ProcessService(logger); + windowsService = new WindowsServiceConfigService(processService, logger); + + var commandLineParser = new Parser(settings => settings.CaseSensitive = false); + + try + { + var optionsResult = commandLineParser.ParseArguments(args); optionsResult.WithParsed(options => { ProcessUpdate(options); @@ -36,14 +53,14 @@ namespace Jackett.Updater ); optionsResult.WithNotParsed(errors => { - Engine.Logger.Error(HelpText.AutoBuild(optionsResult)); - Engine.Logger.Error("Failed to process update arguments!"); + logger.Error(HelpText.AutoBuild(optionsResult)); + logger.Error("Failed to process update arguments!"); Console.ReadKey(); }); } catch (Exception e) { - Engine.Logger.Error(e, "Exception applying update!"); + logger.Error(e, "Exception applying update!"); } } @@ -61,21 +78,20 @@ namespace Jackett.Updater try { var proc = Process.GetProcessById(pid); - Engine.Logger.Info("Killing process " + proc.Id); + logger.Info("Killing process " + proc.Id); proc.Kill(); var exited = proc.WaitForExit(5000); if (!exited) - Engine.Logger.Info("Process " + pid.ToString() + " didn't exit within 5 seconds"); - + logger.Info("Process " + pid.ToString() + " didn't exit within 5 seconds"); } catch (ArgumentException) { - Engine.Logger.Info("Process " + pid.ToString() + " is already dead"); + logger.Info("Process " + pid.ToString() + " is already dead"); } catch (Exception e) { - Engine.Logger.Info("Error killing process " + pid.ToString()); - Engine.Logger.Info(e); + logger.Info("Error killing process " + pid.ToString()); + logger.Info(e); } } } @@ -83,7 +99,7 @@ namespace Jackett.Updater private void ProcessUpdate(UpdaterConsoleOptions options) { var updateLocation = GetUpdateLocation(); - if(!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/"))) + if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/"))) { updateLocation += Path.DirectorySeparatorChar; } @@ -95,18 +111,18 @@ namespace Jackett.Updater pids = Array.ConvertAll(pidsStr, pid => int.Parse(pid)); } - var isWindows = System.Environment.OSVersion.Platform != PlatformID.Unix; + var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; var trayRunning = false; var trayProcesses = Process.GetProcessesByName("JackettTray"); if (isWindows) { if (trayProcesses.Count() > 0) - { + { foreach (var proc in trayProcesses) { try { - Engine.Logger.Info("Killing tray process " + proc.Id); + logger.Info("Killing tray process " + proc.Id); proc.Kill(); trayRunning = true; } @@ -118,9 +134,9 @@ namespace Jackett.Updater // On unix we kill the PIDs after the update so e.g. systemd can automatically restart the process KillPids(pids); } - Engine.Logger.Info("Finding files in: " + updateLocation); + logger.Info("Finding files in: " + updateLocation); var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories); - foreach(var file in files) + foreach (var file in files) { var fileName = Path.GetFileName(file).ToLowerInvariant(); @@ -130,20 +146,21 @@ namespace Jackett.Updater { continue; } - try { - Engine.Logger.Info("Copying " + fileName); + try + { + logger.Info("Copying " + fileName); var dest = Path.Combine(options.Path, file.Substring(updateLocation.Length)); var destDir = Path.GetDirectoryName(dest); if (!Directory.Exists(destDir)) { - Engine.Logger.Info("Creating directory " + destDir); + logger.Info("Creating directory " + destDir); Directory.CreateDirectory(destDir); } File.Copy(file, dest, true); } - catch(Exception e) + catch (Exception e) { - Engine.Logger.Error(e); + logger.Error(e); } } @@ -157,17 +174,16 @@ namespace Jackett.Updater var deleteDir = Path.Combine(options.Path, oldDir); if (Directory.Exists(deleteDir)) { - Engine.Logger.Info("Deleting directory " + deleteDir); + logger.Info("Deleting directory " + deleteDir); Directory.Delete(deleteDir, true); } } catch (Exception e) { - Engine.Logger.Error(e); + logger.Error(e); } } - // delete old files string[] oldFiles = new string[] { "Content/css/jquery.dataTables.css", @@ -209,20 +225,20 @@ namespace Jackett.Updater "Definitions/torrentwtf.yml", }; - foreach (var oldFIle in oldFiles) + foreach (var oldFile in oldFiles) { try { - var deleteFile = Path.Combine(options.Path, oldFIle); + var deleteFile = Path.Combine(options.Path, oldFile); if (File.Exists(deleteFile)) { - Engine.Logger.Info("Deleting file " + deleteFile); + logger.Info("Deleting file " + deleteFile); File.Delete(deleteFile); } } catch (Exception e) { - Engine.Logger.Error(e); + logger.Error(e); } } @@ -232,7 +248,7 @@ namespace Jackett.Updater if (options.NoRestart == false) { - if (trayRunning) + if (trayRunning || options.StartTray) { var startInfo = new ProcessStartInfo() { @@ -242,16 +258,22 @@ namespace Jackett.Updater }; Process.Start(startInfo); + + if (!windowsService.ServiceExists()) + { + //User was running the tray icon, but not using the Windows service, starting Tray icon will start JackettConsole as well + return; + } } - if(string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase)) + if (string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase)) { - var serviceHelper = new ServiceConfigService(null, null); - if (serviceHelper.ServiceExists()) + if (windowsService.ServiceExists()) { - serviceHelper.Start(); + windowsService.Start(); } - } else + } + else { var startInfo = new ProcessStartInfo() { @@ -266,7 +288,7 @@ namespace Jackett.Updater startInfo.FileName = "mono"; } - Engine.Logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments); + logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments); Process.Start(startInfo); } } @@ -275,7 +297,7 @@ namespace Jackett.Updater private string GetUpdateLocation() { var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase); - return new FileInfo(HttpUtility.UrlDecode(location.AbsolutePath)).DirectoryName; + return new FileInfo(WebUtility.UrlDecode(location.AbsolutePath)).DirectoryName; } } } diff --git a/src/Jackett.Updater/Properties/AssemblyInfo.cs b/src/Jackett.Updater/Properties/AssemblyInfo.cs deleted file mode 100644 index abbc39bda..000000000 --- a/src/Jackett.Updater/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Reflection; -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("Jackett.Updater")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Jackett.Updater")] -[assembly: AssemblyCopyright("Copyright © 2015")] -[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("a61e311a-6f8b-4497-b5e4-2ea8994c7bd8")] - -// 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("0.0.0.0")] -[assembly: AssemblyFileVersion("0.0.0.0")] diff --git a/src/Jackett.Updater/UpdaterConsoleOptions.cs b/src/Jackett.Updater/UpdaterConsoleOptions.cs index 92cc06330..6baf43d2c 100644 --- a/src/Jackett.Updater/UpdaterConsoleOptions.cs +++ b/src/Jackett.Updater/UpdaterConsoleOptions.cs @@ -18,5 +18,8 @@ namespace Jackett.Updater [Option("KillPids", HelpText = "PIDs which will be killed before (Windows) or after (Unix) the update")] public string KillPids { get; set; } + + [Option("StartTray", HelpText = "Indicates that the updater should start the tray icon")] + public bool StartTray { get; set; } } }