jellyfin-server/MediaBrowser.Server.Startup.Common/Migrations/UpdateLevelMigration.cs

131 lines
4.5 KiB
C#
Raw Normal View History

2016-08-22 18:28:24 +00:00
using System;
2016-09-05 05:39:14 +00:00
using System.Collections.Generic;
2016-08-22 18:28:24 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Implementations.Updates;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
2016-09-17 06:08:13 +00:00
using MediaBrowser.Model.Logging;
2016-08-22 18:28:24 +00:00
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Updates;
namespace MediaBrowser.Server.Startup.Common.Migrations
{
public class UpdateLevelMigration : IVersionMigration
{
private readonly IServerConfigurationManager _config;
private readonly IServerApplicationHost _appHost;
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
private readonly string _releaseAssetFilename;
2016-09-17 06:08:13 +00:00
private readonly ILogger _logger;
2016-08-22 18:28:24 +00:00
2016-09-17 06:08:13 +00:00
public UpdateLevelMigration(IServerConfigurationManager config, IServerApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, string releaseAssetFilename, ILogger logger)
2016-08-22 18:28:24 +00:00
{
_config = config;
_appHost = appHost;
_httpClient = httpClient;
_jsonSerializer = jsonSerializer;
_releaseAssetFilename = releaseAssetFilename;
2016-09-17 06:08:13 +00:00
_logger = logger;
2016-08-22 18:28:24 +00:00
}
2016-09-17 06:08:13 +00:00
public async Task Run()
2016-08-22 18:28:24 +00:00
{
var lastVersion = _config.Configuration.LastVersion;
var currentVersion = _appHost.ApplicationVersion;
if (string.Equals(lastVersion, currentVersion.ToString(), StringComparison.OrdinalIgnoreCase))
{
return;
}
try
{
var updateLevel = _config.Configuration.SystemUpdateLevel;
await CheckVersion(currentVersion, updateLevel, CancellationToken.None).ConfigureAwait(false);
}
2016-09-17 06:08:13 +00:00
catch (Exception ex)
2016-08-22 18:28:24 +00:00
{
2016-09-17 06:08:13 +00:00
_logger.ErrorException("Error in update migration", ex);
2016-08-22 18:28:24 +00:00
}
}
2016-09-05 05:39:14 +00:00
private async Task CheckVersion(Version currentVersion, PackageVersionClass currentUpdateLevel, CancellationToken cancellationToken)
2016-08-22 18:28:24 +00:00
{
2016-09-09 06:59:23 +00:00
var releases = await new GithubUpdater(_httpClient, _jsonSerializer)
2016-08-25 21:30:35 +00:00
.GetLatestReleases("MediaBrowser", "Emby", _releaseAssetFilename, cancellationToken).ConfigureAwait(false);
2016-08-22 18:28:24 +00:00
2016-09-05 05:39:14 +00:00
var newUpdateLevel = GetNewUpdateLevel(currentVersion, currentUpdateLevel, releases);
if (newUpdateLevel != currentUpdateLevel)
{
_config.Configuration.SystemUpdateLevel = newUpdateLevel;
_config.SaveConfiguration();
}
}
private PackageVersionClass GetNewUpdateLevel(Version currentVersion, PackageVersionClass currentUpdateLevel, List<GithubUpdater.RootObject> releases)
{
var newUpdateLevel = currentUpdateLevel;
2016-08-22 18:28:24 +00:00
2016-08-26 17:24:04 +00:00
// If the current version is later than current stable, set the update level to beta
if (releases.Count >= 1)
2016-08-22 18:28:24 +00:00
{
2016-08-26 17:24:04 +00:00
var release = releases[0];
2016-08-26 20:37:19 +00:00
var version = ParseVersion(release.tag_name);
2016-09-05 05:39:14 +00:00
if (version != null)
2016-08-25 21:30:35 +00:00
{
2016-09-05 05:39:14 +00:00
if (currentVersion > version)
{
newUpdateLevel = PackageVersionClass.Beta;
}
else
{
return PackageVersionClass.Release;
}
2016-08-25 21:30:35 +00:00
}
2016-08-22 18:28:24 +00:00
}
2016-08-25 21:30:35 +00:00
2016-08-26 17:24:04 +00:00
// If the current version is later than current beta, set the update level to dev
if (releases.Count >= 2)
2016-08-22 18:28:24 +00:00
{
2016-08-26 17:24:04 +00:00
var release = releases[1];
2016-08-26 20:37:19 +00:00
var version = ParseVersion(release.tag_name);
2016-09-05 05:39:14 +00:00
if (version != null)
2016-08-25 21:30:35 +00:00
{
2016-09-05 05:39:14 +00:00
if (currentVersion > version)
{
newUpdateLevel = PackageVersionClass.Dev;
}
else
{
return PackageVersionClass.Beta;
}
2016-08-25 21:30:35 +00:00
}
2016-08-22 18:28:24 +00:00
}
2016-08-25 21:30:35 +00:00
2016-09-05 05:39:14 +00:00
return newUpdateLevel;
2016-08-22 18:28:24 +00:00
}
2016-08-26 20:37:19 +00:00
private Version ParseVersion(string versionString)
{
2016-09-17 06:08:13 +00:00
if (!string.IsNullOrWhiteSpace(versionString))
2016-08-26 20:37:19 +00:00
{
2016-09-17 06:08:13 +00:00
var parts = versionString.Split('.');
if (parts.Length == 3)
{
versionString += ".0";
}
2016-08-26 20:37:19 +00:00
}
Version version;
Version.TryParse(versionString, out version);
return version;
}
2016-08-22 18:28:24 +00:00
}
}