SafeDispatch/SafeMobileLIB_DLL/Failover/GenerateCofigFiles.cs

142 lines
5.6 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Text;
using System.IO;
namespace SafeMobileLib
{
public class GenerateConfigFiles
{
static string CR = Environment.NewLine;
public static void CreateAndCopyPostgresqlConfMaster(string masterIP, string appPath)
{
StringBuilder createText = new StringBuilder(4096);
string path = appPath;
// This text is added only once to the file.
if (File.Exists(path))
File.Delete(path);
createText.Append($"listen_addresses = '*'{CR}port = 5432{CR}max_connections = 1000{CR}shared_buffers = 32MB{CR}");
createText.Append($"datestyle = 'iso, mdy'{CR}lc_messages = 'English_United States.1252'{CR}lc_monetary = 'English_United States.1252'{CR}");
createText.Append($"lc_numeric = 'English_United States.1252'{CR}lc_time = 'English_United States.1252'{CR}default_text_search_config = 'pg_catalog.english'");
createText.Append($"{CR}{CR}wal_level = hot_standby{CR}max_wal_senders = 3{CR}wal_keep_segments = 500{CR}wal_log_hints = on{CR}");
createText.Append($"max_replication_slots = 1{CR}archive_mode = on{CR}archive_command = 'copy \"%p\" \"\\\\\\\\{masterIP}\\\\archive_folder\\\\%f\"'{CR}archive_timeout = 0");
// write to file
File.WriteAllText(path, createText.ToString());
// Show the content file to the console.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
Utils.WriteLine("File successfuly written to: " + path, ConsoleColor.DarkCyan);
}
public static void CreateAndCopyHbaConf(string slaveIP, string appPath)
{
string path = appPath;
// This text is added only once to the file.
if (File.Exists(path))
File.Delete(path);
string createText = $"host all all 127.0.0.1/32 md5{CR}host all all 0.0.0.0/0 md5{CR}" +
$"host all all ::1/128 md5{CR}host replication postgres {slaveIP}/32 trust";
File.WriteAllText(path, createText);
// Show the content file to the console.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
Utils.WriteLine("File successfuly written to: " + path, ConsoleColor.DarkCyan);
}
public static void CreateAndCopyPostgresqlConfSlave(string appPath)
{
string path = appPath;
StringBuilder createText = new StringBuilder(4096);
// This text is added only once to the file.
if (File.Exists(path))
File.Delete(path);
createText.Append($"listen_addresses = '*'{CR}port = 5432{CR}max_connections = 1000{CR}shared_buffers = 32MB{CR}");
createText.Append($"datestyle = 'iso, mdy'{CR}lc_messages = 'English_United States.1252'{CR}lc_monetary = 'English_United States.1252'{CR}");
createText.Append($"lc_numeric = 'English_United States.1252'{CR}lc_time = 'English_United States.1252'{CR}default_text_search_config = 'pg_catalog.english'");
createText.Append($"{CR}{CR}wal_level = hot_standby{CR}max_wal_senders = 3{CR}wal_keep_segments = 500{CR}");
createText.Append($"max_replication_slots = 1{CR}hot_standby = on{CR}log_destination = stderr{CR}logging_collector = on");
File.WriteAllText(path, createText.ToString());
// Show the content file to the console.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
Utils.WriteLine("File successfuly written to: " + path, ConsoleColor.DarkCyan);
}
public static void CreateRecoveryConf(string masterIP, string path, string safemobilePath)
{
StringBuilder createText = new StringBuilder(4096);
// This text is added only once to the file.
if (File.Exists(path))
File.Delete(path);
createText.Append($"standby_mode = 'on'{CR}primary_conninfo = 'host = {masterIP} port = 5432 user = postgres password = wizdemo26'{CR}");
createText.Append($"primary_slot_name = 'replication_slot_node'{CR}restore_command = 'copy \"\\\\\\\\{masterIP}\\\\archive_folder\\\\%f\" \"%p\"'{CR}");
createText.Append($"trigger_file = '{safemobilePath.Replace(@"\", "/")}/failover.txt'{CR}");
createText.Append($"recovery_target_timeline = 'latest'");
File.WriteAllText(path, createText.ToString());
// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
Utils.WriteLine("File successfuly written to: " + path, ConsoleColor.DarkCyan);
}
public static string CheckCurrentConfiguration(string appPath)
{
string line;
bool valid = false;
bool has_hot_standby = false;
using (StreamReader file = new StreamReader(appPath))
{
while ((line = file.ReadLine()) != null)
{
if (line.Replace(" ", "").Contains("max_replication_slots"))
valid = true;
if (line.Replace(" ", "").Equals("hot_standby=on"))
has_hot_standby = true;
}
if (!valid)
return string.Empty;
if (has_hot_standby)
return "slave";
return "master";
}
}
}
}