993 lines
35 KiB
Python
993 lines
35 KiB
Python
from settings import config
|
|
from flask import g, url_for, request, session, make_response
|
|
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.apache.models.webservers import VirtualHost, UsersFtp, WebServer, WebServerPort
|
|
from paramecio2.libraries.mtemplates import PTemplate, env_theme
|
|
from paramecio2.libraries.formsutils import show_form
|
|
from paramecio2.libraries.db.coreforms import HiddenForm, SelectForm, TextForm, PasswordForm
|
|
from paramecio2.libraries.slugify import slugify
|
|
from modules.pastafari2.libraries.configtask import config_task
|
|
from modules.pastafari2.libraries.progress import load_progress
|
|
import os
|
|
|
|
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')
|
|
|
|
system_path='./ssh/'
|
|
|
|
if hasattr(config, 'pastafari_system_path'):
|
|
system_path=config.pastafari_system_path
|
|
|
|
config_task.pastafari_paths.append('modules/apache/tasks')
|
|
|
|
@admin_app.route('/webservers/servers/')
|
|
def webservers():
|
|
|
|
return t.load_template('webservers.phtml', title=_('Webservers'), path_module='admin_app.webservers')
|
|
|
|
@admin_app.route('/get_webservers', methods=['POST'])
|
|
def get_webservers():
|
|
|
|
|
|
db=g.connection
|
|
|
|
group_sql=''
|
|
|
|
count_data=[]
|
|
sql_data=[]
|
|
|
|
fields=[[_('Hostname'), True], ['IP', True], [_('Options'), False]]
|
|
arr_order_fields=['hostname', 'ip']
|
|
|
|
count_query=['select count(webserver.id) as num_elements from webserver', count_data]
|
|
|
|
# server.id as select_id,
|
|
|
|
# select hostname, ip, date, num_updates, id from serverofuser where user_id=%s;
|
|
|
|
#str_query=['select webserver.hostname, webserver.ip, webserver.id from webserver,serverdbtask', sql_data]
|
|
|
|
str_query=['select serverdbtask.hostname, serverdbtask.ip, webserver.id as id from webserver, serverdbtask WHERE serverdbtask.id=webserver.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=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost', server_id=row_id), _('Server users'))]
|
|
arr_options=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost', webserver_id=row_id), _('Websites'))]
|
|
#
|
|
#arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.ports', webserver_id=row_id), _('HTTP Ports')))
|
|
#arr_options.append('<a href="{}">{}</a>'.format("", _('Edit')))
|
|
#arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.webservers_edit_languages', webserver_id=row_id), _('Languages')))
|
|
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.delete_webserver', webserver_id=row_id), _('Delete')))
|
|
|
|
return '<br />'.join(arr_options)
|
|
|
|
def post_virtual_update(admin, item_id):
|
|
|
|
with admin.model.query('select * from virtualhost where id=%s', [admin.virtualhost_id]) as cursor:
|
|
arr_virtualhost=cursor.fetchone()
|
|
|
|
with admin.model.query('select uid_hosting, gid_hosting from webserver where id=%s', [arr_virtualhost['webserver_id']]) as cursor:
|
|
arr_webserver=cursor.fetchone()
|
|
|
|
if virtualhost:
|
|
|
|
#arr_user=admin.model.select_a_row(item_id, [], True)
|
|
|
|
home_user=arr_virtualhost['home']
|
|
|
|
admin.model.query('update usersftp set uid=%s, gid=%s, home=%s, virtualhost_id=%s WHERE id=%s', [arr_webserver['uid_hosting'], arr_webserver['gid_hosting'], home_user, admin.virtualhost_id, item_id])
|
|
|
|
#virtualhost_id=request.args.get('')
|
|
|
|
return True
|
|
|
|
@admin_app.route('/webservers/delete/<int:webserver_id>', methods=['GET', 'POST'])
|
|
def delete_webserver(webserver_id):
|
|
|
|
db=g.connection
|
|
|
|
webserver=WebServer(db)
|
|
|
|
arr_webserver=webserver.select_a_row(webserver_id)
|
|
|
|
if arr_webserver:
|
|
|
|
return t.load_template('del_webserver.phtml', title=_('Remove webserver'), path_module='admin_app.webservers', webserver_id=arr_webserver['id'], domain=arr_webserver['server_id'])
|
|
|
|
else:
|
|
return ""
|
|
|
|
@admin_app.route('/delete_webserver/<int:webserver_id>', methods=['GET', 'POST'])
|
|
def delete_webserver_db(webserver_id):
|
|
|
|
db=g.connection
|
|
|
|
error=1
|
|
|
|
webserver=WebServer(db)
|
|
|
|
if webserver.set_conditions('WHERE id=%s', [webserver_id]).delete():
|
|
|
|
error=0
|
|
|
|
return {'error': error}
|
|
|
|
# Ports of ftp server
|
|
|
|
@admin_app.route('/webservers/ports/<int:webserver_id>', methods=['GET', 'POST'])
|
|
def ports(webserver_id):
|
|
|
|
db=g.connection
|
|
|
|
ports=WebServerPort(db)
|
|
|
|
webserver=WebServer(db)
|
|
|
|
arr_webserver=webserver.select_a_row(webserver_id)
|
|
|
|
#print(arr_webserver)
|
|
|
|
ports.set_conditions('WHERE webserver_id=%s', [webserver_id])
|
|
|
|
ports.fields['webserver_id'].name_form=HiddenForm
|
|
ports.fields['webserver_id'].extra_parameters=[]
|
|
|
|
ports.fields['webserver_id'].default_value=request.args.get('webserver_id', webserver_id)
|
|
|
|
admin=GenerateAdminClass(ports, url_for('admin_app.ports', webserver_id=webserver_id), t)
|
|
|
|
admin.list.fields_showed=['ip', 'port']
|
|
|
|
#usersftp=UsersFtp(db)
|
|
|
|
#arr_user=usersftp.select_a_row(user_id, [], True)
|
|
|
|
form_admin=admin.show()
|
|
|
|
if type(form_admin).__name__=='str':
|
|
|
|
return t.load_template('ports.phtml', title=_('Server ports'), path_module='admin_app.ports', form_admin=form_admin, webserver_id=webserver_id, hostname=arr_webserver['server_id'])
|
|
else:
|
|
|
|
return form_admin
|
|
|
|
"""
|
|
webserver=WebServer(db)
|
|
|
|
arr_webserver=webserver.select_a_row(webserver_id, [], True)
|
|
|
|
if arr_webserver:
|
|
|
|
return t.load_template('virtualhosts.phtml', title=_('Virtual Hosts'), path_module='admin_app.webservers', webserver_id=webserver_id)
|
|
|
|
else:
|
|
|
|
return ''
|
|
"""
|
|
|
|
|
|
# FTP USERS for virtualhost
|
|
|
|
@admin_app.route('/webservers/server_users/<int:virtualhost_id>', methods=['GET', 'POST'])
|
|
def server_users(virtualhost_id):
|
|
|
|
db=g.connection
|
|
|
|
usersftp=UsersFtp(db)
|
|
|
|
usersftp.set_conditions('WHERE virtualhost_id=%s', [virtualhost_id])
|
|
|
|
usersftp.fields['passwd'].protected=False
|
|
|
|
vhost=VirtualHost(db)
|
|
|
|
arr_vhost=vhost.select_a_row(virtualhost_id, [], True)
|
|
|
|
webserver_id=arr_vhost['webserver_id']
|
|
|
|
#usersftp.check_user=False
|
|
#usersftp.check_email=False
|
|
|
|
admin=GenerateAdminClass(usersftp, url_for('admin_app.server_users', virtualhost_id=virtualhost_id), t)
|
|
|
|
#admin.arr_fields_edit=['user', 'passwd']
|
|
admin.list.search_fields=['user']
|
|
admin.list.fields_showed=['user', 'quota_mb']
|
|
|
|
admin.arr_fields_edit=['user', 'email', 'passwd', 'repeat_password', 'quota_mb']
|
|
|
|
admin.virtualhost_id=virtualhost_id
|
|
|
|
admin.post_update=post_virtual_update
|
|
|
|
admin.list.arr_extra_options=[option_users]
|
|
|
|
#admin.post_update=
|
|
|
|
#return t.load_template('serverusers.phtml', title=_('Server users'), path_module='admin_app.server_users', admin=admin)
|
|
|
|
form_admin=admin.show()
|
|
|
|
if type(form_admin).__name__=='str':
|
|
|
|
return t.load_template('serverusers.phtml', title=_('FTP users'), path_module='admin_app.webservers', form_admin=form_admin, webserver_id=webserver_id)
|
|
else:
|
|
|
|
return form_admin
|
|
|
|
def option_users(url, row_id, row):
|
|
|
|
#arr_options=['<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost, user_id=user_id), _('Server users'))]
|
|
#
|
|
#arr_options.append('<a href="{}">{}</a>'.format("", _('Edit')))
|
|
#arr_options.append('<a href="{}">{}</a>'.format("", _('Delete')))
|
|
|
|
#return '<br />'.join(arr_options)
|
|
|
|
arr_options=[] #['<a href="'+url_for('admin_app.virtualhost', user_id=row_id)+'">'+_('Virtual Hosts')+'</a>']
|
|
|
|
arr_options+=SimpleList.standard_options(url, row_id, row)
|
|
|
|
return arr_options
|
|
|
|
@admin_app.route('/webservers/virtualhost/<int:webserver_id>')
|
|
def virtualhost(webserver_id):
|
|
|
|
db=g.connection
|
|
|
|
#usersftp=UsersFtp(db)
|
|
|
|
#arr_user=usersftp.select_a_row(user_id, [], True)
|
|
|
|
webserver=WebServer(db)
|
|
|
|
#arr_webserver=webserver.select_a_row(webserver_id, [], True)
|
|
arr_webserver=None
|
|
with db.query('select webserver.id, serverdbtask.hostname from webserver, serverdbtask where webserver.server_id=serverdbtask.id AND webserver.id=%s', [webserver_id]) as cursor:
|
|
arr_webserver=cursor.fetchone()
|
|
|
|
if arr_webserver:
|
|
|
|
usernames=[]
|
|
|
|
with db.query('select DISTINCT username from virtualhost WHERE webserver_id=%s', [webserver_id]) as cursor:
|
|
usernames=cursor.fetchall()
|
|
|
|
return t.load_template('virtualhosts.phtml', title=_('Virtual Hosts'), path_module='admin_app.webservers', webserver_id=webserver_id, hostname=arr_webserver['hostname'], usernames=usernames)
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
def options_vhosts(row_id, row):
|
|
|
|
arr_options=[]
|
|
|
|
#arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.server_users', virtualhost_id=row_id), _('Ftp users')))
|
|
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.webapps', virtualhost_id=row_id), _('Webapps')))
|
|
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.edit_virtualhost', virtualhost_id=row_id), _('Edit')))
|
|
arr_options.append('<a href="{}">{}</a>'.format(url_for('admin_app.virtualhost_delete', virtualhost_id=row_id), _('Delete')))
|
|
|
|
return '<br />'.join(arr_options)
|
|
|
|
def options_username(row_id, row):
|
|
|
|
return '<a class="change_password" href="'+url_for('admin_app.change_user_password')+'">'+row['username']+'</a>'
|
|
|
|
@admin_app.route('/get_virtualhosts', methods=['POST'])
|
|
def get_virtualhosts():
|
|
|
|
db=g.connection
|
|
|
|
webserver_id=request.args.get('webserver_id', '0')
|
|
|
|
username=request.args.get('username', '').strip()
|
|
|
|
group_sql=''
|
|
|
|
count_data=[webserver_id]
|
|
sql_data=[webserver_id]
|
|
|
|
extra_sql=''
|
|
|
|
if username!='':
|
|
count_data.append(username)
|
|
sql_data.append(username)
|
|
extra_sql=' AND username=%s'
|
|
|
|
fields=[[_('Domain'), True], [_('User'), True], [_('Options'), False]]
|
|
arr_order_fields=['domain']
|
|
|
|
count_query=['select count(virtualhost.id) as num_elements from virtualhost WHERE webserver_id=%s'+extra_sql, count_data]
|
|
|
|
# server.id as select_id,
|
|
|
|
# select hostname, ip, date, num_updates, id from serverofuser where webserver_id=%s;
|
|
|
|
#str_query=['select webserver.hostname, webserver.ip, webserver.id from webserver,serverdbtask', sql_data]
|
|
|
|
str_query=['select domain, username, id from virtualhost WHERE webserver_id=%s'+extra_sql, 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['username']=options_username
|
|
ajax.func_fields['id']=options_vhosts
|
|
ajax.limit=0
|
|
|
|
return ajax.show()
|
|
|
|
|
|
@admin_app.route('/add_virtualhost/<int:webserver_id>', methods=['GET'])
|
|
def add_virtualhost(webserver_id):
|
|
|
|
db=g.connection
|
|
|
|
#usersftp=UsersFtp(db)
|
|
|
|
#arr_user=usersftp.select_a_row(user_id, [], True)
|
|
|
|
#webserver_id=arr_user['webserver_id']
|
|
|
|
virtual=VirtualHost(db)
|
|
|
|
virtual.fields['webserver_id'].name_form=HiddenForm
|
|
virtual.fields['webserver_id'].extra_parameters=[]
|
|
|
|
virtual.fields['webserver_id'].default_value=request.args.get('webserver_id', webserver_id)
|
|
|
|
#virtual.fields['cgi_type'].name_form=SelectForm
|
|
#virtual.fields['cgi_type'].extra_parameters=[{'': _('Anything cgi content'), 'php74': 'PHP 7.4 (php-fpm)', 'php80': 'PHP 8.0 (php-fpm)', 'php81': 'PHP 8.1 (php-fpm)', 'php82': 'PHP 8.2 (php-fpm)', 'python3': 'Python3 (mod_wsgi)'}]
|
|
virtual.fields['username'].label=_('Virtualhost user')
|
|
virtual.fields['username'].help=_('The virtualhost is saved inside unix user. You can set the username in this field')
|
|
|
|
virtual.fields['cgi_type'].default_value=request.args.get('cgi_type', '')
|
|
|
|
virtual.fields['ip'].label=_('Ip of virtualhost, empty if server default')
|
|
virtual.fields['ip'].help=_('Ip of virtualhost, empty if server default. If you don\'t know that value your need, leave it empty')
|
|
|
|
virtual.fields['domain'].help=_('The domain of new website')
|
|
|
|
virtual.fields['port'].help=_('Change the base port used for the website if you want use behind of a reverse proxy')
|
|
|
|
virtual.fields['port'].default_value=80
|
|
|
|
#virtual.fields['user_id'].name_form=
|
|
|
|
#'php74', 'php80', 'php81', 'php82', 'wsgi'
|
|
|
|
virtual.create_forms(['domain', 'webserver_id', 'ip', 'username'])
|
|
|
|
"""
|
|
virtual.forms['password']=PasswordForm('password', '')
|
|
virtual.forms['password'].label=_('User password')
|
|
virtual.forms['password'].help=_('User password used for the user if you want access to this server using this user. <strong>Remember, the password is not saved in pastafari</strong>')
|
|
virtual.forms['repeat_password']=PasswordForm('repeat_password', '')
|
|
virtual.forms['repeat_password'].label=_('Repeat User password')
|
|
"""
|
|
|
|
|
|
#print(virtual.fields.keys())
|
|
|
|
arr_form=virtual.forms
|
|
#print(arr_form['user_id'].model.fields.keys())
|
|
|
|
|
|
form=show_form({}, arr_form, t, yes_error=True, pass_values=False, modelform_tpl='forms/modelform.phtml')
|
|
|
|
return t.load_template('add_virtualhost.phtml', title=_('Add Website'), path_module='admin_app.webservers', form=form, webserver_id=webserver_id)
|
|
|
|
|
|
@admin_app.route('/save_virtualhost/', methods=['POST'])
|
|
def save_virtualhost():
|
|
|
|
db=g.connection
|
|
|
|
domain=request.form.get('domain', '').strip()
|
|
|
|
#home=slugify(request.form.get('home' '').strip())
|
|
|
|
webserver_id=request.form.get('webserver_id', '0')
|
|
|
|
cgi_type=request.form.get('cgi_type', '')
|
|
|
|
port=80 #request.form.get('port', '0')
|
|
|
|
ip=request.form.get('ip', '')
|
|
|
|
error=0
|
|
|
|
error_form={}
|
|
|
|
webserver=WebServer(db)
|
|
|
|
virtualhost=VirtualHost(db)
|
|
|
|
if virtualhost.fields['domain'].check(domain)=='':
|
|
error_form['#domain_error']='Sorry, invalid domain'
|
|
error=1
|
|
|
|
check_port=int(virtualhost.fields['port'].check(port))
|
|
|
|
if check_port==0 or check_port<0 or (check_port<1000 and check_port!=80) or check_port>65536:
|
|
error_form['#port_error']='Sorry, invalid port'
|
|
error=1
|
|
else:
|
|
port=check_port
|
|
|
|
ip=virtualhost.fields['ip'].check(ip)
|
|
|
|
if ip=='':
|
|
ip='*'
|
|
|
|
username=virtualhost.fields['username'].check(request.form.get('username', ''))
|
|
|
|
if username=='':
|
|
error_form['#username_error']='Sorry, invalid username'
|
|
error=1
|
|
|
|
arr_virtualhost=virtualhost.set_conditions('WHERE domain=%s', [domain]).select_a_row_where([], True)
|
|
|
|
if arr_virtualhost:
|
|
error_form['#domain_error']='Sorry, domain exists in server'
|
|
error=1
|
|
|
|
"""
|
|
if 'password' in post and 'repeat_password' in post:
|
|
|
|
if post['password'].strip()!='' and post['password']==post['repeat_password']:
|
|
|
|
self.data['password']=post['password'].strip()
|
|
|
|
else:
|
|
|
|
self.arr_form['password'].error=True
|
|
self.arr_form['password'].txt_error='Passwords doesn\'t match'
|
|
error=True
|
|
"""
|
|
|
|
"""
|
|
password=request.form.get('password', '')
|
|
|
|
repeat_password=request.form.get('repeat_password', '')
|
|
|
|
if password!=repeat_password:
|
|
error_form['#password_error']='Passwords doesn\'t match'
|
|
error=1
|
|
"""
|
|
|
|
"""
|
|
if virtualhost.fields['home'].check(home)=='':
|
|
error_form['#home_error']='Sorry, invalid home'
|
|
error=1
|
|
"""
|
|
|
|
task_id=0
|
|
|
|
serverdb=ServerDbTask(db)
|
|
|
|
arr_webserver=webserver.set_conditions('WHERE webserver.id=%s', [webserver_id]).select_a_row_where([], True)
|
|
|
|
if arr_webserver and not error:
|
|
|
|
arr_server=serverdb.select_a_row(arr_webserver['server_id'], [], True)
|
|
|
|
#username=arr_webserver['username']
|
|
|
|
#print(arr_user)
|
|
|
|
#server_id=arr_user['webserver_id_server_id']
|
|
|
|
#username=arr_user['webserver_id_username']
|
|
|
|
#Check if not exists domain, etc
|
|
|
|
#arr_server=serverdb.select_a_row(server_id)
|
|
|
|
#print(arr_server)
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
#user=config_task.remote_user
|
|
|
|
ssh_key_priv=system_path+'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.apache.tasks.apache.apache.add_vhost', 'Add Apache virtualhost', 'add_apache_virtualhost', 'Task for add new virtualhost to Apache Server', {'domain': domain, 'email': config.portal_email, 'webserver_id': arr_webserver['id'], 'user': username, 'root_dir': root_dir, 'cgi_type': cgi_type, 'ip': ip, 'port': port}, config_task.remote_user, '', '', url_for('admin_app.virtualhost', webserver_id=arr_webserver['id']), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
error=1
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
else:
|
|
|
|
|
|
error=1
|
|
|
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
|
|
|
@admin_app.route('/virtualhost/progress/<int:webserver_id>/')
|
|
def virtualhost_progress(webserver_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('admin_app.webservers')+'">'+_('Webservers list')+'</a> >> <a href="'+url_for('admin_app.virtualhost', webserver_id=webserver_id)+'">'+_('Websites')+'</a></p>'
|
|
|
|
return load_progress(db, t, return_tree=return_tree, path_module='admin_app.webservers')
|
|
|
|
@admin_app.route('/virtualhost/delete/<int:virtualhost_id>')
|
|
def virtualhost_delete(virtualhost_id):
|
|
|
|
db=g.connection
|
|
|
|
vhost=VirtualHost(db)
|
|
|
|
usersftp=UsersFtp(db)
|
|
|
|
arr_virtualhost=vhost.select_a_row(virtualhost_id, [], True)
|
|
|
|
#arr_user=usersftp.select_a_row(arr_virtualhost['user_id'], [], True)
|
|
|
|
return t.load_template('del_virtualhost.phtml', title=_('Remove Virtual Host'), path_module='admin_app.webservers', webserver_id=arr_virtualhost['webserver_id'], virtualhost_id=virtualhost_id, domain=arr_virtualhost['domain'])
|
|
|
|
@admin_app.route('/delete_virtualhost/', methods=['POST'])
|
|
def delete_virtualhost():
|
|
|
|
virtualhost_id=request.form.get('virtualhost_id', '0')
|
|
|
|
error=0
|
|
|
|
error_form={}
|
|
|
|
task_id=0
|
|
|
|
db=g.connection
|
|
|
|
vhost=VirtualHost(db)
|
|
|
|
#usersftp=UsersFtp(db)
|
|
|
|
arr_virtualhost=vhost.select_a_row(virtualhost_id, [], True)
|
|
|
|
if arr_virtualhost:
|
|
|
|
webserver_id=arr_virtualhost['webserver_id']
|
|
|
|
#arr_user=usersftp.set_conditions('WHERE usersftp.id=%s', [webserver_id]).select_a_row_where([])
|
|
|
|
#server_id=arr_virtualhost['server_id']
|
|
|
|
#username=arr_virtualhost['username']
|
|
|
|
root_dir=arr_virtualhost['home']
|
|
|
|
#ftp_user=arr_user['user']
|
|
|
|
webserver=WebServer(db)
|
|
|
|
arr_webserver=webserver.select_a_row(webserver_id, [], True)
|
|
|
|
#username=arr_webserver['username']
|
|
|
|
serverdb=ServerDbTask(db)
|
|
|
|
arr_server=serverdb.select_a_row(arr_webserver['server_id'], [], True)
|
|
|
|
domain=arr_virtualhost['domain']
|
|
username=arr_virtualhost['username']
|
|
|
|
if arr_virtualhost:
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
ssh_key_priv=system_path+'id_rsa'
|
|
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.apache.tasks.apache.apache.delete_vhost', 'Remove Apache virtualhost', 'remove_apache_virtualhost', 'Task for remove a virtualhost to Apache Server', {'domain': domain, 'email': config.portal_email, 'webserver_id': arr_webserver['id'], 'user': username, 'root_dir': root_dir, 'cgi_type': ''}, config_task.remote_user, '', '', url_for('admin_app.virtualhost', webserver_id=webserver_id), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
|
|
error=1
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
else:
|
|
|
|
error=1
|
|
|
|
pass
|
|
else:
|
|
error=1
|
|
|
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
|
|
|
@admin_app.route('/edit_virtualhost/<int:virtualhost_id>', methods=['GET'])
|
|
def edit_virtualhost(virtualhost_id):
|
|
|
|
db=g.connection
|
|
|
|
virtual=VirtualHost(db)
|
|
|
|
#arr_vhost=virtual.select_a_row(virtualhost_id, [], True)
|
|
with virtual.query('select virtualhost.*, webserver.id, serverdbtask.distro, serverdbtask.id, serverdbtask.hostname from virtualhost, webserver, serverdbtask WHERE virtualhost.id=%s AND virtualhost.webserver_id=webserver.id AND webserver.server_id=serverdbtask.id', [virtualhost_id]) as cursor:
|
|
|
|
arr_vhost=cursor.fetchone()
|
|
|
|
#{'id': 3, 'domain': 'wp.cuchulu.com', 'home': '/home/developer/sites/wp.cuchulu.com', 'username': 'developer', 'webserver_id': 13, 'ip': '', 'port': 0, 'ssl_port': 0, 'cgi_type': '', 'ssl': 0, 'aliases': '', 'redirect_ssl': 0, 'indexes': 0, 'allow_override': 1, 'php': '', 'webserver.id': 13, 'distro': 'arch'}
|
|
|
|
if arr_vhost:
|
|
|
|
#virtual=VirtualHost(db)
|
|
|
|
virtual.fields['aliases'].label=_('Domain aliases')
|
|
virtual.fields['aliases'].help=_('Domain aliases are the other domain names of this server. For example www.domain.com, other-site.domain.com. You can separate the domains with commas')
|
|
|
|
virtual.fields['webserver_id'].name_form=HiddenForm
|
|
virtual.fields['webserver_id'].extra_parameters=[]
|
|
|
|
#virtual.fields['webserver_id'].default_value=arr_vhost['webserver_id']
|
|
|
|
#virtual.fields['cgi_type'].name_form=SelectForm
|
|
#virtual.fields['cgi_type'].extra_parameters=[{'': _('Anything cgi content'), 'php74': 'PHP 7.4 (php-fpm)', 'php80': 'PHP 8.0 (php-fpm)', 'php81': 'PHP 8.1 (php-fpm)', 'php82': 'PHP 8.2 (php-fpm)', 'python3': 'Python3 (mod_wsgi)'}]
|
|
|
|
#virtual.fields['cgi_type'].default_value=arr_vhost['cgi_type']
|
|
|
|
virtual.fields['ip'].label=_('Ip of virtualhost, empty if server default')
|
|
virtual.fields['ip'].help=_('Ip of virtualhost, empty if server default. If you don\'t know that value your need, leave it empty')
|
|
|
|
virtual.fields['domain'].help=_('The domain of new website')
|
|
|
|
virtual.fields['port'].help=_('Change the base port used for the website if you want use behind of a reverse proxy. The port need to be specified in http ports edit in webservers')
|
|
virtual.fields['ssl_port'].help=_('Change the base port used for the website if you want use behind of a reverse proxy. The port need to be specified in http ports edit in webservers')
|
|
|
|
virtual.fields['ssl'].name_form=SelectForm
|
|
virtual.fields['ssl'].extra_parameters=[{0: 'No', 1: 'Lets Encrypt', 2: 'SSL Files'}]
|
|
|
|
virtual.fields['redirect_ssl'].name_form=SelectForm
|
|
virtual.fields['redirect_ssl'].extra_parameters=[{0: _('No'), 1: _('Yes')}]
|
|
|
|
virtual.fields['indexes'].name_form=SelectForm
|
|
virtual.fields['indexes'].extra_parameters=[{0: _('No'), 1: _('Yes')}]
|
|
virtual.fields['indexes'].help=_('Add support show files if index.html not exists?')
|
|
|
|
virtual.fields['allow_override'].name_form=SelectForm
|
|
virtual.fields['allow_override'].extra_parameters=[{0: _('No'), 1: _('Yes')}]
|
|
virtual.fields['allow_override'].help=_('Add support for htaccess?')
|
|
|
|
|
|
virtual.fields['php'].label=_('PHP support')
|
|
virtual.fields['php'].name_form=SelectForm
|
|
"""
|
|
if arr_vhost['distro']!='arch':
|
|
virtual.fields['php'].extra_parameters=[{'': 'No php support', '7.4': 'PHP 7.4', '8.0': 'PHP 8.0', '8.1': 'PHP 8.1', '8.2': 'PHP 8.2', '8.3': 'PHP 8.3'}]
|
|
else:
|
|
virtual.fields['php'].extra_parameters=[{'': 'No php support', '8.1': 'PHP 8.1 (On Arch, php-legacy for maximum compatibility)'}]
|
|
"""
|
|
with db.query('select * from phpserver where server_id=%s', [arr_vhost['serverdbtask.id']]) as cursor:
|
|
arr_php=cursor.fetchall()
|
|
|
|
#print(arr_php)
|
|
|
|
virtual.fields['php'].extra_parameters=[{'': 'No php support'}]
|
|
|
|
for p in arr_php:
|
|
#print(p['version'])
|
|
virtual.fields['php'].extra_parameters[0][p['version']]='PHP '+p['version']
|
|
|
|
virtual.fields['php'].help=_('Add support to php to virtualhost. You can choose ophp version that you prefer')
|
|
|
|
#'port', 'ssl_port',
|
|
|
|
virtual.create_forms(['domain', 'aliases', 'webserver_id', 'ip', 'indexes', 'allow_override', 'redirect_ssl', 'ssl', 'php'])
|
|
|
|
|
|
#print(virtual.fields.keys())
|
|
|
|
arr_form=virtual.forms
|
|
#print(arr_form['user_id'].model.fields.keys())
|
|
|
|
arr_form['ssl_crt']=TextForm('ssl_crt', '')
|
|
|
|
arr_form['ssl_crt'].help=_('Certificate text from file of ssl certificate, normally ending with .crt')
|
|
|
|
arr_form['ssl_crt'].label=_('Ssl certificate')
|
|
|
|
arr_form['ssl_crt'].css='hide_form'
|
|
|
|
arr_form['ssl_key']=TextForm('ssl_key', '')
|
|
|
|
arr_form['ssl_key'].help=_('Key text from file of ssl certificate, normally ending with .key')
|
|
|
|
arr_form['ssl_key'].label=_('Ssl key')
|
|
|
|
arr_form['ssl_key'].css='hide_form'
|
|
|
|
if arr_vhost['ssl_port']==0:
|
|
arr_vhost['ssl_port']=443
|
|
|
|
form=show_form(arr_vhost, arr_form, t, yes_error=True, pass_values=True, modelform_tpl='forms/modelform.phtml')
|
|
|
|
return t.load_template('edit_virtualhost.phtml', title=_('Edit Virtual Host'), path_module='admin_app.webservers', form=form, webserver_id=arr_vhost['webserver_id'], hostname=arr_vhost['hostname'], virtualhost_id=virtualhost_id)
|
|
|
|
pass
|
|
|
|
return ""
|
|
|
|
@admin_app.route('/save_edit_virtualhost/', methods=['POST'])
|
|
def save_edit_virtualhost():
|
|
|
|
db=g.connection
|
|
|
|
domain=request.form.get('domain', '').strip()
|
|
|
|
#home=slugify(request.form.get('home' '').strip())
|
|
|
|
webserver_id=request.form.get('webserver_id', '0')
|
|
virtualhost_id=request.args.get('virtualhost_id', '0')
|
|
|
|
cgi_type=request.form.get('cgi_type', '')
|
|
|
|
#port=request.form.get('port', '0')
|
|
|
|
#ssl_port=request.form.get('ssl_port', '0')
|
|
port=80
|
|
ssl_port=443
|
|
|
|
|
|
ssl=request.form.get('ssl', '0')
|
|
|
|
indexes=request.form.get('indexes', '0')
|
|
|
|
redirect_ssl=request.form.get('redirect_ssl', '0')
|
|
|
|
if indexes!='1':
|
|
indexes='0'
|
|
|
|
allow_override=request.form.get('allow_override', '0')
|
|
|
|
if allow_override!='1':
|
|
allow_override='0'
|
|
|
|
ip=request.form.get('ip', '')
|
|
|
|
error=0
|
|
|
|
error_form={}
|
|
|
|
webserver=WebServer(db)
|
|
|
|
virtualhost=VirtualHost(db)
|
|
|
|
if virtualhost.fields['domain'].check(domain)=='':
|
|
error_form['#domain_error']='Sorry, invalid domain'
|
|
error=1
|
|
|
|
check_port=int(virtualhost.fields['port'].check(port))
|
|
|
|
if check_port==0 or check_port<0 or (check_port<1000 and check_port!=80) or check_port>65536:
|
|
error_form['#port_error']='Sorry, invalid port'
|
|
error=1
|
|
else:
|
|
port=check_port
|
|
|
|
check_ssl_port=int(virtualhost.fields['ssl_port'].check(ssl_port))
|
|
|
|
if check_ssl_port==0 or check_ssl_port<0 or (check_ssl_port<1000 and check_ssl_port!=443) or check_ssl_port>65536:
|
|
error_form['#ssl_port_error']='Sorry, invalid port'
|
|
error=1
|
|
else:
|
|
ssl_port=check_ssl_port
|
|
|
|
check_ssl=int(virtualhost.fields['ssl'].check(ssl))
|
|
|
|
if check_ssl<0 and check_ssl>2:
|
|
error_form['#ssl_error']='Sorry, invalid ssl options'
|
|
error=1
|
|
else:
|
|
ssl=check_ssl
|
|
|
|
if ssl==2:
|
|
#Load text ssl files
|
|
ssl_crt_text=request.form.get('ssl_crt', '').strip()
|
|
ssl_key_text=request.form.get('ssl_key', '').strip()
|
|
|
|
if ssl_crt_text!='':
|
|
with open('modules/webservers/scripts/files/'+domain+'-ssl.crt', 'w') as f:
|
|
f.write(ssl_crt_text)
|
|
|
|
if ssl_key_text!='':
|
|
with open('modules/webservers/scripts/files/'+domain+'-ssl.key', 'w') as f:
|
|
f.write(ssl_key_text)
|
|
|
|
ip=virtualhost.fields['ip'].check(ip)
|
|
|
|
if ip=='':
|
|
ip='*'
|
|
|
|
"""
|
|
if virtualhost.fields['home'].check(home)=='':
|
|
error_form['#home_error']='Sorry, invalid home'
|
|
error=1
|
|
"""
|
|
aliases=request.form.get('aliases', '')
|
|
|
|
arr_aliases=[]
|
|
|
|
if aliases!='':
|
|
arr_aliases=[virtualhost.fields['domain'].check(alias.strip()) for alias in aliases.split(',') if virtualhost.fields['domain'].check(alias.strip())!='']
|
|
|
|
if len(arr_aliases)>0:
|
|
aliases=",".join(arr_aliases)
|
|
|
|
php=request.form.get('php', '')
|
|
|
|
if php!='':
|
|
if php!='8.2' and php!='8.3' and php!='8.4':
|
|
php=''
|
|
|
|
task_id=0
|
|
|
|
serverdb=ServerDbTask(db)
|
|
|
|
arr_webserver=webserver.set_conditions('WHERE webserver.id=%s', [webserver_id]).select_a_row_where([], True)
|
|
|
|
if arr_webserver and not error:
|
|
|
|
arr_server=serverdb.select_a_row(arr_webserver['server_id'], [], True)
|
|
|
|
arr_virtualhost=virtualhost.select_a_row(virtualhost_id, [], True)
|
|
|
|
username=arr_virtualhost['username']
|
|
|
|
#print(arr_user)
|
|
|
|
#server_id=arr_user['webserver_id_server_id']
|
|
|
|
#username=arr_user['webserver_id_username']
|
|
|
|
#Check if not exists domain, etc
|
|
|
|
#arr_server=serverdb.select_a_row(server_id)
|
|
|
|
#print(arr_server)
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
#user=config_task.remote_user
|
|
|
|
ssh_key_priv=system_path+'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=arr_virtualhost['home']
|
|
|
|
#ftp_user=arr_user['user']
|
|
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.apache.tasks.apache.apache.edit_vhost', 'Edit Apache virtualhost', 'edit_apache_virtualhost', 'Task for edit a virtualhost to Apache Server', {'domain': domain, 'email': config.portal_email, 'webserver_id': arr_webserver['id'], 'user': username, 'root_dir': root_dir, 'cgi_type': cgi_type, 'ip': ip, 'port': port, 'ssl': ssl, 'ssl_port': ssl_port, 'aliases': aliases, 'indexes': indexes, 'allow_override': allow_override, 'virtualhost_id': virtualhost_id, 'redirect_ssl': redirect_ssl, 'php': php}, config_task.remote_user, '', '', url_for('admin_app.virtualhost', webserver_id=arr_webserver['id']), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
error=1
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
else:
|
|
|
|
|
|
error=1
|
|
|
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
|
|
|
@admin_app.route('/webservers/change_user_password', methods=['POST'])
|
|
def change_user_password():
|
|
|
|
webserver_id=request.args.get('webserver_id', '0')
|
|
|
|
error=0
|
|
|
|
error_form={}
|
|
|
|
task_id=0
|
|
|
|
db=g.connection
|
|
|
|
vhost=VirtualHost(db)
|
|
|
|
#usersftp=UsersFtp(db)
|
|
|
|
webserver=WebServer(db)
|
|
|
|
arr_webserver=webserver.select_a_row(webserver_id, [], True)
|
|
|
|
if arr_webserver:
|
|
"""
|
|
password=request.form.get('password', '')
|
|
|
|
repeat_password=request.form.get('repeat_password', '')
|
|
|
|
if password!=repeat_password and password!='':
|
|
error_form['#password_error']='Passwords doesn\'t match'
|
|
password=''
|
|
error=1
|
|
"""
|
|
|
|
#ssh-keygen -l -f id_rsa.pub
|
|
|
|
ssh_pub_key=request.form.get('ssh_pub_key', '')
|
|
|
|
user=request.form.get('user', '')
|
|
|
|
if user=='':
|
|
error_form['#user_error']=_('Error: you need an user')
|
|
error=1
|
|
|
|
if ssh_pub_key=='':
|
|
error_form['#ssh_pub_key_error']=_('Error: you need a ssh public key')
|
|
error=1
|
|
|
|
#username=arr_webserver['username']
|
|
|
|
serverdb=ServerDbTask(db)
|
|
|
|
arr_server=serverdb.select_a_row(arr_webserver['server_id'], [], True)
|
|
|
|
sshtask=SSHTask(db)
|
|
|
|
ssh_key_priv=system_path+'id_rsa'
|
|
|
|
#'password': password
|
|
|
|
if not sshtask.run_task(arr_server['ip'], 'modules.pastafari2.tasks.system.change_password_user', 'Change user password', 'Change user password', 'Task for change unix user password', {'user': user, 'ssh_pub_key': ssh_pub_key}, config_task.remote_user, '', '', url_for('admin_app.virtualhost', webserver_id=webserver_id), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
|
|
|
error=1
|
|
|
|
task_id=sshtask.task_id
|
|
|
|
else:
|
|
error=1
|
|
|
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
|
|
|
@admin_app.route('/webservers/users_list', methods=['GET'])
|
|
def users_list():
|
|
|
|
return {}
|