Browse Source

dhcp override

develop
Robin Thoni 9 years ago
parent
commit
170f64b7bc

+ 29
- 2
src/Business/PdnsSlave.cpp View File

7
 #include "DataAccess/HostsConfig.h"
7
 #include "DataAccess/HostsConfig.h"
8
 #include "PdnsSlave.h"
8
 #include "PdnsSlave.h"
9
 
9
 
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
+
10
 PdnsSlave::PdnsSlave(const std::string &filePath)
18
 PdnsSlave::PdnsSlave(const std::string &filePath)
11
     : _filePath(filePath)
19
     : _filePath(filePath)
12
 {
20
 {
50
     if (!res)
58
     if (!res)
51
         return res;
59
         return res;
52
     _actions = res.getData();
60
     _actions = res.getData();
53
-    for (auto action : _actions)
54
-        std::cout << action->getSqlQuery() << std::endl << std::endl;
55
     return res;
61
     return res;
56
 }
62
 }
63
+
64
+BResult PdnsSlave::overridePdns()
65
+{
66
+    return true;
67
+}
68
+
69
+BResult PdnsSlave::overrideDhcp()
70
+{
71
+    std::string hosts;
72
+    for (auto a : _actions)
73
+        hosts += a->getDhcpConf();
74
+
75
+    auto dhcp = _dhcpdTemplateContent;
76
+    replace(dhcp, "%%HOSTS%%", hosts);
77
+    std::ofstream file(_dhcpdFilePath);
78
+    if (!file)
79
+        return BResult().error("Could not open file");
80
+    file << dhcp;
81
+    file.close();
82
+    return true;
83
+}

+ 4
- 0
src/Business/PdnsSlave.h View File

21
 
21
 
22
     Result<Actions> readHosts();
22
     Result<Actions> readHosts();
23
 
23
 
24
+    BResult overridePdns();
25
+
26
+    BResult overrideDhcp();
27
+
24
 private:
28
 private:
25
     std::string _filePath;
29
     std::string _filePath;
26
 
30
 

+ 10
- 0
src/DBO/Actions/Action.cpp View File

26
 {
26
 {
27
     return "SET @domain_id=(SELECT get_domain_id(\"" + _domain + "\"));\n" + getSql();
27
     return "SET @domain_id=(SELECT get_domain_id(\"" + _domain + "\"));\n" + getSql();
28
 }
28
 }
29
+
30
+const std::string Action::getDhcpConf() const
31
+{
32
+    return getDhcp();
33
+}
34
+
35
+const std::string Action::getDhcp() const
36
+{
37
+    return "";
38
+}

+ 4
- 0
src/DBO/Actions/Action.h View File

15
     Action();
15
     Action();
16
     virtual ~Action();
16
     virtual ~Action();
17
 
17
 
18
+    const std::string getDhcpConf() const;
19
+
18
     const std::string getSqlQuery() const;
20
     const std::string getSqlQuery() const;
19
 
21
 
20
     const std::string &getDomain() const;
22
     const std::string &getDomain() const;
24
 protected:
26
 protected:
25
     virtual const std::string getSql() const = 0;
27
     virtual const std::string getSql() const = 0;
26
 
28
 
29
+    virtual const std::string getDhcp() const;
30
+
27
     std::string _domain;
31
     std::string _domain;
28
 };
32
 };
29
 
33
 

+ 13
- 0
src/DBO/Actions/ActionAddHost.cpp View File

122
         reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
122
         reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
123
     return reversedValue;
123
     return reversedValue;
124
 }
124
 }
125
+
126
+const std::string ActionAddHost::getDhcp() const
127
+{
128
+    if (!_dhcpMac.empty() && _recordType == "A")
129
+    {
130
+        return "\thost " + _host + " {\n"
131
+                "\t\thardware ethernet " + _dhcpMac + ";\n"
132
+                "\t\tfixed-address " + _host + "." + _domain + ";\n"
133
+                "\t}\n";
134
+    }
135
+    else
136
+        return "";
137
+}

+ 2
- 0
src/DBO/Actions/ActionAddHost.h View File

39
 protected:
39
 protected:
40
     const std::string getSql() const override;
40
     const std::string getSql() const override;
41
 
41
 
42
+    const std::string getDhcp() const override;
43
+
42
 private:
44
 private:
43
     const std::string getReversedValue() const;
45
     const std::string getReversedValue() const;
44
 
46
 

+ 23
- 0
src/Interface/MainClass.cpp View File

19
 {
19
 {
20
     CommandLineOption configFile("config", 'c', "Path to the configuration file",
20
     CommandLineOption configFile("config", 'c', "Path to the configuration file",
21
                                  "FILE", "/etc/pdns-slave/pdns-slave.json");
21
                                  "FILE", "/etc/pdns-slave/pdns-slave.json");
22
+    CommandLineOption check("check", 'C', "Only check configurations", "", "");
22
     CommandLineOption help("help", 'h', "Displays this help", "", "");
23
     CommandLineOption help("help", 'h', "Displays this help", "", "");
23
     CommandLineParser parser(_argc, _argv);
24
     CommandLineParser parser(_argc, _argv);
24
     parser.addOption(&help);
25
     parser.addOption(&help);
25
     parser.addOption(&configFile);
26
     parser.addOption(&configFile);
27
+    parser.addOption(&check);
26
     if (!parser.parse())
28
     if (!parser.parse())
27
         return parser.showHelp(EX_USAGE);
29
         return parser.showHelp(EX_USAGE);
28
     if (help.isSet())
30
     if (help.isSet())
62
     }
64
     }
63
     res2.print();
65
     res2.print();
64
 
66
 
67
+    if (!check.isSet())
68
+    {
69
+        std::cout << "Overriding dns" << std::endl;
70
+        if (!(res = pdnsSlave.overridePdns()))
71
+        {
72
+            std::cerr << "Failed to override dns" << std::endl;
73
+            res.print();
74
+            return 4;
75
+        }
76
+        res.print();
77
+
78
+        std::cout << "Overriding dhcp" << std::endl;
79
+        if (!(res = pdnsSlave.overrideDhcp()))
80
+        {
81
+            std::cerr << "Failed to override dhcp" << std::endl;
82
+            res.print();
83
+            return 5;
84
+        }
85
+        res.print();
86
+    }
87
+
65
     return 0;
88
     return 0;
66
 }
89
 }

Loading…
Cancel
Save