Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallHelper.cs
T

193 lines
7.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
2014-03-04 11:16:42 +11:00
using System.Linq;
using System.Net.Http;
2014-03-04 11:16:42 +11:00
using System.Web;
using LightInject;
2018-03-27 10:04:07 +02:00
using Semver;
2014-03-04 11:16:42 +11:00
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
2017-12-18 18:26:32 +01:00
using Umbraco.Core.Migrations.Install;
2016-12-16 17:56:10 +01:00
using Umbraco.Core.Persistence;
2016-04-12 19:55:50 +02:00
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
using Umbraco.Web.Cache;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.Install.InstallSteps;
using Umbraco.Web.Install.Models;
namespace Umbraco.Web.Install
{
public sealed class InstallHelper
{
2016-11-29 10:31:25 +01:00
private readonly DatabaseBuilder _databaseBuilder;
2016-09-01 19:06:08 +02:00
private readonly HttpContextBase _httpContext;
private readonly ILogger _logger;
private readonly IGlobalSettings _globalSettings;
private InstallationType? _installationType;
public InstallHelper(UmbracoContext umbracoContext,
DatabaseBuilder databaseBuilder,
ILogger logger, IGlobalSettings globalSettings)
{
2016-09-01 19:06:08 +02:00
_httpContext = umbracoContext.HttpContext;
_logger = logger;
_globalSettings = globalSettings;
2016-11-29 10:31:25 +01:00
_databaseBuilder = databaseBuilder;
}
2014-02-27 10:16:30 +01:00
public InstallationType GetInstallationType()
{
return _installationType ?? (_installationType = IsBrandNewInstall ? InstallationType.NewInstall : InstallationType.Upgrade).Value;
}
2016-06-09 11:57:13 +02:00
internal static void DeleteLegacyInstaller()
{
if (Directory.Exists(IOHelper.MapPath(SystemDirectories.Install)))
{
if (Directory.Exists(IOHelper.MapPath("~/app_data/temp/install_backup")))
{
//this means the backup already exists with files but there's no files in it, so we'll delete the backup and re-run it
if (Directory.GetFiles(IOHelper.MapPath("~/app_data/temp/install_backup")).Any() == false)
{
Directory.Delete(IOHelper.MapPath("~/app_data/temp/install_backup"), true);
Directory.Move(IOHelper.MapPath(SystemDirectories.Install), IOHelper.MapPath("~/app_data/temp/install_backup"));
}
}
else
{
Directory.Move(IOHelper.MapPath(SystemDirectories.Install), IOHelper.MapPath("~/app_data/temp/install_backup"));
}
}
if (Directory.Exists(IOHelper.MapPath("~/Areas/UmbracoInstall")))
{
Directory.Delete(IOHelper.MapPath("~/Areas/UmbracoInstall"), true);
}
}
internal void InstallStatus(bool isCompleted, string errorMsg)
{
try
{
2016-09-01 19:06:08 +02:00
var userAgent = _httpContext.Request.UserAgent;
// Check for current install Id
var installId = Guid.NewGuid();
var installCookie = _httpContext.Request.GetCookieValue(Constants.Web.InstallerCookieName);
if (string.IsNullOrEmpty(installCookie) == false)
{
if (Guid.TryParse(installCookie, out installId))
{
// check that it's a valid Guid
if (installId == Guid.Empty)
installId = Guid.NewGuid();
}
}
_httpContext.Response.Cookies.Set(new HttpCookie(Constants.Web.InstallerCookieName, "1"));
2017-05-12 14:49:44 +02:00
2016-09-01 19:06:08 +02:00
var dbProvider = string.Empty;
if (IsBrandNewInstall == false)
2016-04-12 19:55:50 +02:00
{
// we don't have DatabaseProvider anymore... doing it differently
//dbProvider = ApplicationContext.Current.DatabaseContext.DatabaseProvider.ToString();
2017-09-22 18:28:21 +02:00
dbProvider = GetDbProviderString(Current.SqlContext);
2016-04-12 19:55:50 +02:00
}
2016-09-01 19:06:08 +02:00
var check = new org.umbraco.update.CheckForUpgrade();
check.Install(installId,
IsBrandNewInstall == false,
isCompleted,
DateTime.Now,
UmbracoVersion.Current.Major,
UmbracoVersion.Current.Minor,
UmbracoVersion.Current.Build,
UmbracoVersion.CurrentComment,
errorMsg,
userAgent,
dbProvider);
}
catch (Exception ex)
{
_logger.Error<InstallHelper>("An error occurred in InstallStatus trying to check upgrades", ex);
}
}
2017-09-22 18:28:21 +02:00
internal static string GetDbProviderString(ISqlContext sqlContext)
2016-04-12 19:55:50 +02:00
{
var dbProvider = string.Empty;
// we don't have DatabaseProvider anymore...
//dbProvider = ApplicationContext.Current.DatabaseContext.DatabaseProvider.ToString();
//
// doing it differently
2017-09-22 18:28:21 +02:00
var syntax = sqlContext.SqlSyntax;
2016-04-12 19:55:50 +02:00
if (syntax is SqlCeSyntaxProvider)
dbProvider = "SqlServerCE";
else if (syntax is MySqlSyntaxProvider)
dbProvider = "MySql";
else if (syntax is SqlServerSyntaxProvider)
dbProvider = (syntax as SqlServerSyntaxProvider).ServerVersion.IsAzure ? "SqlAzure" : "SqlServer";
return dbProvider;
}
/// <summary>
/// Checks if this is a brand new install meaning that there is no configured version and there is no configured database connection
/// </summary>
private bool IsBrandNewInstall
{
get
{
2017-05-12 14:49:44 +02:00
var databaseSettings = ConfigurationManager.ConnectionStrings[Constants.System.UmbracoConnectionName];
if (_globalSettings.ConfigurationStatus.IsNullOrWhiteSpace()
2016-11-29 10:31:25 +01:00
&& _databaseBuilder.IsConnectionStringConfigured(databaseSettings) == false)
{
//no version or conn string configured, must be a brand new install
return true;
}
//now we have to check if this is really a new install, the db might be configured and might contain data
2016-11-29 10:31:25 +01:00
if (_databaseBuilder.IsConnectionStringConfigured(databaseSettings) == false
|| _databaseBuilder.IsDatabaseConfigured == false)
{
return true;
}
2017-05-12 14:49:44 +02:00
return _databaseBuilder.HasSomeNonDefaultUser() == false;
}
}
internal IEnumerable<Package> GetStarterKits()
{
var packages = new List<Package>();
try
{
2016-09-01 19:06:08 +02:00
var requestUri = $"http://our.umbraco.org/webapi/StarterKit/Get/?umbracoVersion={UmbracoVersion.Current}";
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
using (var httpClient = new HttpClient())
using (var response = httpClient.SendAsync(request).Result)
{
packages = response.Content.ReadAsAsync<IEnumerable<Package>>().Result.ToList();
}
}
catch (AggregateException ex)
{
_logger.Error<InstallHelper>("Could not download list of available starter kits", ex);
}
return packages;
}
}
2017-07-20 11:21:28 +02:00
}