From 2e0f3016809898e95fb8fa8ed8cbaddc5ddebb25 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 21 Oct 2017 22:49:36 +0100 Subject: [PATCH] Adding logging into the auto updater and also added more logging around the create inital user for #1604 --- src/Ombi.Updater/IInstaller.cs | 7 ++++ src/Ombi.Updater/Installer.cs | 37 +++++++++++++----- src/Ombi.Updater/Ombi.Updater.csproj | 16 ++++++++ src/Ombi.Updater/Program.cs | 44 +++++++++++++++++++++- src/Ombi/Controllers/IdentityController.cs | 26 ++++++++++--- src/Ombi/Startup.cs | 2 +- 6 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/Ombi.Updater/IInstaller.cs diff --git a/src/Ombi.Updater/IInstaller.cs b/src/Ombi.Updater/IInstaller.cs new file mode 100644 index 000000000..f8c14fc06 --- /dev/null +++ b/src/Ombi.Updater/IInstaller.cs @@ -0,0 +1,7 @@ +namespace Ombi.Updater +{ + public interface IInstaller + { + void Start(StartupOptions opt); + } +} \ No newline at end of file diff --git a/src/Ombi.Updater/Installer.cs b/src/Ombi.Updater/Installer.cs index 91f52858b..dbf6943e7 100644 --- a/src/Ombi.Updater/Installer.cs +++ b/src/Ombi.Updater/Installer.cs @@ -4,11 +4,19 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Threading; +using Microsoft.Extensions.Logging; namespace Ombi.Updater { - public class Installer + public class Installer : IInstaller { + public Installer(ILogger log) + { + _log = log; + } + + private readonly ILogger _log; + public void Start(StartupOptions opt) { // Kill Ombi Process @@ -19,23 +27,25 @@ namespace Ombi.Updater while (p.FindProcessByName(opt.ProcessName).Any()) { Thread.Sleep(500); - Console.WriteLine("Found another process called Ombi, KILLING!"); + _log.LogDebug("Found another process called {0}, KILLING!", opt.ProcessName); var proc = p.FindProcessByName(opt.ProcessName).FirstOrDefault(); if (proc != null) { - Console.WriteLine($"[{proc.Id}] - {proc.Name} - Path: {proc.StartPath}"); + _log.LogDebug($"[{proc.Id}] - {proc.Name} - Path: {proc.StartPath}"); p.Kill(proc.Id); } } + _log.LogDebug("Starting to move the files"); MoveFiles(opt); - + _log.LogDebug("Files replaced"); // Start Ombi StartOmbi(opt); } private void StartOmbi(StartupOptions options) { + _log.LogDebug("Starting ombi"); var fileName = "Ombi.exe"; if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { @@ -52,7 +62,7 @@ namespace Ombi.Updater { proc.Start(); } - + _log.LogDebug("Ombi started, now exiting"); Environment.Exit(0); } @@ -60,16 +70,25 @@ namespace Ombi.Updater { var location = System.Reflection.Assembly.GetEntryAssembly().Location; location = Path.GetDirectoryName(location); + _log.LogDebug("We are currently in dir {0}", location); + _log.LogDebug("Ombi is installed at {0}", options.ApplicationPath); //Now Create all of the directories foreach (string dirPath in Directory.GetDirectories(location, "*", SearchOption.AllDirectories)) - Directory.CreateDirectory(dirPath.Replace(location, options.ApplicationPath)); - + { + var newDir = dirPath.Replace(location, options.ApplicationPath); + Directory.CreateDirectory(newDir); + _log.LogDebug("Created dir {0}", newDir); + } //Copy all the files & Replaces any files with the same name - foreach (string newPath in Directory.GetFiles(location, "*.*", + foreach (string currentPath in Directory.GetFiles(location, "*.*", SearchOption.AllDirectories)) - File.Copy(newPath, newPath.Replace(location, options.ApplicationPath), true); + { + var newFile = currentPath.Replace(location, options.ApplicationPath); + File.Copy(currentPath, newFile, true); + _log.LogDebug("Replaced file {0}", newFile); + } } } } diff --git a/src/Ombi.Updater/Ombi.Updater.csproj b/src/Ombi.Updater/Ombi.Updater.csproj index 3891a0ce9..35c059105 100644 --- a/src/Ombi.Updater/Ombi.Updater.csproj +++ b/src/Ombi.Updater/Ombi.Updater.csproj @@ -10,4 +10,20 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Ombi.Updater/Program.cs b/src/Ombi.Updater/Program.cs index 55e6797f4..93ce79fce 100644 --- a/src/Ombi.Updater/Program.cs +++ b/src/Ombi.Updater/Program.cs @@ -1,6 +1,13 @@ using System; using System.Diagnostics; +using System.IO; using System.Linq; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; +using ILogger = Serilog.ILogger; namespace Ombi.Updater { @@ -14,11 +21,44 @@ namespace Ombi.Updater var options = CheckArgs(args); - var install = new Installer(); - install.Start(options); + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); + + // Create service provider + var serviceProvider = serviceCollection.BuildServiceProvider(); + + // Run app + serviceProvider.GetService().Start(options); } + private static void ConfigureServices(IServiceCollection serviceCollection) + { + // Add logging + serviceCollection.AddSingleton(new LoggerFactory() + .AddConsole() + .AddSerilog() + .AddDebug()); + serviceCollection.AddLogging(); + + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.RollingFile(Path.Combine("Logs", "log-{Date}.txt")) + .Enrich.FromLogContext() + .CreateLogger(); + + // Build configuration + var configuration = new ConfigurationBuilder() + .SetBasePath(AppContext.BaseDirectory) + .Build(); + + // Add access to generic IConfigurationRoot + serviceCollection.AddSingleton(configuration); + + //// Add services + serviceCollection.AddTransient(); + } + private static StartupOptions CheckArgs(string[] args) { if(args.Length <= 1) diff --git a/src/Ombi/Controllers/IdentityController.cs b/src/Ombi/Controllers/IdentityController.cs index c8c492dc5..7b9e42457 100644 --- a/src/Ombi/Controllers/IdentityController.cs +++ b/src/Ombi/Controllers/IdentityController.cs @@ -30,6 +30,7 @@ using Ombi.Settings.Settings.Models.Notifications; using Ombi.Store.Entities; using Ombi.Store.Repository; using Ombi.Store.Repository.Requests; +using IdentityResult = Microsoft.AspNetCore.Identity.IdentityResult; using OmbiIdentityResult = Ombi.Models.Identity.IdentityResult; namespace Ombi.Controllers @@ -102,19 +103,34 @@ namespace Ombi.Controllers var result = await UserManager.CreateAsync(userToCreate, user.Password); if (result.Succeeded) { + _log.LogInformation("Created User {0}", userToCreate.UserName); await CreateRoles(); - await UserManager.AddToRoleAsync(userToCreate, OmbiRoles.Admin); + _log.LogInformation("Created the roles"); + var roleResult = await UserManager.AddToRoleAsync(userToCreate, OmbiRoles.Admin); + if (!roleResult.Succeeded) + { + LogErrors(roleResult); + } + else + { + _log.LogInformation("Added the Admin role"); + } } if (!result.Succeeded) { - foreach (var err in result.Errors) - { - _log.LogCritical(err.Description); - } + LogErrors(result); } return result.Succeeded; } + private void LogErrors(IdentityResult result) + { + foreach (var err in result.Errors) + { + _log.LogCritical(err.Description); + } + } + private async Task CreateRoles() { await CreateRole(OmbiRoles.AutoApproveMovie); diff --git a/src/Ombi/Startup.cs b/src/Ombi/Startup.cs index 805d6bab8..a44c60422 100644 --- a/src/Ombi/Startup.cs +++ b/src/Ombi/Startup.cs @@ -173,7 +173,7 @@ namespace Ombi app.UsePathBase(settings.BaseUrl); } - app.UseHangfireServer(new BackgroundJobServerOptions { WorkerCount = 1, ServerTimeout = TimeSpan.FromDays(1)}); + app.UseHangfireServer(new BackgroundJobServerOptions { WorkerCount = 1, ServerTimeout = TimeSpan.FromDays(1), ShutdownTimeout = TimeSpan.FromDays(1)}); app.UseHangfireDashboard(settings.BaseUrl.HasValue() ? $"{settings.BaseUrl}/hangfire" : "/hangfire", new DashboardOptions {