/// <summary>
        /// Gets a data package, either by downloading one or using the last one that was downloaded. Returns the path to the ZIP file for the package. Also
        /// archives downloaded data packages and deletes those that are too old to be useful. Installation Support Utility use only.
        /// </summary>
        public string GetDataPackage(bool forceNewPackageDownload, OperationResult operationResult)
        {
            var dataExportToRsisWebSiteNotPermitted = InstallationTypeElements is LiveInstallationElements liveInstallationElements &&
                                                      liveInstallationElements.DataExportToRsisWebSiteNotPermitted;

            if (dataExportToRsisWebSiteNotPermitted ? !File.Exists(IsuStatics.GetDataPackageZipFilePath(FullName)) : !DataPackageSize.HasValue)
            {
                return("");
            }

            var downloadedPackagesFolder = EwlStatics.CombinePaths(ConfigurationLogic.DownloadedDataPackagesFolderPath, FullName);

            var packageZipFilePath = "";

            // See if we can re-use an existing package.
            if (!forceNewPackageDownload && Directory.Exists(downloadedPackagesFolder))
            {
                var downloadedPackages = IoMethods.GetFilePathsInFolder(downloadedPackagesFolder);
                if (downloadedPackages.Any())
                {
                    packageZipFilePath = downloadedPackages.First();
                }
            }

            // Download a package from RSIS if the user forces this behavior or if there is no package available on disk.
            if (forceNewPackageDownload || packageZipFilePath.Length == 0)
            {
                packageZipFilePath = EwlStatics.CombinePaths(downloadedPackagesFolder, "{0}-Package.zip".FormatWith(DateTime.Now.ToString("yyyy-MM-dd")));

                // If the update data installation is a live installation for which data export to the RSIS web site is not permitted, get the data package from disk.
                if (dataExportToRsisWebSiteNotPermitted)
                {
                    IoMethods.CopyFile(IsuStatics.GetDataPackageZipFilePath(FullName), packageZipFilePath);
                }
                else
                {
                    operationResult.TimeSpentWaitingForNetwork =
                        EwlStatics.ExecuteTimedRegion(() => operationResult.NumberOfBytesTransferred = downloadDataPackage(packageZipFilePath));
                }
            }

            deleteOldFiles(downloadedPackagesFolder, InstallationTypeElements is LiveInstallationElements);
            return(packageZipFilePath);
        }
        /// <summary>
        /// Deletes all (but one - the last file is never deleted) *.zip files in the given folder that are old, but keeps increasingly sparse archive packages alive.
        /// </summary>
        private void deleteOldFiles(string folderPath, bool keepHistoricalArchive)
        {
            if (!Directory.Exists(folderPath))
            {
                return;
            }
            var files = IoMethods.GetFilePathsInFolder(folderPath, "*.zip");

            // Never delete the last (most recent) file. It makes it really inconvenient for developers if this happens.
            foreach (var fileName in files.Skip(1))
            {
                var creationTime = File.GetCreationTime(fileName);
                // We will delete everything more than 2 months old, keep saturday backups between 1 week and 2 months old, and keep everything less than 4 days old.
                if (!keepHistoricalArchive || creationTime < DateTime.Now.AddMonths(-2) ||
                    (creationTime < DateTime.Now.AddDays(-4) && creationTime.DayOfWeek != DayOfWeek.Saturday))
                {
                    IoMethods.DeleteFile(fileName);
                }
            }
        }
Exemple #3
0
        private static int Main(string[] args)
        {
            GlobalInitializationOps.InitStatics(new GlobalInitializer(), "Development Utility", true);
            try {
                return(GlobalInitializationOps.ExecuteAppWithStandardExceptionHandling(
                           () => {
                    try {
                        AppStatics.Init();

                        if (args.Length < 2)
                        {
                            throw new UserCorrectableException("You must specify the installation path as the first argument and the operation name as the second.");
                        }

                        // Create installations folder from template if necessary.
                        var installationPath = args[0];
                        var templateFolderPath = getInstallationsFolderPath(installationPath, true);
                        var message = "";
                        if (!Directory.Exists(templateFolderPath))
                        {
                            message = "No installation-configuration template exists.";
                        }
                        else
                        {
                            var configurationFolderPath = getInstallationsFolderPath(installationPath, false);
                            if (IoMethods.GetFilePathsInFolder(configurationFolderPath, searchOption: SearchOption.AllDirectories).Any())
                            {
                                message = "Installation configuration already exists.";
                            }
                            else
                            {
                                IoMethods.CopyFolder(templateFolderPath, configurationFolderPath, false);
                                Console.WriteLine("Created installation configuration from template.");
                            }
                        }

                        if (args[1] == "CreateInstallationConfiguration")
                        {
                            if (message.Any())
                            {
                                throw new UserCorrectableException(message);
                            }
                            return;
                        }

                        // Get installation.
                        DevelopmentInstallation installation;
                        try {
                            installation = getInstallation(installationPath);
                        }
                        catch (Exception e) {
                            throw new UserCorrectableException("The installation at \"" + installationPath + "\" is invalid.", e);
                        }

                        // Get operation.
                        var operations = AssemblyTools.BuildSingletonDictionary <Operation, string>(Assembly.GetExecutingAssembly(), i => i.GetType().Name);
                        var operationName = args[1];
                        if (!operations.ContainsKey(operationName))
                        {
                            throw new UserCorrectableException(operationName + " is not a known operation.");
                        }
                        var operation = operations[operationName];

                        if (!operation.IsValid(installation))
                        {
                            throw new UserCorrectableException("The " + operation.GetType().Name + " operation cannot be performed on this installation.");
                        }
                        operation.Execute(installation, args.Skip(2).ToImmutableArray(), new OperationResult());
                    }
                    catch (Exception e) {
                        Output.WriteTimeStampedError(e.ToString());
                        if (e is UserCorrectableException)
                        {
                            throw new DoNotEmailOrLogException();
                        }
                        throw;
                    }
                }));
            }
            finally {
                GlobalInitializationOps.CleanUpStatics();
            }
        }