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