Example #1
0
        protected override Task <HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath)
        {
            HttpResponseMessage response = Request.CreateResponse();
            // GetQueryNameValuePairs returns an IEnumerable<KeyValuePair<string, string>>
            // KeyValuePair is a value type.
            var fileName = Request.GetQueryNameValuePairs().FirstOrDefault(p => p.Key.Equals("fileName", StringComparison.OrdinalIgnoreCase)).Value;

            fileName         = fileName ?? Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip";
            response.Content = ZipStreamContent.Create(fileName, Tracer, zip =>
            {
                foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos())
                {
                    var directoryInfo = fileSysInfo as DirectoryInfoBase;
                    if (directoryInfo != null)
                    {
                        zip.AddDirectory(directoryInfo, Tracer, fileSysInfo.Name);
                    }
                    else
                    {
                        // Add it at the root of the zip
                        zip.AddFile(fileSysInfo.FullName, Tracer, String.Empty);
                    }
                }
            });
            return(Task.FromResult(response));
        }
Example #2
0
        public HttpResponseMessage GetDeploymentScript()
        {
            using (_tracer.Step("DeploymentService.GetDeploymentScript"))
            {
                if (!_deploymentManager.GetResults().Any())
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Need to deploy website to get deployment script."));
                }

                string deploymentScriptContent = _deploymentManager.GetDeploymentScriptContent();

                if (deploymentScriptContent == null)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Operation only supported if not using a custom deployment script"));
                }

                HttpResponseMessage response = Request.CreateResponse();
                response.Content = ZipStreamContent.Create("deploymentscript.zip", _tracer, zip =>
                {
                    // Add deploy.cmd to zip file
                    zip.AddFile(DeploymentManager.DeploymentScriptFileName, deploymentScriptContent);

                    // Add .deployment to cmd file
                    zip.AddFile(DeploymentSettingsProvider.DeployConfigFile, "[config]\ncommand = {0}\n".FormatInvariant(DeploymentManager.DeploymentScriptFileName));
                });

                return(response);
            }
        }
Example #3
0
        public HttpResponseMessage GCDump(int id, int maxDumpCountK = 0, string format = null)
        {
            using (_tracer.Step("ProcessController.GCDump"))
            {
                DumpFormat dumpFormat = ParseDumpFormat(format, DumpFormat.DiagSession);
                var        process    = GetProcessById(id);
                var        ext        = dumpFormat == DumpFormat.DiagSession ? "diagsession" : "gcdump";

                string dumpFile = Path.Combine(_environment.LogFilesPath, "minidump", "dump." + ext);
                FileSystemHelpers.EnsureDirectory(_fileSystem, Path.GetDirectoryName(dumpFile));
                FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);

                string resourcePath = GetResponseFileName(process.ProcessName, "gcdump");
                try
                {
                    using (_tracer.Step(String.Format("GCDump pid={0}, name={1}, file={2}", process.Id, process.ProcessName, dumpFile)))
                    {
                        process.GCDump(dumpFile, resourcePath, maxDumpCountK, _tracer, _settings.GetCommandIdleTimeout());
                        _tracer.Trace("GCDump size={0}", new FileInfo(dumpFile).Length);
                    }
                }
                catch (Exception ex)
                {
                    _tracer.TraceError(ex);
                    FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                }

                if (dumpFormat == DumpFormat.Zip)
                {
                    string responseFileName      = GetResponseFileName(process.ProcessName, "zip");
                    HttpResponseMessage response = Request.CreateResponse();
                    response.Content = ZipStreamContent.Create(responseFileName, _tracer, zip =>
                    {
                        try
                        {
                            zip.AddFile(dumpFile, String.Empty);
                        }
                        finally
                        {
                            FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);
                        }
                    });
                    return(response);
                }
                else
                {
                    string responseFileName      = GetResponseFileName(process.ProcessName, ext);
                    HttpResponseMessage response = Request.CreateResponse();
                    response.Content = new StreamContent(FileStreamWrapper.OpenRead(dumpFile, _fileSystem));
                    response.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/octet-stream");
                    response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                    response.Content.Headers.ContentDisposition.FileName = responseFileName;
                    return(response);
                }
            }
        }
Example #4
0
        public HttpResponseMessage GetLog()
        {
            HttpResponseMessage response = Request.CreateResponse();

            response.Content = ZipStreamContent.Create(String.Format("dump-{0:MM-dd-HH-mm-ss}.zip", DateTime.UtcNow), _tracer, zip =>
            {
                AddFilesToZip(zip);
            });
            return(response);
        }
        public HttpResponseMessage DownloadFunctions(bool includeCsproj = true, bool includeAppSettings = false)
        {
            var tracer = _traceFactory.GetTracer();

            using (tracer.Step($"{nameof(FunctionController)}.{nameof(DownloadFunctions)}({includeCsproj}, {includeAppSettings})"))
            {
                var appName  = ServerConfiguration.GetApplicationName();
                var fileName = $"{appName}.zip";
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = ZipStreamContent.Create(fileName, tracer, zip => _manager.CreateArchive(zip, includeAppSettings, includeCsproj, appName))
                });
            }
        }
Example #6
0
        public HttpResponseMessage GetDockerLogsZip()
        {
            using (_tracer.Step("DiagnosticsController.GetDockerLogsZip"))
            {
                var currentDockerLogFilenames = GetCurrentDockerLogFilenames();

                HttpResponseMessage response = Request.CreateResponse();
                response.Content = ZipStreamContent.Create(String.Format("dockerlogs-{0:MM-dd-HH-mm-ss}.zip", DateTime.UtcNow), _tracer, zip =>
                {
                    foreach (var filename in currentDockerLogFilenames)
                    {
                        zip.AddFile(filename, _tracer);
                    }
                });
                return(response);
            }
        }
Example #7
0
        public HttpResponseMessage GetDockerLogsZip()
        {
            using (_tracer.Step("DiagnosticsController.GetDockerLogsZip"))
            {
                // Also search for "today's" files in sub folders. Windows containers archives log files
                // when they reach a certain size.
                var currentDockerLogFilenames = GetCurrentDockerLogFilenames(SearchOption.AllDirectories);

                HttpResponseMessage response = Request.CreateResponse();
                response.Content = ZipStreamContent.Create(String.Format("dockerlogs-{0:MM-dd-HH-mm-ss}.zip", DateTime.UtcNow), _tracer, zip =>
                {
                    foreach (var filename in currentDockerLogFilenames)
                    {
                        zip.AddFile(filename, _tracer);
                    }
                });
                return(response);
            }
        }
Example #8
0
        //Referencing Zip code from
        //https://github.com/projectkudu/kudu/blob/Kudu.Services/Diagnostics/DiagnosticsController.cs
        public HttpResponseMessage Get(string sessionId, string downloadableFileType, string diagnoserName)
        {
            var pathslist = new List <DaaSFileInfo>();

            PopulateFileList(pathslist, sessionId, downloadableFileType, diagnoserName);
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            if (pathslist.Count > 1 || downloadableFileType == "MemoryDumps")
            {
                response.Content = ZipStreamContent.Create(
                    String.Format("{0}-{1}-{2}.zip", sessionId, diagnoserName, downloadableFileType),
                    zip =>
                {
                    ZipArchiveExtensions.AddFilesToZip(pathslist, zip);
                });
            }
            else if (pathslist.Count == 0)
            {
                string html = @"<B>DaaS Session Error:</B>
                            <BR/>No files are present. 
                            <BR/>Please try again later if the session is still in progress.
                            <BR/>Please invoke a new session if the session is not in progress.";

                response.Content = new StringContent(html);
                response.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/html");
                response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = "nofiles.html";
            }
            else
            {
                response.Content = new StreamContent(
                    new FileStream(pathslist[0].FilePath, FileMode.Open)
                    );
                FileInfo fi = new FileInfo(pathslist[0].FilePath);
                response.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/html");
                response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = String.Concat(pathslist[0].Prefix, fi.Name);
            }
            return(response);
        }
Example #9
0
        protected override Task <HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath)
        {
            HttpResponseMessage response = Request.CreateResponse();

            response.Content = ZipStreamContent.Create(Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip", Tracer, zip =>
            {
                foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos())
                {
                    var directoryInfo = fileSysInfo as DirectoryInfoBase;
                    if (directoryInfo != null)
                    {
                        zip.AddDirectory(directoryInfo, fileSysInfo.Name);
                    }
                    else
                    {
                        // Add it at the root of the zip
                        zip.AddFile(fileSysInfo.FullName, String.Empty);
                    }
                }
            });
            return(Task.FromResult(response));
        }
Example #10
0
        public HttpResponseMessage MiniDump(int id, int dumpType = 0, string format = null)
        {
            using (_tracer.Step("ProcessController.MiniDump"))
            {
                DumpFormat dumpFormat = ParseDumpFormat(format, DumpFormat.Raw);
                if (dumpFormat != DumpFormat.Raw && dumpFormat != DumpFormat.Zip)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                       String.Format(CultureInfo.CurrentCulture, Resources.Error_DumpFormatNotSupported, dumpFormat)));
                }

                string sitePolicy = _settings.GetWebSitePolicy();
                if ((MINIDUMP_TYPE)dumpType == MINIDUMP_TYPE.WithFullMemory && sitePolicy.Equals(FreeSitePolicy, StringComparison.OrdinalIgnoreCase))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                       String.Format(CultureInfo.CurrentCulture, Resources.Error_FullMiniDumpNotSupported, sitePolicy)));
                }

                var process = GetProcessById(id);

                string dumpFile = Path.Combine(_environment.LogFilesPath, "minidump", "minidump.dmp");
                FileSystemHelpers.EnsureDirectory(_fileSystem, Path.GetDirectoryName(dumpFile));
                FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);

                try
                {
                    using (_tracer.Step(String.Format("MiniDump pid={0}, name={1}, file={2}", process.Id, process.ProcessName, dumpFile)))
                    {
                        process.MiniDump(dumpFile, (MINIDUMP_TYPE)dumpType);
                        _tracer.Trace("MiniDump size={0}", new FileInfo(dumpFile).Length);
                    }
                }
                catch (Exception ex)
                {
                    _tracer.TraceError(ex);
                    FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                }

                if (dumpFormat == DumpFormat.Raw)
                {
                    string responseFileName = GetResponseFileName(process.ProcessName, "dmp");

                    HttpResponseMessage response = Request.CreateResponse();
                    response.Content = new StreamContent(FileStreamWrapper.OpenRead(dumpFile, _fileSystem));
                    response.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/octet-stream");
                    response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                    response.Content.Headers.ContentDisposition.FileName = responseFileName;
                    return(response);
                }
                else if (dumpFormat == DumpFormat.Zip)
                {
                    string responseFileName = GetResponseFileName(process.ProcessName, "zip");

                    HttpResponseMessage response = Request.CreateResponse();
                    response.Content = ZipStreamContent.Create(responseFileName, _tracer, zip =>
                    {
                        try
                        {
                            zip.AddFile(dumpFile, String.Empty);
                        }
                        finally
                        {
                            FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile);
                        }

                        foreach (var fileName in new[] { "sos.dll", "mscordacwks.dll" })
                        {
                            string filePath = Path.Combine(ProcessExtensions.ClrRuntimeDirectory, fileName);
                            if (_fileSystem.File.Exists(filePath))
                            {
                                zip.AddFile(filePath, String.Empty);
                            }
                        }
                    });
                    return(response);
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                       String.Format(CultureInfo.CurrentCulture, Resources.Error_DumpFormatNotSupported, dumpFormat)));
                }
            }
        }