From cd4507f47d62fe9fbdcc82331255582c9bd8a924 Mon Sep 17 00:00:00 2001 From: Jirayut 'Dear' Nimsaeng Date: Tue, 5 Jul 2016 17:38:34 +0700 Subject: [PATCH 1/5] Improve create_db.py script by add check to can safety rerun --- create_db.py | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/create_db.py b/create_db.py index a82d228..8dda567 100755 --- a/create_db.py +++ b/create_db.py @@ -5,7 +5,25 @@ from config import SQLALCHEMY_MIGRATE_REPO from app import db from app.models import Role, Setting import os.path -db.create_all() +import time +import sys + +# Create schema +if os.environ.get('WAITFOR_DB') is None: + db.create_all() +else: + for i in range(0, int(os.environ.get('WAITFOR_DB'))): + try: + db.create_all() + break + except: + print("INFO: Wait for database server") + sys.stdout.flush() + time.sleep(1) + continue + print("ERROR: Couldn't connect to database server") + exit(1) + # create initial user roles and turn off maintenance mode admin_role = Role('Administrator', 'Administrator') user_role = Role('User', 'User') @@ -13,15 +31,24 @@ maintenance_setting = Setting('maintenance', 'False') fullscreen_layout_setting = Setting('fullscreen_layout', 'True') record_helper_setting = Setting('record_helper', 'True') default_table_size_setting = Setting('default_record_table_size', '15') -db.session.add(admin_role) -db.session.add(user_role) -db.session.add(maintenance_setting) -db.session.add(fullscreen_layout_setting) -db.session.add(record_helper_setting) -db.session.add(default_table_size_setting) -db.session.commit() + +# Check if record already exists +if not db.session.query(Role).filter_by(name="Administrator").first(): + db.session.add(admin_role) +if not db.session.query(Role).filter_by(name="User").first(): + db.session.add(user_role) +if not db.session.query(Setting).filter_by(name="maintenance").first(): + db.session.add(maintenance_setting) +if not db.session.query(Setting).filter_by(name="fullscreen_layout").first(): + db.session.add(fullscreen_layout_setting) +if not db.session.query(Setting).filter_by(name="record_helper").first(): + db.session.add(record_helper_setting) +if not db.session.query(Setting).filter_by(name="default_record_table_size").first(): + db.session.add(default_table_size_setting) +db_commit = db.session.commit() + if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) -else: +elif db_commit is not None: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) From c2a1c63dc81f297ec8e0203801d451c774ea580e Mon Sep 17 00:00:00 2001 From: Jirayut 'Dear' Nimsaeng Date: Tue, 5 Jul 2016 20:04:13 +0700 Subject: [PATCH 2/5] Improve create_db.py --- create_db.py | 102 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/create_db.py b/create_db.py index 8dda567..41cbd65 100755 --- a/create_db.py +++ b/create_db.py @@ -8,47 +8,71 @@ import os.path import time import sys -# Create schema -if os.environ.get('WAITFOR_DB') is None: - db.create_all() -else: - for i in range(0, int(os.environ.get('WAITFOR_DB'))): - try: - db.create_all() - break - except: - print("INFO: Wait for database server") - sys.stdout.flush() - time.sleep(1) - continue +def start(): + wait_time = get_waittime_from_env() + + if not connect_db(wait_time): print("ERROR: Couldn't connect to database server") exit(1) -# create initial user roles and turn off maintenance mode -admin_role = Role('Administrator', 'Administrator') -user_role = Role('User', 'User') -maintenance_setting = Setting('maintenance', 'False') -fullscreen_layout_setting = Setting('fullscreen_layout', 'True') -record_helper_setting = Setting('record_helper', 'True') -default_table_size_setting = Setting('default_record_table_size', '15') + init_records() -# Check if record already exists -if not db.session.query(Role).filter_by(name="Administrator").first(): - db.session.add(admin_role) -if not db.session.query(Role).filter_by(name="User").first(): - db.session.add(user_role) -if not db.session.query(Setting).filter_by(name="maintenance").first(): - db.session.add(maintenance_setting) -if not db.session.query(Setting).filter_by(name="fullscreen_layout").first(): - db.session.add(fullscreen_layout_setting) -if not db.session.query(Setting).filter_by(name="record_helper").first(): - db.session.add(record_helper_setting) -if not db.session.query(Setting).filter_by(name="default_record_table_size").first(): - db.session.add(default_table_size_setting) -db_commit = db.session.commit() +def get_waittime_from_env(): + return int(os.environ.get('WAITFOR_DB', 1)) -if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): - api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') - api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) -elif db_commit is not None: - api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) +def connect_db(wait_time): + for i in xrange(0, wait_time): + print("INFO: Wait for database server") + sys.stdout.flush() + try: + db.create_all() + return True + except: + time.sleep(1) + + return False + +def init_roles(db, role_names): + name_of_roles = map(lambda r: r.name, role_names) + rows = db.session.query(Role).filter(Role.name.in_(name_of_roles)).all() + name_of_rows = map(lambda r: r.name, rows) + roles = filter(lambda r: r.name not in rows, role_names) + + for role in roles: + db.session.add(role) + +def init_settings(db, setting_names): + name_of_settings = map(lambda r: r.name, setting_names) + rows = db.session.query(Setting).filter(Setting.name.in_(name_of_settings)).all() + name_of_rows = map(lambda r: r.name, rows) + + settings = filter(lambda r: r.name not in rows, setting_names) + + for setting in settings: + db.session.add(setting) + +def init_records(): + # create initial user roles and turn off maintenance mode + admin_role = Role('Administrator', 'Administrator') + user_role = Role('User', 'User') + maintenance_setting = Setting('maintenance', 'False') + fullscreen_layout_setting = Setting('fullscreen_layout', 'True') + record_helper_setting = Setting('record_helper', 'True') + default_table_size_setting = Setting('default_record_table_size', '15') + + # Check if record already exists + init_roles(db, [admin_role, user_role]) + init_settings(db, [maintenance_setting, fullscreen_layout_setting, record_helper_setting, default_table_size_setting]) + + db_commit = db.session.commit() + commit_version_control(db_commit) + +def commit_version_control(db_commit): + if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): + api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + elif db_commit is not None: + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) + +if __name__ == '__main__': + start() From c2f7a528d49650086a359f76de7c6aebcba3dbf0 Mon Sep 17 00:00:00 2001 From: Jirayut 'Dear' Nimsaeng Date: Thu, 7 Jul 2016 11:09:18 +0700 Subject: [PATCH 3/5] Fix create_db.py --- create_db.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/create_db.py b/create_db.py index 41cbd65..33aac84 100755 --- a/create_db.py +++ b/create_db.py @@ -33,26 +33,40 @@ def connect_db(wait_time): return False def init_roles(db, role_names): + + # Get key name of data name_of_roles = map(lambda r: r.name, role_names) + + # Query to get current data rows = db.session.query(Role).filter(Role.name.in_(name_of_roles)).all() name_of_rows = map(lambda r: r.name, rows) - roles = filter(lambda r: r.name not in rows, role_names) + # Check which data that need to insert + roles = filter(lambda r: r.name not in name_of_rows, role_names) + + # Insert data for role in roles: db.session.add(role) def init_settings(db, setting_names): + + # Get key name of data name_of_settings = map(lambda r: r.name, setting_names) + + # Query to get current data rows = db.session.query(Setting).filter(Setting.name.in_(name_of_settings)).all() + + # Check which data that need to insert name_of_rows = map(lambda r: r.name, rows) + settings = filter(lambda r: r.name not in name_of_rows, setting_names) - settings = filter(lambda r: r.name not in rows, setting_names) - + # Insert data for setting in settings: db.session.add(setting) def init_records(): - # create initial user roles and turn off maintenance mode + + # Create initial user roles and turn off maintenance mode admin_role = Role('Administrator', 'Administrator') user_role = Role('User', 'User') maintenance_setting = Setting('maintenance', 'False') From 0737aceddc4721071ea2cfa791ebf423b727f482 Mon Sep 17 00:00:00 2001 From: Jirayut 'Dear' Nimsaeng Date: Thu, 7 Jul 2016 12:14:40 +0700 Subject: [PATCH 4/5] Add Docker Compose file --- config_template_docker.py | 46 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 config_template_docker.py create mode 100644 docker-compose.yml diff --git a/config_template_docker.py b/config_template_docker.py new file mode 100644 index 0000000..b7a9ffa --- /dev/null +++ b/config_template_docker.py @@ -0,0 +1,46 @@ +import os +basedir = os.path.abspath(os.path.dirname(__file__)) + +# BASIC APP CONFIG +WTF_CSRF_ENABLED = True +SECRET_KEY = 'We are the world' +BIND_ADDRESS = '0.0.0.0' +PORT = 9393 +LOGIN_TITLE = "PDNS" + +# TIMEOUT - for large zones +TIMEOUT = 10 + +# LOG CONFIG +LOG_LEVEL = 'DEBUG' +LOG_FILE = '/dev/stdout' + +# Upload +UPLOAD_DIR = os.path.join(basedir, 'upload') + +# DATABASE CONFIG +SQLALCHEMY_DATABASE_URI = 'mysql://root:PowerDNSAdminPassword@mysqldb/powerdns-admin' +SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') +SQLALCHEMY_TRACK_MODIFICATIONS = True + +# LDAP CONFIG +LDAP_TYPE = 'ldap' # use 'ad' for MS Active Directory +LDAP_URI = 'ldaps://your-ldap-server:636' +LDAP_USERNAME = 'cn=dnsuser,ou=users,ou=services,dc=duykhanh,dc=me' +LDAP_PASSWORD = 'dnsuser' +LDAP_SEARCH_BASE = 'ou=System Admins,ou=People,dc=duykhanh,dc=me' +# Additional options only if LDAP_TYPE=ldap +LDAP_USERNAMEFIELD = 'uid' +LDAP_FILTER = '(objectClass=inetorgperson)' + +#Default Auth +BASIC_ENABLED = True +SIGNUP_ENABLED = True + +# POWERDNS CONFIG +PDNS_STATS_URL = 'http://powerdns-server:8081' +PDNS_API_KEY = 'PowerDNSAPIKey' +PDNS_VERSION = '4.0.0' + +# RECORDS ALLOWED TO EDIT +RECORDS_ALLOW_EDIT = ['A', 'AAAA', 'CNAME', 'SPF', 'PTR', 'MX', 'TXT'] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c9271b5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +version: '2' + +services: + + powerdns-authoritative: + image: winggundamth/powerdns-mysql:trusty + hostname: powerdns-authoritative + depends_on: + - powerdns-authoritative-mariadb + links: + - powerdns-authoritative-mariadb:mysqldb + ports: + - 172.17.0.1:53:53/udp + - 8081:8081 + environment: + - PDNS_DB_HOST=mysqldb + - PDNS_DB_USERNAME=root + - PDNS_DB_NAME=powerdns + - PDNS_DB_PASSWORD=PowerDNSPassword + - PDNS_API_KEY=PowerDNSAPIKey + + powerdns-authoritative-mariadb: + image: mariadb:10.1.15 + hostname: powerdns-authoritative-mariadb + environment: + - MYSQL_DATABASE=powerdns + - MYSQL_ROOT_PASSWORD=PowerDNSPassword + + powerdns-admin: + image: winggundamth/powerdns-admin:trusty + hostname: powerdns-admin + depends_on: + - powerdns-admin-mariadb + - powerdns-authoritative + links: + - powerdns-admin-mariadb:mysqldb + - powerdns-authoritative:powerdns-server + volumes: + - ./:/home/web/powerdns-admin + ports: + - 9393:9393 + environment: + - WAITFOR_DB=60 + + powerdns-admin-mariadb: + image: mariadb:10.1.15 + hostname: powerdns-admin-mariadb + environment: + - MYSQL_DATABASE=powerdns-admin + - MYSQL_ROOT_PASSWORD=PowerDNSAdminPassword From d742ca24809e7ec091427838b2b5f9842b949cdd Mon Sep 17 00:00:00 2001 From: Jirayut 'Dear' Nimsaeng Date: Thu, 7 Jul 2016 12:38:58 +0700 Subject: [PATCH 5/5] Add new table records --- create_db.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/create_db.py b/create_db.py index 33aac84..ea3c78d 100755 --- a/create_db.py +++ b/create_db.py @@ -65,14 +65,16 @@ def init_settings(db, setting_names): db.session.add(setting) def init_records(): - + # Create initial user roles and turn off maintenance mode admin_role = Role('Administrator', 'Administrator') user_role = Role('User', 'User') maintenance_setting = Setting('maintenance', 'False') fullscreen_layout_setting = Setting('fullscreen_layout', 'True') record_helper_setting = Setting('record_helper', 'True') + login_ldap_first_setting = Setting('login_ldap_first', 'True') default_table_size_setting = Setting('default_record_table_size', '15') + default_domain_table_size_setting = Setting('default_domain_table_size', '10') # Check if record already exists init_roles(db, [admin_role, user_role])