Esempio n. 1
0
        private XDocument GetDocument()
        {
            if (!FileSystemHelpers.FileExists(_path))
            {
                FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(_path));
                return(CreateDocumentRoot());
            }

            try
            {
                XDocument document;
                using (var stream = FileSystemHelpers.OpenRead(_path))
                {
                    document = XDocument.Load(stream);
                }

                return(document);
            }
            catch
            {
                // If the profile gets corrupted then delete it
                FileSystemHelpers.DeleteFileSafe(_path);

                // Return a new document
                return(CreateDocumentRoot());
            }
        }
Esempio n. 2
0
        internal static void SetupFileServer(IApplicationBuilder app, string fileDirectoryPath, string requestPath)
        {
            // Create deployment logs directory if it doesn't exist
            FileSystemHelpers.CreateDirectory(fileDirectoryPath);

            // Set up custom content types - associating file extension to MIME type
            var provider = new FileExtensionContentTypeProvider
            {
                Mappings =
                {
                    [".py"]     = "text/html",
                    [".env"]    = "text/html",
                    [".cshtml"] = "text/html",
                    [".log"]    = "text/html",
                    [".image"]  = "image/png"
                }
            };

            app.UseFileServer(new FileServerOptions
            {
                FileProvider = new PhysicalFileProvider(
                    fileDirectoryPath),
                RequestPath             = requestPath,
                EnableDirectoryBrowsing = true,
                StaticFileOptions       =
                {
                    ServeUnknownFileTypes = true,
                    DefaultContentType    = "text/plain",
                    ContentTypeProvider   = provider
                }
            });
        }
Esempio n. 3
0
        private SiteExtensionInfo EnablePreInstalledExtension(SiteExtensionInfo info, ITracer tracer)
        {
            string id = info.Id;
            string installationDirectory = GetInstallationDirectory(id);

            try
            {
                if (FileSystemHelpers.DirectoryExists(installationDirectory))
                {
                    FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                }

                if (ExtensionRequiresApplicationHost(info))
                {
                    if (info.Type == SiteExtensionInfo.SiteExtensionType.PreInstalledMonaco)
                    {
                        GenerateApplicationHostXdt(installationDirectory,
                                                   _preInstalledExtensionDictionary[id].ExtensionUrl, isPreInstalled: true);
                    }
                }
                else
                {
                    FileSystemHelpers.CreateDirectory(installationDirectory);
                }
            }
            catch (Exception ex)
            {
                tracer.TraceError(ex);
                FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                return(null);
            }

            return(GetPreInstalledExtension(id));
        }
Esempio n. 4
0
        /// <summary>
        /// <para>1. Download package</para>
        /// <para>2. Generate xdt file if not exist</para>
        /// <para>3. Deploy site extension job</para>
        /// <para>4. Execute install.cmd if exist</para>
        /// </summary>
        private async Task <UIPackageMetadata> InstallExtension(UIPackageMetadata package, string installationDirectory, string feedUrl)
        {
            ITracer tracer = _traceFactory.GetTracer();

            try
            {
                if (FileSystemHelpers.DirectoryExists(installationDirectory))
                {
                    FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                }

                SourceRepository remoteRepo = GetRemoteRepository(feedUrl);

                // Copy content folder
                // Copy nupkg file for package list/lookup
                FileSystemHelpers.CreateDirectory(installationDirectory);
                // package path from local repo
                string packageLocalFilePath = GetNuGetPackageFile(package.Identity.Id, package.Identity.Version.ToString());
                using (tracer.Step("Download site extension: {0}", package.Identity))
                {
                    await remoteRepo.DownloadPackageToFolder(package.Identity, installationDirectory, pathToLocalCopyOfNudpk : packageLocalFilePath);
                }

                // If there is no xdt file, generate default.
                using (tracer.Step("Check if applicationhost.xdt file existed."))
                {
                    GenerateApplicationHostXdt(installationDirectory, '/' + package.Identity.Id, isPreInstalled: false, tracer: tracer);
                }

                using (tracer.Step("Trigger site extension job"))
                {
                    OperationManager.Attempt(() => DeploySiteExtensionJobs(package.Identity.Id));
                }

                var    externalCommandFactory = new ExternalCommandFactory(_environment, _settings, installationDirectory);
                string installScript          = Path.Combine(installationDirectory, _installScriptName);
                if (FileSystemHelpers.FileExists(installScript))
                {
                    using (tracer.Step("Execute install.cmd"))
                    {
                        OperationManager.Attempt(() =>
                        {
                            Executable exe = externalCommandFactory.BuildCommandExecutable(installScript,
                                                                                           installationDirectory,
                                                                                           _settings.GetCommandIdleTimeout(), NullLogger.Instance);
                            exe.ExecuteWithProgressWriter(NullLogger.Instance, _traceFactory.GetTracer(), String.Empty);
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                tracer.TraceError(ex);
                FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                throw;
            }

            return(await _localRepository.GetLatestPackageById(package.Identity.Id));
        }
Esempio n. 5
0
        private IPackage InstallExtension(IPackage package, string installationDirectory)
        {
            try
            {
                if (FileSystemHelpers.DirectoryExists(installationDirectory))
                {
                    FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                }

                foreach (IPackageFile file in package.GetContentFiles())
                {
                    // It is necessary to place applicationHost.xdt under site extension root.
                    string contentFilePath = file.Path.Substring("content/".Length);
                    string fullPath        = Path.Combine(installationDirectory, contentFilePath);
                    FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(fullPath));
                    using (Stream writeStream = FileSystemHelpers.OpenWrite(fullPath), readStream = file.GetStream())
                    {
                        OperationManager.Attempt(() => readStream.CopyTo(writeStream));
                    }
                }

                // If there is no xdt file, generate default.
                GenerateApplicationHostXdt(installationDirectory, '/' + package.Id, isPreInstalled: false);

                OperationManager.Attempt(() => DeploySiteExtensionJobs(package.Id));

                var    externalCommandFactory = new ExternalCommandFactory(_environment, _settings, installationDirectory);
                string installScript          = Path.Combine(installationDirectory, _installScriptName);
                if (FileSystemHelpers.FileExists(installScript))
                {
                    OperationManager.Attempt(() =>
                    {
                        Executable exe = externalCommandFactory.BuildCommandExecutable(installScript,
                                                                                       installationDirectory,
                                                                                       _settings.GetCommandIdleTimeout(), NullLogger.Instance);
                        exe.ExecuteWithProgressWriter(NullLogger.Instance, _traceFactory.GetTracer(), String.Empty);
                    });
                }

                // Copy nupkg file for package list/lookup
                FileSystemHelpers.CreateDirectory(installationDirectory);
                string packageFilePath = GetNuGetPackageFile(package.Id, package.Version.ToString());
                using (
                    Stream readStream = package.GetStream(), writeStream = FileSystemHelpers.OpenWrite(packageFilePath))
                {
                    OperationManager.Attempt(() => readStream.CopyTo(writeStream));
                }
            }
            catch (Exception ex)
            {
                ITracer tracer = _traceFactory.GetTracer();
                tracer.TraceError(ex);
                FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                throw;
            }

            return(_localRepository.FindPackage(package.Id));
        }
        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);
        }
        private static async Task WriteExtensionsJson()
        {
            var file = Path.Combine(Environment.CurrentDirectory, ".vscode", "extensions.json");

            if (!FileSystemHelpers.DirectoryExists(Path.GetDirectoryName(file)))
            {
                FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(file));
            }

            await WriteFiles(file, await StaticResources.VsCodeExtensionsJson);
        }
Esempio n. 8
0
        private static void GenerateApplicationHostXdt(string installationDirectory, string relativeUrl, bool isPreInstalled)
        {
            // If there is no xdt file, generate default.
            FileSystemHelpers.CreateDirectory(installationDirectory);
            string xdtPath = Path.Combine(installationDirectory, _applicationHostFile);

            if (!FileSystemHelpers.FileExists(xdtPath))
            {
                string xdtContent = CreateDefaultXdtFile(relativeUrl, isPreInstalled);
                OperationManager.Attempt(() => FileSystemHelpers.WriteAllText(xdtPath, xdtContent));
            }
        }
        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);
        }
Esempio n. 10
0
        private static void CreateLockInfoFile(string operationName)
        {
            FileSystemHelpers.CreateDirectory(locksPath + "/deployment");
            var lockInfo = new LinuxLockInfo();

            lockInfo.heldByPID    = Process.GetCurrentProcess().Id;
            lockInfo.heldByTID    = Thread.CurrentThread.ManagedThreadId;
            lockInfo.heldByWorker = System.Environment.GetEnvironmentVariable(Constants.AzureWebsiteInstanceId);
            lockInfo.heldByOp     = operationName;
            lockInfo.lockExpiry   = DateTime.UtcNow.AddSeconds(defaultLockTimeout);
            var json = JsonConvert.SerializeObject(lockInfo);

            FileSystemHelpers.WriteAllText(locksPath + "/deployment/info.lock", json);
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        public IPackage InstallExtension(IPackage package, string installationDirectory)
        {
            try
            {
                if (FileSystemHelpers.DirectoryExists(installationDirectory))
                {
                    FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                }

                foreach (IPackageFile file in package.GetContentFiles())
                {
                    // It is necessary to place applicationHost.xdt under site extension root.
                    string contentFilePath = file.Path.Substring("content/".Length);
                    string fullPath        = Path.Combine(installationDirectory, contentFilePath);
                    FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(fullPath));
                    using (Stream writeStream = FileSystemHelpers.OpenWrite(fullPath), readStream = file.GetStream())
                    {
                        OperationManager.Attempt(() => readStream.CopyTo(writeStream));
                    }
                }

                // If there is no xdt file, generate default.
                string xdtPath = Path.Combine(installationDirectory, "applicationHost.xdt");
                if (!FileSystemHelpers.FileExists(xdtPath))
                {
                    string xdtContent = CreateDefaultXdtFile(package.Id);
                    OperationManager.Attempt(() => FileSystemHelpers.WriteAllText(xdtPath, xdtContent));
                }

                // Copy nupkg file for package list/lookup
                FileSystemHelpers.CreateDirectory(installationDirectory);
                string packageFilePath = Path.Combine(installationDirectory,
                                                      String.Format("{0}.{1}.nupkg", package.Id, package.Version));
                using (
                    Stream readStream = package.GetStream(), writeStream = FileSystemHelpers.OpenWrite(packageFilePath))
                {
                    OperationManager.Attempt(() => readStream.CopyTo(writeStream));
                }
            }
            catch (Exception ex)
            {
                ITracer tracer = _traceFactory.GetTracer();
                tracer.TraceError(ex);
                FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                return(null);
            }


            return(_localRepository.FindPackage(package.Id));
        }
Esempio n. 13
0
        private static void CreateLockInfoFile(string operationName)
        {
            FileSystemHelpers.CreateDirectory(locksPath + "/deployment");
            //Console.WriteLine("CreatingLockDir - Created Actually");
            var lockInfo = new LinuxLockInfo();

            lockInfo.heldByPID    = Process.GetCurrentProcess().Id;
            lockInfo.heldByTID    = Thread.CurrentThread.ManagedThreadId;
            lockInfo.heldByWorker = System.Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
            lockInfo.heldByOp     = operationName;
            lockInfo.lockExpiry   = DateTime.UtcNow.AddSeconds(lockTimeout);
            //Console.WriteLine("CreatingLockDir - LockInfoObj : "+lockInfo);
            var json = JsonConvert.SerializeObject(lockInfo);

            FileSystemHelpers.WriteAllText(locksPath + "/deployment/info.lock", json);
        }
Esempio n. 14
0
        private static void GenerateApplicationHostXdt(string installationDirectory, string relativeUrl, ITracer tracer = null)
        {
            // If there is no xdt file, generate default.
            FileSystemHelpers.CreateDirectory(installationDirectory);
            string xdtPath = Path.Combine(installationDirectory, Constants.ApplicationHostXdtFileName);

            if (!FileSystemHelpers.FileExists(xdtPath))
            {
                if (tracer != null)
                {
                    tracer.Trace("Missing xdt file, creating one.");
                }

                string xdtContent = CreateDefaultXdtFile(relativeUrl, "%XDT_EXTENSIONPATH%");
                OperationManager.Attempt(() => FileSystemHelpers.WriteAllText(xdtPath, xdtContent));
            }
        }
        private async Task WriteSample()
        {
            if (InitSample)
            {
                const string functionJson = @"{
  ""disabled"": false,
  ""bindings"": [
    {
      ""authLevel"": ""anonymous"",
      ""type"": ""httpTrigger"",
      ""direction"": ""in"",
      ""name"": ""req""
    },
    {
      ""type"": ""http"",
      ""direction"": ""out"",
      ""name"": ""res""
    }
  ]
}
";
                const string indexJs      = @"module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: 'Hello ' + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: 'Please pass a name on the query string or in the request body'
        };
    }
    context.done();
};";
                FileSystemHelpers.CreateDirectory("HttpFunction");
                await WriteFiles(Path.Combine("HttpFunction", "function.json"), functionJson);
                await WriteFiles(Path.Combine("HttpFunction", "index.js"), indexJs);
            }
        }
        public static async Task <NodeDebuggerStatus> TrySetupNodeDebuggerAsync()
        {
            try
            {
                var existingLaunchJson = await(FileSystemHelpers.FileExists(LaunchJsonPath)
                    ? TaskUtilities.SafeGuardAsync(async() => JsonConvert.DeserializeObject <VsCodeLaunch>(await FileSystemHelpers.ReadAllTextFromFileAsync(LaunchJsonPath)))
                    : Task.FromResult <VsCodeLaunch>(null));

                FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(LaunchJsonPath));

                if (existingLaunchJson == null)
                {
                    await FileSystemHelpers.WriteAllTextToFileAsync(LaunchJsonPath, JsonConvert.SerializeObject(DefaultLaunchJson, Formatting.Indented));

                    return(NodeDebuggerStatus.Created);
                }

                var functionsDebugConfig = existingLaunchJson.Configurations
                                           .FirstOrDefault(c => c.Type.Equals("node", StringComparison.OrdinalIgnoreCase) &&
                                                           c.Request.Equals("attach", StringComparison.OrdinalIgnoreCase));

                if (functionsDebugConfig == null)
                {
                    existingLaunchJson.Configurations = existingLaunchJson.Configurations.Concat(DefaultLaunchJson.Configurations);
                    await FileSystemHelpers.WriteAllTextToFileAsync(LaunchJsonPath, JsonConvert.SerializeObject(existingLaunchJson, Formatting.Indented));

                    return(NodeDebuggerStatus.Updated);
                }
                else
                {
                    return(NodeDebuggerStatus.AlreadyCreated);
                }
            }
            catch (Exception e)
            {
                if (StaticSettings.IsDebug)
                {
                    ColoredConsole.Error.WriteLine(ErrorColor(e.ToString()));
                }
                return(NodeDebuggerStatus.Error);
            }
        }
Esempio n. 17
0
        protected override async Task <HttpResponseMessage> CreateDirectoryPutResponse(DirectoryInfoBase info, string localFilePath)
        {
            try
            {
                var     isRequestJSON  = Request.Content.Headers?.ContentType?.MediaType?.Equals("application/json", StringComparison.OrdinalIgnoreCase);
                var     targetPath     = localFilePath;
                var     isArmTemplate  = false;
                JObject requestContent = null;
                Uri     packageUri     = null;
                if (isRequestJSON == true)
                {
                    requestContent = await Request.Content.ReadAsAsync <JObject>();

                    var payload = requestContent;
                    if (ArmUtils.IsArmRequest(Request))
                    {
                        payload       = payload.Value <JObject>("properties");
                        isArmTemplate = ArmUtils.IsAzureResourceManagerUserAgent(Request);
                    }

                    var uri = payload?.Value <string>("packageUri");
                    if (!Uri.TryCreate(uri, UriKind.Absolute, out packageUri))
                    {
                        throw new InvalidOperationException($"Payload contains invalid '{uri}' packageUri property");
                    }

                    var path = payload?.Value <string>("path");
                    if (!string.IsNullOrEmpty(path))
                    {
                        targetPath = Path.Combine(targetPath, path);
                        FileSystemHelpers.CreateDirectory(targetPath);
                    }
                }

                using (packageUri == null ? Tracer.Step($"Extracting content to {targetPath}")
                    : Tracer.Step("Extracting content from {0} to {1}", StringUtils.ObfuscatePath(packageUri.AbsoluteUri), targetPath))
                {
                    var content = packageUri == null ? Request.Content
                        : await DeploymentHelper.GetArtifactContentFromURL(new ArtifactDeploymentInfo(null, null) { RemoteURL = packageUri.AbsoluteUri }, Tracer);

                    using (var stream = await content.ReadAsStreamAsync())
                    {
                        // The unzipping is done over the existing folder, without first removing existing files.
                        // Hence it's more of a PATCH than a PUT. We should consider supporting both with the right semantic.
                        // Though a true PUT at the root would be scary as it would wipe all existing files!
                        var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
                        zipArchive.Extract(targetPath, Tracer);
                    }
                }

                if (isArmTemplate && requestContent != null)
                {
                    requestContent.Value <JObject>("properties").Add("provisioningState", "Succeeded");
                    return(Request.CreateResponse(HttpStatusCode.OK, requestContent));
                }

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(ArmUtils.CreateErrorResponse(Request, HttpStatusCode.BadRequest, ex));
            }
        }
Esempio n. 18
0
        public async Task <ScanRequestResult> StartScan(String timeout, String mainScanDirPath, String id, String host)
        {
            using (_tracer.Step("Start scan in the background"))
            {
                String  folderPath          = Path.Combine(mainScanDirPath, Constants.ScanFolderName + id);
                String  filePath            = Path.Combine(folderPath, Constants.ScanStatusFile);
                Boolean hasFileModifcations = true;

                if (_deploymentLock.IsHeld)
                {
                    return(ScanRequestResult.ScanAlreadyInProgress);
                }

                //Create unique scan folder and scan status file
                _deploymentLock.LockOperation(() =>
                {
                    //Check if files are modified
                    if (CheckModifications(mainScanDirPath))
                    {
                        //Create unique scan directory for current scan
                        FileSystemHelpers.CreateDirectory(folderPath);
                        _tracer.Trace("Unique scan directory created for scan {0}", id);

                        //Create scan status file inside folder
                        FileSystemHelpers.CreateFile(filePath).Close();

                        //Create temp file to check if scan is still running
                        string tempScanFilePath = GetTempScanFilePath(mainScanDirPath);
                        tempScanFilePath        = Path.Combine(mainScanDirPath, Constants.TempScanFile);
                        FileSystemHelpers.CreateFile(tempScanFilePath).Close();

                        UpdateScanStatus(folderPath, ScanStatus.Starting);
                    }
                    else
                    {
                        hasFileModifcations = false;
                    }
                }, "Creating unique scan folder", TimeSpan.Zero);

                if (!hasFileModifcations)
                {
                    return(ScanRequestResult.NoFileModifications);
                }

                //Start Backgorund Scan
                using (var timeoutCancellationTokenSource = new CancellationTokenSource())
                {
                    var successfullyScanned = PerformBackgroundScan(_tracer, folderPath, timeoutCancellationTokenSource.Token, id, mainScanDirPath);

                    //Wait till scan task completes or the timeout goes off
                    if (await Task.WhenAny(successfullyScanned, Task.Delay(Int32.Parse(timeout), timeoutCancellationTokenSource.Token)) == successfullyScanned)
                    {
                        //If scan task completes before timeout
                        //Delete excess scan folders, just keep the maximum number allowed
                        await DeletePastScans(mainScanDirPath, _tracer);

                        //Create new Manifest file containing the modified timestamps
                        String manifestPath = Path.Combine(mainScanDirPath, Constants.ScanManifest);
                        if (FileSystemHelpers.FileExists(manifestPath))
                        {
                            FileSystemHelpers.DeleteFileSafe(manifestPath);
                        }
                        JObject manifestObj = new JObject();

                        //Write to the manifest with new timestamps of the modified file
                        ModifyManifestFile(manifestObj, Constants.ScanDir);
                        File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifestObj));

                        //Path to common log file for azure monitor
                        String aggrLogPath = Path.Combine(mainScanDirPath, Constants.AggregrateScanResults);

                        //This checks if result scan log is formed
                        //If yes, it will append necessary logs to the aggregrate log file
                        //Current appended logs will be "Scanned files","Infected files", and details of infected files
                        String currLogPath = Path.Combine(folderPath, Constants.ScanLogFile);
                        if (FileSystemHelpers.FileExists(currLogPath))
                        {
                            StreamReader file = new StreamReader(currLogPath);
                            string       line;
                            while ((line = file.ReadLine()) != null)
                            {
                                if (line.Contains("FOUND") || line.Contains("Infected files") || line.Contains("Scanned files"))
                                {
                                    //logType "Infected" means this log line represents details of infected files
                                    String logType = "Infected";
                                    if (line.Contains("Infected files") || line.Contains("Scanned files"))
                                    {
                                        //logType "Info" means this log line represents total number of scanned or infected files
                                        logType = "Info";
                                    }
                                    FileSystemHelpers.AppendAllTextToFile(aggrLogPath, DateTime.UtcNow.ToString(@"M/d/yyyy hh:mm:ss tt") + "," + id + "," + logType + "," + host + "," + line + '\n');
                                }
                            }
                        }

                        return(successfullyScanned.Result
                        ? ScanRequestResult.RunningAynschronously
                        : ScanRequestResult.AsyncScanFailed);
                    }
                    else
                    {
                        //Timeout went off before scan task completion
                        //Cancel scan task
                        timeoutCancellationTokenSource.Cancel();

                        //Scan process will be cancelled
                        //wait till scan status file is appropriately updated
                        await successfullyScanned;

                        //Delete excess scan folders, just keep the maximum number allowed
                        await DeletePastScans(mainScanDirPath, _tracer);

                        return(ScanRequestResult.AsyncScanFailed);
                    }
                }
            }
        }
Esempio n. 19
0
        /// <summary>
        /// <para>Handle both file and folder create/update</para>
        /// <para>If local file timestamp the same as the file in server, assume file/folder unchanged, will skip action.</para>
        /// </summary>
        private async Task HandlingUpdateOrCreate(OneDriveModel.OneDriveChange change, string wwwroot, string accessToken)
        {
            if (change.IsDeleted)
            {
                return;
            }

            string fullPath = GetDestinationPath(wwwroot, change.Path);

            if (fullPath == null)
            {
                TraceMessage("Ignore folder {0}", change.Path);
                return;
            }

            if (change.IsFile)
            {
                // return "1/1/1601 12:00:00 AM" when file not existed
                DateTime lastWriteTime = FileSystemHelpers.GetLastWriteTimeUtc(fullPath);
                if (lastWriteTime != change.LastModifiedUtc)
                {
                    string parentDir = Path.GetDirectoryName(fullPath);
                    FileSystemHelpers.EnsureDirectory(parentDir);

                    var now     = DateTime.UtcNow;
                    var success = false;
                    try
                    {
                        using (var client = CreateHttpClient(accessToken))
                            using (HttpResponseMessage response = await client.GetAsync(change.ContentUri))
                            {
                                var fileResponse = await ProcessResponse <Dictionary <string, object> >("HandlingUpdateOrCreate", response);

                                string downloadUrl = (string)fileResponse["@content.downloadUrl"];

                                using (HttpResponseMessage downloadResponse = await client.GetAsync(downloadUrl))
                                    using (Stream stream = await downloadResponse.EnsureSuccessStatusCode().Content.ReadAsStreamAsync())
                                    {
                                        await WriteToFile(stream, fullPath);
                                    }
                            }

                        FileSystemHelpers.SetLastWriteTimeUtc(fullPath, change.LastModifiedUtc);

                        success = true;
                    }
                    finally
                    {
                        var elapse = (DateTime.UtcNow - now).TotalMilliseconds;
                        var result = success ? "successful" : "failed";
                        if (FileSystemHelpers.FileExists(fullPath))
                        {
                            TraceMessage("Update file {0} {1} ({2:0}ms)", fullPath, result, elapse);
                        }
                        else
                        {
                            TraceMessage("Create file {0} {1} ({2:0}ms)", fullPath, result, elapse);
                        }
                    }
                }
                else
                {
                    _tracer.Trace("Timestamp not changed. Skip updating file {0}", fullPath);
                }
            }
            else
            {
                if (FileSystemHelpers.DirectoryExists(fullPath))
                {
                    TraceMessage("Directory {0} exists, no action performed.", fullPath);
                }
                else
                {
                    TraceMessage("Creating directory {0} ...", fullPath);
                    FileSystemHelpers.CreateDirectory(fullPath);
                }
            }
        }
Esempio n. 20
0
        /// <summary>
        /// <para>1. Download package</para>
        /// <para>2. Generate xdt file if not exist</para>
        /// <para>3. Deploy site extension job</para>
        /// <para>4. Execute install.cmd if exist</para>
        /// </summary>
        private async Task <UIPackageMetadata> InstallExtension(UIPackageMetadata package, string installationDirectory, string feedUrl, SiteExtensionInfo.SiteExtensionType type, ITracer tracer)
        {
            try
            {
                if (FileSystemHelpers.DirectoryExists(installationDirectory))
                {
                    FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                }

                SourceRepository remoteRepo = GetRemoteRepository(feedUrl);

                // Copy content folder
                // Copy nupkg file for package list/lookup
                FileSystemHelpers.CreateDirectory(installationDirectory);
                // package path from local repo
                string packageLocalFilePath = GetNuGetPackageFile(package.Identity.Id, package.Identity.Version.ToString());
                using (tracer.Step("Download site extension: {0}", package.Identity))
                {
                    string extractPath = installationDirectory;
                    if (SiteExtensionInfo.SiteExtensionType.WebRoot == type)
                    {
                        extractPath = _environment.WebRootPath;
                        FileSystemHelpers.EnsureDirectory(extractPath);
                    }

                    await remoteRepo.DownloadPackageToFolder(package.Identity, extractPath, pathToLocalCopyOfNudpk : packageLocalFilePath);

                    if (SiteExtensionInfo.SiteExtensionType.WebRoot == type)
                    {
                        // if install to WebRoot, check if there is any xdt file come with package
                        // if there is one, move it to site extension folder
                        string xdtFile = Path.Combine(extractPath, Constants.ApplicationHostXdtFileName);
                        if (File.Exists(xdtFile))
                        {
                            tracer.Trace("Use xdt file from package.");
                            string newXdtFile = Path.Combine(installationDirectory, Constants.ApplicationHostXdtFileName);

                            tracer.Trace("Moving {0} to {1}", xdtFile, newXdtFile);
                            FileSystemHelpers.MoveFile(xdtFile, newXdtFile);
                        }
                        else
                        {
                            tracer.Trace("No xdt file come with package.");
                        }
                    }
                }

                // ignore below action if we install packge to wwwroot
                if (SiteExtensionInfo.SiteExtensionType.WebRoot != type)
                {
                    // If there is no xdt file, generate default.
                    using (tracer.Step("Check if applicationhost.xdt file existed."))
                    {
                        GenerateApplicationHostXdt(installationDirectory, '/' + package.Identity.Id, isPreInstalled: false, tracer: tracer);
                    }

                    using (tracer.Step("Trigger site extension job"))
                    {
                        OperationManager.Attempt(() => DeploySiteExtensionJobs(package.Identity.Id));
                    }

                    var    externalCommandFactory = new ExternalCommandFactory(_environment, _settings, installationDirectory);
                    string installScript          = Path.Combine(installationDirectory, _installScriptName);
                    if (FileSystemHelpers.FileExists(installScript))
                    {
                        using (tracer.Step("Execute install.cmd"))
                        {
                            OperationManager.Attempt(() =>
                            {
                                Executable exe = externalCommandFactory.BuildCommandExecutable(installScript,
                                                                                               installationDirectory,
                                                                                               _settings.GetCommandIdleTimeout(), NullLogger.Instance);
                                exe.ExecuteWithProgressWriter(NullLogger.Instance, _traceFactory.GetTracer(), String.Empty);
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                tracer.TraceError(ex);
                FileSystemHelpers.DeleteDirectorySafe(installationDirectory);
                throw;
            }

            return(await _localRepository.GetLatestPackageById(package.Identity.Id));
        }
Esempio n. 21
0
        /// <summary>
        /// Helper function to download package from given url, and place package (only 'content' folder from package) to given folder
        /// </summary>
        /// <param name="identity">Package identity</param>
        /// <param name="destinationFolder">Folder where we copy the package content (content folder only) to</param>
        /// <param name="pathToLocalCopyOfNudpk">File path where we copy the nudpk to</param>
        /// <returns></returns>
        public static async Task DownloadPackageToFolder(this SourceRepository srcRepo, PackageIdentity identity, string destinationFolder, string pathToLocalCopyOfNudpk = null)
        {
            var downloadResource = await srcRepo.GetResourceAndValidateAsync <DownloadResource>();

            using (Stream sourceStream = await downloadResource.GetStream(identity, CancellationToken.None))
            {
                if (sourceStream == null)
                {
                    // package not exist from feed
                    throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Package {0} - {1} not found when try to download.", identity.Id, identity.Version.ToNormalizedString()));
                }

                Stream packageStream = sourceStream;
                try
                {
                    if (!sourceStream.CanSeek)
                    {
                        // V3 DownloadResource.GetStream does not support seek operations.
                        // https://github.com/NuGet/NuGet.Protocol/issues/15

                        MemoryStream memStream = new MemoryStream();

                        try
                        {
                            byte[] buffer = new byte[2048];

                            int bytesRead = 0;
                            do
                            {
                                bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
                                memStream.Write(buffer, 0, bytesRead);
                            } while (bytesRead != 0);

                            await memStream.FlushAsync();

                            memStream.Position = 0;

                            packageStream = memStream;
                        }
                        catch
                        {
                            memStream.Dispose();
                            throw;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(pathToLocalCopyOfNudpk))
                    {
                        string packageFolderPath = Path.GetDirectoryName(pathToLocalCopyOfNudpk);
                        FileSystemHelpers.CreateDirectory(packageFolderPath);
                        using (Stream writeStream = FileSystemHelpers.OpenWrite(pathToLocalCopyOfNudpk))
                        {
                            OperationManager.Attempt(() => packageStream.CopyTo(writeStream));
                        }

                        // set position back to the head of stream
                        packageStream.Position = 0;
                    }

                    using (ZipFile zipFile = ZipFile.Read(packageStream))
                    {
                        // we only care about stuff under "content" folder
                        int substringStartIndex = @"content/".Length;
                        IEnumerable <ZipEntry> contentEntries = zipFile.Entries.Where(e => e.FileName.StartsWith(@"content/", StringComparison.InvariantCultureIgnoreCase));
                        foreach (var entry in contentEntries)
                        {
                            string fullPath = Path.Combine(destinationFolder, entry.FileName.Substring(substringStartIndex));
                            FileSystemHelpers.CreateDirectory(Path.GetDirectoryName(fullPath));
                            using (Stream writeStream = FileSystemHelpers.OpenWrite(fullPath))
                            {
                                // let the thread go with itself, so that once file finsihed writing, doesn`t need to request thread context from main thread
                                await entry.OpenReader().CopyToAsync(writeStream).ConfigureAwait(false);
                            }
                        }
                    }
                }
                finally
                {
                    if (packageStream != null && !sourceStream.CanSeek)
                    {
                        // in this case, we created a copy of the source stream in memoery, need to manually dispose
                        packageStream.Dispose();
                    }
                }
            }
        }