You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PdnsSlave.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <fstream>
  5. #include <DataAccess/MySql.h>
  6. #include <DataAccess/PgSql.h>
  7. #include "DataAccess/PdnsSlaveConfig.h"
  8. #include "DataAccess/HostsConfig.h"
  9. #include "PdnsSlave.h"
  10. bool replace(std::string& str, const std::string& from, const std::string& to) {
  11. size_t start_pos = str.find(from);
  12. if(start_pos == std::string::npos)
  13. return false;
  14. str.replace(start_pos, from.length(), to);
  15. return true;
  16. }
  17. PdnsSlave::PdnsSlave(const std::string &filePath)
  18. : _filePath(filePath)
  19. {
  20. }
  21. BResult PdnsSlave::readConfig()
  22. {
  23. PdnsSlaveConfig conf(_filePath);
  24. BResult res;
  25. if (!(res = conf.readConfig()))
  26. return res;
  27. _dhcpdFilePath = conf.getDhcpdFilePath();
  28. _dhcpdTemplatePath = conf.getDhcpdTemplatePath();
  29. _hostsPath = conf.getHostsPath();
  30. _dbType = conf.getDbType();
  31. _masterConfig = conf.getMasterConfig();
  32. _slaveConfig = conf.getSlaveConfig();
  33. return true;
  34. }
  35. BResult PdnsSlave::readDhcpdTemplate()
  36. {
  37. std::ifstream dhcpTemplate(_dhcpdTemplatePath);
  38. if (!dhcpTemplate)
  39. return BResult().error("Could not open file");
  40. while (!dhcpTemplate.eof())
  41. {
  42. std::string line;
  43. getline(dhcpTemplate, line);
  44. _dhcpdTemplateContent += line + "\n";
  45. }
  46. dhcpTemplate.close();
  47. return true;
  48. }
  49. Result<Actions> PdnsSlave::readHosts()
  50. {
  51. HostsConfig conf(_hostsPath);
  52. auto res = conf.readConfig();
  53. if (!res)
  54. return res;
  55. _actions = res.getData();
  56. return res;
  57. }
  58. BResult PdnsSlave::overridePdns()
  59. {
  60. AbstractSql* sqlDb = 0;
  61. if (_dbType == "mysql") {
  62. sqlDb = new MySql(_masterConfig, _slaveConfig);
  63. }
  64. else if (_dbType == "pgsql") {
  65. sqlDb = new PgSql(_masterConfig, _slaveConfig);
  66. }
  67. else {
  68. return BResult().error("Invalid database type " + _dbType);
  69. }
  70. auto res = sqlDb->dump();
  71. if (!res)
  72. return res;
  73. if (!(res = sqlDb->insert()))
  74. return res;
  75. std::string sql;
  76. for (auto a : _actions)
  77. sql += a->getSql(sqlDb);
  78. res = sqlDb->override(sql);
  79. delete sqlDb;
  80. return res;
  81. }
  82. BResult PdnsSlave::overrideDhcp()
  83. {
  84. std::string hosts;
  85. for (auto a : _actions)
  86. hosts += a->getDhcpConf();
  87. auto dhcp = _dhcpdTemplateContent;
  88. replace(dhcp, "%%HOSTS%%", hosts);
  89. std::ofstream file(_dhcpdFilePath);
  90. if (!file)
  91. return BResult().error("Could not open file");
  92. file << dhcp;
  93. file.close();
  94. return true;
  95. }