Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/758 add the ability to add search indexes to existing instances #784

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/SIM.Base/SolrStateResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ namespace SIM
{
public class SolrStateResolver
{
public virtual bool IsSolrAvailable(string solrUrl)
{
try
{
var request = WebRequest.Create(solrUrl) as HttpWebRequest;
request.Method = "HEAD";
using (var response = request.GetResponse() as HttpWebResponse)
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (WebException)
{
return false;
}
}

public virtual SolrState.CurrentState GetServiceState(ServiceControllerWrapper service)
{
if (service != null)
Expand Down Expand Up @@ -76,6 +93,27 @@ public virtual string GetVersion(string solrUrl)

return string.Empty;
}

public virtual string GetSeparateSolrAttribute(string solrUrl, string attributName)
{
HttpClient client = new HttpClient();

using (Stream stream = client.GetStreamAsync($"{solrUrl}/admin/info/system?wt=json").Result)
using (StreamReader streamReader = new StreamReader(stream))
using (JsonReader reader = new JsonTextReader(streamReader))
{
while (reader.Read())
{
if (string.Equals(reader.Path, attributName, StringComparison.OrdinalIgnoreCase)
&& !string.Equals((string)reader.Value, attributName, StringComparison.OrdinalIgnoreCase))
{
return (string)reader.Value;
}
}
}

return string.Empty;
}
}

public class ServiceControllerWrapper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using JetBrains.Annotations;
using SIM.Extensions;
using Sitecore.Diagnostics.Base;

namespace SIM.Pipelines.InstallSearchIndexes
{
public class CreateIndexDirectoryAction : InstallSearchIndexesProcessor
{
protected override void Process([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

foreach (var index in args._AvailableSearchIndexesDictionary)
{
string newCorePath = args.SolrFolder.EnsureEnd(@"\") + index.Value;
CreateIndexDirectory(args.SolrVersion, args.SolrFolder, newCorePath);
}
}

private void CreateIndexDirectory(string solrVersion, string solrFolder, string newCorePath)
{
string sourcePath = GetSourceConfPath(solrVersion, solrFolder);
FileSystem.FileSystem.Local.Directory.Copy(sourcePath, newCorePath, recursive: true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "sourcePath" folder may not exist.

}

private string GetSourceConfPath(string solrVersion, string solrFolder)
{
string confPath = string.Empty;

if (!string.IsNullOrEmpty(solrVersion) && char.IsDigit(solrVersion[0]))
{
int firstDigit = int.Parse(solrVersion[0].ToString());

if (firstDigit >= 7)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible not rely on digit 7 in this case? This looks like hardcode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to check the Solr version is seven or higher

{
confPath = solrFolder.EnsureEnd(@"\") + @"\configsets\_default";
}
else
{
confPath = solrFolder.EnsureEnd(@"\") + @"\configsets\data_driven_schema_configs";
}
}

return confPath;
}
}
}
36 changes: 36 additions & 0 deletions src/SIM.Pipelines/InstallSearchIndexes/CreateSolrCoreAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using JetBrains.Annotations;
using SIM.Extensions;
using Sitecore.Diagnostics.Base;
using System.IO;

namespace SIM.Pipelines.InstallSearchIndexes
{
public class CreateSolrCoreAction : InstallSearchIndexesProcessor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the "Action" suffix from the name since it is a processor?

{
protected override void Process([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

foreach (var index in args._AvailableSearchIndexesDictionary)
{
string newCorePath = args.SolrFolder.EnsureEnd(@"\") + index.Value;

RequestAndGetResponseStream($"{args.SolrUrl}/admin/cores?action=CREATE&name={index.Value}&instanceDir={newCorePath}&config=solrconfig.xml&schema=schema.xml&dataDir=data");
UpdateCorePropertiesFile(index.Value, newCorePath);
}
}

private Stream RequestAndGetResponseStream(string url)
{
return WebRequestHelper.RequestAndGetResponse(url).GetResponseStream();
}

private void UpdateCorePropertiesFile(string coreName, string newCorePath)
{
string filePath = string.Format(newCorePath.EnsureEnd(@"\") + @"core.properties");
string newText = @"update.autoCreateFields=false" + "\r\n" + "name=" + coreName;
FileSystem.FileSystem.Local.File.Delete(filePath);
FileSystem.FileSystem.Local.File.WriteAllText(filePath, newText);
}
}
}
49 changes: 49 additions & 0 deletions src/SIM.Pipelines/InstallSearchIndexes/InstallSearchIndexesArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using JetBrains.Annotations;
using SIM.Instances;
using SIM.Pipelines.Processors;
using Sitecore.Diagnostics.Base;
using System.Collections.Generic;

namespace SIM.Pipelines.InstallSearchIndexes
{
public class InstallSearchIndexesArgs : ProcessorArgs
{
[UsedImplicitly]
public string InstanceName
{
get
{
return Instance != null ? Instance.Name : string.Empty;
}
}

public Dictionary<string, string> _AvailableSearchIndexesDictionary;

public string SolrUrl;

public string SolrVersion;

public string SolrFolder;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an indent between these properties.

public Instance Instance { get; }

public string AuthCookies { get; }

public IDictionary<string, string> Headers { get; }

public InstallSearchIndexesArgs([NotNull] Instance instance, [CanBeNull] Dictionary<string, string> availableSearchIndexesDictionary, [NotNull] string solrUrl, [NotNull] string solrVersion, [NotNull] string solrFolder, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] string cookies = null)
{
Assert.ArgumentNotNull(instance, nameof(instance));
Assert.ArgumentNotNull(solrUrl, nameof(solrUrl));
Assert.ArgumentNotNull(solrVersion, nameof(solrVersion));
Assert.ArgumentNotNull(solrFolder, nameof(solrFolder));

SolrUrl = solrUrl;
SolrVersion = solrVersion;
SolrFolder = solrFolder;
_AvailableSearchIndexesDictionary = availableSearchIndexesDictionary;
Instance = instance;
AuthCookies = cookies;
Headers = headers;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using JetBrains.Annotations;
using SIM.Pipelines.Processors;
using Sitecore.Diagnostics.Base;

namespace SIM.Pipelines.InstallSearchIndexes
{
public abstract class InstallSearchIndexesProcessor : Processor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one processor fails, other processors should be in the Pending state:
image

{
public override sealed long EvaluateStepsCount(ProcessorArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

return EvaluateStepsCount((InstallSearchIndexesArgs)args);
}

public override sealed bool IsRequireProcessing(ProcessorArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

return IsRequireProcessing((InstallSearchIndexesArgs)args);
}

protected virtual long EvaluateStepsCount([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

return 1;
}

protected virtual bool IsRequireProcessing([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

return true;
}

protected override void Process(ProcessorArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

Process((InstallSearchIndexesArgs)args);
}

protected abstract void Process([NotNull] InstallSearchIndexesArgs args);
}
}
60 changes: 60 additions & 0 deletions src/SIM.Pipelines/InstallSearchIndexes/PopulateNewIndexAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using JetBrains.Annotations;
using SIM.Extensions;
using SIM.Instances;
using Sitecore.Diagnostics.Base;
using System;
using System.Collections.Generic;

namespace SIM.Pipelines.InstallSearchIndexes
{
public class PopulateNewIndexAction : InstallSearchIndexesProcessor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the "Action" suffix from the name since it is a processor?
In addition, I would suggest changing its name to "PopulateManagedSchema".

{
protected override void Process([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

foreach (var index in args._AvailableSearchIndexesDictionary)
{
string newCorePath = args.SolrFolder.EnsureEnd(@"\") + index.Value;
PopulateNewIndex(index.Key, args.Instance, args.Headers, args.AuthCookies);
}
}

private void PopulateNewIndex(string coreID, Instance instance, IDictionary<string, string> headers, string authCookie)
{
var popUrl = instance.GetUrl(@"/sitecore/admin/PopulateManagedSchema.aspx?indexes=" + coreID);

try
{
int sitecoreVersion;

int.TryParse(instance.Product.ShortVersion, out sitecoreVersion);

if (sitecoreVersion >= 91)
{
if (headers == null)
{
return;
}
var result = WebRequestHelper.DownloadString(popUrl, headers: headers).Trim();

}
else if (sitecoreVersion == 90)
{
string instanceUri = instance.GetUrl();

if (string.IsNullOrEmpty(authCookie))
{
return;
}
var result = WebRequestHelper.DownloadString(popUrl, cookies: authCookie).Trim();
}
}
catch (Exception ex)
{
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we catch an exception, it is a good idea to add logging.
If it does not make sense here then "(Exception ex)" can be removed.

}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unnecessary line.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using JetBrains.Annotations;
using SIM.Extensions;
using Sitecore.Diagnostics.Base;
using System.Xml;

namespace SIM.Pipelines.InstallSearchIndexes
{
public class UpdateManagedSchemaAction : InstallSearchIndexesProcessor
{
protected override void Process([NotNull] InstallSearchIndexesArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

foreach (var index in args._AvailableSearchIndexesDictionary)
{
string newCorePath = args.SolrFolder.EnsureEnd(@"\") + index.Value;

UpdateManagedSchemaFile(newCorePath);
}
}

private void UpdateManagedSchemaFile(string newCorePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(newCorePath.EnsureEnd(@"\") + @"conf\managed-schema");
XmlElement newField = doc.CreateElement("field");
newField.SetAttribute("name", "_uniqueid");
newField.SetAttribute("type", "string");
newField.SetAttribute("indexed", "true");
newField.SetAttribute("required", "true");
newField.SetAttribute("stored", "true");
XmlNode schemaNode = doc.SelectSingleNode("/schema");

if (schemaNode != null)
{
schemaNode.AppendChild(newField);
}

XmlNode uniqueKeyNode = doc.SelectSingleNode("/schema/uniqueKey");
if (uniqueKeyNode != null)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unnecessary line.

{
uniqueKeyNode.InnerText = "_uniqueid";
}

doc.Save(newCorePath.EnsureEnd(@"\") + @"conf\managed-schema");
}
}
}
6 changes: 6 additions & 0 deletions src/SIM.Pipelines/PipelinesConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ public static class PipelinesConfig
</processor>
</processor>
</installmodules>
<installSearchIndexes title=""Installing search indexes on the {InstanceName} instance"" >
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional space should be added to each line.

<processor type=""SIM.Pipelines.InstallSearchIndexes.CreateIndexDirectoryAction, SIM.Pipelines"" title=""Creating index directories"" />
<processor type=""SIM.Pipelines.InstallSearchIndexes.UpdateManagedSchemaAction, SIM.Pipelines"" title=""Updating managed schema files"" />
<processor type=""SIM.Pipelines.InstallSearchIndexes.CreateSolrCoreAction, SIM.Pipelines"" title=""Creating Solr cores"" />
<processor type=""SIM.Pipelines.InstallSearchIndexes.PopulateNewIndexAction, SIM.Pipelines"" title=""Populating created indexes"" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest changing the title to "Populating managed schema files".

</installSearchIndexes>
<backup title=""Backing up the {InstanceName} instance"">
<processor type=""SIM.Pipelines.Backup.BackupDatabases, SIM.Pipelines"" title=""Backing up databases"" />
<processor type=""SIM.Pipelines.Backup.BackupMongoDatabases, SIM.Pipelines"" title=""Backing up MongoDB databases"" />
Expand Down
6 changes: 6 additions & 0 deletions src/SIM.Pipelines/SIM.Pipelines.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<Compile Include="Import\ImportRestoreMongoDatabases.cs" />
<Compile Include="Import\UpdateConnectionStrings.cs" />
<Compile Include="Import\UpdateDataFolder.cs" />
<Compile Include="InstallSearchIndexes\CreateIndexDirectoryAction.cs" />
<Compile Include="InstallSearchIndexes\CreateSolrCoreAction.cs" />
<Compile Include="InstallSearchIndexes\InstallSearchIndexesArgs.cs" />
<Compile Include="InstallSearchIndexes\InstallSearchIndexesProcessor.cs" />
<Compile Include="InstallSearchIndexes\PopulateNewIndexAction.cs" />
<Compile Include="InstallSearchIndexes\UpdateManagedSchemaAction.cs" />
<Compile Include="Install\Containers\AddHostsProcessor.cs" />
<Compile Include="Install\Containers\GenerateModulesData.cs" />
<Compile Include="Install\Containers\ConvertLicenseProcessor.cs" />
Expand Down
Loading