From ce62606000febbe6022e982a4287a6a4365b837a Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Fri, 22 Sep 2017 10:19:14 +0100 Subject: [PATCH] #1460 Added the Updater, it all seems to be working correctly. #865 --- .../Jobs/Ombi/IOmbiAutomaticUpdater.cs | 2 + .../Jobs/Ombi/OmbiAutomaticUpdater.cs | 34 +++++++++-- .../Settings/Models/UpdateSettings.cs | 7 +++ .../ClientApp/app/interfaces/ISettings.ts | 4 ++ src/Ombi/ClientApp/app/services/index.ts | 1 + .../app/services/settings.service.ts | 11 ++++ .../ClientApp/app/services/update.service.ts | 20 +++++++ .../app/settings/update/update.component.html | 36 ++++++++++++ .../app/settings/update/update.component.ts | 58 +++++++++++++++++++ src/Ombi/Controllers/IdentityController.cs | 1 - src/Ombi/Controllers/JobController.cs | 40 +++++++++++++ src/Ombi/Controllers/SettingsController.cs | 22 +++++++ src/Ombi/Ombi.csproj | 1 - 13 files changed, 229 insertions(+), 8 deletions(-) create mode 100644 src/Ombi.Settings/Settings/Models/UpdateSettings.cs create mode 100644 src/Ombi/ClientApp/app/services/update.service.ts create mode 100644 src/Ombi/ClientApp/app/settings/update/update.component.html create mode 100644 src/Ombi/ClientApp/app/settings/update/update.component.ts create mode 100644 src/Ombi/Controllers/JobController.cs diff --git a/src/Ombi.Schedule/Jobs/Ombi/IOmbiAutomaticUpdater.cs b/src/Ombi.Schedule/Jobs/Ombi/IOmbiAutomaticUpdater.cs index bd007eb92..dea7bb121 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/IOmbiAutomaticUpdater.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/IOmbiAutomaticUpdater.cs @@ -6,5 +6,7 @@ namespace Ombi.Schedule.Ombi public interface IOmbiAutomaticUpdater { Task Update(PerformContext context); + string[] GetVersion(); + Task UpdateAvailable(string branch, string currentVersion); } } \ No newline at end of file diff --git a/src/Ombi.Schedule/Jobs/Ombi/OmbiAutomaticUpdater.cs b/src/Ombi.Schedule/Jobs/Ombi/OmbiAutomaticUpdater.cs index 95c9c43d9..9b6ae998b 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/OmbiAutomaticUpdater.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/OmbiAutomaticUpdater.cs @@ -5,10 +5,8 @@ using System.IO; using System.IO.Compression; using System.Linq; using System.Net; -using System.Net.Http; using System.Reflection; using System.Runtime.InteropServices; -using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; using Hangfire.Console; using Hangfire.Server; @@ -16,29 +14,53 @@ using Microsoft.Extensions.Logging; using Ombi.Api.Service; using Ombi.Api.Service.Models; +using Ombi.Core.Settings; using Ombi.Helpers; using Ombi.Schedule.Ombi; +using Ombi.Settings.Settings.Models; namespace Ombi.Schedule.Jobs.Ombi { public class OmbiAutomaticUpdater : IOmbiAutomaticUpdater { - public OmbiAutomaticUpdater(ILogger log, IOmbiService service) + public OmbiAutomaticUpdater(ILogger log, IOmbiService service, + ISettingsService s) { Logger = log; OmbiService = service; + Settings = s; } private ILogger Logger { get; } private IOmbiService OmbiService { get; } + private ISettingsService Settings { get;s } private static PerformContext Ctx { get; set; } + public string[] GetVersion() + { + var productVersion = AssemblyHelper.GetRuntimeVersion(); + var productArray = productVersion.Split('-'); + return productArray; + } + public async Task UpdateAvailable(string branch, string currentVersion) + { + var updates = await OmbiService.GetUpdates(branch); + var serverVersion = updates.UpdateVersionString; + return !serverVersion.Equals(currentVersion, StringComparison.CurrentCultureIgnoreCase); + } + public async Task Update(PerformContext c) { Ctx = c; Ctx.WriteLine("Starting the updater"); - // IF AutoUpdateEnabled => - // ELSE Return; + + var settings = await Settings.GetSettingsAsync(); + if (!settings.AutoUpdateEnabled) + { + Ctx.WriteLine("Auto update is not enabled"); + return; + } + var currentLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); Ctx.WriteLine("Path: {0}", currentLocation); @@ -48,7 +70,7 @@ namespace Ombi.Schedule.Jobs.Ombi try { - var productArray = productVersion.Split('-'); + var productArray = GetVersion(); var version = productArray[0]; Ctx.WriteLine("Version {0}", version); var branch = productArray[1]; diff --git a/src/Ombi.Settings/Settings/Models/UpdateSettings.cs b/src/Ombi.Settings/Settings/Models/UpdateSettings.cs new file mode 100644 index 000000000..ccf4d2f86 --- /dev/null +++ b/src/Ombi.Settings/Settings/Models/UpdateSettings.cs @@ -0,0 +1,7 @@ +namespace Ombi.Settings.Settings.Models +{ + public class UpdateSettings : Settings + { + public bool AutoUpdateEnabled { get; set; } + } +} \ No newline at end of file diff --git a/src/Ombi/ClientApp/app/interfaces/ISettings.ts b/src/Ombi/ClientApp/app/interfaces/ISettings.ts index 4a4bf2ce5..f9dc1e884 100644 --- a/src/Ombi/ClientApp/app/interfaces/ISettings.ts +++ b/src/Ombi/ClientApp/app/interfaces/ISettings.ts @@ -17,6 +17,10 @@ export interface IOmbiSettings extends ISettings { allowExternalUsersToAuthenticate: boolean; } +export interface IUpdateSettings extends ISettings { + autoUpdateEnabled: boolean; +} + export interface IEmbySettings extends ISettings { enable: boolean; servers: IEmbyServer[]; diff --git a/src/Ombi/ClientApp/app/services/index.ts b/src/Ombi/ClientApp/app/services/index.ts index 95b9020cc..56545e3ec 100644 --- a/src/Ombi/ClientApp/app/services/index.ts +++ b/src/Ombi/ClientApp/app/services/index.ts @@ -9,3 +9,4 @@ export * from "./search.service"; export * from "./service.helpers"; export * from "./settings.service"; export * from "./status.service"; +export * from "./update.service"; diff --git a/src/Ombi/ClientApp/app/services/settings.service.ts b/src/Ombi/ClientApp/app/services/settings.service.ts index d1523468e..35dacb03c 100644 --- a/src/Ombi/ClientApp/app/services/settings.service.ts +++ b/src/Ombi/ClientApp/app/services/settings.service.ts @@ -18,6 +18,7 @@ import { IRadarrSettings, ISlackNotificationSettings, ISonarrSettings, + IUpdateSettings } from "../interfaces"; import { ServiceAuthHelpers } from "./service.helpers"; @@ -166,4 +167,14 @@ export class SettingsService extends ServiceAuthHelpers { .post(`${this.url}/notifications/slack`, JSON.stringify(settings), { headers: this.headers }) .map(this.extractData).catch(this.handleError); } + + public getUpdateSettings(): Observable { + return this.httpAuth.get(`${this.url}/update`).map(this.extractData).catch(this.handleError); + } + + public saveUpdateSettings(settings: IUpdateSettings): Observable { + return this.httpAuth + .post(`${this.url}/update`, JSON.stringify(settings), { headers: this.headers }) + .map(this.extractData).catch(this.handleError); + } } diff --git a/src/Ombi/ClientApp/app/services/update.service.ts b/src/Ombi/ClientApp/app/services/update.service.ts new file mode 100644 index 000000000..68fe42f28 --- /dev/null +++ b/src/Ombi/ClientApp/app/services/update.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from "@angular/core"; +import { Http } from "@angular/http"; +import { AuthHttp } from "angular2-jwt"; +import { Observable } from "rxjs/Rx"; + +import { ServiceAuthHelpers } from "./service.helpers"; + +@Injectable() +export class UpdateService extends ServiceAuthHelpers { + constructor(http: AuthHttp, private regularHttp: Http) { + super(http, "/api/v1/Jobs/"); + } + public forceUpdate(): Observable { + return this.regularHttp.post(`${this.url}update/`, { headers: this.headers }).map(this.extractData); + } + + public checkForNewUpdate(): Observable { + return this.http.get(`${this.url}update/`).map(this.extractData); + } +} diff --git a/src/Ombi/ClientApp/app/settings/update/update.component.html b/src/Ombi/ClientApp/app/settings/update/update.component.html new file mode 100644 index 000000000..2332940ec --- /dev/null +++ b/src/Ombi/ClientApp/app/settings/update/update.component.html @@ -0,0 +1,36 @@ + + +
+
+ Update Settings + +
+
+
+
+ + +
+
+ +
+
+ +
+
+
+
+ +
+
+ + +
+
+ +
+
+
+
+
+
diff --git a/src/Ombi/ClientApp/app/settings/update/update.component.ts b/src/Ombi/ClientApp/app/settings/update/update.component.ts new file mode 100644 index 000000000..aeaa7c0c5 --- /dev/null +++ b/src/Ombi/ClientApp/app/settings/update/update.component.ts @@ -0,0 +1,58 @@ +import { Component, OnInit } from "@angular/core"; +import { FormBuilder, FormGroup } from "@angular/forms"; + +import { NotificationService } from "../../services"; +import { SettingsService, UpdateService } from "../../services"; + +@Component({ + templateUrl: "./update.component.html", +}) +export class UpdateComponent implements OnInit { + + public form: FormGroup; + public updateAvailable = false; + + constructor(private settingsService: SettingsService, + private notificationService: NotificationService, + private updateService: UpdateService, + private fb: FormBuilder) { } + + public ngOnInit() { + + this.settingsService.getUpdateSettings() + .subscribe(x => { + this.form = this.fb.group({ + autoUpdateEnabled: [x.autoUpdateEnabled], + }); + }); + } + + public checkForUpdate() { + this.updateService.checkForNewUpdate().subscribe(x => { + if (x) { + this.updateAvailable = true; + this.notificationService.success("Update", "There is a new update available"); + } + }); + } + + public update() { + this.updateService.forceUpdate().subscribe(); + this.notificationService.success("Update", "We triggered the update job"); + } + + public onSubmit(form: FormGroup) { + if (form.invalid) { + this.notificationService.error("Validation", "Please check your entered values"); + return; + } + this.settingsService.saveUpdateSettings(form.value) + .subscribe(x => { + if (x) { + this.notificationService.success("Settings Saved", "Successfully saved Update settings"); + } else { + this.notificationService.error("Settings Saved", "There was an error when saving the Update settings"); + } + }); + } +} diff --git a/src/Ombi/Controllers/IdentityController.cs b/src/Ombi/Controllers/IdentityController.cs index 2a767d4c7..c234c3186 100644 --- a/src/Ombi/Controllers/IdentityController.cs +++ b/src/Ombi/Controllers/IdentityController.cs @@ -32,7 +32,6 @@ namespace Ombi.Controllers /// /// The Identity Controller, the API for everything Identity/User related /// - /// [PowerUser] [ApiV1] [Produces("application/json")] diff --git a/src/Ombi/Controllers/JobController.cs b/src/Ombi/Controllers/JobController.cs new file mode 100644 index 000000000..2b2c33052 --- /dev/null +++ b/src/Ombi/Controllers/JobController.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using Hangfire; +using Microsoft.AspNetCore.Mvc; +using Ombi.Api.Service; +using Ombi.Attributes; +using Ombi.Schedule.Ombi; + +namespace Ombi.Controllers +{ + [ApiV1] + [Admin] + [Produces("application/json")] + public class JobController : Controller + { + public JobController(IOmbiAutomaticUpdater updater) + { + _updater = updater; + } + + private readonly IOmbiAutomaticUpdater _updater; + + [HttpPost("update")] + public bool ForceUpdate() + { + BackgroundJob.Enqueue(() => _updater.Update(null)); + return true; + } + + [HttpGet("update")] + public async Task CheckForUpdate() + { + var productArray = _updater.GetVersion(); + var version = productArray[0]; + var branch = productArray[1]; + var updateAvailable = await _updater.UpdateAvailable(branch, version); + + return updateAvailable; + } + } +} \ No newline at end of file diff --git a/src/Ombi/Controllers/SettingsController.cs b/src/Ombi/Controllers/SettingsController.cs index c4b6a9bb6..9caa3b719 100644 --- a/src/Ombi/Controllers/SettingsController.cs +++ b/src/Ombi/Controllers/SettingsController.cs @@ -266,6 +266,28 @@ namespace Ombi.Controllers return result; } + /// + /// Save the Update settings. + /// + /// The settings. + /// + [HttpPost("Update")] + public async Task UpdateSettings([FromBody]UpdateSettings settings) + { + return await Save(settings); + } + + /// + /// Gets the Update Settings. + /// + /// + [HttpGet("Update")] + public async Task UpdateSettings() + { + return await Get(); + } + + /// /// Saves the email notification settings. /// diff --git a/src/Ombi/Ombi.csproj b/src/Ombi/Ombi.csproj index 02a7cad66..545926557 100644 --- a/src/Ombi/Ombi.csproj +++ b/src/Ombi/Ombi.csproj @@ -69,7 +69,6 @@ -