Jackett.Updater: Make compatible with both legacy & .NET Core

This commit is contained in:
flightlevel
2018-06-24 11:31:08 +10:00
parent 13f2eea298
commit 7f9fff4683
6 changed files with 95 additions and 173 deletions

View File

@@ -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<UpdaterConsoleOptions>(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<UpdaterConsoleOptions>(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;
}
}
}