More work on the Updater

pull/1488/head
Jamie.Rees 8 years ago
parent dcf97a1008
commit d9be265abe

@ -0,0 +1,98 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Ombi.Core.Update
{
public class UpdateEngine
{
public async Task Update(UpdateOptions options)
{
if (options.Status == UpdateStatus.UptoDate)
{
// We don't need to update...
return;
}
// Download zip into temp location
var path = await Download(options);
Extract(path);
// TODO Run the Update.exe and pass in the args
}
private void Extract(string path)
{
using (var zip = ZipFile.OpenRead(path))
{
path = Path.GetDirectoryName(path);
foreach (var entry in zip.Entries.Skip(1))
{
var fullname = string.Empty;
if (entry.FullName.Contains("publish/")) // Don't extract the publish folder, we are already in there
{
fullname = entry.FullName.Replace("publish/", string.Empty);
}
var fullPath = Path.Combine(path, fullname);
if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(fullPath);
}
else
{
entry.ExtractToFile(fullPath, true);
Console.WriteLine("Restored {0}", entry.FullName);
}
}
}
}
/// <summary>
/// Downloads the specified zip from the options and returns the zip path.
/// </summary>
/// <param name="options">The options.</param>
/// <returns></returns>
private async Task<string> Download(UpdateOptions options)
{
// Create temp path
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var current = Path.GetDirectoryName(location);
var tempDir = Directory.CreateDirectory(Path.Combine(current, "UpdateTemp"));
var tempZip = Path.Combine(tempDir.FullName, "Ombi.zip");
if (File.Exists(tempZip))
{
return tempZip;
}
using (var httpClient = new HttpClient())
using (var contentStream = await httpClient.GetStreamAsync(options.DownloadUrl))
using (var fileStream = new FileStream(tempZip, FileMode.Create, FileAccess.Write, FileShare.None, 1048576, true))
{
await contentStream.CopyToAsync(fileStream);
}
return tempZip;
}
public UpdateOptions CheckForUpdate()
{
return new UpdateOptions
{
Status = UpdateStatus.Available,
DownloadUrl = "https://ci.appveyor.com/api/buildjobs/tsghsfcaoqin2wbk/artifacts/Ombi_windows.zip",
UpdateDate = DateTime.Now,
Version = "3.0.0"
};
}
}
}

@ -0,0 +1,12 @@
using System;
namespace Ombi.Core.Update
{
public class UpdateOptions
{
public UpdateStatus Status { get; set; }
public string DownloadUrl { get; set; }
public string Version { get; set; }
public DateTime UpdateDate { get; set; }
}
}

@ -0,0 +1,8 @@
namespace Ombi.Core.Update
{
public enum UpdateStatus
{
Available,
UptoDate
}
}

@ -1,5 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text; using System.Text;
namespace Ombi.Updater namespace Ombi.Updater
@ -13,8 +16,49 @@ namespace Ombi.Updater
p.Kill(options.OmbiProcessId); p.Kill(options.OmbiProcessId);
// Make sure the process has been killed
if (p.FindProcessByName("Ombi").Any())
{
// throw
}
MoveFiles(options);
// Start Ombi
StartOmbi(options);
}
private void StartOmbi(StartupOptions options)
{
var start = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = Path.Combine(options.ApplicationPath,"Ombi.exe")
};
using (var proc = new Process { StartInfo = start })
{
proc.Start();
}
Environment.Exit(0);
} }
private void MoveFiles(StartupOptions options)
{
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
location = Path.GetDirectoryName(location);
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(location, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(location, options.ApplicationPath));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(location, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(location, options.ApplicationPath), true);
}
} }
} }

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>

@ -14,6 +14,8 @@ namespace Ombi.Updater
var options = CheckArgs(args); var options = CheckArgs(args);
var install = new Installer();
install.Start(options);
} }
@ -26,12 +28,12 @@ namespace Ombi.Updater
} }
var p = new ProcessProvider(); var p = new ProcessProvider();
var ombiProc = p.FindProcessByName("Ombi").FirstOrDefault().Id; var ombiProc = p.FindProcessByName("Ombi").FirstOrDefault();
return new StartupOptions return new StartupOptions
{ {
ApplicationPath = args[0], ApplicationPath = args[0],
OmbiProcessId = ombiProc OmbiProcessId = ombiProc?.Id ?? -1
}; };
} }
} }

@ -0,0 +1,9 @@
{
"profiles": {
"Ombi.Updater": {
"commandName": "Project",
"commandLineArgs": "C:\\Users\\Jamie.Rees\\Source\\Repos\\PlexRequests.Net\\src\\Ombi\\bin\\Debug\\netcoreapp1.1",
"workingDirectory": "C:\\Users\\Jamie.Rees\\Source\\Repos\\PlexRequests.Net\\src\\Ombi\\bin\\Debug\\netcoreapp1.1\\UpdateTemp"
}
}
}

@ -31,6 +31,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Ombi.Core.Settings; using Ombi.Core.Settings;
using Ombi.Core.Settings.Models; using Ombi.Core.Settings.Models;
using Ombi.Core.Update;
namespace Ombi.Controllers namespace Ombi.Controllers
{ {
@ -70,5 +71,14 @@ namespace Ombi.Controllers
return new { Result = settings?.Wizard ?? false}; return new { Result = settings?.Wizard ?? false};
} }
[AllowAnonymous]
[HttpGet("Update")]
public async Task Update()
{
var u = new UpdateEngine();
var result = u.CheckForUpdate();
await u.Update(result);
}
} }
} }
Loading…
Cancel
Save