-
Notifications
You must be signed in to change notification settings - Fork 76
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
base: develop
Are you sure you want to change the base?
Changes from all commits
854d246
caa0c4e
0c1448c
2dbcbfa
43157a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
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); | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,6 +237,12 @@ public static class PipelinesConfig | |
</processor> | ||
</processor> | ||
</installmodules> | ||
<installSearchIndexes title=""Installing search indexes on the {InstanceName} instance"" > | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"" /> | ||
|
There was a problem hiding this comment.
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.