private async Task <ActionResult> GetInstallFile(string organizationID, string platformID)
        {
            try
            {
                if (await FileLock.WaitAsync(TimeSpan.FromSeconds(15)))
                {
                    var    scheme       = AppConfig.RedirectToHttps ? "https" : Request.Scheme;
                    var    fileContents = new List <string>();
                    string fileName;
                    byte[] fileBytes;
                    switch (platformID)
                    {
                    case "Windows":
                    {
                        var filePath = Path.Combine(HostEnv.WebRootPath, "Downloads", $"Remotely_Installer.exe");
                        fileBytes = await System.IO.File.ReadAllBytesAsync(filePath);

                        fileName = $"Remotely_Installer-{organizationID}.exe";
                        break;
                    }

                    case "Linux-x64":
                    {
                        fileName = "Install-Linux-x64.sh";

                        fileContents.AddRange(await System.IO.File.ReadAllLinesAsync(Path.Combine(HostEnv.WebRootPath, "Downloads", $"{fileName}")));

                        var hostIndex = fileContents.IndexOf("HostName=");
                        var orgIndex  = fileContents.IndexOf("Organization=");

                        fileContents[hostIndex] = $"HostName=\"{scheme}://{Request.Host}\"";
                        fileContents[orgIndex]  = $"Organization=\"{organizationID}\"";
                        fileBytes = Encoding.UTF8.GetBytes(string.Join("\n", fileContents));
                        break;
                    }

                    default:
                        return(BadRequest());
                    }

                    return(File(fileBytes, "application/octet-stream", $"{fileName}"));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status408RequestTimeout));
                }
            }
            finally
            {
                if (FileLock.CurrentCount == 0)
                {
                    FileLock.Release();
                }
            }
        }
Ejemplo n.º 2
0
        private int GenerateAndDeployUnreliableTransportSettings()
        {
            string filePath = System.IO.Path.Combine(this.nodeSettings.DeploymentFoldersInfo.WorkFolder, UnreliableTransportSettings.SettingsFileName);

            DeployerTrace.WriteNoise("Writing Unreliable Transport Settings file at {0}", filePath);

            UnreliableTransportSettings unreliableTransportSettings = new UnreliableTransportSettings(this.clusterSettings);

            // acquiring lock to read and write to unreliable transport settings file.
            FileLock fileLock = new FileLock(filePath, false /* writing lock */);

            if (!fileLock.Acquire(new TimeSpan(0, 0, 5)))
            {
                DeployerTrace.WriteWarning("Failed to acquire lock to load Unreliable Transport Settings File. Aborting loading at file: {0}", filePath);
                return(Constants.ErrorCode_Failure);
            }

            // checks if it necessary to merge
            if (!File.Exists(filePath))
            {
                // writes the result to Unreliable Transport Settings File
                unreliableTransportSettings.WriteUnreliableTransportSettingsFile(filePath);
            }
            else
            {
                // creates a new UnreliableTransportSettings from existing UnreliableTransportSettings file
                UnreliableTransportSettings unreliableTransportSettingsExisting = new UnreliableTransportSettings(filePath);
                // merge files giving precedence to settings coming from cluster manifest
                unreliableTransportSettingsExisting.Merge(unreliableTransportSettings);
                // writes the result to Unreliable Transport Settings File
                unreliableTransportSettingsExisting.WriteUnreliableTransportSettingsFile(filePath);
            }
#if DotNetCoreClrLinux
            Helpers.UpdateFilePermission(filePath);
#endif
            // releasing lock.
            fileLock.Release();

            return(Constants.ErrorCode_Success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Asserts that the storage lock file in the given directory has been
        /// released.
        /// </summary>
        /// <remarks>
        /// Asserts that the storage lock file in the given directory has been
        /// released.  This method works by trying to acquire the lock file itself.  If
        /// locking fails here, then the main code must have failed to release it.
        /// </remarks>
        /// <param name="dir">the storage directory to check</param>
        /// <exception cref="System.IO.IOException">if there is an unexpected I/O error</exception>
        public static void AssertFileLockReleased(string dir)
        {
            StorageLocation sl       = StorageLocation.Parse(dir);
            FilePath        lockFile = new FilePath(sl.GetFile(), Storage.StorageFileLock);

            try
            {
                using (RandomAccessFile raf = new RandomAccessFile(lockFile, "rws"))
                {
                    using (FileChannel channel = raf.GetChannel())
                    {
                        FileLock Lock = channel.TryLock();
                        NUnit.Framework.Assert.IsNotNull(string.Format("Lock file at %s appears to be held by a different process."
                                                                       , lockFile.GetAbsolutePath()), Lock);
                        if (Lock != null)
                        {
                            try
                            {
                                Lock.Release();
                            }
                            catch (IOException e)
                            {
                                FsDatasetImpl.Log.Warn(string.Format("I/O error releasing file lock %s.", lockFile
                                                                     .GetAbsolutePath()), e);
                                throw;
                            }
                        }
                    }
                }
            }
            catch (OverlappingFileLockException)
            {
                NUnit.Framework.Assert.Fail(string.Format("Must release lock file at %s.", lockFile
                                                          .GetAbsolutePath()));
            }
        }