Testing config read/write in target environment
This commit is contained in:
+107
-8
@@ -1,33 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Activizr.Database
|
||||
{
|
||||
public partial class PirateDb
|
||||
{
|
||||
[Serializable]
|
||||
public class Configuration
|
||||
{
|
||||
public Configuration() {}
|
||||
public Configuration (Credentials read, Credentials write, Credentials admin)
|
||||
{
|
||||
this.Read = read;
|
||||
this.Write = write;
|
||||
this.Admin = admin;
|
||||
}
|
||||
|
||||
public static bool IsConfigured()
|
||||
{
|
||||
string testString = DatabaseConnect.Default.Admin;
|
||||
if (Configuration._configuration == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Configuration.Load();
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
string testString = _configuration.Admin.Username;
|
||||
|
||||
return !String.IsNullOrEmpty(DatabaseConnect.Default.Admin);
|
||||
}
|
||||
|
||||
public static bool SetConfiguration(string readConnect, string writeConnect, string adminConnect)
|
||||
public Credentials Read { get; set; }
|
||||
public Credentials Write { get; set; }
|
||||
public Credentials Admin { get; set; }
|
||||
|
||||
private static Configuration _configuration;
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
DatabaseConnect.Default["Read"] = readConnect;
|
||||
DatabaseConnect.Default["Write"] = writeConnect;
|
||||
DatabaseConnect.Default["Admin"] = adminConnect;
|
||||
DatabaseConnect.Default.Save();
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
|
||||
|
||||
// TODO: Test a connect string to see if we can read, and return true only on success
|
||||
using (FileStream readFileStream = new FileStream(GetConfigurationFileName(), FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
|
||||
return true;
|
||||
_configuration = (Configuration) serializer.Deserialize(readFileStream);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Set(Configuration configuration)
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
|
||||
|
||||
using (TextWriter writeFileStream = new StreamWriter(GetConfigurationFileName()))
|
||||
{
|
||||
serializer.Serialize(writeFileStream, configuration);
|
||||
}
|
||||
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public static string GetConfigurationFileName()
|
||||
{
|
||||
// If we're running in a HTTP context, use the Mono path mapper, as we're in an Apache process.
|
||||
// Otherwise, use a relative path from the binary.
|
||||
|
||||
if (HttpContext.Current != null)
|
||||
{
|
||||
// Apache process, so use MapPath
|
||||
|
||||
return HttpContext.Current.Server.MapPath("~/database.config");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Backend process - just use the simple filename (and copy frontend's config to backend's on daemon start).
|
||||
|
||||
return "database.config";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class ServerSet
|
||||
{
|
||||
public ServerSet()
|
||||
{
|
||||
// paramless ctor to enable serialization
|
||||
ServerPriorities = new List<string>();
|
||||
}
|
||||
|
||||
public ServerSet (string singleServer)
|
||||
{
|
||||
ServerPriorities = new List<string>();
|
||||
ServerPriorities.Add(singleServer);
|
||||
}
|
||||
|
||||
public List<string> ServerPriorities { get; set; } // array of semicolon-delimited hostnames; topmost string in array is first-priority servers, and so on.
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Credentials
|
||||
{
|
||||
public Credentials()
|
||||
{
|
||||
// paramless ctor to enable serialization
|
||||
}
|
||||
|
||||
public Credentials (string database, ServerSet servers, string username, string password)
|
||||
{
|
||||
this.Database = database;
|
||||
this.ServerSet = servers;
|
||||
this.Username = username;
|
||||
this.Password = password;
|
||||
}
|
||||
|
||||
public string Database { get; set; }
|
||||
public ServerSet ServerSet { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,16 +252,21 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page
|
||||
{
|
||||
// Store database credentials
|
||||
|
||||
string readConnect = this.TextCredentialsReadDatabase.Text + "|" + this.TextCredentialsReadServer.Text + "|" +
|
||||
this.TextCredentialsReadUser.Text + "|" + this.TextCredentialsReadPassword.Text;
|
||||
|
||||
string writeConnect = this.TextCredentialsWriteDatabase.Text + "|" + this.TextCredentialsWriteServer.Text + "|" +
|
||||
this.TextCredentialsWriteUser.Text + "|" + this.TextCredentialsWritePassword.Text;
|
||||
|
||||
string adminConnect = this.TextCredentialsAdminDatabase.Text + "|" + this.TextCredentialsAdminServer.Text + "|" +
|
||||
this.TextCredentialsAdminUser.Text + "|" + this.TextCredentialsAdminPassword.Text;
|
||||
|
||||
PirateDb.Configuration.SetConfiguration(readConnect, writeConnect, adminConnect);
|
||||
PirateDb.Configuration.Set(
|
||||
new PirateDb.Configuration(
|
||||
new PirateDb.Credentials(
|
||||
this.TextCredentialsReadDatabase.Text,
|
||||
new PirateDb.ServerSet(this.TextCredentialsReadServer.Text),
|
||||
this.TextCredentialsReadUser.Text,
|
||||
this.TextCredentialsReadPassword.Text),
|
||||
new PirateDb.Credentials(this.TextCredentialsWriteDatabase.Text,
|
||||
new PirateDb.ServerSet(this.TextCredentialsWriteServer.Text),
|
||||
this.TextCredentialsWriteUser.Text,
|
||||
this.TextCredentialsWritePassword.Text),
|
||||
new PirateDb.Credentials(this.TextCredentialsAdminDatabase.Text,
|
||||
new PirateDb.ServerSet(this.TextCredentialsAdminServer.Text),
|
||||
this.TextCredentialsAdminUser.Text,
|
||||
this.TextCredentialsAdminPassword.Text)));
|
||||
|
||||
// Initialize database. For now, fake it.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user