From b950f4824553fde1a740dc87a9ad0e8ff6fc17ad Mon Sep 17 00:00:00 2001 From: absurdo Date: Sat, 1 Jul 2023 01:03:30 +0200 Subject: [PATCH] Fixes in maridb --- admin/app.py | 73 ++++++++++++- models/mariadb.py | 2 + scripts/update_mariadb.py | 115 ++++++++++++++++++++ tasks/change_ip_mysql.py | 128 +++++++++++++++++++++++ templates/admin/edit_mariadbserver.phtml | 31 ++++++ 5 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 scripts/update_mariadb.py create mode 100644 tasks/change_ip_mysql.py create mode 100644 templates/admin/edit_mariadbserver.phtml diff --git a/admin/app.py b/admin/app.py index aa191f0..9d98cba 100644 --- a/admin/app.py +++ b/admin/app.py @@ -15,6 +15,8 @@ from paramecio2.libraries.db.coreforms import HiddenForm, SelectForm, TextForm from paramecio2.libraries.slugify import slugify from modules.pastafari2.libraries.configtask import config_task from modules.pastafari2.libraries.progress import load_progress +from modules.pastafari2.models.tasks import Task as SSHTask +from modules.pastafari2.libraries.configtask import config_task import os try: @@ -79,7 +81,7 @@ def options_options(row_id, row): #arr_options=['{}'.format(url_for('admin_app.virtualhost', dbservermariadb_id=row_id), I18n.lang('dbservermariadb', 'websites', 'Websites'))] # #arr_options.append('{}'.format(url_for('admin_app.ports', dbservermariadb_id=row_id), I18n.lang('dbservermariadb', 'http_ports', 'HTTP Ports'))) - #arr_options.append('{}'.format("", I18n.lang('dbservermariadb', 'edit', 'Edit'))) + arr_options.append('{}'.format(url_for('admin_app.edit_mariadbserver', mariadbserver_id=row_id), I18n.lang('dbservermariadb', 'edit', 'Edit'))) arr_options.append('{}'.format(url_for('admin_app.delete_mariadbserver', mariadbserver_id=row_id), I18n.lang('dbservermariadb', 'delete', 'Delete'))) return '
'.join(arr_options) @@ -115,3 +117,72 @@ def delete_mariadbserver_db(mariadbserver_id): return {'error': error} +@admin_app.route('/mariadbservers/edit/', methods=['GET', 'POST']) +def edit_mariadbserver(mariadbserver_id): + + db=g.connection + + mariadbserver=DbServerMariaDb(db) + + arr_mariadbserver=mariadbserver.select_a_row(mariadbserver_id) + + if arr_mariadbserver: + + return t.load_template('edit_mariadbserver.phtml', title=I18n.lang('mariadbservers', 'edit_mariadbserver', 'Edit MariaDb server'), path_module='admin_app.mariadbservers', mariadbserver_id=arr_mariadbserver['id'], domain=arr_mariadbserver['server_id'], access_ip=arr_mariadbserver['access_ip']) + + else: + return "" + +@admin_app.route('/mariadbservers/task/', methods=['GET', 'POST']) +def edit_mariadbserver_task(mariadbserver_id): + + db=g.connection + + access_ip=request.form.get('access_ip').strip() + + error=0 + + error_form={} + + serverdb=DbServerMariaDb(db) + server=ServerDbTask(db) + + arr_serverdb=serverdb.select_a_row(mariadbserver_id, [], True) + + if access_ip=='': + access_ip='127.0.0.1' + + if arr_serverdb: + + arr_server=server.select_a_row(arr_serverdb['server_id']) + + print(arr_serverdb['server_id']) + + access_ip=serverdb.fields['access_ip'].check(access_ip) + + if not access_ip: + error=1 + error_form['#access_ip_error']=I18n.lang('mariadb', 'error_wrong_ip_format', 'Error: wrong ip format') + # modules/mariadb/tasks/change_ip_mysql.py + + if not error: + + sshtask=SSHTask(db) + + #user=config_task.remote_user + + ssh_key_priv='./ssh/id_rsa' + + if not sshtask.run_task(arr_server['ip'], 'modules.mariadb.tasks.change_ip_mysql', 'Edit mariadb server', 'edit_mariadb_server', 'Task for edit a MariaDB server', {'ip': access_ip, 'mariadb_id': mariadbserver_id}, config_task.remote_user, '', '', url_for('admin_app.edit_mariadbserver', mariadbserver_id=mariadbserver_id), ssh_key_priv): + error=1 + + task_id=sshtask.task_id + + return {'error': error, 'error_form': error_form, 'task_id': task_id} + + else: + + error_form['#access_ip_error']=I18n.lang('mariadb', 'error_cannot_set_the_task', 'Error: cannot set the task') + error=1 + + return {'error': error, 'error_form': error_form} diff --git a/models/mariadb.py b/models/mariadb.py index f892fe0..810332a 100644 --- a/models/mariadb.py +++ b/models/mariadb.py @@ -24,5 +24,7 @@ class DbServerMariaDb(WebModel): super().__init__(connection) self.register(corefields.ForeignKeyField('server_id', ServerDbTask(connection), 11, False, 'id', 'hostname', select_fields=[])) + self.register(IpField('access_ip')) + diff --git a/scripts/update_mariadb.py b/scripts/update_mariadb.py new file mode 100644 index 0000000..3d9f1ef --- /dev/null +++ b/scripts/update_mariadb.py @@ -0,0 +1,115 @@ +#!/opt/pythonenv/bin/python3 -u + +import sys +import subprocess +import argparse +import platform +import distro + +pyv=platform.python_version_tuple() + +if pyv[0]!='3': + print('Need python 3 for execute this script') + sys.exit(1) + +parser = argparse.ArgumentParser(description='Script for create a new mariadb server.') + +parser.add_argument('--ip', help='The IP where mariadb petitions are listened, if not, only listen in localhost') + +args = parser.parse_args() + +linux_distro=distro.id() + +#Dash, the default debian stretch shell, don't support <<< + +#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password password your_password' +#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password_again password your_password' + +print('Updating ip from MariaDB...') + +if linux_distro=='debian': + + # sed -i 's/old-text/new-text/g' input.txt + + # /etc/mysql/mariadb.conf.d/50-server.cnf + # bind-address = 127.0.0.1 + + print('Setting the ip...') + + """ + if args.ip: + if subprocess.call("sudo sed -i 's/^bind-address = .*\\n$/bind-address = {}\\n/g' /etc/mysql/mariadb.conf.d/50-server.cnf".format(args.ip), shell=True) > 0: + print('Error, cannot update MariaDB...') + sys.exit(1) + """ + + if args.ip: + + ip=args.ip + print('Changing IP to listen...') + + else: + + ip='127.0.0.1' + + print('Listen localhost by default...') + + if subprocess.call("sudo sed -i 's/^bind-address = .*$/bind-address = {}/g' /etc/mysql/mariadb.conf.d/50-server.cnf".format(args.ip), shell=True) > 0: + print('Error, cannot modify MariaDB ip...') + sys.exit(1) + + + if subprocess.call("sudo systemctl restart mariadb", shell=True) > 0: + print('Error, cannot restart mariadb') + sys.exit(1) + + print('Ip configured successfully!') + +elif linux_distro=='arch': + + if args.ip: + + ip=args.ip + print('Changing IP to listen...') + + else: + + ip='127.0.0.1' + + print('Listen localhost by default...') + + if subprocess.call("sudo sed -i 's/^\[server\]$/[server]\\n\\nbind-address = {}/g' /etc/my.cnf.d/server.cnf".format(ip), shell=True) > 0: + print('Error, cannot modify MariaDB ip...') + sys.exit(1) + +elif linux_distro=='rocky' or linux_distro=='alma' or linux_distro=='fedora': + + if args.ip: + + ip=args.ip + print('Changing IP to listen...') + + else: + + ip='127.0.0.1' + + print('Listen localhost by default...') + + + if subprocess.call("sudo sed -i 's/pid-file=\/run\/mariadb\/mariadb\.pid/pid-file=\/run\/mariadb\/mariadb.pid\\nbind-address = {}/g' /etc/my.cnf.d/mariadb-server.cnf".format(ip), shell=True) > 0: + print('Error, cannot modify MariaDB ip...') + sys.exit(1) + + +if subprocess.call("sudo systemctl restart mariadb", shell=True) > 0: + print('Error, cannot restart mariadb') + sys.exit(1) + + +""" +if subprocess.call("sudo echo 'mariadb-server mariadb-server/root_password_again password "+args.password+"' | sudo debconf-set-selections", shell=True) > 0: + print('Error, cannot set the password again') + sys.exit(1) +""" + +print('Setted the password') diff --git a/tasks/change_ip_mysql.py b/tasks/change_ip_mysql.py new file mode 100644 index 0000000..2e99d52 --- /dev/null +++ b/tasks/change_ip_mysql.py @@ -0,0 +1,128 @@ +#/usr/bin/env python3 + +from modules.pastafari2.libraries.task import Task +#from modules.pastafari.models.tasks import TaskModel +from paramecio2.libraries.db import coreforms +from paramecio2.libraries.db.extrafields.ipfield import IpField +from paramecio2.libraries.formsutils import show_form +from collections import OrderedDict +from modules.pastafari2.models.pastafari2 import ServerDbTask +try: + from modules.mariadb.models.mariadb import DbServerMariaDb + server_db=True +except: + server_db=False + + +class ServerTask(Task): + + def __init__(self, server, conn, remote_user='root', remote_password='', private_key='./ssh/id_rsa', password_key='', remote_path='pastafari2', task_id=0, data={}): + + super().__init__(server, conn, remote_user, remote_password, private_key, password_key, remote_path, task_id, data) + + self.name_task='MariaDB installation' + + self.description_task='Update of a standalone mysql server' + + self.codename_task='update_mysql' + + self.files=[['modules/mariadb/scripts/update_mariadb.py', 0o700]] + + # Format first array element is command with the interpreter, the task is agnostic, the files in os directory. The commands are setted with 750 permission. + # First element is the file, next elements are the arguments + + #self.commands_to_execute=[['modules/pastafari/scripts/servers/databases/mysql/install_mariadb.py', '']]; + + #THe files to delete + + self.delete_files=[] + + self.delete_directories=['modules/mariadb/scripts'] + + #self.task=Task(conn) + + self.one_time=False + + self.version='1.0' + + self.arr_form=OrderedDict() + + #self.commands_to_execute=[['modules/pastafari/scripts/servers/databases/mariadb/install_mariadb.py', '--password=%s' % self.data['mysql_password']]] + + def post_task(self): + + if server_db: + dbserver=DbServerMariaDb(self.connection) + + dbserver.safe_query() + + serverdb=ServerDbTask(self.connection) + + #arr_server=serverdb.set_conditions('WHERE ip=%s', [self.server]).select_a_row_where() + arr_mariadb=dbserver.set_conditions('WHERE id=%s', [self.data['mariadb_id']]).select_a_row_where([], True) + + if arr_mariadb: + dbserver.set_conditions('WHERE id=%s', [self.data['mariadb_id']]).update({'access_ip': self.data['ip']}) + + return True + + + def pre_task(self): + + ip_option='' + + if 'ip' in self.data: + ip_option='--ip='+self.data['ip'] + + self.commands_to_execute=[['modules/mariadb/scripts/update_mariadb.py', '%s' % (ip_option)]] + + return True + + """ + def form(self, t, yes_error=False, pass_values=False, values={}): + + #Here load the form for it task + + return '

Mariadb/MySQL configuration

'+show_form(values, self.arr_form, t, yes_error, pass_values) + + def check_form(self, post): + + error=False + + if 'mysql_password' in post and 'repeat_mysql_password' in post: + + if post['mysql_password'].strip()!='' and post['mysql_password']==post['repeat_mysql_password']: + + self.data['mysql_password']=post['mysql_password'].strip() + + else: + + self.arr_form['mysql_password'].error=True + self.arr_form['mysql_password'].txt_error='Passwords doesn\'t match' + error=True + + if 'access_localhost' in post: + ip_check=IpField('ip') + + ip_host=post['access_localhost'].strip() + + if ip_host!='': + + ip_host=ip_check.check(ip_host) + + if ip_host!='': + self.data['ip']=ip_host + + if ip_check.error: + + self.arr_form['access_localhost'].error=True + self.arr_form['access_localhost'].txt_error='Wrong ip format' + error=True + + + if error: + return False + else: + return True + """ + diff --git a/templates/admin/edit_mariadbserver.phtml b/templates/admin/edit_mariadbserver.phtml new file mode 100644 index 0000000..21fa8cc --- /dev/null +++ b/templates/admin/edit_mariadbserver.phtml @@ -0,0 +1,31 @@ +<%inherit file="dashboard.phtml"/> +<%block name="content"> +

${lang('mariadbservers', 'mariadbservers_list', 'MariaDB servers list')} >> ${lang('mariadbservers', 'edit_mariadbserver', 'Edit MariaDB server')}

+
+ +

${domain}

+

${lang('mariadbservers', 'the_mariadbserver_edit_explain', 'You can change the ip access of your mariadb server here')}

+ +

+

+

${lang('mariadbservers', 'mariadbservers_list', 'mariadbservers list')} >> ${lang('mariadbservers', 'edit_mariadbserver', 'Edit mariadbserver')}

+ +<%block name="jscript_block"> + + +