/// <summary>
        /// Installs the packages found in the package source folder.
        /// </summary>
        private void InstallPackages()
        {
            //Check to see if there is a post-step pending, and skip package install if there is
            if (File.Exists(Path.Combine(_packageSource, STARTUP_POST_STEP_PACKAGE_FILENAME)))
            {
                Log.Info("Install packages skipped because there is a post step pending", this);

                return;
            }

            InstallLogger installLogger = new InstallLogger(new RootLogger(Level.ALL));

            //Return if another installation is happening
            if (_installingPackage)
            {
                Log.Info("Install packages skipped because another package is being installed.", this);

                return;
            }

            try
            {
                //Block further package installs
                _installingPackage = true;

                //Find pending packages. This loop may not complete if there were binary/config changes
                foreach (string updatePackageFilename in Directory.GetFiles(_packageSource, "*.update", SearchOption.TopDirectoryOnly))
                {
                    if (ShutdownDetected)
                    {
                        Log.Info("Install packages aborting dur to shutdown", this);

                        break;
                    }

                    //Prevent shutdown
                    using (new ShutdownGuard())
                    {
                        PackageInstallationInfo installationInfo = new PackageInstallationInfo
                        {
                            Action = UpgradeAction.Upgrade,
                            Mode = InstallMode.Install,
                            Path = updatePackageFilename
                        };

                        string installationHistoryRoot = null;
                        List<ContingencyEntry> logMessages = new List<ContingencyEntry>();

                        try
                        {
                            //Run the installer
                            logMessages = UpdateHelper.Install(installationInfo, installLogger, out installationHistoryRoot);

                            if (_updateConfigurationFiles)
                            {
                                FindAndUpdateChangedConfigs(Path.GetFileNameWithoutExtension(updatePackageFilename));
                            }

                            //Sleep for 4 seconds to see if there was a file change that could cause a problem
                            Thread.Sleep(4000);

                            //Abort if Sitecore is shutting down. The install post steps will have to be completed later
                            if (ShutdownDetected)
                            {
                                RunPostStepsAtStartup(updatePackageFilename, installationHistoryRoot);

                                RestartSitecoreServer();

                                break;
                            }
                            else
                            {
                                ExecutePostSteps(new PostStepDetails
                                {
                                    HistoryPath = installationHistoryRoot,
                                    PostStepPackageFilename = updatePackageFilename
                                });
                            }
                        }
                        catch (PostStepInstallerException ex)
                        {
                            logMessages = ex.Entries;
                            installationHistoryRoot = ex.HistoryPath;

                            throw ex;
                        }
                        catch (Exception ex)
                        {
                            Log.Error("Package install failed", ex, this);

                            ThreadPool.QueueUserWorkItem(new WaitCallback((ctx) =>
                            {
                                try
                                {
                                    //The update package may be locked because the file object hasn't been disposed. Wait for it.
                                    Thread.Sleep(100);

                                    //I really hate this, but I couldn't find another reliable way to ensure the locked file is closed before I move it.
                                    GC.Collect(2);
                                    GC.WaitForPendingFinalizers();

                                    File.Move(updatePackageFilename, updatePackageFilename + ".error_" + DateTime.Now.ToString("yyyyMMdd.hhmmss"));
                                }
                                catch(Exception ex1)
                                {
                                    Log.Error("Error moving broken package", ex1, this);
                                }
                            }));

                            break;
                        }
                        finally
                        {
                            if (installationHistoryRoot != null)
                            {
                                //Write logs
                                installLogger.WriteMessages(Path.Combine(installationHistoryRoot, "Install.log"));

                                SaveInstallationMessages(installationHistoryRoot, logMessages);
                            }
                        }
                    }
                }
            }
            finally
            {
                _installingPackage = false;
            }
        }
        /// <summary>
        /// Executes the post install steps
        /// </summary>
        /// <param name="postStepDetails"></param>
        internal static void ExecutePostSteps(PostStepDetails postStepDetails)
        {
            InstallLogger installLogger = new InstallLogger(new RootLogger(Level.ALL));

            try
            {
                //Load the metadata from the update package
                MetadataView metedateView = UpdateHelper.LoadMetadata(postStepDetails.PostStepPackageFilename);
                List<ContingencyEntry> logMessages = new List<ContingencyEntry>();

                //Execute the post install steps
                DiffInstaller diffInstaller = new DiffInstaller(UpgradeAction.Upgrade);
                diffInstaller.ExecutePostInstallationInstructions(postStepDetails.PostStepPackageFilename, postStepDetails.HistoryPath, InstallMode.Update, metedateView, installLogger, ref logMessages);

                //Move the update package into the history folder
                File.Move(postStepDetails.PostStepPackageFilename, Path.Combine(postStepDetails.HistoryPath, Path.GetFileName(postStepDetails.PostStepPackageFilename)));
            }
            catch (Exception ex)
            {
                Log.Fatal("Post step execution failed", ex, "InstallPackage");
            }
            finally
            {
                //Write logs
                installLogger.WriteMessages(Path.Combine(postStepDetails.HistoryPath, "Install.log"));
            }
        }
        private void RunPostInitializeStepsIfNeeded()
        {
            string startupPostStepPackageFile = Path.Combine(_packageSource, InstallPackage.STARTUP_POST_STEP_PACKAGE_FILENAME);

            //remove post step flag file if it exists
            if (File.Exists(startupPostStepPackageFile))
            {
                try
                {
                    using (new SecurityDisabler())
                    {
                        //Load the post step details
                        XmlSerializer serializer = new XmlSerializer(typeof(PostStepDetails));
                        using (TextReader writer = new StreamReader(startupPostStepPackageFile))
                        {
                            PostStepDetails details = serializer.Deserialize(writer) as PostStepDetails;

                            if (details != null)
                            {
                                InstallLogger installLogger = new InstallLogger(new RootLogger(Level.ALL));

                                try
                                {
                                    InstallPackage.ExecutePostSteps(installLogger, details);

                                    InstallPackage.NotifiyPackageComplete(InstallPackage.SUCCESS, details);
                                }
                                catch (Exception ex)
                                {
                                    Log.Fatal("An error occured when running post steps", ex, this);

                                    InstallPackage.NotifiyPackageComplete(InstallPackage.FAIL, details);
                                }
                                finally
                                {
                                    installLogger.WriteMessages(Path.Combine(details.HistoryPath, "Install.log"));
                                }
                            }
                        }
                    }
                }
                finally
                {
                    //cleanup the post step
                    File.Delete(startupPostStepPackageFile);
                }
            }
        }