314 lines
13 KiB
Python
314 lines
13 KiB
Python
from modules.phpserver import phpserver_app
|
|
from settings import config
|
|
from flask import g, url_for, request, session, make_response, abort, flash
|
|
from paramecio2.libraries.generate_admin_class import GenerateAdminClass
|
|
from paramecio2.libraries.lists import SimpleList
|
|
from paramecio2.libraries.i18n import I18n, PGetText
|
|
from paramecio2.modules.admin import admin_app, t as admin_t
|
|
from paramecio2.libraries.db.webmodel import WebModel
|
|
from paramecio2.libraries.lists import AjaxList
|
|
from modules.pastafari2.models.pastafari2 import ServerGroup, ServerDbTask, UpdateServerScripts
|
|
from modules.pastafari2.models.tasks import Task as SSHTask
|
|
from modules.mariadb.models.mariadb import DbServerMariaDb
|
|
from paramecio2.libraries.mtemplates import PTemplate, env_theme
|
|
from paramecio2.libraries.formsutils import show_form
|
|
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
|
|
from modules.pastafari2.libraries.progress import load_progress
|
|
import os
|
|
import configparser
|
|
|
|
try:
|
|
import ujson as json
|
|
except:
|
|
import json
|
|
|
|
pgettext=PGetText(__file__+'/../')
|
|
|
|
_=pgettext.gettext
|
|
|
|
env=env_theme(__file__)
|
|
|
|
t=PTemplate(env)
|
|
|
|
t.env.directories=admin_t.env.directories
|
|
|
|
t.env.directories.insert(1, os.path.dirname(__file__).replace('/admin', '')+'/templates/admin')
|
|
t.env.directories.insert(2, '../pastafari2/templates/admin')
|
|
|
|
config_task.pastafari_paths.append('modules/phpserver/tasks')
|
|
|
|
@phpserver_app.route('/phpserver/servers')
|
|
def php_dashboard():
|
|
|
|
return t.load_template('phpservers.phtml', title=_('PHP Servers'), path_module='phpserver_app.php_dashboard')
|
|
|
|
@phpserver_app.route('/phpserver/get_phpservers', methods=['POST'])
|
|
def get_phpservers():
|
|
|
|
|
|
db=g.connection
|
|
|
|
group_sql=''
|
|
|
|
count_data=[]
|
|
sql_data=[]
|
|
|
|
fields=[[_('Version'), True], [_('Hostname'), True], ['IP', True], [_('Options'), False]]
|
|
arr_order_fields=['version', 'hostname', 'ip']
|
|
|
|
count_query=['select count(phpserver.id) as num_elements from phpserver', count_data]
|
|
|
|
str_query=['select phpserver.version, serverdbtask.hostname, serverdbtask.ip, phpserver.id as id from serverdbtask, phpserver WHERE serverdbtask.id=phpserver.server_id', sql_data]
|
|
|
|
ajax=AjaxList(db, fields, arr_order_fields, count_query, str_query)
|
|
|
|
#ajax.func_fields['id']=options_server
|
|
#ajax.func_fields['ip']=options_ip
|
|
#ajax.func_fields['select_id']=options_selected
|
|
ajax.func_fields['id']=options_options
|
|
ajax.limit=0
|
|
|
|
return ajax.show()
|
|
|
|
def options_options(row_id, row):
|
|
|
|
arr_options=[]
|
|
|
|
#arr_options=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost', server_id=row_id), I18n.lang('dbservermariadb', 'server_users', 'Server users'))]
|
|
#arr_options=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost', dbservermariadb_id=row_id), _('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(url_for('phpserver_app.php_edit_server', php_server_id=row_id), _('Edit')))
|
|
arr_options.append('<a href="{}">{}</a>'.format(url_for('phpserver_app.delete_php', php_server_id=row_id), _('Delete')))
|
|
|
|
return '<br />'.join(arr_options)
|
|
|
|
@phpserver_app.route('/phpserver/delete_php/<int:php_server_id>')
|
|
def delete_php(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
with db.query('select serverdbtask.hostname, phpserver.version from serverdbtask, phpserver where phpserver.server_id=serverdbtask.id and phpserver.id=%s', [php_server_id]) as cursor:
|
|
arr_server=cursor.fetchone()
|
|
|
|
if arr_server:
|
|
|
|
#select * from virtualhost, webserver, serverdbtask, phpserver where virtualhost.webserver_id=webserver.id AND serverdbtask.id=webserver.server_id AND phpserver.server_id=serverdbtask.id AND phpserver.id=19
|
|
with db.query('select count(*) as num_php_vhosts from virtualhost, webserver, serverdbtask, phpserver where virtualhost.webserver_id=webserver.id AND serverdbtask.id=webserver.server_id AND phpserver.server_id=serverdbtask.id AND phpserver.id=%s AND virtualhost.php=%s', [php_server_id, arr_server['version']]) as cursor:
|
|
num_php_vhosts=cursor.fetchone()['num_php_vhosts']
|
|
|
|
|
|
return t.load_template('del_phpserver.phtml', title=_('Remove PHP FPM server'), path_module='phpserver_app.php_dashboard', php_server_id=php_server_id, domain=arr_server['hostname'], num_php_vhosts=num_php_vhosts)
|
|
|
|
abort(404)
|
|
|
|
@phpserver_app.route('/phpserver/delete_php_task/<int:php_server_id>', methods=['POST'])
|
|
def delete_php_task(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
error=1
|
|
|
|
task_id=0
|
|
|
|
error_form={}
|
|
|
|
with db.query('select serverdbtask.hostname, serverdbtask.ip, serverdbtask.ssh_port, phpserver.version from serverdbtask, phpserver where phpserver.server_id=serverdbtask.id and phpserver.id=%s', [php_server_id]) as cursor:
|
|
arr_server=cursor.fetchone()
|
|
|
|
if arr_server:
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
#user=config_task.remote_user
|
|
|
|
ssh_key_priv='./ssh/id_rsa'
|
|
|
|
#run_task(self, server, path, name_task, codename_task, description_task, data={}, user='', password='', where_sql_server='', url='', ssh_key_priv='', ssh_key_password='', send_task=True)
|
|
# user=user, password='', where_sql_server=where_sql, ssh_key_priv=ssh_key_priv, url='', data=data, send_task=True
|
|
|
|
#root_dir='/home/'+username+'/sites/'+domain
|
|
|
|
#ftp_user=arr_user['user']
|
|
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.phpserver.tasks.php.php.delete_php', 'Delete PHP server', 'delete_phpserver', 'Task for delete php-fpm server from server', {'version': arr_server['version'], 'php_server_id': php_server_id}, config_task.remote_user, '', '', url_for('phpserver_app.php_dashboard'), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
|
|
error=1
|
|
|
|
else:
|
|
error=0
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
|
|
|
@phpserver_app.route('/phpserver/progress/<int:php_server_id>/')
|
|
def php_progress(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
#Webservers list >> Web users >> Virtual Hosts
|
|
#cursor=db.query('select `usersftp`.`id` from `usersftp`')
|
|
|
|
return_tree='<p><a href="'+url_for('phpserver_app.php_dashboard')+'">'+_('PHP servers list')+'</a></p>'
|
|
|
|
return load_progress(db, t, return_tree=return_tree, path_module='phpserver_app.php_dashboard')
|
|
|
|
@phpserver_app.route('/phpserver/edit_server/<int:php_server_id>/')
|
|
def php_edit_server(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
with db.query('select serverdbtask.hostname, phpserver.version from serverdbtask, phpserver where phpserver.server_id=serverdbtask.id and phpserver.id=%s', [php_server_id]) as cursor:
|
|
arr_server=cursor.fetchone()
|
|
|
|
if arr_server:
|
|
|
|
#arr_form=BaseForm
|
|
|
|
return t.load_template('edit_phpserver.phtml', title=_('Edit PHP FPM server'), path_module='phpserver_app.php_dashboard', domain=arr_server['hostname'], php_server_id=php_server_id)
|
|
|
|
return {}
|
|
|
|
@phpserver_app.route('/phpserver/get_php_server/<int:php_server_id>/')
|
|
def get_php_server(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
error=1
|
|
error_form={}
|
|
task_id=0
|
|
|
|
with db.query('select serverdbtask.hostname, serverdbtask.ip, serverdbtask.ssh_port, phpserver.version from serverdbtask, phpserver where phpserver.server_id=serverdbtask.id and phpserver.id=%s', [php_server_id]) as cursor:
|
|
arr_server=cursor.fetchone()
|
|
|
|
if arr_server:
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
#user=config_task.remote_user
|
|
|
|
|
|
|
|
#run_task(self, server, path, name_task, codename_task, description_task, data={}, user='', password='', where_sql_server='', url='', ssh_key_priv='', ssh_key_password='', send_task=True)
|
|
# user=user, password='', where_sql_server=where_sql, ssh_key_priv=ssh_key_priv, url='', data=data, send_task=True
|
|
|
|
#root_dir='/home/'+username+'/sites/'+domain
|
|
|
|
#ftp_user=arr_user['user']
|
|
|
|
#if not sshtask.run_task(arr_server['ip'], 'modules.phpserver.tasks.php.php.delete_php', 'Delete PHP server', 'delete_phpserver', 'Task for delete php-fpm server from server', {'version': arr_server['version'], 'php_server_id': php_server_id}, config_task.remote_user, '', '', url_for('phpserver_app.php_dashboard'), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.phpserver.tasks.php.php.get_php_ini', 'Get PHP.ini from server', 'get_php_ini', 'Task for get php.ini config from server', {'version': arr_server['version'], 'php_server_id': php_server_id}, config_task.remote_user, '', '', url_for('phpserver_app.php_dashboard'), ssh_key_priv=config_task.ssh_private_key, ssh_key_password=config_task.ssh_private_key_password, send_task=True, ssh_port=arr_server['ssh_port']):
|
|
|
|
error=1
|
|
|
|
else:
|
|
error=0
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
# wait while tasks is completed
|
|
|
|
# No more of 60 seconds
|
|
|
|
if task_id:
|
|
|
|
arr_log={}
|
|
|
|
check_task=True
|
|
|
|
result={}
|
|
|
|
while check_task:
|
|
|
|
with db.query('select * from logtask where task_id=%s order by id DESC limit 1', [task_id]) as cursor:
|
|
|
|
arr_log=cursor.fetchone()
|
|
|
|
if arr_log:
|
|
|
|
if arr_log['status']==1:
|
|
|
|
error=arr_log['error']
|
|
check_task=False
|
|
if not error:
|
|
with db.query('select * from resulttask where task_id=%s', [task_id]) as cursor:
|
|
result=cursor.fetchone()
|
|
# PHP configuration
|
|
|
|
#
|
|
|
|
#config=configparser.ConfigParser()
|
|
#print(json.loads(result['message'])['file'])
|
|
#config.read_string(json.loads(result['message'])['file'])
|
|
|
|
#print(config['PHP']['display_errors'])
|
|
|
|
return {'error': error, 'error_form': error_form, 'log': arr_log, 'result': result, 'task_id': task_id}
|
|
|
|
@phpserver_app.route('/phpserver/save_php_ini/<int:php_server_id>/', methods=['POST'])
|
|
def save_php_ini(php_server_id):
|
|
|
|
db=g.connection
|
|
|
|
error=1
|
|
error_form={}
|
|
task_id=0
|
|
|
|
result={}
|
|
arr_log={}
|
|
|
|
with db.query('select serverdbtask.hostname, serverdbtask.ip, serverdbtask.ssh_port, phpserver.version from serverdbtask, phpserver where phpserver.server_id=serverdbtask.id and phpserver.id=%s', [php_server_id]) as cursor:
|
|
arr_server=cursor.fetchone()
|
|
|
|
if arr_server:
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
file=request.form.get('php_ini_file')
|
|
|
|
if file.strip()!='':
|
|
|
|
with open('./tmp/php.ini', 'w') as f:
|
|
f.write(file)
|
|
|
|
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.phpserver.tasks.php.php.save_php_ini', 'Save PHP.ini in server', 'save_php_ini', 'Task for save php.ini config on server', {'version': arr_server['version'], 'php_server_id': php_server_id}, config_task.remote_user, '', '', url_for('phpserver_app.php_dashboard'), ssh_key_priv=config_task.ssh_private_key, ssh_key_password=config_task.ssh_private_key_password, send_task=True, ssh_port=arr_server['ssh_port']):
|
|
|
|
error=1
|
|
|
|
else:
|
|
error=0
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
if task_id:
|
|
|
|
check_task=True
|
|
|
|
while check_task:
|
|
|
|
with db.query('select * from logtask where task_id=%s order by id DESC limit 1', [task_id]) as cursor:
|
|
|
|
arr_log=cursor.fetchone()
|
|
|
|
if arr_log:
|
|
|
|
if arr_log['status']==1:
|
|
|
|
error=arr_log['error']
|
|
check_task=False
|
|
|
|
if not error:
|
|
flash(_('The php configuration was sucessfully changed'))
|
|
|
|
else:
|
|
error=1
|
|
error_form['empty_file']=1
|
|
|
|
return {'error': error, 'error_form': error_form, 'log': arr_log, 'task_id': task_id}
|