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 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 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 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') scripts_lists={'wordpress': ('Wordpress', 'modules.apache.tasks.apache.scripts.wordpress'), 'phpmyadmin': ('PHPMyAdmin', 'modules.apache.tasks.apache.scripts.phpmyadmin'), 'flask': ('Flask application', 'modules.apache.tasks.apache.scripts.flask'), 'proxy': ('Webproxy', 'modules.apache.tasks.apache.scripts.proxy')} if hasattr(config, 'webservers_scripts_lists'): scripts_lists.update(config.webservers_scripts_lists) arr_paths={v[1]:1 for v in scripts_lists.values()} for path in arr_paths.keys(): t.env.directories.append(path) @admin_app.route('/webapps/', methods=['GET']) def webapps(virtualhost_id): db=g.connection vhost=VirtualHost(db) arr_vhost=vhost.select_a_row(virtualhost_id, [], True) if arr_vhost: with db.query('select serverdbtask.id from serverdbtask, webserver WHERE webserver.id=%s AND serverdbtask.id=webserver.server_id', [arr_vhost['webserver_id']]) as cursor: arr_server=cursor.fetchone() pass return t.load_template('webapps.phtml', title=I18n.lang('webservers', 'webapps', 'Webapps'), path_module='admin_app.webservers', domain=arr_vhost['domain'], webserver_id=arr_vhost['webserver_id'], virtualhost_id=virtualhost_id, server_id=arr_server['id']) return {} def options_webapps(row_id, row): arr_options=[] #arr_options.append('{}'.format(url_for('admin_app.server_users', virtualhost_id=row_id), I18n.lang('webserver', 'ftp_users', 'Ftp users'))) #arr_options.append('{}'.format(url_for('admin_app.webapps', virtualhost_id=row_id), I18n.lang('webserver', 'webapps', 'Webapps'))) #arr_options.append('{}'.format(url_for('admin_app.edit_virtualhost', virtualhost_id=row_id), I18n.lang('webserver', 'edit', 'Edit'))) arr_options.append('{}'.format("", I18n.lang('webserver', 'delete', 'Delete'))) return '
'.join(arr_options) @admin_app.route('/get_webapps', methods=['POST']) def get_webapps(): db=g.connection virtualhost_id=request.args.get('virtualhost_id', '0') group_sql='' count_data=[] sql_data=[virtualhost_id] fields=[[I18n.lang('webservers', 'app_name', 'App'), True], [I18n.lang('webservers', 'path', 'Path'), True], [I18n.lang('webservers', 'options', 'Options'), False]] arr_order_fields=['app_name'] count_query=['select count(webapp.id) as num_elements from webapp', count_data] # server.id as select_id, # select hostname, ip, date, num_updates, id from serverofuser where user_id=%s; #str_query=['select virtualhost.hostname, virtualhost.ip, virtualhost.id from virtualhost,serverdbtask', sql_data] str_query=['select app_name, path, id from webapp WHERE virtualhost_id=%s', 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_webapps ajax.limit=0 return ajax.show() @admin_app.route('/add_new_app/') def add_new_app(virtualhost_id): #ids #task db=g.connection vhost=VirtualHost(db) arr_vhost=vhost.select_a_row(virtualhost_id, [], True) if arr_vhost: #arr_server=serverdb.set_conditions('WHERE id=%s', [arr_vhost with vhost.query('select serverdbtask.id as server_id, serverdbtask.ip, webserver.id from serverdbtask, webserver WHERE webserver.id=%s AND webserver.server_id=serverdbtask.id', [arr_vhost['webserver_id']]) as cursor: arr_server=cursor.fetchone() return t.load_template('add_webapp.phtml', title=I18n.lang('webservers', 'add_webapp', 'Add webapp'), path_module='admin_app.webservers', domain=arr_vhost['domain'], webserver_id=arr_vhost['webserver_id'], virtualhost_id=virtualhost_id, scripts_lists=scripts_lists, server_id=arr_server['server_id']) return {} @admin_app.route('/add_new_app_form/') def add_new_app_form(virtualhost_id): db=g.connection vhost=VirtualHost(db) arr_vhost=vhost.select_a_row(virtualhost_id, [], True) if arr_vhost: form_app=request.args.get('name_app', '') if form_app in scripts_lists: tpl_form_path=form_app+'.html' #with open(tpl_form_path) as f: # tpl_form=f.read() tpl_form=t.load_template(tpl_form_path) return t.load_template('add_webapp_form.phtml', title=I18n.lang('webservers', 'add_webapp', 'Add webapp'), path_module='admin_app.webservers', domain=arr_vhost['domain'], webserver_id=arr_vhost['webserver_id'], virtualhost_id=virtualhost_id, tpl_form=tpl_form) return {} @admin_app.route('/delete_webapp/', methods=['GET', 'POST']) def delete_webapp(webapp_id): #db=g.connection #return t.load_template('del_virtualhost.phtml', title=I18n.lang('webservers', 'remove_virtualhost', 'Remove Virtual Host'), path_module='admin_app.webservers', webserver_id=arr_virtualhost['webserver_id'], virtualhost_id=virtualhost_id, domain=arr_virtualhost['domain']) # Create task and redirect to multiprogress pass