From: Frank Brehm Date: Fri, 21 Jul 2017 09:20:40 +0000 (+0200) Subject: Start adding Support for Mysql as target DB in pp_lib/import_pdnsdata.py X-Git-Tag: 0.1.2~160 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=0476dae64df9fc76842895bec7b3e610d4c0c1c1;p=pixelpark%2Fadmin-tools.git Start adding Support for Mysql as target DB in pp_lib/import_pdnsdata.py --- diff --git a/db/create-pdns-db-mysql.sql b/db/create-pdns-db-mysql.sql new file mode 100644 index 0000000..66e4fa8 --- /dev/null +++ b/db/create-pdns-db-mysql.sql @@ -0,0 +1,252 @@ +SET FOREIGN_KEY_CHECKS = 0; +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; +SET time_zone = "+02:00"; + +-- User 'pdns' +GRANT USAGE ON *.* TO 'pdns'@'%' IDENTIFIED BY PASSWORD '*7FB5687DEFF0A845B3D64D1D3504F88D48D5206E'; +GRANT USAGE ON *.* TO 'pdns'@'localhost' IDENTIFIED BY PASSWORD '*7FB5687DEFF0A845B3D64D1D3504F88D48D5206E'; + +-- +-- Datenbank: `pdns` +-- +CREATE DATABASE IF NOT EXISTS `pdns` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; + +GRANT ALL PRIVILEGES ON `pdns`.* TO 'pdns'@'%'; +GRANT ALL PRIVILEGES ON `pdns`.* TO 'pdns'@'localhost'; + +USE `pdns`; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `comments` +-- + +DROP TABLE IF EXISTS `comments`; +CREATE TABLE `comments` ( + `id` int(11) NOT NULL, + `domain_id` int(11) NOT NULL, + `name` varchar(255) NOT NULL, + `type` varchar(10) NOT NULL, + `modified_at` int(11) NOT NULL, + `account` varchar(40) NOT NULL, + `comment` text NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `cryptokeys` +-- + +DROP TABLE IF EXISTS `cryptokeys`; +CREATE TABLE `cryptokeys` ( + `id` int(11) NOT NULL, + `domain_id` int(11) NOT NULL, + `flags` int(11) NOT NULL, + `active` tinyint(1) DEFAULT NULL, + `content` text DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `domainmetadata` +-- + +DROP TABLE IF EXISTS `domainmetadata`; +CREATE TABLE `domainmetadata` ( + `id` int(11) NOT NULL, + `domain_id` int(11) NOT NULL, + `kind` varchar(32) DEFAULT NULL, + `content` text DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `domains` +-- + +DROP TABLE IF EXISTS `domains`; +CREATE TABLE `domains` ( + `id` int(11) NOT NULL, + `name` varchar(255) NOT NULL, + `master` varchar(128) DEFAULT NULL, + `last_check` int(11) DEFAULT NULL, + `type` varchar(6) NOT NULL, + `notified_serial` int(11) DEFAULT NULL, + `account` varchar(40) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `records` +-- + +DROP TABLE IF EXISTS `records`; +CREATE TABLE `records` ( + `id` int(11) NOT NULL, + `domain_id` int(11) DEFAULT NULL, + `name` varchar(255) DEFAULT NULL, + `type` varchar(10) DEFAULT NULL, + `content` text DEFAULT NULL, + `ttl` int(11) DEFAULT NULL, + `prio` int(11) DEFAULT NULL, + `change_date` int(11) DEFAULT NULL, + `disabled` tinyint(1) DEFAULT 0, + `ordername` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, + `auth` tinyint(1) DEFAULT 1 +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `supermasters` +-- + +DROP TABLE IF EXISTS `supermasters`; +CREATE TABLE `supermasters` ( + `ip` varchar(64) NOT NULL, + `nameserver` varchar(255) NOT NULL, + `account` varchar(40) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `tsigkeys` +-- + +DROP TABLE IF EXISTS `tsigkeys`; +CREATE TABLE `tsigkeys` ( + `id` int(11) NOT NULL, + `name` varchar(255) DEFAULT NULL, + `algorithm` varchar(50) DEFAULT NULL, + `secret` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Indizes der exportierten Tabellen +-- + +-- +-- Indizes für die Tabelle `comments` +-- +ALTER TABLE `comments` + ADD PRIMARY KEY (`id`), + ADD KEY `comments_domain_id_idx` (`domain_id`), + ADD KEY `comments_name_type_idx` (`name`,`type`), + ADD KEY `comments_order_idx` (`domain_id`,`modified_at`); + +-- +-- Indizes für die Tabelle `cryptokeys` +-- +ALTER TABLE `cryptokeys` + ADD PRIMARY KEY (`id`), + ADD KEY `domainidindex` (`domain_id`); + +-- +-- Indizes für die Tabelle `domainmetadata` +-- +ALTER TABLE `domainmetadata` + ADD PRIMARY KEY (`id`), + ADD KEY `domainmetadata_idx` (`domain_id`,`kind`); + +-- +-- Indizes für die Tabelle `domains` +-- +ALTER TABLE `domains` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `name_index` (`name`); + +-- +-- Indizes für die Tabelle `records` +-- +ALTER TABLE `records` + ADD PRIMARY KEY (`id`), + ADD KEY `nametype_index` (`name`,`type`), + ADD KEY `domain_id` (`domain_id`), + ADD KEY `recordorder` (`domain_id`,`ordername`); + +-- +-- Indizes für die Tabelle `supermasters` +-- +ALTER TABLE `supermasters` + ADD PRIMARY KEY (`ip`,`nameserver`); + +-- +-- Indizes für die Tabelle `tsigkeys` +-- +ALTER TABLE `tsigkeys` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `namealgoindex` (`name`,`algorithm`); + +-- +-- AUTO_INCREMENT für exportierte Tabellen +-- + +-- +-- AUTO_INCREMENT für Tabelle `comments` +-- +ALTER TABLE `comments` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT für Tabelle `cryptokeys` +-- +ALTER TABLE `cryptokeys` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT für Tabelle `domainmetadata` +-- +ALTER TABLE `domainmetadata` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT für Tabelle `domains` +-- +ALTER TABLE `domains` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT für Tabelle `records` +-- +ALTER TABLE `records` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT für Tabelle `tsigkeys` +-- +ALTER TABLE `tsigkeys` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- Constraints der exportierten Tabellen +-- + +-- +-- Constraints der Tabelle `comments` +-- +ALTER TABLE `comments` + ADD CONSTRAINT `fk_comment_domain` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`); + +-- +-- Constraints der Tabelle `cryptokeys` +-- +ALTER TABLE `cryptokeys` + ADD CONSTRAINT `fk_cryptokey_domain` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`); + +-- +-- Constraints der Tabelle `domainmetadata` +-- +ALTER TABLE `domainmetadata` + ADD CONSTRAINT `fk_metadata_domain` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`); + +-- +-- Constraints der Tabelle `records` +-- +ALTER TABLE `records` + ADD CONSTRAINT `fk_record_domain` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`); + +SET FOREIGN_KEY_CHECKS = 1; +COMMIT; + diff --git a/etc/import-pdnsdata.ini.default b/etc/import-pdnsdata.ini.default index d4d186c..3605fca 100644 --- a/etc/import-pdnsdata.ini.default +++ b/etc/import-pdnsdata.ini.default @@ -19,10 +19,13 @@ [tgt_db] +# Type of the database - valid values are 'postgresql' and 'mysql' (default: 'postgresql') +#type = postgresql + # Hostname of the PostgreSQL server to import the data to (Default: systemshare.pixelpark.com) #host = systemshare.pixelpark.com -# TCP-Portnumber of the target PostgreSQL server (Default: 5432) +# TCP-Portnumber of the target PostgreSQL server (Default: 5432 for postgresql and 3306 for mysql)) #port = 5432 # The target PostgreSQL database to use (Default: pdns) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index a9e920d..cfe942b 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -30,7 +30,7 @@ from .common import pp from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.5.3' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) # ============================================================================= @@ -50,8 +50,10 @@ class ImportPdnsdataApp(PpConfigApplication): default_src_db_user = 'pdns' # Target DB data + default_tgt_db_type = 'postgresql' default_tgt_db_host = 'systemshare.pixelpark.com' - default_tgt_db_port = 5432 + default_tgt_db_port_psql = 5432 + default_tgt_db_port_mysql = 3306 default_tgt_db_schema = 'pdns' default_tgt_db_user = 'pdns' @@ -71,8 +73,11 @@ class ImportPdnsdataApp(PpConfigApplication): self.src_db_user = self.default_src_db_user self.src_db_pass = None + self.tgt_db_type = self.default_tgt_db_type self.tgt_db_host = self.default_tgt_db_host - self.tgt_db_port = self.default_tgt_db_port + self.tgt_db_port = self.default_tgt_db_port_psql + if self.tgt_db_type == 'mysql': + self.tgt_db_port = self.default_tgt_db_port_mysql self.tgt_db_schema = self.default_tgt_db_schema self.tgt_db_user = self.default_tgt_db_user self.tgt_db_pass = None @@ -208,11 +213,26 @@ class ImportPdnsdataApp(PpConfigApplication): LOG.debug("Evaluating config section {n!r}:\n{s}".format( n=section_name, s=pp(section))) + if 'type' in section: + db_type = section['type'].lower().strip() + if db_type not in ('mysql', 'postgresql', 'postgres', 'psql'): + LOG.error('Invalid database type {!r} in configuration section {!r} found.'.format( + section['type'], section_name)) + self.config_has_errors = True + else: + if db_type == 'mysql': + self.tgt_db_type = 'mysql' + self.tgt_db_port = self.default_tgt_db_port_mysql + else: + self.tgt_db_type = 'postgresql' + self.tgt_db_port = self.default_tgt_db_port_psql + if 'host' in section: host = section['host'].lower().strip() if not host: LOG.error('Invalid target hostname {!r} in configuration section {!r} found.'.format( section['host'], section_name)) + self.config_has_errors = True else: try: _ = socket.getaddrinfo(host, 3306, proto=socket.IPPROTO_TCP)