Beispiel #1
0
        private CompanyInfo CreateCompanyForDatabase(XmlDocument serverConfigDocument, string database, string defaultLocale, DateTime created, bool isActive, string host, string iisIPAddress, int iisPort, string iisApplicationPool, bool createClientScripts)
        {
            string directoryName = BuildUniqueName(host, GetDirectories());
            string targetPath = Path.Combine(_codePath, directoryName);
            string webPath = Path.Combine(targetPath, "Web");
            string webCommonName = GetDefaultWebName(host);

            IIisManager iisManager = IisManager.Create(webPath);
            bool x64 = iisManager.Is64Bit();
            string[] sites = iisManager.ListSites();
            string[] pools = iisManager.ListApplicationPools();
            string siteName = BuildUniqueName(webCommonName, sites);

            CompanyInfo company = new CompanyInfo();
            company.Created = created;
            company.IsActive = isActive;
            company.IsScheduleServiceEnabled = true;
            company.Scheme = "http";
            company.Host = host;
            company.Port = (iisPort != 80 ? iisPort.ToString(CultureInfo.InvariantCulture) : string.Empty);
            company.Database = database;
            company.CodePath = Path.Combine("Code", directoryName);
            company.Language.Locale = defaultLocale;

            bool directoryCopied = false;
            bool isapiExtensionCreated = false;
            bool imPoolCreated = false;

            try
            {
                // Copy code to new directory
                CopyDirectory(_sourcePath, targetPath);
                directoryCopied = true;

                // Get code version
                FileVersionInfo version = FileVersionInfo.GetVersionInfo(Path.Combine(webPath, @"Portal\bin\Mediachase.IBN.Business.dll"));
                company.CodeVersion = version.FileBuildPart;

                // Allow writing to directories
                SetWritePermissions(webPath, WebName.IMDownload);
                SetWritePermissions(webPath, @"Portal\Admin\Log\Error");
                SetWritePermissions(webPath, @"Portal\Layouts\Images\Charts");

                // Update web.config
                string connectionString = SqlServerSettings.BuildConnectionString(AuthenticationType.SqlServer, _sqlServer, company.Database, _sqlPortalUser, _sqlPortalPassword);
                UpdateWebConfig(Path.Combine(webPath, "Portal"), connectionString, company.Id, Path.Combine(webPath, WebName.IMDownload));

                // Create ISAPI extension
                isapiExtensionCreated = iisManager.AddIsapiExtension(webCommonName);

                if (iisManager.IsApplicationPoolSupported)
                {
                    // Create IM application pool
                    company.IMPool = BuildUniqueName(webCommonName + " IM", pools);
                    iisManager.CreateApplicationPool(company.IMPool, false);
                    imPoolCreated = true;

                    // Create portal application pool
                    if (string.IsNullOrEmpty(iisApplicationPool))
                    {
                        company.PortalPool = BuildUniqueName(webCommonName + " Portal", pools);
                        iisManager.CreateApplicationPool(company.PortalPool, true);
                        company.IsPortalPoolCreated = true;
                    }
                    else
                        company.PortalPool = iisApplicationPool;
                }

                // Create web site
                company.SiteId = iisManager.CreateCompanySite(siteName, iisIPAddress, iisPort, host, x64, company.IMPool, company.PortalPool);

                // Register ASP.NET 2.0
                if (iisManager.IisVersion < 7)
                {
                    string windir = System.Environment.ExpandEnvironmentVariables(@"%SystemRoot%");
                    string regiis = string.Format(CultureInfo.InvariantCulture, @"{0}\Microsoft.NET\Framework{1}\v2.0.50727\aspnet_regiis.exe", windir, x64 ? "64" : "");

                    if (_registerAspNet)
                        StartProcess(regiis, string.Format(CultureInfo.InvariantCulture, "-s w3svc/{0}/", company.SiteId));

                    if (createClientScripts)
                        StartProcess(regiis, "-c");
                }

                // Start web site
                try
                {
                    iisManager.StartSite(company.SiteId);
                }
                catch (Exception ex)
                {
                    Log.WriteException("CreateCompanyForDatabase", ex);
                }

                // Update ibn.config
                company.SaveGeneralInfo(serverConfigDocument);
            }
            catch
            {
                #region Undo changes

                // Delete web site
                try
                {
                    if (company.SiteId > 0)
                        iisManager.DeleteSite(company.SiteId);
                }
                catch
                {
                }

                if (iisManager.IsApplicationPoolSupported)
                {
                    // Delete portal application pool
                    try
                    {
                        if (company.IsPortalPoolCreated)
                            iisManager.DeleteApplicationPool(company.PortalPool);
                    }
                    catch
                    {
                    }

                    // Delete IM application pool
                    try
                    {
                        if (imPoolCreated)
                            iisManager.DeleteApplicationPool(company.IMPool);
                    }
                    catch
                    {
                    }
                }

                // Delete ISAPI extension
                try
                {
                    if (isapiExtensionCreated)
                        iisManager.DeleteIsapiExtension();
                }
                catch
                {
                }

                // Delete code directory
                try
                {
                    if (directoryCopied)
                        Directory.Delete(targetPath, true);
                }
                catch
                {
                }

                #endregion

                throw;
            }

            return company;
        }