Beispiel #1
0
        public virtual Task <HttpResponseMessage> PutItem()
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);
            bool itemExists        = info.Attributes >= 0;

            if (itemExists && (info.Attributes & FileAttributes.Directory) != 0)
            {
                return(CreateDirectoryPutResponse(info, localFilePath));
            }
            else
            {
                // If request URI ends in a "/" then attempt to create the directory.
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    return(CreateDirectoryPutResponse(info, localFilePath));
                }

                // We are ready to update the file
                return(CreateItemPutResponse(info, localFilePath, itemExists));
            }
        }
Beispiel #2
0
        private static void OldRunsCleanup(string jobName, IEnvironment environment, ITraceFactory traceFactory, IDeploymentSettingsManager settings)
        {
            // if max is 5 and we have 5 we still want to remove one to make room for the next
            // that's why we decrement max value by 1
            int maxRuns = settings.GetWebJobsHistorySize() - 1;

            string            historyPath      = Path.Combine(environment.JobsDataPath, Constants.TriggeredPath, jobName);
            DirectoryInfoBase historyDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(historyPath);

            if (!historyDirectory.Exists)
            {
                return;
            }

            DirectoryInfoBase[] historyRunsDirectories = historyDirectory.GetDirectories();
            if (historyRunsDirectories.Length <= maxRuns)
            {
                return;
            }

            var directoriesToRemove = historyRunsDirectories.OrderByDescending(d => d.Name).Skip(maxRuns);

            foreach (DirectoryInfoBase directory in directoriesToRemove)
            {
                try
                {
                    directory.Delete(true);
                }
                catch (Exception ex)
                {
                    traceFactory.GetTracer().TraceError(ex);
                }
            }
        }
Beispiel #3
0
        private static string GetPreInstalledLatestVersion(string directory)
        {
            if (!FileSystemHelpers.DirectoryExists(directory))
            {
                return(null);
            }

            string[] pathStrings = FileSystemHelpers.GetDirectories(directory);

            if (pathStrings.Length == 0)
            {
                return(null);
            }

            return(pathStrings.Max(path =>
            {
                string versionString = FileSystemHelpers.DirectoryInfoFromDirectoryName(path).Name;
                SemanticVersion semVer;
                if (SemanticVersion.TryParse(versionString, out semVer))
                {
                    return semVer;
                }
                else
                {
                    return new SemanticVersion(0, 0, 0, 0);
                }
            }).ToString());
        }
Beispiel #4
0
        public static IEnumerable <VfsStatEntry> GetEntries(string baseAddress, string query)
        {
            if (!String.IsNullOrEmpty(SystemDrivePath))
            {
                var dir = FileSystemHelpers.DirectoryInfoFromDirectoryName(SystemDrivePath + Path.DirectorySeparatorChar);
                yield return(new VfsStatEntry
                {
                    Name = SystemDriveFolder,
                    MTime = dir.LastWriteTimeUtc,
                    CRTime = dir.CreationTimeUtc,
                    Mime = "inode/shortcut",
                    Href = baseAddress + Uri.EscapeUriString(SystemDriveFolder + VfsControllerBase.UriSegmentSeparator) + query,
                    Path = dir.FullName
                });
            }

            if (!String.IsNullOrEmpty(LocalSiteRootPath))
            {
                var dir = FileSystemHelpers.DirectoryInfoFromDirectoryName(LocalSiteRootPath);
                yield return(new VfsStatEntry
                {
                    Name = LocalSiteRootFolder,
                    MTime = dir.LastWriteTimeUtc,
                    CRTime = dir.CreationTimeUtc,
                    Mime = "inode/shortcut",
                    Href = baseAddress + Uri.EscapeUriString(LocalSiteRootFolder + VfsControllerBase.UriSegmentSeparator) + query,
                    Path = dir.FullName
                });
            }
        }
Beispiel #5
0
 private static string GetLatestPreInstalledExtensionVersion(string id)
 {
     try
     {
         IEnumerable <string> pathStrings = FileSystemHelpers.GetDirectories("D:\\Program Files (x86)\\SiteExtensions\\" + id);
         SemanticVersion      maxVersion  = pathStrings.Max(path =>
         {
             string versionString = FileSystemHelpers.DirectoryInfoFromDirectoryName(path).Name;
             SemanticVersion semVer;
             if (SemanticVersion.TryParse(versionString, out semVer))
             {
                 return(semVer);
             }
             else
             {
                 return(new SemanticVersion(0, 0, 0, 0));
             }
         });
         return(maxVersion.ToString());
     }
     catch (IOException)
     {
         return(null);
     }
 }
Beispiel #6
0
        public TriggeredJobRun GetJobRun(string jobName, string runId)
        {
            string            triggeredJobRunPath      = Path.Combine(JobsDataPath, jobName, runId);
            DirectoryInfoBase triggeredJobRunDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(triggeredJobRunPath);

            return(BuildJobRun(triggeredJobRunDirectory, jobName, isLatest: true));
        }
Beispiel #7
0
            public LogFileFinder(IEnvironment env, ITracer tracer, LogFileAccessStats stats = null)
            {
                ExcludedFiles = new HashSet <string>();
                IncludedFiles = new HashSet <string>();

                _stats     = stats;
                _tracer    = tracer;
                _directory = FileSystemHelpers.DirectoryInfoFromDirectoryName(env.ApplicationLogFilesPath);
            }
Beispiel #8
0
        internal static Dictionary <string, FileInfoBase> GetJobDirectoryFileMap(string sourceDirectory)
        {
            DirectoryInfoBase jobBinariesDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(sourceDirectory);

            FileInfoBase[] files = jobBinariesDirectory.GetFiles("*.*", SearchOption.AllDirectories);

            int sourceDirectoryPathLength = sourceDirectory.Length + 1;

            return(files.ToDictionary(p => p.FullName.Substring(sourceDirectoryPathLength), q => q, StringComparer.OrdinalIgnoreCase));
        }
        private async Task LocalZipFetch(IRepository repository, DeploymentInfoBase deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
        {
            var zipDeploymentInfo = (ZipDeploymentInfo)deploymentInfo;

            // If this was a request with a Zip URL in the JSON, we need to deploy the zip locally and get the path
            // Otherwise, for this kind of deployment, RepositoryUrl is a local path.
            var sourceZipFile = !string.IsNullOrEmpty(zipDeploymentInfo.ZipURL)
                ? await DeployZipLocally(zipDeploymentInfo, tracer)
                : zipDeploymentInfo.RepositoryUrl;

            var extractTargetDirectory = repository.RepositoryPath;

            var info     = FileSystemHelpers.FileInfoFromFileName(sourceZipFile);
            var sizeInMb = (info.Length / (1024f * 1024f)).ToString("0.00", CultureInfo.InvariantCulture);

            var message = String.Format(
                CultureInfo.InvariantCulture,
                "Cleaning up temp folders from previous zip deployments and extracting pushed zip file {0} ({1} MB) to {2}",
                info.FullName,
                sizeInMb,
                extractTargetDirectory);

            logger.Log(message);

            using (tracer.Step(message))
            {
                // If extractTargetDirectory already exists, rename it so we can delete it concurrently with
                // the unzip (along with any other junk in the folder)
                var targetInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(extractTargetDirectory);
                if (targetInfo.Exists)
                {
                    var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
                    using (tracer.Step(string.Format("Renaming extractTargetDirectory({0}) to tempDirectory({1})", targetInfo.FullName, moveTarget)))
                    {
                        targetInfo.MoveTo(moveTarget);
                    }
                }

                var cleanTask   = Task.Run(() => DeleteFilesAndDirsExcept(sourceZipFile, extractTargetDirectory, tracer));
                var extractTask = Task.Run(() =>
                {
                    FileSystemHelpers.CreateDirectory(extractTargetDirectory);

                    using (var file = info.OpenRead())
                        using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
                        {
                            zip.Extract(extractTargetDirectory, tracer, _settings.GetZipDeployDoNotPreserveFileTime());
                        }
                });

                await Task.WhenAll(cleanTask, extractTask);
            }

            CommitRepo(repository, zipDeploymentInfo);
        }
Beispiel #10
0
        // OneDeploy Fetch handler for non-zip artifacts.
        // For zip files, OneDeploy uses the LocalZipHandler Fetch handler
        // NOTE: Do not access the request stream as it may have been closed during asynchronous scenarios
        private async Task OneDeployFetch(IRepository repository, DeploymentInfoBase deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
        {
            var artifactDeploymentInfo = (ArtifactDeploymentInfo)deploymentInfo;

            // This is the path where the artifact being deployed is staged, before it is copied to the final target location
            var artifactDirectoryStagingPath = repository.RepositoryPath;

            var targetInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(artifactDirectoryStagingPath);

            if (targetInfo.Exists)
            {
                // If tempDirPath already exists, rename it so we can delete it later
                var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
                using (tracer.Step(string.Format("Renaming ({0}) to ({1})", targetInfo.FullName, moveTarget)))
                {
                    targetInfo.MoveTo(moveTarget);
                }
            }

            // Create artifact staging directory before later use
            Directory.CreateDirectory(artifactDirectoryStagingPath);
            var artifactFileStagingPath = Path.Combine(artifactDirectoryStagingPath, deploymentInfo.TargetFileName);

            // If RemoteUrl is non-null, it means the content needs to be downloaded from the Url source to the staging location
            // Else, it had been downloaded already so we just move the downloaded file to the staging location
            if (!string.IsNullOrWhiteSpace(artifactDeploymentInfo.RemoteURL))
            {
                using (tracer.Step("Saving request content to {0}", artifactFileStagingPath))
                {
                    var content = await DeploymentHelper.GetArtifactContentFromURL(artifactDeploymentInfo, tracer);

                    var copyTask = content.CopyToAsync(artifactFileStagingPath, tracer);

                    // Deletes all files and directories except for artifactFileStagingPath and artifactDirectoryStagingPath
                    var cleanTask = Task.Run(() => DeleteFilesAndDirsExcept(artifactFileStagingPath, artifactDirectoryStagingPath, tracer));

                    // Lets the copy and cleanup tasks to run in parallel and wait for them to finish
                    await Task.WhenAll(copyTask, cleanTask);
                }
            }
            else
            {
                var srcInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(deploymentInfo.RepositoryUrl);
                using (tracer.Step(string.Format("Moving {0} to {1}", targetInfo.FullName, artifactFileStagingPath)))
                {
                    srcInfo.MoveTo(artifactFileStagingPath);
                }

                // Deletes all files and directories except for artifactFileStagingPath and artifactDirectoryStagingPath
                DeleteFilesAndDirsExcept(artifactFileStagingPath, artifactDirectoryStagingPath, tracer);
            }

            // The deployment flow expects at least 1 commit in the IRepository commit, refer to CommitRepo() for more info
            CommitRepo(repository, artifactDeploymentInfo);
        }
 private FileInfoBase[] ListLogFiles()
 {
     try
     {
         return(FileSystemHelpers.DirectoryInfoFromDirectoryName(_directoryPath).GetFiles(SiteExtensionLogSearchPattern));
     }
     catch
     {
         return(new FileInfoBase[0]);
     }
 }
Beispiel #12
0
        public virtual Task <HttpResponseMessage> DeleteItem(bool recursive = false)
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase dirInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (dirInfo.Attributes < 0)
            {
                HttpResponseMessage notFoundResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("'{0}' not found.", dirInfo.FullName));
                return(Task.FromResult(notFoundResponse));
            }
            else if ((dirInfo.Attributes & FileAttributes.Directory) != 0)
            {
                try
                {
                    dirInfo.Delete(recursive);
                }
                catch (Exception ex)
                {
                    Tracer.TraceError(ex);
                    HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
                        HttpStatusCode.Conflict, Resources.VfsControllerBase_CannotDeleteDirectory);
                    return(Task.FromResult(conflictDirectoryResponse));
                }

                // Delete directory succeeded.
                HttpResponseMessage successResponse = Request.CreateResponse(HttpStatusCode.OK);
                return(Task.FromResult(successResponse));
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to delete the file
                var fileInfo = FileSystemHelpers.FileInfoFromFileName(localFilePath);
                return(CreateFileDeleteResponse(fileInfo));
            }
        }
Beispiel #13
0
        private DirectoryInfoBase[] GetJobRunsDirectories(string jobName)
        {
            string            jobHistoryPath      = Path.Combine(JobsDataPath, jobName);
            DirectoryInfoBase jobHistoryDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(jobHistoryPath);

            if (!jobHistoryDirectory.Exists)
            {
                return(null);
            }

            return(jobHistoryDirectory.GetDirectories("*", SearchOption.TopDirectoryOnly));
        }
Beispiel #14
0
        private static int CalculateHashForJob(string jobBinariesPath)
        {
            var updateDatesString = new StringBuilder();
            DirectoryInfoBase jobBinariesDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(jobBinariesPath);

            FileInfoBase[] files = jobBinariesDirectory.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfoBase file in files)
            {
                updateDatesString.Append(file.LastWriteTimeUtc.Ticks);
            }

            return(updateDatesString.ToString().GetHashCode());
        }
        private Task LocalZipFetch(IRepository repository, DeploymentInfoBase deploymentInfo, string targetBranch,
                                   ILogger logger, ITracer tracer)
        {
            var zipDeploymentInfo = (ZipDeploymentInfo)deploymentInfo;

            // For this kind of deployment, RepositoryUrl is a local path.
            var sourceZipFile          = zipDeploymentInfo.RepositoryUrl;
            var extractTargetDirectory = repository.RepositoryPath;

            var info     = FileSystemHelpers.FileInfoFromFileName(sourceZipFile);
            var sizeInMb = (info.Length / (1024f * 1024f)).ToString("0.00", CultureInfo.InvariantCulture);

            var message = String.Format(
                CultureInfo.InvariantCulture,
                "Cleaning up temp folders from previous zip deployments and extracting pushed zip file {0} ({1} MB) to {2}",
                info.FullName,
                sizeInMb,
                extractTargetDirectory);

            logger.Log(message);

            using (tracer.Step(message))
            {
                // If extractTargetDirectory already exists, rename it so we can delete it concurrently with
                // the unzip (along with any other junk in the folder)
                var targetInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(extractTargetDirectory);
                if (targetInfo.Exists)
                {
                    var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
                    targetInfo.MoveTo(moveTarget);
                }

                DeleteFilesAndDirsExcept(sourceZipFile, extractTargetDirectory, tracer);

                FileSystemHelpers.CreateDirectory(extractTargetDirectory);

                using (var file = info.OpenRead())

                    using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
                    {
                        deploymentInfo.repositorySymlinks = zip.Extract(extractTargetDirectory, preserveSymlinks: ShouldPreserveSymlinks());

                        CreateZipSymlinks(deploymentInfo.repositorySymlinks, extractTargetDirectory);

                        PermissionHelper.ChmodRecursive("777", extractTargetDirectory, tracer, TimeSpan.FromMinutes(1));
                    }
            }

            CommitRepo(repository, zipDeploymentInfo);
            return(Task.CompletedTask);
        }
Beispiel #16
0
        private async Task LocalZipFetch(IRepository repository, DeploymentInfoBase deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
        {
            var zipDeploymentInfo = (ZipDeploymentInfo)deploymentInfo;

            // For this kind of deployment, RepositoryUrl is a local path.
            var sourceZipFile          = zipDeploymentInfo.RepositoryUrl;
            var extractTargetDirectory = repository.RepositoryPath;

            var info     = FileSystemHelpers.FileInfoFromFileName(sourceZipFile);
            var sizeInMb = (info.Length / (1024f * 1024f)).ToString("0.00", CultureInfo.InvariantCulture);

            var message = String.Format(
                CultureInfo.InvariantCulture,
                "Cleaning up temp folders from previous zip deployments and extracting pushed zip file {0} ({1} MB) to {2}",
                info.FullName,
                sizeInMb,
                extractTargetDirectory);

            logger.Log(message);

            using (tracer.Step(message))
            {
                // If extractTargetDirectory already exists, rename it so we can delete it concurrently with
                // the unzip (along with any other junk in the folder)
                var targetInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(extractTargetDirectory);
                if (targetInfo.Exists)
                {
                    var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
                    targetInfo.MoveTo(moveTarget);
                }

                var cleanTask   = Task.Run(() => DeleteFilesAndDirsExcept(sourceZipFile, extractTargetDirectory, tracer));
                var extractTask = Task.Run(() =>
                {
                    FileSystemHelpers.CreateDirectory(extractTargetDirectory);

                    using (var file = info.OpenRead())
                        using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
                        {
                            zip.Extract(extractTargetDirectory);
                        }
                });

                await Task.WhenAll(cleanTask, extractTask);
            }

            // Needed in order for repository.GetChangeSet() to work.
            // Similar to what OneDriveHelper and DropBoxHelper do.
            repository.Commit(zipDeploymentInfo.Message, zipDeploymentInfo.Author, zipDeploymentInfo.AuthorEmail);
        }
Beispiel #17
0
        public virtual Task <HttpResponseMessage> GetItem()
        {
            string localFilePath = GetLocalFilePath();

            HttpResponseMessage response;

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (info.Attributes < 0)
            {
                HttpResponseMessage notFoundResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("'{0}' not found.", info.FullName));
                return(Task.FromResult(notFoundResponse));
            }
            else if ((info.Attributes & FileAttributes.Directory) != 0)
            {
                // If request URI does NOT end in a "/" then redirect to one that does
                if (localFilePath[localFilePath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path += "/";
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }
                else
                {
                    return(CreateDirectoryGetResponse(info, localFilePath));
                }
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    HttpResponseMessage redirectResponse = Request.CreateResponse(HttpStatusCode.TemporaryRedirect);
                    UriBuilder          location         = new UriBuilder(Request.RequestUri);
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    redirectResponse.Headers.Location = location.Uri;
                    return(Task.FromResult(redirectResponse));
                }

                // We are ready to get the file
                return(CreateItemGetResponse(info, localFilePath));
            }
        }
        private static IEnumerable <SemanticVersion> GetSemanticVersionsFromFolder(string folder)
        {
            var directoryInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(folder);

            if (directoryInfo.Exists)
            {
                foreach (var dir in directoryInfo.GetDirectories())
                {
                    if (SemanticVersion.TryParse(dir.Name, out SemanticVersion semver))
                    {
                        yield return(semver);
                    }
                }
            }
        }
Beispiel #19
0
        private static IEnumerable <Dictionary <string, string> > GetNodeVersions()
        {
            string nodeRoot      = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "nodejs");
            var    directoryInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(nodeRoot);

            if (directoryInfo.Exists)
            {
                return(directoryInfo.GetDirectories()
                       .Where(dir => _versionRegex.IsMatch(dir.Name))
                       .Select(dir => new Dictionary <string, string>
                {
                    { VersionKey, dir.Name },
                    { "npm", TryReadNpmVersion(dir) }
                }));
            }
            return(Enumerable.Empty <Dictionary <string, string> >());
        }
        public virtual Task <IActionResult> DeleteItem(bool recursive = false)
        {
            string localFilePath = GetLocalFilePath();

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out IActionResult response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase dirInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (dirInfo.Attributes < 0)
            {
                return(Task.FromResult((IActionResult)NotFound(String.Format("'{0}' not found.", dirInfo.FullName))));
            }
            else if ((dirInfo.Attributes & FileAttributes.Directory) != 0)
            {
                try
                {
                    dirInfo.Delete(recursive);
                }
                catch (Exception ex)
                {
                    Tracer.TraceError(ex);
                    return(Task.FromResult((IActionResult)StatusCode(StatusCodes.Status409Conflict, Resources.VfsControllerBase_CannotDeleteDirectory)));
                }

                // Delete directory succeeded.
                return(Task.FromResult((IActionResult)Ok()));
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }

                // We are ready to delete the file
                var fileInfo = FileSystemHelpers.FileInfoFromFileName(localFilePath);
                return(CreateFileDeleteResponse(fileInfo));
            }
        }
        public static void CleanupDeletedJobs(IEnumerable <string> existingJobs, string jobsDataPath, ITracer tracer)
        {
            DirectoryInfoBase jobsDataDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(jobsDataPath);

            if (jobsDataDirectory.Exists)
            {
                DirectoryInfoBase[]  jobDataDirectories    = jobsDataDirectory.GetDirectories("*", SearchOption.TopDirectoryOnly);
                IEnumerable <string> allJobDataDirectories = jobDataDirectories.Select(j => j.Name);
                IEnumerable <string> directoriesToRemove   = allJobDataDirectories.Except(existingJobs, StringComparer.OrdinalIgnoreCase);
                foreach (string directoryToRemove in directoriesToRemove)
                {
                    using (tracer.Step("CleanupDeletedJobs"))
                    {
                        tracer.Trace("Removed job data path as the job was already deleted: " + directoryToRemove);
                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(jobsDataPath, directoryToRemove));
                    }
                }
            }
        }
        public virtual Task <IActionResult> GetItem()
        {
            string localFilePath = GetLocalFilePath();

            if (VfsSpecialFolders.TryHandleRequest(Request, localFilePath, out IActionResult response))
            {
                return(Task.FromResult(response));
            }

            DirectoryInfoBase info = FileSystemHelpers.DirectoryInfoFromDirectoryName(localFilePath);

            if (info.Attributes < 0)
            {
                return(Task.FromResult((IActionResult)NotFound(String.Format("'{0}' not found.", info.FullName))));
            }
            else if ((info.Attributes & FileAttributes.Directory) != 0)
            {
                // If request URI does NOT end in a "/" then redirect to one that does
                if (localFilePath[localFilePath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path += "/";
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }
                else
                {
                    return(CreateDirectoryGetResponse(info, localFilePath));
                }
            }
            else
            {
                // If request URI ends in a "/" then redirect to one that does not
                if (localFilePath[localFilePath.Length - 1] == Path.DirectorySeparatorChar)
                {
                    UriBuilder location = new UriBuilder(UriHelper.GetRequestUri(Request));
                    location.Path = location.Path.TrimEnd(_uriSegmentSeparator);
                    return(Task.FromResult((IActionResult)RedirectPreserveMethod(location.Uri.ToString())));
                }

                // We are ready to get the file
                return(CreateItemGetResponse(info, localFilePath));
            }
        }
Beispiel #23
0
        private static string GetPreInstalledLatestVersion(string directory)
        {
            IEnumerable <string> pathStrings = FileSystemHelpers.GetDirectories(directory);

            SemanticVersion maxVersion = pathStrings.Max(path =>
            {
                string versionString = FileSystemHelpers.DirectoryInfoFromDirectoryName(path).Name;
                SemanticVersion semVer;
                if (SemanticVersion.TryParse(versionString, out semVer))
                {
                    return(semVer);
                }
                else
                {
                    return(new SemanticVersion(0, 0, 0, 0));
                }
            });

            return(maxVersion.ToString());
        }
Beispiel #24
0
        private DirectoryInfoBase[] GetJobRunsDirectories(string jobName)
        {
            string            jobHistoryPath      = Path.Combine(JobsDataPath, jobName);
            DirectoryInfoBase jobHistoryDirectory = FileSystemHelpers.DirectoryInfoFromDirectoryName(jobHistoryPath);

            if (!jobHistoryDirectory.Exists)
            {
                return(null);
            }

            try
            {
                return(jobHistoryDirectory.GetDirectories("*", SearchOption.TopDirectoryOnly));
            }
            catch (Exception ex)
            {
                Analytics.UnexpectedException(ex);
                return(null);
            }
        }
Beispiel #25
0
        private static void SetPreInstalledExtensionInfo(SiteExtensionInfo info)
        {
            try
            {
                string directory = GetPreInstalledDirectory(info.Id);

                if (info.Type == SiteExtensionInfo.SiteExtensionType.PreInstalledNonKudu)
                {
                    IEnumerable <string> pathStrings = FileSystemHelpers.GetDirectories(directory);

                    SemanticVersion maxVersion = pathStrings.Max(path =>
                    {
                        string versionString = FileSystemHelpers.DirectoryInfoFromDirectoryName(path).Name;
                        SemanticVersion semVer;
                        if (SemanticVersion.TryParse(versionString, out semVer))
                        {
                            return(semVer);
                        }
                        else
                        {
                            return(new SemanticVersion(0, 0, 0, 0));
                        }
                    });

                    info.Version = maxVersion.ToString();
                }
                else if (info.Type == SiteExtensionInfo.SiteExtensionType.PreInstalledKuduModule)
                {
                    info.Version = typeof(SiteExtensionManager).Assembly.GetName().Version.ToString();
                }
            }
            catch (IOException)
            {
                info.Version = null;
            }

            info.LocalIsLatestVersion = true;
        }
Beispiel #26
0
        /// <summary>
        /// Populates a <see cref="ZipArchive"/> with the content of the function app.
        /// It can also include local.settings.json and .csproj files for a full Visual Studio project.
        /// sln file is not included since it changes between VS versions and VS can auto-generate it from the csproj.
        /// All existing functions are added as content with "Always" copy to output.
        /// </summary>
        /// <param name="zip">the <see cref="ZipArchive"/> to be populated with function app content.</param>
        /// <param name="includeAppSettings">Optional: indicates whether to add local.settings.json or not to the archive. Default is false.</param>
        /// <param name="includeCsproj">Optional: indicates whether to add a .csproj to the archive. Default is false.</param>
        /// <param name="projectName">Optional: the name for *.csproj file if <paramref name="includeCsproj"/> is true. Default is appName.</param>
        public void CreateArchive(ZipArchive zip, bool includeAppSettings = false, bool includeCsproj = false, string projectName = null)
        {
            var tracer        = _traceFactory.GetTracer();
            var directoryInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(_environment.FunctionsPath);

            // First add the entire wwwroot folder at the root of the zip.
            IList <ZipArchiveEntry> files;

            zip.AddDirectory(directoryInfo, tracer, string.Empty, out files);

            if (includeAppSettings)
            {
                // include local.settings.json if needed.
                files.Add(AddAppSettingsFile(zip));
            }

            if (includeCsproj)
            {
                // include .csproj for Visual Studio if needed.
                projectName = projectName ?? ServerConfiguration.GetApplicationName();
                AddCsprojFile(zip, files, projectName);
            }
        }
Beispiel #27
0
        private async Task OneDeployFetch(IRepository repository, DeploymentInfoBase deploymentInfo, string targetBranch,
                                          ILogger logger, ITracer tracer)
        {
            var artifactDeploymentInfo = (ArtifactDeploymentInfo)deploymentInfo;

            // For this kind of deployment, RepositoryUrl is a local path.
            var sourceZipFile = artifactDeploymentInfo.RepositoryUrl;

            // This is the path where the artifact being deployed is staged, before it is copied to the final target location
            var artifactDirectoryStagingPath = repository.RepositoryPath;

            var info     = FileSystemHelpers.FileInfoFromFileName(sourceZipFile);
            var sizeInMb = (info.Length / (1024f * 1024f)).ToString("0.00", CultureInfo.InvariantCulture);

            var message = String.Format(
                CultureInfo.InvariantCulture,
                "Cleaning up temp folders from previous zip deployments and extracting pushed zip file {0} ({1} MB) to {2}",
                info.FullName,
                sizeInMb,
                artifactDirectoryStagingPath);

            using (tracer.Step(message))
            {
                var targetInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(artifactDirectoryStagingPath);
                if (targetInfo.Exists)
                {
                    // If the staging path already exists, rename it so we can delete it later
                    var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
                    using (tracer.Step(string.Format("Renaming ({0}) to ({1})", targetInfo.FullName, moveTarget)))
                    {
                        targetInfo.MoveTo(moveTarget);
                    }
                }

                //
                // We want to create a directory structure under 'extractTargetDirectory'
                // such that it exactly matches the directory structure specified
                // by deploymentInfo.TargetSubDirectoryRelativePath
                //
                string stagingSubDirPath = artifactDirectoryStagingPath;

                if (!string.IsNullOrWhiteSpace(artifactDeploymentInfo.TargetSubDirectoryRelativePath))
                {
                    stagingSubDirPath = Path.Combine(artifactDirectoryStagingPath, artifactDeploymentInfo.TargetSubDirectoryRelativePath);
                }

                // Create artifact staging directory hierarchy before later use
                Directory.CreateDirectory(stagingSubDirPath);

                var artifactFileStagingPath = Path.Combine(stagingSubDirPath, deploymentInfo.TargetFileName);

                var srcInfo = FileSystemHelpers.DirectoryInfoFromDirectoryName(deploymentInfo.RepositoryUrl);
                using (tracer.Step(string.Format("Moving {0} to {1}", targetInfo.FullName, artifactFileStagingPath)))
                {
                    srcInfo.MoveTo(artifactFileStagingPath);
                }

                // Deletes all files and directories except for artifactFileStagingPath and artifactDirectoryStagingPath
                DeleteFilesAndDirsExcept(artifactFileStagingPath, artifactDirectoryStagingPath, tracer);

                // The deployment flow expects at least 1 commit in the IRepository commit, refer to CommitRepo() for more info
                CommitRepo(repository, artifactDeploymentInfo);
            }
        }