First pass of the updater working. #29

pull/226/head
tidusjar 9 years ago
parent eafa0486f8
commit 030c013862

@ -1,8 +1,7 @@
#region Copyright #region Copyright
// /************************************************************************ // /************************************************************************
// Copyright (c) 2016 Jamie Rees // Copyright (c) 2016 Jamie Rees
// File: CouchPotatoApi.cs // File: SickrageApi.cs
// Created By: Jamie Rees // Created By: Jamie Rees
// //
// Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
@ -24,31 +23,28 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
using Polly;
#endregion #endregion
using System.Linq;
using System; using System;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog; using NLog;
using PlexRequests.Api.Interfaces; using PlexRequests.Api.Interfaces;
using PlexRequests.Api.Models.SickRage; using PlexRequests.Api.Models.SickRage;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using RestSharp;
using Newtonsoft.Json.Linq;
using PlexRequests.Helpers.Exceptions; using PlexRequests.Helpers.Exceptions;
using RestSharp;
namespace PlexRequests.Api namespace PlexRequests.Api
{ {
public class SickrageApi : ISickRageApi public class SickrageApi : ISickRageApi
{ {
private static Logger Log = LogManager.GetCurrentClassLogger(); private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public SickrageApi() public SickrageApi()
{ {
@ -57,22 +53,38 @@ namespace PlexRequests.Api
private ApiRequest Api { get; } private ApiRequest Api { get; }
public SickRageSeasonList VerifyShowHasLoaded(int tvdbId, string apiKey, Uri baseUrl)
public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] seasons, string quality, string apiKey,
Uri baseUrl)
{ {
Log.Trace("Entered `VerifyShowHasLoaded({0} <- id)`", tvdbId);
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.seasonlist", Method = Method.GET };
request.AddUrlSegment("apiKey", apiKey);
request.AddQueryParameter("tvdbid", tvdbId.ToString());
try
{
var policy = RetryHandler.RetryAndWaitPolicy(
null,
(exception, timespan) => Log.Error(exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan));
var obj = policy.Execute(() => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl));
return obj;
}
catch (Exception e)
{
Log.Error(e);
return new SickRageSeasonList();
}
}
public async Task<SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] seasons, string quality, string apiKey, Uri baseUrl)
{
var futureStatus = seasons.Length > 0 && !seasons.Any(x => x == seasonCount) ? SickRageStatus.Skipped : SickRageStatus.Wanted; var futureStatus = seasons.Length > 0 && !seasons.Any(x => x == seasonCount) ? SickRageStatus.Skipped : SickRageStatus.Wanted;
var status = seasons.Length > 0 ? SickRageStatus.Skipped : SickRageStatus.Wanted; var status = seasons.Length > 0 ? SickRageStatus.Skipped : SickRageStatus.Wanted;
Log.Trace("Future Status: {0}", futureStatus); Log.Trace("Future Status: {0}", futureStatus);
Log.Trace("Current Status: {0}", status); Log.Trace("Current Status: {0}", status);
var request = new RestRequest var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.addnew", Method = Method.GET };
{
Resource = "/api/{apiKey}/?cmd=show.addnew",
Method = Method.GET
};
request.AddUrlSegment("apiKey", apiKey); request.AddUrlSegment("apiKey", apiKey);
request.AddQueryParameter("tvdbid", tvdbId.ToString()); request.AddQueryParameter("tvdbid", tvdbId.ToString());
request.AddQueryParameter("status", status); request.AddQueryParameter("status", status);
@ -83,10 +95,11 @@ namespace PlexRequests.Api
request.AddQueryParameter("initial", quality); request.AddQueryParameter("initial", quality);
} }
var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan)); var policy = RetryHandler.RetryAndWaitPolicy(
null,
(exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan));
var obj = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
var obj = policy.Execute( () => Api.Execute<SickRageTvAdd>(request, baseUrl));
Log.Trace("obj Result:"); Log.Trace("obj Result:");
Log.Trace(obj.DumpJson()); Log.Trace(obj.DumpJson());
@ -134,7 +147,7 @@ namespace PlexRequests.Api
{ {
Log.Trace("Adding season {0}", s); Log.Trace("Adding season {0}", s);
var result = await AddSeason(tvdbId, s, apiKey, baseUrl); var result = await AddSeason(tvdbId, s, apiKey, baseUrl);
Log.Trace("SickRage adding season results: "); Log.Trace("SickRage adding season results: ");
Log.Trace(result.DumpJson()); Log.Trace(result.DumpJson());
} }
@ -153,105 +166,61 @@ namespace PlexRequests.Api
public SickRagePing Ping(string apiKey, Uri baseUrl) public SickRagePing Ping(string apiKey, Uri baseUrl)
{ {
var request = new RestRequest var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=sb.ping", Method = Method.GET };
{
Resource = "/api/{apiKey}/?cmd=sb.ping",
Method = Method.GET
};
var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) => Log.Error (exception, "Exception when calling Ping for SR, Retrying {0}", timespan));
var policy = RetryHandler.RetryAndWaitPolicy(
null,
(exception, timespan) => Log.Error(exception, "Exception when calling Ping for SR, Retrying {0}", timespan));
request.AddUrlSegment("apiKey", apiKey); request.AddUrlSegment("apiKey", apiKey);
var obj = policy.Execute( () => Api.ExecuteJson<SickRagePing>(request, baseUrl)); var obj = policy.Execute(() => Api.ExecuteJson<SickRagePing>(request, baseUrl));
return obj; return obj;
} }
public SickRageSeasonList VerifyShowHasLoaded(int tvdbId, string apiKey, Uri baseUrl)
{
Log.Trace("Entered `VerifyShowHasLoaded({0} <- id)`", tvdbId);
var request = new RestRequest
{
Resource = "/api/{apiKey}/?cmd=show.seasonlist",
Method = Method.GET
};
request.AddUrlSegment("apiKey", apiKey);
request.AddQueryParameter("tvdbid", tvdbId.ToString());
try
{
var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) =>
Log.Error (exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan));
var obj = policy.Execute( () => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl));
return obj;
}
catch (Exception e)
{
Log.Error(e);
return new SickRageSeasonList();
}
}
public async Task<SickRageTvAdd> AddSeason(int tvdbId, int season, string apiKey, Uri baseUrl) public async Task<SickRageTvAdd> AddSeason(int tvdbId, int season, string apiKey, Uri baseUrl)
{ {
var request = new RestRequest var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=episode.setstatus", Method = Method.GET };
{
Resource = "/api/{apiKey}/?cmd=episode.setstatus",
Method = Method.GET
};
request.AddUrlSegment("apiKey", apiKey); request.AddUrlSegment("apiKey", apiKey);
request.AddQueryParameter("tvdbid", tvdbId.ToString()); request.AddQueryParameter("tvdbid", tvdbId.ToString());
request.AddQueryParameter("season", season.ToString()); request.AddQueryParameter("season", season.ToString());
request.AddQueryParameter("status", SickRageStatus.Wanted); request.AddQueryParameter("status", SickRageStatus.Wanted);
await Task.Run(() => Thread.Sleep(2000)); await Task.Run(() => Thread.Sleep(2000));
return await Task.Run(() => return await Task.Run(
{ () =>
{
var policy = RetryHandler.RetryAndWaitPolicy (null,(exception, timespan) => var policy = RetryHandler.RetryAndWaitPolicy(
Log.Error (exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan)); null,
(exception, timespan) => Log.Error(exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan));
var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl)); var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
return result; return result;
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
public async Task<SickrageShows> GetShows(string apiKey, Uri baseUrl) public async Task<SickrageShows> GetShows(string apiKey, Uri baseUrl)
{ {
var request = new RestRequest var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=shows", Method = Method.GET };
{
Resource = "/api/{apiKey}/?cmd=shows",
Method = Method.GET
};
request.AddUrlSegment("apiKey", apiKey); request.AddUrlSegment("apiKey", apiKey);
return await Task.Run( return await Task.Run(
() => () =>
{ {
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] { var policy = RetryHandler.RetryAndWaitPolicy(
TimeSpan.FromSeconds (5), new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(30) },
TimeSpan.FromSeconds(10), (exception, timespan) => Log.Error(exception, "Exception when calling GetShows for SR, Retrying {0}", timespan));
TimeSpan.FromSeconds(30)
}, (exception, timespan) =>
Log.Error (exception, "Exception when calling GetShows for SR, Retrying {0}", timespan));
return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl));
return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl));
} }
catch (ApiRequestException) catch (ApiRequestException)
{ {
Log.Error("There has been a API exception when Getting the Sickrage shows"); Log.Error("There has been a API exception when Getting the Sickrage shows");
return null; return null;
} }
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
} }

@ -420,6 +420,10 @@
<Project>{92433867-2B7B-477B-A566-96C382427525}</Project> <Project>{92433867-2B7B-477B-A566-96C382427525}</Project>
<Name>PlexRequests.Store</Name> <Name>PlexRequests.Store</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\PlexRequests.Updater\PlexRequests.Updater.csproj">
<Project>{ebe6fc1c-7b4b-47e9-af54-0ee0604a2be5}</Project>
<Name>PlexRequests.Updater</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Content\search.js"> <Content Include="Content\search.js">

@ -61,6 +61,9 @@ namespace PlexRequests.UI
x => x.Port, x => x.Port,
e => -1); e => -1);
var updated = result.MapResult(x => x.Updated, e => false);
//TODO
PrintToConsole("Starting Up! Please wait, this can usually take a few seconds.", ConsoleColor.Yellow); PrintToConsole("Starting Up! Please wait, this can usually take a few seconds.", ConsoleColor.Yellow);
Log.Trace("Getting product version"); Log.Trace("Getting product version");

@ -24,7 +24,6 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System.Text;
using CommandLine; using CommandLine;
@ -51,6 +50,14 @@ namespace PlexRequests.UI.Start
public int Port { get; set; } public int Port { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="StartupOptions"/> is updated.
/// </summary>
/// <value>
/// <c>true</c> if updated; otherwise, <c>false</c>.
/// </value>
[Option('u', "updated", Required = false, HelpText = "This should only be used by the internal application")]
public bool Updated { get; set; }
} }
} }

@ -31,6 +31,7 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.IO.Compression" /> <Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" /> <Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Windows.Forms" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
@ -50,5 +51,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -7,7 +7,8 @@ namespace PlexRequests.Updater
public static void Main (string[] args) public static void Main (string[] args)
{ {
Console.WriteLine ("Starting PlexRequests .Net updater"); Console.WriteLine ("Starting PlexRequests .Net updater");
var s = new Updater();
s.Start(args[0]);
} }
} }
} }

@ -1,42 +1,201 @@
using System; #region Copyright
using PlexRequests.Core; // /************************************************************************
using System.Net; // Copyright (c) 2016 Jamie Rees
// File: Updater.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Net;
using System.Windows.Forms;
namespace PlexRequests.Updater namespace PlexRequests.Updater
{ {
public class Updater public class Updater
{ {
public void Start(){ private string BackupPath { get; set; }
var c = new StatusChecker (); private bool Error { get; set; }
private string TempPath { get; set; }
try {
public void RestoreBackup()
var release = c.GetStatus (); {
if(!release.UpdateAvailable) Console.WriteLine("Update failed, restoring backup");
{ using (var archive = ZipFile.OpenRead(BackupPath))
Console.WriteLine ("No Update availble, shutting down"); {
} foreach (var entry in archive.Entries)
{
using(var client = new WebClient()) var fullPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)), entry.FullName);
using(var ms = new MemoryStream(client.DownloadData(release.DownloadUri), false))
using(var gz = new GZipStream(ms, CompressionLevel.Optimal)) if (string.IsNullOrEmpty(entry.Name))
{ {
// TODO decompress stream Directory.CreateDirectory(fullPath);
} }
else
{
} catch (Exception ex) { if (entry.Name.Contains("PlexRequests.Updater"))
{
Console.WriteLine (ex.Message); entry.ExtractToFile(fullPath + "_Updated", true);
Console.WriteLine ("Oops... Looks like we cannot update!"); continue;
Console.ReadLine (); }
}
} entry.ExtractToFile(fullPath, true);
Console.WriteLine("Update failed, restoring backup");
}
}
} }
} }
public void Start(string downloadPath)
{
try
{
BackupCurrentVersion();
var dir = CreateTempPath();
TempPath = Path.Combine(dir.FullName, "PlexRequestsUpdate.zip");
CheckAndDelete(TempPath);
Console.WriteLine("Downloading new version");
using (var client = new WebClient())
{
client.DownloadFile(downloadPath, TempPath);
}
Console.WriteLine("Downloaded!");
// Replace files
using (var archive = ZipFile.OpenRead(TempPath))
{
foreach (var entry in archive.Entries)
{
var fullname = string.Empty;
if (entry.FullName.Contains("Release/")) // Don't extract the release folder, we are already in there
{
fullname = entry.FullName.Replace("Release/", string.Empty);
}
var fullPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)), fullname);
if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(fullPath);
}
else
{
if (entry.Name.Contains("PlexRequests.Updater"))
{
entry.ExtractToFile(fullPath + "_Updated", true);
continue;
}
entry.ExtractToFile(fullPath, true);
Console.WriteLine("Restored {0}", entry.FullName);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Oops... Looks like we cannot update!");
Console.ReadLine();
Error = true;
}
finally
{
File.Delete(TempPath);
if (Error)
{
RestoreBackup();
}
FinishUpdate();
}
}
private void BackupCurrentVersion()
{
Console.WriteLine("Backing up the current version");
try
{
var dir = Directory.CreateDirectory("BackupSystem");
var applicationPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath));
var allfiles = Directory.GetFiles(applicationPath, "*.*", SearchOption.AllDirectories);
BackupPath = Path.Combine(dir.FullName, "PlexRequestsBackup.zip");
CheckAndDelete(BackupPath);
using (var fileStream = new FileStream(BackupPath, FileMode.CreateNew))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
{
foreach (var file in allfiles)
{
if (file.Contains("BackupSystem"))
continue;
var info = Path.GetFileName(file);
archive.CreateEntryFromFile(file, info);
}
}
Console.WriteLine("All backed up!");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine();
Environment.Exit(1);
}
}
private void CheckAndDelete(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
private DirectoryInfo CreateTempPath()
{
try
{
return Directory.CreateDirectory("UpdateTemp");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine();
Environment.Exit(1);
return null;
}
}
private void FinishUpdate()
{
var startInfo = new ProcessStartInfo("PlexRequests.exe") { Arguments = Error ? "-u false" : "-u true" };
Process.Start(startInfo);
Environment.Exit(0);
}
}
}

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Polly-Signed" version="4.2.0" targetFramework="net45" />
</packages>
Loading…
Cancel
Save