コード例 #1
0
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            try
            {
                if (SkipDatabaseInstallation)
                {
                    EventLog.WriteEntry(Service.SyslogServiceName,
                                        "Installation of database skipped - manual configuration will be required",
                                        EventLogEntryType.Warning);
                    return;
                }

                DatabaseInstaller dbInsSys  = GetSystemInstaller();
                DatabaseInstaller dbInsUser = GetUserInstaller();

                // Ensure same username not given if not using IWA
                if (!dbInsSys.TrustedConnection && !dbInsUser.TrustedConnection &&
                    (dbInsSys.UserName == dbInsUser.UserName))
                {
                    throw new Exception("When not using Windows authentication the username given to "
                                        + "create the database must be different from the username Syslog Server will use");
                }

                // Check database does not already exist
                if (dbInsSys.DatabaseExists)
                {
                    throw new InstallException(
                              String.Format("The database {0} already exists on the server {1}",
                                            dbInsSys.Database, dbInsSys.Server));
                }

                // Check user can access the database server
                if (!dbInsUser.TrustedConnection)
                {
                    try
                    {
                        dbInsUser.TestMasterConnection();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format(
                                                "Unable to connect to the database server as user {0}: {1}",
                                                dbInsUser.UserName, ex.Message));
                    }
                }

                // Time to do the actual work

                // Create database
                dbInsSys.CreateDatabase(Assembly.GetExecutingAssembly(),
                                        "Aonaware.SyslogService.database.create.sql");

                // Add system logon credentials stateServer for uninstall / rollback
                // Todo: encrypt this
                stateSaver.Add(_serverString, dbInsSys.Server);
                stateSaver.Add(_dataString, dbInsSys.Database);
                stateSaver.Add(_userString, dbInsSys.UserName);
                stateSaver.Add(_passString, dbInsSys.Password);

                // If not using IWA for our user connection, add the user to the service role
                if (!dbInsUser.TrustedConnection)
                {
                    Dictionary <string, string> rep = new Dictionary <string, string>();
                    rep["%SVCUSER%"] = dbInsUser.UserName;

                    dbInsSys.ExecuteGenericSQL(Assembly.GetExecutingAssembly(),
                                               "Aonaware.SyslogService.database.users.sql", rep);
                }

                // Finally modify the config file
                string configFile = Assembly.GetExecutingAssembly().Location + ".config";
                dbInsUser.ModifyConfigFile(configFile, "Aonaware.SyslogService.Properties.Settings.DbConnection");
            }
            catch (InstallException inst)
            {
                throw inst;
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(Service.SyslogServiceName, ex.Message,
                                    EventLogEntryType.Error);
                throw new InstallException(ex.Message);
            }
        }
コード例 #2
0
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            try
            {
                if (SkipDatabaseInstallation)
                {
                    EventLog.WriteEntry(_syslogWebName,
                                        "Installation of database skipped - manual configuration will be required",
                                        EventLogEntryType.Warning);
                    return;
                }

                DatabaseInstaller dbInsSys  = GetSystemInstaller();
                DatabaseInstaller dbInsUser = GetUserInstaller();

                // Check that the system login can access the database
                try
                {
                    dbInsSys.TestMasterConnection();
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format(
                                            "Unable to connect to the database server as user {0}: {1}",
                                            dbInsUser.UserName, ex.Message));
                }

                // If user login given, check that too
                if (!dbInsUser.TrustedConnection)
                {
                    try
                    {
                        dbInsUser.TestMasterConnection();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format(
                                                "Unable to connect to the database server as user {0}: {1}",
                                                dbInsUser.UserName, ex.Message));
                    }
                }

                // Check that the database already exists
                if (!dbInsSys.DatabaseExists)
                {
                    throw new InstallException(
                              String.Format("The database {0} does not exists on the server {1}",
                                            dbInsSys.Database, dbInsSys.Server));
                }

                // If we are installing this application
                //  - on the same computer as SQL Server / MSDE
                //  - using IWA
                // Then add the ASP.NET service account to the database
                if (dbInsUser.TrustedConnection && dbInsUser.LocalServer)
                {
                    try
                    {
                        // Determine which account to use
                        string accountDBName = String.Empty;
                        string accountNTName = String.Empty;

                        // Search for IIS user

                        List <LocalUsersAndGroups.User> au = LocalUsersAndGroups.Users();
                        foreach (LocalUsersAndGroups.User u in au)
                        {
                            if (u.Name.ToUpper() == _aspNet)
                            {
                                accountDBName = u.Name;
                                accountNTName = u.Caption;
                                break;
                            }
                        }

                        if (accountDBName.Length == 0)
                        {
                            // Assume network service
                            accountDBName = "NETWORK SERVICE";
                            accountNTName = @"NT AUTHORITY\NETWORK SERVICE";
                        }

                        // Add account to database if needed
                        Dictionary <string, string> rep = new Dictionary <string, string>();
                        rep["%NTUSER%"]    = accountNTName;
                        rep["%NEWDBUSER%"] = accountDBName;

                        dbInsSys.ExecuteGenericSQL(Assembly.GetExecutingAssembly(),
                                                   "Aonaware.SyslogWeb.database.createuser.sql", rep);

                        // Add some info to the event log
                        EventLog.WriteEntry(_syslogWebName, String.Format(
                                                "Automatically added account {0} to database", accountNTName),
                                            EventLogEntryType.Information);
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry(_syslogWebName,
                                            String.Format("Unable to create ASP.NET account on database server: {0}",
                                                          ex.Message), EventLogEntryType.Warning);
                    }
                }

                // If we are using SQL Server authentication, give access to database
                if (!dbInsUser.TrustedConnection)
                {
                    Dictionary <string, string> rep = new Dictionary <string, string>();
                    rep["%SVCUSER%"] = dbInsUser.UserName;

                    dbInsSys.ExecuteGenericSQL(Assembly.GetExecutingAssembly(),
                                               "Aonaware.SyslogWeb.database.users.sql", rep);
                }

                // Finally modify the config file
                string targetDir  = TargetDirectory;
                string configFile = Path.Combine(targetDir, "Web.config");
                dbInsUser.ModifyConfigFile(configFile, "Aonaware.SyslogWeb.Properties.Settings.DbConnection");
            }
            catch (InstallException inst)
            {
                throw inst;
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(_syslogWebName, ex.Message,
                                    EventLogEntryType.Error);
                throw new InstallException(ex.Message);
            }
        }