Пример #1
0
        static void Update()
        {
            LogFile.WriteMessageFormat("*** Updating Mediachase {0} ***", IbnConst.ProductName);

            //GlobalContext.Current = new GlobalContext(Settings.InstallDirWeb + @"portal\Apps");
            DBHelper2.Init(new DBHelper(string.Format(CultureInfo.InvariantCulture, "Data source={0};Initial catalog=master;User Id={1};Password={2};Connection Timeout=36000", Settings.SqlServer, Settings.SqlUser, Settings.SqlPassword)));
            DBHelper2.DBHelper.CommandTimeout = CommandTimeout;

            IConfigurator configurator  = Configurator.Create();
            IIisManager   oldIisManager = IisManager.Create(Settings.InstallDirWeb);
            bool          x64           = oldIisManager.Is64Bit();

            // Get copmanies
            CompanyInfo[] oldCompanies = CompanyInfo.ListCompanies(DBHelper2.DBHelper);
            bool          isAsp        = (oldCompanies.Length > 1);

            List <string> newCompanies = ListNewCompanies(configurator);
            bool          firstLaunch  = (newCompanies == null);

            List <CompanyInfo> companies = new List <CompanyInfo>();

            if (firstLaunch)
            {
                companies.AddRange(oldCompanies);
            }
            else
            {
                foreach (CompanyInfo oldCompany in oldCompanies)
                {
                    if (!newCompanies.Contains(oldCompany.Host))
                    {
                        companies.Add(oldCompany);
                    }
                }
            }

            string newAspPath = Path.Combine(Settings.InstallDir, "Asp");
            string codePath   = Path.Combine(Settings.InstallDir, "Code");
            string sourcePath = Path.Combine(codePath, "_Source");
            string newWebPath = Path.Combine(sourcePath, "Web");

            try
            {
                // Stop old web site
                oldIisManager.StopSite(Settings.SiteId);

                // Stop old services
                if (firstLaunch)
                {
                    StopService("AlertService" + IbnConst.VersionMajorMinor, false);
                    StopService("McOleDBService" + IbnConst.VersionMajorMinor, false);
                    StopService("ScheduleService" + IbnConst.VersionMajorMinor, false);

                    // Unregister old components
                    RegisterComponents(false, GetOldComponents());

                    // Unregister old IM Server
                    RegisterIMServer(false, Settings.InstallDirWeb, x64);

                    // TODO: ? Delete ISAPI extension
                    // TODO: ? Delete portal application pool
                    // TODO: ? Delete IM application pool

                    // Copy old files to new location: InstallPath\Code\_Source
                    LogFile.WriteLine("Copying old files to new location...");

                    CopyDirectory(Path.Combine(Settings.InstallDir, @"Web\Asp"), newAspPath);
                    CopyDirectory(Settings.InstallDir, sourcePath, "Tools");
                    CopyDirectory(Settings.InstallDir, sourcePath, @"Web\Download");
                    CopyDirectory(Settings.InstallDir, sourcePath, @"Web\instmsg");
                    CopyDirectory(Settings.InstallDir, sourcePath, @"Web\instmsg64");
                    CopyDirectory(Settings.InstallDir, sourcePath, @"Web\Portal");

                    LogWriteOk();

                    // Delete error logs
                    Directory.Delete(Path.Combine(sourcePath, @"Web\Portal\Admin\Log\Error"), true);

                    // Update files
                    UpdateFiles();

                    // Register new components
                    RegisterComponents(true, GetNewComponents());

                    // Register new IM Server
                    RegisterIMServer(true, newWebPath, x64);

                    // Start new services
                    StartNewServices();
                }
                firstLaunch = false;

                Pause("Restore clean databases.");

                for (int i = 0; i < companies.Count; i++)
                {
                    CompanyInfo company = companies[i];

                    LogFile.WriteMessageFormat("* Converting company {0} of {1}: '{2}' *", i + 1, companies.Count, company.Host);

                    try
                    {
                        ConvertCompany(configurator, isAsp, company);

                        // Delete binding from old web site
                        try
                        {
                            if (isAsp)
                            {
                                oldIisManager.DeleteBinding(Settings.SiteId, company.Host);
                            }
                        }
                        catch
                        {
                        }

                        LogFile.WriteLine("* OK *");
                    }
                    catch (Exception ex)
                    {
                        LogFile.WriteLine("* Failed *");
                        LogFile.WriteLine("");

                        if (isAsp)
                        {
                            LogFile.WriteLine(ex.ToString());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                Settings.SaveVersion();

                Pause("Backup databases.");

                LogFile.WriteLine("*** OK ***");
            }
            catch (Exception ex)
            {
                LogFile.WriteLine("*** Failed ***");
                LogFile.WriteLine("");
                LogFile.WriteLine(ex.ToString());
                LogFile.WriteLine("");
                LogFile.WriteLine("Rollback started");

                #region Undo changes

                try
                {
                    if (!isAsp || firstLaunch)
                    {
                        // Unregister new components
                        try
                        {
                            RegisterComponents(false, GetNewComponents());
                        }
                        catch
                        {
                        }

                        // Unregister new IM server
                        try
                        {
                            RegisterIMServer(false, newWebPath, x64);
                        }
                        catch
                        {
                        }

                        // Recover files
                        try
                        {
                            RecoverFiles();
                        }
                        catch
                        {
                        }

                        // Delete [InstallDir]\Code
                        try
                        {
                            Directory.Delete(codePath, true);
                        }
                        catch
                        {
                        }

                        // Delete [InstallDir]\Asp
                        try
                        {
                            Directory.Delete(newAspPath, true);
                        }
                        catch
                        {
                        }

                        // Register old components
                        try
                        {
                            RegisterComponents(true, GetOldComponents());
                        }
                        catch
                        {
                        }

                        // Register old IM Server
                        try
                        {
                            RegisterIMServer(true, Settings.InstallDirWeb, x64);
                        }
                        catch
                        {
                        }

                        // Start old services
                        try
                        {
                            StartOldServices();
                        }
                        catch
                        {
                        }
                    }

                    // Start old web site
                    try
                    {
                        oldIisManager.StartSite(Settings.SiteId);
                    }
                    catch
                    {
                    }
                }
                catch
                {
                }

                #endregion

                LogFile.WriteLine("Rollback finished");
                throw;
            }
            finally
            {
                LogFile.WriteLine("");
                // Delete backup files
                DeleteBackupFiles();
            }
        }