PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Kudu.Services/Deployment/DeploymentController.cs

https://github.com/moacap/kudu
C# | 185 lines | 163 code | 18 blank | 4 comment | 5 complexity | 56969d8724c101a86cee5d8fea79d3d1 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Web.Http;
  9. using Kudu.Contracts.Infrastructure;
  10. using Kudu.Contracts.Tracing;
  11. using Kudu.Core.Deployment;
  12. using Kudu.Services.Infrastructure;
  13. using Newtonsoft.Json.Linq;
  14. namespace Kudu.Services.Deployment
  15. {
  16. public class DeploymentController : ApiController
  17. {
  18. private readonly IDeploymentManager _deploymentManager;
  19. private readonly ITracer _tracer;
  20. private readonly IOperationLock _deploymentLock;
  21. public DeploymentController(ITracer tracer,
  22. IDeploymentManager deploymentManager,
  23. IOperationLock deploymentLock)
  24. {
  25. _tracer = tracer;
  26. _deploymentManager = deploymentManager;
  27. _deploymentLock = deploymentLock;
  28. }
  29. [HttpDelete]
  30. public void Delete(string id)
  31. {
  32. using (_tracer.Step("DeploymentService.Delete"))
  33. {
  34. _deploymentLock.LockHttpOperation(() =>
  35. {
  36. try
  37. {
  38. _deploymentManager.Delete(id);
  39. }
  40. catch (DirectoryNotFoundException ex)
  41. {
  42. throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
  43. }
  44. catch (InvalidOperationException ex)
  45. {
  46. throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex));
  47. }
  48. });
  49. }
  50. }
  51. [HttpPut]
  52. public void Deploy(string id)
  53. {
  54. JObject result = GetJsonContent();
  55. // Just block here to read the json payload from the body
  56. using (_tracer.Step("DeploymentService.Deploy(id)"))
  57. {
  58. _deploymentLock.LockHttpOperation(() =>
  59. {
  60. try
  61. {
  62. bool clean = false;
  63. if (result != null)
  64. {
  65. clean = result.Value<bool>("clean");
  66. }
  67. string username = null;
  68. AuthUtility.TryExtractBasicAuthUser(Request, out username);
  69. _deploymentManager.Deploy(id, username, clean);
  70. }
  71. catch (FileNotFoundException ex)
  72. {
  73. throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
  74. }
  75. });
  76. }
  77. }
  78. [HttpGet]
  79. [Queryable]
  80. public IQueryable<DeployResult> GetDeployResults()
  81. {
  82. using (_tracer.Step("DeploymentService.GetDeployResults"))
  83. {
  84. return GetResults(Request).AsQueryable();
  85. }
  86. }
  87. [HttpGet]
  88. public IEnumerable<LogEntry> GetLogEntry(string id)
  89. {
  90. using (_tracer.Step("DeploymentService.GetLogEntry"))
  91. {
  92. try
  93. {
  94. IEnumerable<LogEntry> deployments = _deploymentManager.GetLogEntries(id).ToList();
  95. foreach (var entry in deployments)
  96. {
  97. if (entry.HasDetails)
  98. {
  99. entry.DetailsUrl = UriHelper.MakeRelative(Request.RequestUri, entry.Id);
  100. }
  101. }
  102. return deployments;
  103. }
  104. catch (FileNotFoundException ex)
  105. {
  106. throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
  107. }
  108. }
  109. }
  110. [HttpGet]
  111. public IEnumerable<LogEntry> GetLogEntryDetails(string id, string logId)
  112. {
  113. using (_tracer.Step("DeploymentService.GetLogEntryDetails"))
  114. {
  115. try
  116. {
  117. return _deploymentManager.GetLogEntryDetails(id, logId).ToList();
  118. }
  119. catch (FileNotFoundException ex)
  120. {
  121. throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
  122. }
  123. }
  124. }
  125. [HttpGet]
  126. public DeployResult GetResult(string id)
  127. {
  128. using (_tracer.Step("DeploymentService.GetResult"))
  129. {
  130. DeployResult result = _deploymentManager.GetResult(id);
  131. if (result == null)
  132. {
  133. var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format(CultureInfo.CurrentCulture,
  134. Resources.Error_DeploymentNotFound,
  135. id));
  136. throw new HttpResponseException(response);
  137. }
  138. result.Url = Request.RequestUri;
  139. result.LogUrl = UriHelper.MakeRelative(Request.RequestUri, "log");
  140. return result;
  141. }
  142. }
  143. private IEnumerable<DeployResult> GetResults(HttpRequestMessage request)
  144. {
  145. foreach (var result in _deploymentManager.GetResults())
  146. {
  147. result.Url = UriHelper.MakeRelative(request.RequestUri, result.Id);
  148. result.LogUrl = UriHelper.MakeRelative(request.RequestUri, result.Id + "/log");
  149. yield return result;
  150. }
  151. }
  152. private JObject GetJsonContent()
  153. {
  154. try
  155. {
  156. return Request.Content.ReadAsAsync<JObject>().Result;
  157. }
  158. catch
  159. {
  160. // We're going to return null here since we don't want to force a breaking change
  161. // on the client side. If the incoming request isn't application/json, we want this
  162. // to return null.
  163. return null;
  164. }
  165. }
  166. }
  167. }