PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Accelerator/Configuration/ServiceAccelerator.cs

https://bitbucket.org/zgramana/azure-accelerators-project
C# | 171 lines | 99 code | 12 blank | 60 comment | 14 complexity | 6fa755fd13f3439c3ab40208ba853e47 MD5 | raw file
Possible License(s): LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using System.Xml.XPath;
  8. using Microsoft.WindowsAzure.ServiceRuntime;
  9. using IisSite=Microsoft.Web.Administration.Site;
  10. using Microsoft.Web.Administration;
  11. namespace Microsoft.WindowsAzure.Accelerator.Configuration
  12. {
  13. /// <summary>
  14. /// The Windows Azure Accelerators engine's service configuration and definition schema. Used
  15. /// in conjuntion with the base Application definitions to provide the runtime implementation
  16. /// of the base application requirements.
  17. /// Ensure that the service and role names match the names as declared in the .cscfg and
  18. /// .csdef files.
  19. /// </summary>
  20. public partial class ServiceAccelerator
  21. {
  22. /// <summary>
  23. /// Gets the runtime accelerator role configuration.
  24. /// </summary>
  25. /// <value>The runtime role configuration.</value>
  26. public AcceleratorRole RuntimeRole
  27. {
  28. get
  29. {
  30. if (RoleEnvironment.IsAvailable && AcceleratorRole != null && AcceleratorRole.Count > 0)
  31. {
  32. var role = AcceleratorRole.Where(ar => ar.name == RoleEnvironment.CurrentRoleInstance.Role.Name).FirstOrDefault();
  33. return role;
  34. }
  35. return null;
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// The configuration of an accelerator web role which accepts external requests.
  41. /// </summary>
  42. public partial class AcceleratorRole
  43. {
  44. public Site this[String index]
  45. {
  46. get { { return this.Sites.Protect(sites => sites.Site.Where(site => site.name == index).FirstOrDefault()) ?? null; } }
  47. }
  48. }
  49. /// <summary>
  50. /// Specifies a web site or web application that is part of the web role.
  51. /// </summary>
  52. public partial class Site
  53. {
  54. private const String serviceDefinitionLocation = @"//Sites/Site[@name={0}]";
  55. public String RoleName { get { return RoleEnvironment.CurrentRoleInstance.Id; } }
  56. /// <summary>
  57. /// Gets the IIS web site object corresponding to this definition.
  58. /// </summary>
  59. /// <value>The web site.</value>
  60. public Web.Administration.Site WebSite
  61. {
  62. get
  63. { using (var sm = new ServerManager())
  64. {
  65. return sm.Sites.Where(site => site.Name.Replace(RoleEnvironment.CurrentRoleInstance.Id + "_", "") == name).FirstOrDefault();
  66. }
  67. }
  68. }
  69. public override void SetApplicationPool()
  70. {
  71. base.SetApplicationPool();
  72. }
  73. }
  74. /// <summary>
  75. /// Specifies a directory name that is mapped to a physical directory on the local and remote server.
  76. /// </summary>
  77. public partial class VirtualApplication
  78. {
  79. public virtual void SetApplicationPool()
  80. {
  81. }
  82. }
  83. /// <summary>
  84. /// Defines a web application running in IIS.
  85. /// </summary>
  86. public partial class VirtualDirectory
  87. {
  88. /// <summary>
  89. /// Gets the virtual applications.
  90. /// </summary>
  91. /// <value>The virtual applications.</value>
  92. public IEnumerable<VirtualApplication> VirtualApplications
  93. {
  94. get { return this.Items.OnValid(i => i.OfType<VirtualApplication>()); }
  95. }
  96. /// <summary>
  97. /// Returns all descendant virtual directories and applications.
  98. /// </summary>
  99. /// <returns>Descendant virtual directories.</returns>
  100. public IEnumerable<KeyValuePair<String, VirtualDirectory>> Descendants()
  101. {
  102. if (this.Items != null && this.Items.Count > 1)
  103. foreach(var item in Items)
  104. {
  105. yield return new KeyValuePair<String, VirtualDirectory>(String.Format("{0}/{1}", name, item.name), item);
  106. foreach(var descendant in item.Descendants())
  107. yield return descendant;
  108. }
  109. }
  110. /// <summary>
  111. /// Creates the sites directories.
  112. /// </summary>
  113. /// <remarks>
  114. /// BUGBUG: (b|rdm)
  115. /// Mirroring the entire physical structure is not correct. Virtual directories should retain
  116. /// their unique physical separation characteristics. This is a bug. I need to figure out how
  117. /// Azure does this and mirror their virtual directory process.
  118. /// </remarks>
  119. /// <param name="targetContainer">The target path.</param>
  120. /// <returns></returns>
  121. public void CopyPhysicalDirectory(Path targetContainer)
  122. {
  123. //i|
  124. //i|
  125. //i|
  126. if (String.IsNullOrEmpty(name))
  127. throw new ArgumentException(String.Format("Invalid VirtualDirectory name '{0}'.", name));
  128. Path target = System.IO.Path.Combine(targetContainer, name);
  129. //i| First, check if we have a reset on the physical directory;
  130. //i| if so we need to copy from a new location.)
  131. if (!String.IsNullOrEmpty(physicalDirectory))
  132. {
  133. //i| Source can be absolute or relative to the csdef folder.
  134. Path source = physicalDirectory;
  135. if (!source.IsRootPath())
  136. source = ServiceManager.Settings.ServiceDefinitionFile.GetFolder().Combine(source);
  137. if (File.Exists(source))
  138. source.CopyPath(target);
  139. else
  140. Trace.TraceError("Unable to find source directory at '{0}'.", source);
  141. }
  142. }
  143. /// <summary>
  144. /// Copies all virtual directories.
  145. /// </summary>
  146. /// <param name="targetContainer">The target path.</param>
  147. /// <returns></returns>
  148. public void CopyVirtualDirectories(Path targetContainer)
  149. {
  150. //|
  151. //| Process all child VirtualDirectory / VirtualApplication elements.
  152. //|
  153. foreach (var virtualDirectory in Items.OfType<VirtualDirectory>())
  154. {
  155. virtualDirectory.CopyPhysicalDirectory(targetContainer);
  156. }
  157. }
  158. }
  159. }