Beispiel #1
0
        public void backup_existing_version(ChocolateyConfiguration config, IPackage installedPackage)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageBackupLocation);

            var pathResolver   = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
            var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);

            if (!_fileSystem.directory_exists(pkgInstallPath))
            {
                var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
                if (chocoPathResolver != null)
                {
                    chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
                    pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
                }
            }

            if (_fileSystem.directory_exists(pkgInstallPath))
            {
                this.Log().Debug("Backing up existing {0} prior to upgrade.".format_with(installedPackage.Id));

                var backupLocation = pkgInstallPath.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageBackupLocation);

                var errored = false;
                try
                {
                    _fileSystem.move_directory(pkgInstallPath, backupLocation);
                }
                catch (Exception ex)
                {
                    errored = true;
                    this.Log().Error("Error during backup (move phase):{0} {1}".format_with(Environment.NewLine, ex.Message));
                }
                finally
                {
                    try
                    {
                        _fileSystem.copy_directory(backupLocation, pkgInstallPath, overwriteExisting: true);
                    }
                    catch (Exception ex)
                    {
                        errored = true;
                        this.Log().Error("Error during backup (reset phase):{0} {1}".format_with(Environment.NewLine, ex.Message));
                    }
                }

                backup_configuration_files(pkgInstallPath, installedPackage.Version.to_string());

                if (errored)
                {
                    this.Log().Warn(ChocolateyLoggers.Important,
                                    @"There was an error accessing files. This could mean there is a 
 process locking the folder or files. Please make sure nothing is 
 running that would lock the files or folders in this directory prior 
 to upgrade. If the package fails to upgrade, this is likely the cause.");
                }
            }
        }
Beispiel #2
0
        public void rename_legacy_package_version(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            if (pkgInfo != null && pkgInfo.IsSideBySide)
            {
                return;
            }

            var installDirectory = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id);

            if (!_fileSystem.directory_exists(installDirectory))
            {
                // if the folder has a version on it, we need to rename the folder first.
                var pathResolver = new ChocolateyPackagePathResolver(NugetCommon.GetNuGetFileSystem(config, _nugetLogger), useSideBySidePaths: true);
                installDirectory = pathResolver.GetInstallPath(installedPackage);
                if (_fileSystem.directory_exists(installDirectory))
                {
                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.move_directory(installDirectory, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)),
                        "Error during old package rename");
                }
            }
        }
Beispiel #3
0
        public void backup_existing_version(ChocolateyConfiguration config, IPackage installedPackage)
        {
            var pathResolver   = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
            var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);

            if (!_fileSystem.directory_exists(pkgInstallPath))
            {
                var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
                if (chocoPathResolver != null)
                {
                    chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
                    pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
                }
            }

            if (_fileSystem.directory_exists(pkgInstallPath))
            {
                this.Log().Debug("Backing up existing {0} prior to upgrade.".format_with(installedPackage.Id));

                var backupLocation = pkgInstallPath + ApplicationParameters.RollbackPackageSuffix;
                _fileSystem.copy_directory(pkgInstallPath, backupLocation, overwriteExisting: true);
            }
        }
Beispiel #4
0
        public PackageManagerEx GetPackageManager(
            ChocolateyConfiguration configuration,
            Action <PackageOperationEventArgs> installSuccessAction   = null,
            Action <PackageOperationEventArgs> uninstallSuccessAction = null,
            bool addUninstallHandler = false
            )
        {
            IFileSystem          nugetPackagesFileSystem = NugetCommon.GetNuGetFileSystem(configuration, _nugetLogger);
            IPackagePathResolver pathResolver            = NugetCommon.GetPathResolver(configuration, nugetPackagesFileSystem);
            var packageManager = new PackageManagerEx(_registryService, NugetCommon.GetRemoteRepository(configuration, _nugetLogger, _packageDownloader), pathResolver, nugetPackagesFileSystem,
                                                      NugetCommon.GetLocalRepository(pathResolver, nugetPackagesFileSystem, _nugetLogger))
            {
                DependencyVersion = DependencyVersion.Highest,
                Logger            = _nugetLogger,
            };

            // GH-1548
            //note: is this a good time to capture a backup (for dependencies) / maybe grab remembered arguments here instead / and somehow get out of the endless loop!
            //NOTE DO NOT EVER use this method - packageManager.PackageInstalling += (s, e) => { };

            packageManager.PackageInstalled += (s, e) =>
            {
                var pkg = e.Package;
                "chocolatey".Log().Info(ChocolateyLoggers.Important, "{0}{1} v{2}{3}{4}{5}".format_with(
                                            System.Environment.NewLine,
                                            pkg.Id,
                                            pkg.Version.to_string(),
                                            configuration.Force ? " (forced)" : string.Empty,
                                            pkg.IsApproved ? " [Approved]" : string.Empty,
                                            pkg.PackageTestResultStatus == "Failing" && pkg.IsDownloadCacheAvailable ? " - Likely broken for FOSS users (due to download location changes)" : pkg.PackageTestResultStatus == "Failing" ? " - Possibly broken" : string.Empty
                                            ));

                if (installSuccessAction != null)
                {
                    installSuccessAction.Invoke(e);
                }
            };

            if (addUninstallHandler)
            {
                // NOTE DO NOT EVER use this method, or endless loop - packageManager.PackageUninstalling += (s, e) =>

                packageManager.PackageUninstalled += (s, e) =>
                {
                    IPackage pkg = packageManager.LocalRepository.FindPackage(e.Package.Id, e.Package.Version);
                    if (pkg != null)
                    {
                        // install not actually removed, let's clean it up. This is a bug with nuget, where it reports it removed some package and did NOTHING
                        // this is what happens when you are switching from AllowMultiple to just one and back
                        var chocoPathResolver = packageManager.PathResolver as ChocolateyPackagePathResolver;
                        if (chocoPathResolver != null)
                        {
                            chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;

                            // an unfound package folder can cause an endless loop.
                            // look for it and ignore it if doesn't line up with versioning
                            if (nugetPackagesFileSystem.DirectoryExists(chocoPathResolver.GetInstallPath(pkg)))
                            {
                                //todo: This causes an issue with upgrades.
                                // this causes this to be called again, which should then call the uninstallSuccessAction below
                                packageManager.UninstallPackage(pkg, forceRemove: configuration.Force, removeDependencies: false);
                            }

                            chocoPathResolver.UseSideBySidePaths = configuration.AllowMultipleVersions;
                        }
                    }
                    else
                    {
                        if (uninstallSuccessAction != null)
                        {
                            uninstallSuccessAction.Invoke(e);
                        }
                    }
                };
            }

            return(packageManager);
        }