Procházet zdrojové kódy

begin pgsql backend

develop
Robin Thoni před 8 roky
rodič
revize
f8d68b9128

+ 2
- 0
conf/pdns-slave.json.default Zobrazit soubor

@@ -4,6 +4,8 @@
4 4
 
5 5
   "hosts-file":       "/etc/pdns-slave/hosts.json",
6 6
 
7
+  "db-type":          "pgsql",
8
+
7 9
   "master-host":      "ns.example.com",
8 10
   "master-user":      "master-user",
9 11
   "master-password":  "master-password",

+ 17
- 4
src/Business/PdnsSlave.cpp Zobrazit soubor

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include <fstream>
6 6
 #include <DataAccess/MySql.h>
7
+#include <DataAccess/PgSql.h>
7 8
 #include "DataAccess/PdnsSlaveConfig.h"
8 9
 #include "DataAccess/HostsConfig.h"
9 10
 #include "PdnsSlave.h"
@@ -31,6 +32,7 @@ BResult PdnsSlave::readConfig()
31 32
     _dhcpdFilePath = conf.getDhcpdFilePath();
32 33
     _dhcpdTemplatePath = conf.getDhcpdTemplatePath();
33 34
     _hostsPath = conf.getHostsPath();
35
+    _dbType = conf.getDbType();
34 36
     _masterConfig = conf.getMasterConfig();
35 37
     _slaveConfig = conf.getSlaveConfig();
36 38
 
@@ -64,16 +66,27 @@ Result<Actions> PdnsSlave::readHosts()
64 66
 
65 67
 BResult PdnsSlave::overridePdns()
66 68
 {
67
-    MySql mysql(_masterConfig, _slaveConfig);
68
-    auto res = mysql.dump();
69
+    AbstractSql* sqlDb = 0;
70
+    if (_dbType == "mysql") {
71
+        sqlDb = new MySql(_masterConfig, _slaveConfig);
72
+    }
73
+    else if (_dbType == "pgsql") {
74
+        sqlDb = new PgSql(_masterConfig, _slaveConfig);
75
+    }
76
+    else {
77
+        return BResult().error("Invalid database type " + _dbType);
78
+    }
79
+    auto res = sqlDb->dump();
69 80
     if (!res)
70 81
         return res;
71
-    if (!(res = mysql.insert()))
82
+    if (!(res = sqlDb->insert()))
72 83
         return res;
73 84
     std::string sql;
74 85
     for (auto a : _actions)
75 86
         sql += a->getSqlQuery();
76
-    return mysql.override(sql);
87
+    res = sqlDb->override(sql);
88
+    delete sqlDb;
89
+    return res;
77 90
 }
78 91
 
79 92
 BResult PdnsSlave::overrideDhcp()

+ 2
- 0
src/Business/PdnsSlave.h Zobrazit soubor

@@ -34,6 +34,8 @@ private:
34 34
 
35 35
     std::string _hostsPath;
36 36
 
37
+    std::string _dbType;
38
+
37 39
     SqlConfiguration _masterConfig;
38 40
 
39 41
     SqlConfiguration _slaveConfig;

+ 1
- 1
src/CMakeLists.txt Zobrazit soubor

@@ -30,7 +30,7 @@ set(SOURCE_FILES
30 30
   DBO/Actions/ActionDelDomain.h
31 31
   DBO/Actions/ActionAddDomain.cpp
32 32
   DBO/Actions/ActionAddDomain.h
33
-        DataAccess/AbstractSql.cpp DataAccess/AbstractSql.h)
33
+        DataAccess/AbstractSql.cpp DataAccess/AbstractSql.h DataAccess/PgSql.cpp DataAccess/PgSql.h)
34 34
 set(LIBS ${LIBS} jsoncpp)
35 35
 add_executable(pdns-slave ${SOURCE_FILES})
36 36
 target_link_libraries(pdns-slave ${LIBS})

+ 1
- 1
src/DBO/Actions/ActionAddDomain.cpp Zobrazit soubor

@@ -78,7 +78,7 @@ void ActionAddDomain::setTtl(int ttl)
78 78
     _ttl = ttl;
79 79
 }
80 80
 
81
-const std::string ActionAddDomain::getSql() const //ns.rthoni.com rthoni.hotmail.fr 2015080600 28800 7200 604800 86400
81
+const std::string ActionAddDomain::getSql() const
82 82
 {
83 83
     auto soa = _soaNs + " " + _soaMail + " " + std::to_string(time(nullptr)) + " " + std::to_string(_soaRefresh)
84 84
              + " " + std::to_string(_soaRetry)+ " " + std::to_string(_soaExpire) + " " + std::to_string(_soaTtl);

+ 15
- 0
src/DataAccess/AbstractSql.cpp Zobrazit soubor

@@ -0,0 +1,15 @@
1
+//
2
+// Created by robin on 4/6/16.
3
+//
4
+
5
+#include "AbstractSql.h"
6
+
7
+AbstractSql::~AbstractSql()
8
+{
9
+}
10
+
11
+AbstractSql::AbstractSql(const SqlConfiguration &masterConfig, const SqlConfiguration &slaveConfig)
12
+        : _masterConfig(masterConfig)
13
+        , _slaveConfig(slaveConfig)
14
+{
15
+}

+ 31
- 0
src/DataAccess/AbstractSql.h Zobrazit soubor

@@ -0,0 +1,31 @@
1
+//
2
+// Created by robin on 4/6/16.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_ABSTRACTSQL_H
6
+#define PDNS_SLAVE_ABSTRACTSQL_H
7
+
8
+#include <DBO/Result.h>
9
+#include <DBO/SqlConfiguration.h>
10
+#include "DBO/Result.h"
11
+
12
+class AbstractSql
13
+{
14
+public:
15
+    AbstractSql(const SqlConfiguration& masterConfig, const SqlConfiguration& slaveConfig);
16
+    virtual ~AbstractSql();
17
+
18
+    virtual BResult dump() = 0;
19
+
20
+    virtual BResult insert() = 0;
21
+
22
+    virtual BResult override(const std::string& sql) = 0;
23
+
24
+protected:
25
+    SqlConfiguration _masterConfig;
26
+
27
+    SqlConfiguration _slaveConfig;
28
+};
29
+
30
+
31
+#endif //PDNS_SLAVE_ABSTRACTSQL_H

+ 11
- 17
src/DataAccess/MySql.cpp Zobrazit soubor

@@ -11,14 +11,22 @@
11 11
 #define tmpTemplate "/tmp/pdns-slave.XXXXXX"
12 12
 
13 13
 MySql::MySql(const SqlConfiguration &masterConfig, const SqlConfiguration &slaveConfig)
14
-    : _masterConfig(masterConfig)
15
-    , _slaveConfig(slaveConfig)
14
+    : AbstractSql(masterConfig, slaveConfig)
16 15
 {
17 16
 }
18 17
 
19 18
 MySql::~MySql()
20 19
 {
21
-    clean();
20
+    if (!_dumpFilePath.empty())
21
+    {
22
+        unlink(_dumpFilePath.c_str());
23
+        _dumpFilePath = "";
24
+    }
25
+    if (!_overrideFilePath.empty())
26
+    {
27
+        unlink(_overrideFilePath.c_str());
28
+        _overrideFilePath = "";
29
+    }
22 30
 }
23 31
 
24 32
 BResult MySql::dump()
@@ -43,20 +51,6 @@ const std::string MySql::getTempFile()
43 51
     return std::string(tmp);
44 52
 }
45 53
 
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 54
 BResult MySql::insert()
61 55
 {
62 56
     return insertSlave(_dumpFilePath);

+ 3
- 11
src/DataAccess/MySql.h Zobrazit soubor

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

+ 12
- 1
src/DataAccess/PdnsSlaveConfig.cpp Zobrazit soubor

@@ -35,6 +35,12 @@ BResult PdnsSlaveConfig::readConfig()
35 35
         return res;
36 36
     if (!(res = readString(root, "hosts-file", _hostsPath)))
37 37
         return res;
38
+    if (!(res = readString(root, "db-type", _dbType)))
39
+        return res;
40
+    if (_dbType != "mysql" && _dbType != "pgsql")
41
+    {
42
+        return BResult().error("Invalid database type " + _dbType);
43
+    }
38 44
     if (!(res = readSqlConfiguration(root, "master", _masterConfig)))
39 45
         return res;
40 46
     if (!(res = readSqlConfiguration(root, "slave", _slaveConfig)))
@@ -97,4 +103,9 @@ const SqlConfiguration &PdnsSlaveConfig::getMasterConfig() const
97 103
 const SqlConfiguration &PdnsSlaveConfig::getSlaveConfig() const
98 104
 {
99 105
     return _slaveConfig;
100
-}
106
+}
107
+
108
+const std::string &PdnsSlaveConfig::getDbType() const
109
+{
110
+    return _dbType;
111
+}

+ 4
- 0
src/DataAccess/PdnsSlaveConfig.h Zobrazit soubor

@@ -27,6 +27,8 @@ public:
27 27
 
28 28
     const std::string &getHostsPath() const;
29 29
 
30
+    const std::string &getDbType() const;
31
+
30 32
     const SqlConfiguration &getMasterConfig() const;
31 33
 
32 34
     const SqlConfiguration &getSlaveConfig() const;
@@ -45,6 +47,8 @@ private:
45 47
 
46 48
     std::string _hostsPath;
47 49
 
50
+    std::string _dbType;
51
+
48 52
     SqlConfiguration _masterConfig;
49 53
 
50 54
     SqlConfiguration _slaveConfig;

+ 83
- 0
src/DataAccess/PgSql.cpp Zobrazit soubor

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

+ 33
- 0
src/DataAccess/PgSql.h Zobrazit soubor

@@ -0,0 +1,33 @@
1
+//
2
+// Created by robin on 4/6/16.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_PGSQL_H
6
+#define PDNS_SLAVE_PGSQL_H
7
+
8
+#include "AbstractSql.h"
9
+
10
+class PgSql : public AbstractSql
11
+{
12
+public:
13
+    PgSql(const SqlConfiguration& masterConfig, const SqlConfiguration& slaveConfig);
14
+    ~PgSql();
15
+
16
+    BResult dump();
17
+
18
+    BResult insert();
19
+
20
+    BResult override(const std::string& sql);
21
+
22
+private:
23
+    const std::string getTempFile();
24
+
25
+    BResult insertSlave(const std::string& file);
26
+
27
+    std::string _dumpFilePath;
28
+
29
+    std::string _overrideFilePath;
30
+};
31
+
32
+
33
+#endif //PDNS_SLAVE_PGSQL_H

Načítá se…
Zrušit
Uložit