public static void UpdateSettingsMachineNameAndSerial(DatabaseAccessProvider <IPlatformDatabaseServices> dbAccess, Func <string> serialNumberGetter,
                                                              bool skipConnectionStringCheck)
        {
            // This code is used to solve the dangling machine name problem in amazon.
            // We'll start using the serial number to identify a machine instead of its name
            // for the node-specific settings.

            if (skipConnectionStringCheck)
            {
                string currentName = Settings.MachineName;
                if (currentSerial == null)
                {
                    currentSerial = serialNumberGetter();
                }

                using (RegistryKey subKey = SettingsRegistryKey) {
                    object previousNameValue   = subKey.GetValue(machineNameSetting);
                    string previousName        = previousNameValue == null ? null : previousNameValue.ToString();
                    object previousSerialValue = subKey.GetValue(serialSetting);
                    string previousSerial      = previousSerialValue == null ? null : previousSerialValue.ToString();

                    // the first time this code is run the ossys_parameter.host and ossys_server.name columns
                    // contain server names. Update them to the serial number.
                    // This is the 1st time if previousSerial is not set
                    if (previousSerial.IsEmpty())
                    {
                        FixRegistrySettingsPermissions();

                        currentSerial = serialNumberGetter();

                        using (var trans = dbAccess.GetCommitableTransaction()) {
                            UpgradeMachineNameToSerialNumber(trans, currentName, currentSerial);
                            trans.Commit();
                            EventLogger.WriteInfo("Setting Machine Serial to '" + currentSerial + "'.");
                        }
                    }

                    // serial changed - this happens for new instances created from  a server image.
                    // Update the serial number to the new one
                    if (!previousSerial.IsEmpty() && previousSerial != currentSerial)
                    {
                        using (var trans = dbAccess.GetCommitableTransaction()) {
                            UpdateSerialNumber(trans, previousSerial, currentSerial);
                            trans.Commit();
                            EventLogger.WriteInfo("Machine Serial changed from '" + previousSerial + "' to '" + currentSerial + "'.");
                        }
                    }

                    // name changed - this happens with amazon instances whenever they're stopped/started.
                    // Update the machine name to the new one
                    if (!previousName.IsEmpty() && previousName != currentName)
                    {
                        using (var trans = dbAccess.GetCommitableTransaction()) {
                            UpdateMachineName(trans, currentSerial, currentName);
                            trans.Commit();
                            EventLogger.WriteInfo("Machine Name changed from '" + previousName + "' to '" + currentName + "'.");
                        }
                    }

                    // update data in the registry
                    if (currentName == null)
                    {
                        throw new InvalidOperationException("currentName or currentSerial is null");
                    }

                    subKey.SetValue(machineNameSetting, currentName);

                    if (currentSerial == null)
                    {
                        throw new InvalidOperationException("currentSerial is null");
                    }

                    subKey.SetValue(serialSetting, currentSerial);
                }
            }
        }
Пример #2
0
 public static string EscapeAndQualifySqlIdentifier(DatabaseAccessProvider <IPlatformDatabaseServices> provider, string objectName)
 {
     return(provider.DatabaseServices.DMLService.Identifiers.EscapeAndQualifyIdentifierForLocalDatabase(objectName));
 }