Browse Source

mysql override

develop
Robin Thoni 9 years ago
parent
commit
9d3aab7218

+ 11
- 1
src/Business/PdnsSlave.cpp View File

3
 //
3
 //
4
 
4
 
5
 #include <fstream>
5
 #include <fstream>
6
+#include <DataAccess/MySql.h>
6
 #include "DataAccess/PdnsSlaveConfig.h"
7
 #include "DataAccess/PdnsSlaveConfig.h"
7
 #include "DataAccess/HostsConfig.h"
8
 #include "DataAccess/HostsConfig.h"
8
 #include "PdnsSlave.h"
9
 #include "PdnsSlave.h"
63
 
64
 
64
 BResult PdnsSlave::overridePdns()
65
 BResult PdnsSlave::overridePdns()
65
 {
66
 {
66
-    return true;
67
+    MySql mysql(_masterConfig, _slaveConfig);
68
+    auto res = mysql.dump();
69
+    if (!res)
70
+        return res;
71
+    if (!(res = mysql.insert()))
72
+        return res;
73
+    std::string sql;
74
+    for (auto a : _actions)
75
+        sql += a->getSqlQuery();
76
+    return mysql.override(sql);
67
 }
77
 }
68
 
78
 
69
 BResult PdnsSlave::overrideDhcp()
79
 BResult PdnsSlave::overrideDhcp()

+ 1
- 1
src/CMakeLists.txt View File

30
   DBO/Actions/ActionAddDomain.h
30
   DBO/Actions/ActionAddDomain.h
31
   )
31
   )
32
 set(LIBS ${LIBS} jsoncpp)
32
 set(LIBS ${LIBS} jsoncpp)
33
-add_executable(pdns-slave ${SOURCE_FILES})
33
+add_executable(pdns-slave ${SOURCE_FILES} DataAccess/MySql.cpp DataAccess/MySql.h)
34
 target_link_libraries(pdns-slave ${LIBS})
34
 target_link_libraries(pdns-slave ${LIBS})

+ 1
- 1
src/DBO/Actions/Action.cpp View File

24
 
24
 
25
 const std::string Action::getSqlQuery() const
25
 const std::string Action::getSqlQuery() const
26
 {
26
 {
27
-    return "SET @domain_id=(SELECT get_domain_id(\"" + _domain + "\"));\n" + getSql();
27
+    return "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + _domain + "\");\n" + getSql();
28
 }
28
 }
29
 
29
 
30
 const std::string Action::getDhcpConf() const
30
 const std::string Action::getDhcpConf() const

+ 2
- 2
src/DBO/Actions/ActionAddDomain.cpp View File

11
 const std::string ActionAddDomain::getSql() const
11
 const std::string ActionAddDomain::getSql() const
12
 {
12
 {
13
     return "INSERT INTO domains (name, type) VALUES (\"" + _domain + "\", \"MASTER\");\n"
13
     return "INSERT INTO domains (name, type) VALUES (\"" + _domain + "\", \"MASTER\");\n"
14
-            "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES ((SELECT get_domain_id(\""
15
-           + _domain + "\")), (SELECT id FROM users ORDER BY id LIMIT 1), 0);";
14
+            "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES ((SELECT id FROM domains WHERE name=\""
15
+           + _domain + "\"), (SELECT id FROM users ORDER BY id LIMIT 1), 0);";
16
 }
16
 }

+ 1
- 1
src/DBO/Actions/ActionAddHost.cpp View File

37
         auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
37
         auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
38
 
38
 
39
         query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
39
         query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
40
-                 "    VALUES((SELECT get_domain_id(\"" + reverseDomain + "\")), \""
40
+                 "    VALUES((SELECT id FROM domains WHERE name=\"" + reverseDomain + "\"), \""
41
                  + reversedValue + "." + reverseDomain + "\", \"PTR\","
41
                  + reversedValue + "." + reverseDomain + "\", \"PTR\","
42
                  " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
42
                  " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
43
     }
43
     }

+ 87
- 0
src/DataAccess/MySql.cpp View File

1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#include <stdlib.h>
6
+#include <string.h>
7
+#include <unistd.h>
8
+#include <fstream>
9
+#include "MySql.h"
10
+
11
+#define tmpTemplate "/tmp/pdns-slave.XXXXXX"
12
+
13
+MySql::MySql(const SqlConfiguration &masterConfig, const SqlConfiguration &slaveConfig)
14
+    : _masterConfig(masterConfig)
15
+    , _slaveConfig(slaveConfig)
16
+{
17
+}
18
+
19
+MySql::~MySql()
20
+{
21
+    clean();
22
+}
23
+
24
+BResult MySql::dump()
25
+{
26
+    BResult res;
27
+    _dumpFilePath = getTempFile();
28
+    int status = system(std::string("mysqldump --routines '-h" + _masterConfig.getHost() + "'"
29
+                       + " '-u" + _masterConfig.getUser() + "'"
30
+                       + " '-p" + _masterConfig.getPassword() + "'"
31
+                       + " " + _masterConfig.getDatabase()
32
+                       + " > " + _dumpFilePath).c_str());
33
+    if (status == 0)
34
+        return res.ok(true);
35
+    return res.error("mysqldump exited with code " + std::to_string(status));
36
+}
37
+
38
+const std::string MySql::getTempFile()
39
+{
40
+    char tmp[sizeof(tmpTemplate)];
41
+    strcpy(tmp, tmpTemplate);
42
+    mkstemp(tmp);
43
+    return std::string(tmp);
44
+}
45
+
46
+void MySql::clean()
47
+{
48
+    if (!_dumpFilePath.empty())
49
+    {
50
+        unlink(_dumpFilePath.c_str());
51
+        _dumpFilePath = "";
52
+    }
53
+    if (!_overrideFilePath.empty())
54
+    {
55
+        unlink(_overrideFilePath.c_str());
56
+        _overrideFilePath = "";
57
+    }
58
+}
59
+
60
+BResult MySql::insert()
61
+{
62
+    _dumpFilePath = getTempFile();
63
+    return insertSlave(_dumpFilePath);
64
+}
65
+
66
+BResult MySql::override(const std::string &sql)
67
+{
68
+    _overrideFilePath = getTempFile();
69
+    std::ofstream file(_overrideFilePath);
70
+    if (!file)
71
+        return BResult().error("Could not open temp file " + _overrideFilePath);
72
+    file << sql;
73
+    file.close();
74
+    return insertSlave(_overrideFilePath);
75
+}
76
+
77
+BResult MySql::insertSlave(const std::string &file)
78
+{
79
+    BResult res;
80
+    int status = system(std::string("cat " + file + " | mysql '-h" + _slaveConfig.getHost() + "'"
81
+                                    + " '-u" + _slaveConfig.getUser() + "'"
82
+                                    + " '-p" + _slaveConfig.getPassword() + "'"
83
+                                    + " " + _slaveConfig.getDatabase()).c_str());
84
+    if (status == 0)
85
+        return res.ok(true);
86
+    return res.error("mysql exited with code " + std::to_string(status));
87
+}

+ 41
- 0
src/DataAccess/MySql.h View File

1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_MYSQL_H
6
+#define PDNS_SLAVE_MYSQL_H
7
+
8
+#include <DBO/Result.h>
9
+#include <DBO/SqlConfiguration.h>
10
+# include "DBO/Result.h"
11
+
12
+class MySql
13
+{
14
+public:
15
+    MySql(const SqlConfiguration& masterConfig, const SqlConfiguration& slaveConfig);
16
+    virtual ~MySql();
17
+
18
+    BResult dump();
19
+
20
+    BResult insert();
21
+
22
+    BResult override(const std::string& sql);
23
+
24
+    void clean();
25
+
26
+private:
27
+    const std::string getTempFile();
28
+
29
+    BResult insertSlave(const std::string& file);
30
+
31
+    SqlConfiguration _masterConfig;
32
+
33
+    SqlConfiguration _slaveConfig;
34
+
35
+    std::string _dumpFilePath;
36
+
37
+    std::string _overrideFilePath;
38
+};
39
+
40
+
41
+#endif //PDNS_SLAVE_MYSQL_H

Loading…
Cancel
Save