/Development V2/CodePlex.SharePointInstaller/Commands/ExecuteCustomCommands.cs

# · C# · 162 lines · 145 code · 14 blank · 3 comment · 21 complexity · a164d40d082a229330afa69c4ca250a4 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using CodePlex.SharePointInstaller.Configuration;
  5. using CodePlex.SharePointInstaller.Logging;
  6. using CodePlex.SharePointInstaller.Resources;
  7. using CodePlex.SharePointInstaller.Wrappers;
  8. using Microsoft.SharePoint.Administration;
  9. using Microsoft.SharePoint.StsAdmin;
  10. namespace CodePlex.SharePointInstaller.Commands
  11. {
  12. public class ExecuteCustomCommands : DispatcherCmd
  13. {
  14. private IContext context;
  15. private InstallConfiguration configuration;
  16. private SolutionInfo solution;
  17. public ExecuteCustomCommands(IContext installationContext)
  18. {
  19. context = installationContext;
  20. configuration = context.Configuration;
  21. this.solution = context.SolutionInfo;
  22. }
  23. public override string Description
  24. {
  25. get { return CommonUIStrings.ExecuteCustomCommandDescription; }
  26. }
  27. public override bool Execute()
  28. {
  29. DispatchMessage(Environment.NewLine + "Start executing custom post-install commands.");
  30. Log.Info(Environment.NewLine + "Executing custom post-install commands...");
  31. bool success = true;
  32. var commandsToRun = solution.CommandsToRun;
  33. foreach (CommandToRun commandToRun in commandsToRun)
  34. {
  35. success &= RunCommand(commandToRun.Name, configuration.GetCommandToRun(commandToRun), commandToRun.Parameters);
  36. }
  37. DispatchMessage("Finish executing custom post-install commands." + Environment.NewLine);
  38. return true; //discarding actual result otherwise it won't progress to the next command
  39. }
  40. private bool RunCommand(String commandName, ISPStsadmCommand command, String commandLine)
  41. {
  42. if (command == null)
  43. {
  44. return true;
  45. }
  46. return ExecuteCommands(command, ReplaceTokens(commandLine), commandName);
  47. }
  48. public List<String> ReplaceTokens(String commandLine)
  49. {
  50. //create a list of commands for each application/site/web with the placeholders replaced
  51. var commandLines = new List<String>();
  52. if ((!commandLine.Contains(InstallConfiguration.Placeholders.AppUrl)) &&
  53. (!commandLine.Contains(InstallConfiguration.Placeholders.SiteUrl) &&
  54. (!commandLine.Contains(InstallConfiguration.Placeholders.WebUrl))))
  55. {
  56. commandLines.Add(commandLine);
  57. return commandLines;
  58. }
  59. if (context.Action == InstallAction.Upgrade && !solution.ReactivateFeatures)
  60. {
  61. Log.Error(
  62. String.Format(
  63. "Unable to launch the post-install command {0} because it contains tokens which could not be resolved during upgrade action without features reactivation",
  64. commandLine));
  65. return commandLines;
  66. }
  67. var apps = context.WebApps;
  68. var k = 0;
  69. do //using post-condition loops to execute at least once for the case when there're no placeholders
  70. {
  71. if (k < apps.Count)
  72. commandLine = Regex.Replace(commandLine, InstallConfiguration.Placeholders.AppUrl, apps[k].GetResponseUri(SPUrlZone.Default).ToString());
  73. var siteCollections = context.SiteCollections;
  74. var j = 0;
  75. do
  76. {
  77. var tempSiteCollectionCommand = commandLine;
  78. IEntityInfo currentSiteCollection = null;
  79. if (j < siteCollections.Count)
  80. {
  81. currentSiteCollection = siteCollections[j];
  82. tempSiteCollectionCommand = Regex.Replace(tempSiteCollectionCommand, InstallConfiguration.Placeholders.SiteUrl,
  83. currentSiteCollection.Url);
  84. }
  85. var sites = context.Sites;
  86. var i = 0;
  87. do
  88. {
  89. var tempSiteCommand = tempSiteCollectionCommand;
  90. if (i < sites.Count)
  91. tempSiteCommand = Regex.Replace(tempSiteCommand, InstallConfiguration.Placeholders.WebUrl, sites[i].Url);
  92. else if (sites.Count == 0 && currentSiteCollection != null)
  93. {
  94. //resolving WebURL for a site collection's root web
  95. tempSiteCommand = Regex.Replace(tempSiteCommand,
  96. InstallConfiguration.Placeholders.WebUrl,
  97. currentSiteCollection.Url);
  98. }
  99. if (commandLines.IndexOf(tempSiteCommand) == -1)
  100. {
  101. commandLines.Add(tempSiteCommand);
  102. }
  103. i++;
  104. } while (i < sites.Count);
  105. j++;
  106. } while (j < siteCollections.Count);
  107. k++;
  108. } while (k < apps.Count);
  109. return commandLines;
  110. }
  111. private bool ExecuteCommands(ISPStsadmCommand command, IEnumerable<String> commandLines, String commandName)
  112. {
  113. var success = true;
  114. foreach (var commandline in commandLines)
  115. {
  116. try
  117. {
  118. DispatchMessage("Run post-install command {0} {1}.", commandName, commandline);
  119. string output;
  120. int returnCode = command.Run(commandName, CommandToRun.ParseCommandLine(commandName, commandline), out output);
  121. if (returnCode != 0)
  122. {
  123. DispatchErrorMessage("Fail post-install command '{2}' (parameters: {3}) returned an error (code {0}): {1}", returnCode, output, commandName, commandline);
  124. Log.Error(String.Format("Post-install command {2} (parameters: {3}) returned an error (code {0}): {1}", returnCode, output, commandName, commandline));
  125. success = false;
  126. }
  127. else
  128. {
  129. DispatchMessage("Succesfully executed post-install command {0} {1}.", commandName, commandline);
  130. Log.Info(String.Format("Succesfully executed post-install command {0} {1}", commandName,commandline));
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. DispatchErrorMessage("Fail post-install command '{0}'.", commandName);
  136. Log.Error(String.Format("Post-install command {1} (parameters: {2}) returned an error: {0}", e.Message,commandName, commandline), e);
  137. success = false;
  138. }
  139. }
  140. return success;
  141. }
  142. public override bool Rollback()
  143. {
  144. //no rollback for custom commands
  145. return true;
  146. }
  147. }
  148. }