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

@@ -98,6 +98,12 @@ namespace Jackett.Common.Services
return; return;
} }
bool trayIsRunning = false;
if (isWindows)
{
trayIsRunning = Process.GetProcessesByName("JackettTray").Length > 0;
}
try try
{ {
@@ -135,7 +141,7 @@ namespace Jackett.Common.Services
var installDir = Path.GetDirectoryName(ExePath()); var installDir = Path.GetDirectoryName(ExePath());
var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe"); var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe");
if (updaterPath != null) if (updaterPath != null)
StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart); StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart, trayIsRunning);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -212,7 +218,7 @@ namespace Jackett.Common.Services
private async Task<string> DownloadRelease(List<Asset> assets, bool isWindows, string version) private async Task<string> DownloadRelease(List<Asset> 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) if (targetAsset == null)
{ {
@@ -262,7 +268,7 @@ namespace Jackett.Common.Services
return tempDir; 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 exe = Path.GetFileName(ExePath());
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" +a + "\"" : a )).Replace("\"", "\\\""); 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) if (NoRestart)
{
startInfo.Arguments += " --NoRestart"; startInfo.Arguments += " --NoRestart";
}
if (trayWasRunning)
{
startInfo.Arguments += " --StartTray";
}
logger.Info($"Starting updater: {startInfo.FileName} {startInfo.Arguments}"); logger.Info($"Starting updater: {startInfo.FileName} {startInfo.Arguments}");
var procInfo = Process.Start(startInfo); var procInfo = Process.Start(startInfo);

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>

View File

@@ -1,88 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <Project Sdk="Microsoft.NET.Sdk">
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A61E311A-6F8B-4497-B5E4-2EA8994C7BD8}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Jackett.Updater</RootNamespace>
<AssemblyName>JackettUpdater</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<RuntimeIdentifier>win</RuntimeIdentifier>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net452;net461;netcoreapp2.1</TargetFrameworks>
<ApplicationIcon>jackett.ico</ApplicationIcon> <ApplicationIcon>jackett.ico</ApplicationIcon>
<AssemblyName>JackettUpdater</AssemblyName>
<OutputType>Exe</OutputType>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <ProjectReference Include="..\Jackett.Common\Jackett.Common.csproj" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdaterConsoleOptions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Jackett.Common\Jackett.Common.csproj">
<Project>{6B854A1B-9A90-49C0-BC37-9A35C75BCA73}</Project>
<Name>Jackett.Common</Name>
</ProjectReference>
<ProjectReference Include="..\Jackett\Jackett.csproj">
<Project>{e636d5f8-68b4-4903-b4ed-ccfd9c9e899f}</Project>
<Name>Jackett</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="jackett.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project> </Project>

View File

@@ -1,34 +1,51 @@
using CommandLine; using CommandLine;
using CommandLine.Text; using CommandLine.Text;
using Jackett.Common.Models.Config; 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;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Web;
using Jackett.Common;
namespace Jackett.Updater 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); new Program().Run(args);
} }
private void Run(string[] args) private void Run(string[] args)
{ {
Engine.BuildContainer(new RuntimeSettings() RuntimeSettings runtimeSettings = new RuntimeSettings()
{ {
CustomLogFileName = "updater.txt" CustomLogFileName = "updater.txt"
}); };
Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion());
Engine.Logger.Info("Options \"" + string.Join("\" \"", args) + "\""); LogManager.Configuration = LoggingSetup.GetLoggingConfiguration(runtimeSettings);
try { logger = LogManager.GetCurrentClassLogger();
var optionsResult = Parser.Default.ParseArguments<UpdaterConsoleOptions>(args);
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 => optionsResult.WithParsed(options =>
{ {
ProcessUpdate(options); ProcessUpdate(options);
@@ -36,14 +53,14 @@ namespace Jackett.Updater
); );
optionsResult.WithNotParsed(errors => optionsResult.WithNotParsed(errors =>
{ {
Engine.Logger.Error(HelpText.AutoBuild(optionsResult)); logger.Error(HelpText.AutoBuild(optionsResult));
Engine.Logger.Error("Failed to process update arguments!"); logger.Error("Failed to process update arguments!");
Console.ReadKey(); Console.ReadKey();
}); });
} }
catch (Exception e) catch (Exception e)
{ {
Engine.Logger.Error(e, "Exception applying update!"); logger.Error(e, "Exception applying update!");
} }
} }
@@ -61,21 +78,20 @@ namespace Jackett.Updater
try try
{ {
var proc = Process.GetProcessById(pid); var proc = Process.GetProcessById(pid);
Engine.Logger.Info("Killing process " + proc.Id); logger.Info("Killing process " + proc.Id);
proc.Kill(); proc.Kill();
var exited = proc.WaitForExit(5000); var exited = proc.WaitForExit(5000);
if (!exited) 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) catch (ArgumentException)
{ {
Engine.Logger.Info("Process " + pid.ToString() + " is already dead"); logger.Info("Process " + pid.ToString() + " is already dead");
} }
catch (Exception e) catch (Exception e)
{ {
Engine.Logger.Info("Error killing process " + pid.ToString()); logger.Info("Error killing process " + pid.ToString());
Engine.Logger.Info(e); logger.Info(e);
} }
} }
} }
@@ -83,7 +99,7 @@ namespace Jackett.Updater
private void ProcessUpdate(UpdaterConsoleOptions options) private void ProcessUpdate(UpdaterConsoleOptions options)
{ {
var updateLocation = GetUpdateLocation(); var updateLocation = GetUpdateLocation();
if(!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/"))) if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/")))
{ {
updateLocation += Path.DirectorySeparatorChar; updateLocation += Path.DirectorySeparatorChar;
} }
@@ -95,18 +111,18 @@ namespace Jackett.Updater
pids = Array.ConvertAll(pidsStr, pid => int.Parse(pid)); 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 trayRunning = false;
var trayProcesses = Process.GetProcessesByName("JackettTray"); var trayProcesses = Process.GetProcessesByName("JackettTray");
if (isWindows) if (isWindows)
{ {
if (trayProcesses.Count() > 0) if (trayProcesses.Count() > 0)
{ {
foreach (var proc in trayProcesses) foreach (var proc in trayProcesses)
{ {
try try
{ {
Engine.Logger.Info("Killing tray process " + proc.Id); logger.Info("Killing tray process " + proc.Id);
proc.Kill(); proc.Kill();
trayRunning = true; 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 // On unix we kill the PIDs after the update so e.g. systemd can automatically restart the process
KillPids(pids); KillPids(pids);
} }
Engine.Logger.Info("Finding files in: " + updateLocation); logger.Info("Finding files in: " + updateLocation);
var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories); var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories);
foreach(var file in files) foreach (var file in files)
{ {
var fileName = Path.GetFileName(file).ToLowerInvariant(); var fileName = Path.GetFileName(file).ToLowerInvariant();
@@ -130,20 +146,21 @@ namespace Jackett.Updater
{ {
continue; continue;
} }
try { try
Engine.Logger.Info("Copying " + fileName); {
logger.Info("Copying " + fileName);
var dest = Path.Combine(options.Path, file.Substring(updateLocation.Length)); var dest = Path.Combine(options.Path, file.Substring(updateLocation.Length));
var destDir = Path.GetDirectoryName(dest); var destDir = Path.GetDirectoryName(dest);
if (!Directory.Exists(destDir)) if (!Directory.Exists(destDir))
{ {
Engine.Logger.Info("Creating directory " + destDir); logger.Info("Creating directory " + destDir);
Directory.CreateDirectory(destDir); Directory.CreateDirectory(destDir);
} }
File.Copy(file, dest, true); 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); var deleteDir = Path.Combine(options.Path, oldDir);
if (Directory.Exists(deleteDir)) if (Directory.Exists(deleteDir))
{ {
Engine.Logger.Info("Deleting directory " + deleteDir); logger.Info("Deleting directory " + deleteDir);
Directory.Delete(deleteDir, true); Directory.Delete(deleteDir, true);
} }
} }
catch (Exception e) catch (Exception e)
{ {
Engine.Logger.Error(e); logger.Error(e);
} }
} }
// delete old files // delete old files
string[] oldFiles = new string[] { string[] oldFiles = new string[] {
"Content/css/jquery.dataTables.css", "Content/css/jquery.dataTables.css",
@@ -209,20 +225,20 @@ namespace Jackett.Updater
"Definitions/torrentwtf.yml", "Definitions/torrentwtf.yml",
}; };
foreach (var oldFIle in oldFiles) foreach (var oldFile in oldFiles)
{ {
try try
{ {
var deleteFile = Path.Combine(options.Path, oldFIle); var deleteFile = Path.Combine(options.Path, oldFile);
if (File.Exists(deleteFile)) if (File.Exists(deleteFile))
{ {
Engine.Logger.Info("Deleting file " + deleteFile); logger.Info("Deleting file " + deleteFile);
File.Delete(deleteFile); File.Delete(deleteFile);
} }
} }
catch (Exception e) catch (Exception e)
{ {
Engine.Logger.Error(e); logger.Error(e);
} }
} }
@@ -232,7 +248,7 @@ namespace Jackett.Updater
if (options.NoRestart == false) if (options.NoRestart == false)
{ {
if (trayRunning) if (trayRunning || options.StartTray)
{ {
var startInfo = new ProcessStartInfo() var startInfo = new ProcessStartInfo()
{ {
@@ -242,16 +258,22 @@ namespace Jackett.Updater
}; };
Process.Start(startInfo); 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 (windowsService.ServiceExists())
if (serviceHelper.ServiceExists())
{ {
serviceHelper.Start(); windowsService.Start();
} }
} else }
else
{ {
var startInfo = new ProcessStartInfo() var startInfo = new ProcessStartInfo()
{ {
@@ -266,7 +288,7 @@ namespace Jackett.Updater
startInfo.FileName = "mono"; startInfo.FileName = "mono";
} }
Engine.Logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments); logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments);
Process.Start(startInfo); Process.Start(startInfo);
} }
} }
@@ -275,7 +297,7 @@ namespace Jackett.Updater
private string GetUpdateLocation() private string GetUpdateLocation()
{ {
var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase); var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
return new FileInfo(HttpUtility.UrlDecode(location.AbsolutePath)).DirectoryName; return new FileInfo(WebUtility.UrlDecode(location.AbsolutePath)).DirectoryName;
} }
} }
} }

View File

@@ -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")]

View File

@@ -18,5 +18,8 @@ namespace Jackett.Updater
[Option("KillPids", HelpText = "PIDs which will be killed before (Windows) or after (Unix) the update")] [Option("KillPids", HelpText = "PIDs which will be killed before (Windows) or after (Unix) the update")]
public string KillPids { get; set; } public string KillPids { get; set; }
[Option("StartTray", HelpText = "Indicates that the updater should start the tray icon")]
public bool StartTray { get; set; }
} }
} }