1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-29 11:16:19 +00:00
Sonarr/NzbDrone.Core/Update/UpdateService.cs

134 lines
4.9 KiB
C#
Raw Normal View History

2013-04-16 00:08:06 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
2013-05-16 00:16:06 +00:00
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Configuration;
2013-05-16 00:16:06 +00:00
using NzbDrone.Core.Update.Commands;
2013-04-16 00:08:06 +00:00
namespace NzbDrone.Core.Update
{
2013-05-20 00:30:02 +00:00
public interface IUpdateService : IExecute<ApplicationUpdateCommand>
2013-04-16 00:08:06 +00:00
{
Dictionary<DateTime, string> GetUpdateLogFiles();
2013-05-20 00:30:02 +00:00
UpdatePackage AvailableUpdate();
2013-04-16 00:08:06 +00:00
}
2013-05-20 00:30:02 +00:00
public class UpdateService : IUpdateService
2013-04-16 00:08:06 +00:00
{
2013-05-20 00:30:02 +00:00
private readonly IUpdatePackageProvider _updatePackageProvider;
private readonly IEnvironmentProvider _environmentProvider;
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
private readonly IDiskProvider _diskProvider;
private readonly IHttpProvider _httpProvider;
private readonly IConfigFileProvider _configFileProvider;
private readonly ArchiveProvider _archiveProvider;
private readonly IProcessProvider _processProvider;
private readonly Logger _logger;
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
public UpdateService(IUpdatePackageProvider updatePackageProvider, IEnvironmentProvider environmentProvider,
IDiskProvider diskProvider,
IHttpProvider httpProvider, IConfigFileProvider configFileProvider,
ArchiveProvider archiveProvider, IProcessProvider processProvider, Logger logger)
2013-04-16 00:08:06 +00:00
{
2013-05-20 00:30:02 +00:00
_updatePackageProvider = updatePackageProvider;
_environmentProvider = environmentProvider;
_diskProvider = diskProvider;
_httpProvider = httpProvider;
_configFileProvider = configFileProvider;
_archiveProvider = archiveProvider;
_processProvider = processProvider;
_logger = logger;
2013-04-16 00:08:06 +00:00
}
2013-05-20 00:30:02 +00:00
private void InstallUpdate(UpdatePackage updatePackage)
{
var updateSandboxFolder = _environmentProvider.GetUpdateSandboxFolder();
var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName);
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
if (_diskProvider.FolderExists(updateSandboxFolder))
{
_logger.Info("Deleting old update files");
_diskProvider.DeleteFolder(updateSandboxFolder, true);
}
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
_logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
_logger.Info("Download completed for update package from [{0}]", updatePackage.FileName);
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
_logger.Info("Extracting Update package");
_archiveProvider.ExtractArchive(packageDestination, updateSandboxFolder);
_logger.Info("Update package extracted successfully");
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
_logger.Info("Preparing client");
_diskProvider.MoveDirectory(_environmentProvider.GetUpdateClientFolder(),
updateSandboxFolder);
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
_logger.Info("Starting update client");
var startInfo = new ProcessStartInfo
{
FileName = _environmentProvider.GetUpdateClientExePath(),
2013-05-23 05:32:54 +00:00
Arguments = _processProvider.GetCurrentProcess().Id.ToString()
2013-05-20 00:30:02 +00:00
};
var process = _processProvider.Start(startInfo);
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
_processProvider.WaitForExit(process);
_logger.Error("Update process failed");
2013-05-20 00:30:02 +00:00
}
public Dictionary<DateTime, string> GetUpdateLogFiles()
2013-04-16 00:08:06 +00:00
{
2013-05-20 00:30:02 +00:00
var list = new Dictionary<DateTime, string>();
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder()))
2013-04-16 00:08:06 +00:00
{
2013-05-20 00:30:02 +00:00
var provider = CultureInfo.InvariantCulture;
var files =
_diskProvider.GetFiles(_environmentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly)
.ToList();
foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name))
{
list.Add(
DateTime.ParseExact(file.Name.Replace(file.Extension, string.Empty), "yyyy.MM.dd-H-mm", provider),
file.FullName);
}
2013-04-16 00:08:06 +00:00
}
2013-05-20 00:30:02 +00:00
return list;
2013-04-16 00:08:06 +00:00
}
2013-05-20 00:30:02 +00:00
public UpdatePackage AvailableUpdate()
{
var latestAvailable = _updatePackageProvider.GetLatestUpdate();
2013-04-16 00:08:06 +00:00
2013-05-20 00:30:02 +00:00
if (latestAvailable == null || latestAvailable.Version <= _environmentProvider.Version)
{
_logger.Debug("No update available.");
return null;
}
2013-05-16 00:16:06 +00:00
2013-05-20 00:30:02 +00:00
return latestAvailable;
2013-05-16 00:16:06 +00:00
}
2013-05-20 00:30:02 +00:00
public void Execute(ApplicationUpdateCommand message)
{
var latestAvailable = AvailableUpdate();
if (latestAvailable != null)
{
InstallUpdate(latestAvailable);
}
}
2013-05-16 00:16:06 +00:00
}
}