@ -69,40 +69,38 @@ namespace Ombi.Schedule.Jobs.Ombi
public async Task Update ( PerformContext c )
{
Ctx = c ;
Logger . Log Information( "Starting the updater ") ;
Logger . Log Debug( LoggingEvents . Updater , "Starting Update job ") ;
var settings = await Settings . GetSettingsAsync ( ) ;
if ( ! settings . AutoUpdateEnabled )
{
Logger . Log Information( "Auto update is not enabled" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Auto update is not enabled" ) ;
return ;
}
var currentLocation = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) ;
Logger . Log Information( "Path: {0}" , currentLocation ) ;
Logger . Log Debug( LoggingEvents . Updater , "Path: {0}" , currentLocation ) ;
var productVersion = AssemblyHelper . GetRuntimeVersion ( ) ;
Logger . LogInformation ( LoggingEvents . Updater , "Product Version {0}" , productVersion ) ;
Logger . LogInformation ( "Product Version {0}" , productVersion ) ;
Logger . LogDebug ( LoggingEvents . Updater , "Product Version {0}" , productVersion ) ;
try
{
var productArray = GetVersion ( ) ;
var version = productArray [ 0 ] ;
Logger . Log Information( "Version {0}" , version ) ;
Logger . Log Debug( LoggingEvents . Updater , "Version {0}" , version ) ;
var branch = productArray [ 1 ] ;
Logger . Log Information( "Branch Version {0}" , branch ) ;
Logger . Log Debug( LoggingEvents . Updater , "Branch Version {0}" , branch ) ;
Logger . Log Information ( LoggingEvents . Updater , "Version {0}" , version ) ;
Logger . Log Information ( LoggingEvents . Updater , "Branch {0}" , branch ) ;
Logger . Log Debug ( LoggingEvents . Updater , "Version {0}" , version ) ;
Logger . Log Debug ( LoggingEvents . Updater , "Branch {0}" , branch ) ;
Logger . Log Information( "Looking for updates now" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Looking for updates now" ) ;
var updates = await Processor . Process ( branch ) ;
Logger . Log Information( "Updates: {0}" , updates ) ;
Logger . Log Debug( LoggingEvents . Updater , "Updates: {0}" , updates ) ;
var serverVersion = updates . UpdateVersionString ;
Logger . LogInformation ( LoggingEvents . Updater , "Service Version {0}" , updates . UpdateVersionString ) ;
Logger . LogInformation ( "Service Version {0}" , updates . UpdateVersionString ) ;
Logger . LogDebug ( LoggingEvents . Updater , "Service Version {0}" , updates . UpdateVersionString ) ;
if ( ! serverVersion . Equals ( version , StringComparison . CurrentCultureIgnoreCase ) )
{
@ -110,38 +108,37 @@ namespace Ombi.Schedule.Jobs.Ombi
var desc = RuntimeInformation . OSDescription ;
var proce = RuntimeInformation . ProcessArchitecture ;
Logger . LogInformation ( LoggingEvents . Updater , "OS Information: {0} {1}" , desc , proce ) ;
Logger . LogInformation ( "OS Information: {0} {1}" , desc , proce ) ;
Logger . LogDebug ( LoggingEvents . Updater , "OS Information: {0} {1}" , desc , proce ) ;
Downloads download ;
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
Logger . Log Information ( LoggingEvents . Updater , "We are Windows" ) ;
Logger . Log Debug ( LoggingEvents . Updater , "We are Windows" ) ;
download = updates . Downloads . FirstOrDefault ( x = > x . Name . Contains ( "windows.zip" , CompareOptions . IgnoreCase ) ) ;
}
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
{
Logger . Log Information ( LoggingEvents . Updater , "We are OSX" ) ;
Logger . Log Debug ( LoggingEvents . Updater , "We are OSX" ) ;
download = updates . Downloads . FirstOrDefault ( x = > x . Name . Contains ( "osx" , CompareOptions . IgnoreCase ) ) ;
}
else
{
Logger . Log Information ( LoggingEvents . Updater , "We are linux" ) ;
Logger . Log Debug ( LoggingEvents . Updater , "We are linux" ) ;
download = updates . Downloads . FirstOrDefault ( x = > x . Name . Contains ( "linux" , CompareOptions . IgnoreCase ) ) ;
}
if ( download = = null )
{
Logger . Log Information( "There were no downloads" ) ;
Logger . Log Debug( LoggingEvents . Updater , "There were no downloads" ) ;
return ;
}
Logger . Log Information( "Found the download! {0}" , download . Name ) ;
Logger . Log Information( "URL {0}" , download . Url ) ;
Logger . Log Debug( LoggingEvents . Updater , "Found the download! {0}" , download . Name ) ;
Logger . Log Debug( LoggingEvents . Updater , "URL {0}" , download . Url ) ;
Logger . Log Information( "Clearing out Temp Path" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Clearing out Temp Path" ) ;
var tempPath = Path . Combine ( currentLocation , "TempUpdate" ) ;
if ( Directory . Exists ( tempPath ) )
{
D irectory.Delete (tempPath , true ) ;
D eleteD irectory (tempPath ) ;
}
// Temp Path
@ -155,10 +152,10 @@ namespace Ombi.Schedule.Jobs.Ombi
}
// Download it
Logger . Log Information ( LoggingEvents . Updater , "Downloading the file {0} from {1}" , download . Name , download . Url ) ;
Logger . Log Debug ( LoggingEvents . Updater , "Downloading the file {0} from {1}" , download . Name , download . Url ) ;
var extension = download . Name . Split ( '.' ) . Last ( ) ;
var zipDir = Path . Combine ( currentLocation , $"Ombi.{extension}" ) ;
Logger . Log Information( "Zip Dir: {0}" , zipDir ) ;
Logger . Log Debug( LoggingEvents . Updater , "Zip Dir: {0}" , zipDir ) ;
try
{
if ( File . Exists ( zipDir ) )
@ -166,24 +163,24 @@ namespace Ombi.Schedule.Jobs.Ombi
File . Delete ( zipDir ) ;
}
Logger . Log Information( "Starting Download" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Starting Download" ) ;
await DownloadAsync ( download . Url , zipDir , c ) ;
Logger . Log Information( "Finished Download" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Finished Download" ) ;
}
catch ( Exception e )
{
Logger . Log Information( "Error when downloading" ) ;
Logger . Log Information( e . Message ) ;
Logger . Log Debug( LoggingEvents . Updater , "Error when downloading" ) ;
Logger . Log Debug( LoggingEvents . Updater , e . Message ) ;
Logger . LogError ( LoggingEvents . Updater , e , "Error when downloading the zip" ) ;
throw ;
}
// Extract it
Logger . Log Information( "Extracting ZIP" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Extracting ZIP" ) ;
Extract ( zipDir , tempPath ) ;
Logger . Log Information( "Finished Extracting files" ) ;
Logger . Log Information( "Starting the Ombi.Updater process" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Finished Extracting files" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Starting the Ombi.Updater process" ) ;
var updaterExtension = string . Empty ;
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
@ -196,7 +193,7 @@ namespace Ombi.Schedule.Jobs.Ombi
var start = new ProcessStartInfo
{
UseShellExecute = false ,
CreateNoWindow = tru e,
CreateNoWindow = fals e,
FileName = updaterFile ,
Arguments = GetArgs ( settings ) ,
WorkingDirectory = Path . Combine ( Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) , "TempUpdate" ) ,
@ -209,11 +206,12 @@ namespace Ombi.Schedule.Jobs.Ombi
{
start . Password = settings . Password . ToSecureString ( ) ;
}
using ( var proc = new Process { StartInfo = start } )
{
var proc = new Process { StartInfo = start } ;
proc . Start ( ) ;
}
Logger . Log Information( "Bye bye" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Bye bye" ) ;
}
}
catch ( Exception e )
@ -233,7 +231,7 @@ namespace Ombi.Schedule.Jobs.Ombi
var processName = ( settings . ProcessName . HasValue ( ) ? settings . ProcessName : "Ombi" ) ;
var sb = new StringBuilder ( ) ;
sb . Append ( $"--applicationPath \" { currentLocation } \ " --processname \"{processName}\" " ) ;
sb . Append ( $"--applicationPath \" { currentLocation } \ " --processname \"{processName}\" " ) ;
if ( settings . WindowsService )
{
sb . Append ( $"--windowsServiceName \" { settings . WindowsServiceName } \ " " ) ;
@ -273,10 +271,10 @@ namespace Ombi.Schedule.Jobs.Ombi
Logger . LogError ( "Cannot find the file {0}" , scriptToRun ) ;
return ;
}
_processProvider . Start ( scriptToRun , downloadUrl + " " + GetArgs ( settings ) ) ;
Logger . Log Information( "Script started" ) ;
Logger . Log Debug( LoggingEvents . Updater , "Script started" ) ;
}
private void Extract ( string zipDir , string tempPath )
@ -311,7 +309,7 @@ namespace Ombi.Schedule.Jobs.Ombi
public async Task DownloadAsync ( string requestUri , string filename , PerformContext ctx )
{
Logger . LogDebug ( "Starting the DownloadAsync" ) ;
Logger . LogDebug ( LoggingEvents . Updater , "Starting the DownloadAsync" ) ;
using ( var client = new WebClient ( ) )
{
await client . DownloadFileTaskAsync ( requestUri , filename ) ;
@ -337,5 +335,30 @@ namespace Ombi.Schedule.Jobs.Ombi
Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
/// <summary>
/// Depth-first recursive delete, with handling for descendant
/// directories open in Windows Explorer.
/// </summary>
public static void DeleteDirectory ( string path )
{
foreach ( string directory in Directory . GetDirectories ( path ) )
{
DeleteDirectory ( directory ) ;
}
try
{
Directory . Delete ( path , true ) ;
}
catch ( IOException )
{
Directory . Delete ( path , true ) ;
}
catch ( UnauthorizedAccessException )
{
Directory . Delete ( path , true ) ;
}
}
}
}