Fixes in maridb

This commit is contained in:
absurdo 2023-07-01 01:03:30 +02:00
parent a6455547a2
commit b950f48245
5 changed files with 348 additions and 1 deletions

View file

@ -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=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost', dbservermariadb_id=row_id), I18n.lang('dbservermariadb', 'websites', 'Websites'))]
#
#arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.ports', dbservermariadb_id=row_id), I18n.lang('dbservermariadb', 'http_ports', 'HTTP Ports')))
#arr_options.append('<a href="{}">{}</a>'.format("", I18n.lang('dbservermariadb', 'edit', 'Edit')))
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.edit_mariadbserver', mariadbserver_id=row_id), I18n.lang('dbservermariadb', 'edit', 'Edit')))
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.delete_mariadbserver', mariadbserver_id=row_id), I18n.lang('dbservermariadb', 'delete', 'Delete')))
return '<br />'.join(arr_options)
@ -115,3 +117,72 @@ def delete_mariadbserver_db(mariadbserver_id):
return {'error': error}
@admin_app.route('/mariadbservers/edit/<int:mariadbserver_id>', 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/<int:mariadbserver_id>', 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}

View file

@ -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'))

115
scripts/update_mariadb.py Normal file
View file

@ -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')

128
tasks/change_ip_mysql.py Normal file
View file

@ -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 '<h2>Mariadb/MySQL configuration</h2>'+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
"""

View file

@ -0,0 +1,31 @@
<%inherit file="dashboard.phtml"/>
<%block name="content">
<p><a href="${url_for('admin_app.mariadbservers')}">${lang('mariadbservers', 'mariadbservers_list', 'MariaDB servers list')}</a> &gt;&gt; ${lang('mariadbservers', 'edit_mariadbserver', 'Edit MariaDB server')}</p>
<form method="post" name="edit_mariadbserver" id="edit_mariadbserver">
<input type="hidden" name="mariadbserver_id" value="${mariadbserver_id}" />
<h3>${domain}</h3>
<p><strong>${lang('mariadbservers', 'the_mariadbserver_edit_explain', 'You can change the ip access of your mariadb server here')}</strong></p>
<input type="text" name="access_ip" value="${access_ip}" /><span class="error" id="access_ip_error" ></span>
<p><input type="submit" value="${lang('mariadbservers', 'edit_mariadbserver', 'Edit mariadbserver')}" />
</form>
<p><a href="${url_for('admin_app.mariadbservers')}">${lang('mariadbservers', 'mariadbservers_list', 'mariadbservers list')}</a> &gt;&gt; ${lang('mariadbservers', 'edit_mariadbserver', 'Edit mariadbserver')}</p>
</%block>
<%block name="jscript_block">
<script language="Javascript" src="${make_media_url('js/jsutils/posting2.js', 'monit')}"></script>
<script>
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean, upload: {progressbar: '#progressbar', 'total_loader': '#total_loader', 'status': '#status'} */
options={'url': '${url_for("admin_app.edit_mariadbserver_task", mariadbserver_id=mariadbserver_id)}', 'loading': '#layer_loading', 'success': function (data) {
//http://192.168.122.1:5000/pastafari2/progress/?task_id=531
location.href="${url_for('admin_app.pastafari2_progress')}?task_id="+data.task_id;
}
};
$('#edit_mariadbserver').sendPost(options);
</script>
</%block>