Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PdnsSlave.cpp 2.1KB

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