Added support to delete webapps

This commit is contained in:
absurdo 2023-11-28 15:02:38 +01:00
parent 25aaa0a4bf
commit 4f7d88f995
4 changed files with 288 additions and 1 deletions

View file

@ -0,0 +1,172 @@
#!/usr/bin/python3 -u
from collections import OrderedDict
import os
#from modules.pastafari2.models.servers import Server
from modules.apache.models.webservers import WebServer, VirtualHost
import json
from modules.pastafari2.libraries.task import Task
from modules.apache.libraries.webapptask import WebAppTask
from modules.pastafari2.models.pastafari2 import ServerDbTask
from paramecio2.libraries.db import coreforms
from paramecio2.libraries.i18n import I18n
from paramecio2.libraries.formsutils import show_form
from paramecio2.libraries.db.extrafields.usernamefield import UserNameField
from paramecio2.libraries.db.extrafields.emailfield import EmailField
from paramecio2.libraries.db.extrafields.urlfield import DomainField
from paramecio2.libraries.urls import make_url
from modules.apache.admin.install_apps import scripts_lists
from flask import request
class ServerTask(WebAppTask):
def __init__(self, server, conn, remote_user='root', remote_password='', private_key='./ssh/id_rsa', password_key='', remote_path='pastafari2', task_id=0, data={}, port=22):
super().__init__(server, conn, remote_user, remote_password, private_key, password_key, remote_path, task_id, data, port)
self.name_task='Uninstall a webapp using a simple script'
self.description_task='Drop application using a script'
self.codename_task='apache_webserver_delete_app'
self.files=[]
#THe files to delete
self.delete_files=[]
self.delete_directories=['modules/apache/scripts']
self.path_module='admin_app.webservers'
self.links='<p><a href="{}">{}</a> &gt;&gt; <a href="{}">{}</a> &gt;&gt; <a href="{}">{}</a>'.format(make_url('webservers/servers'), I18n.lang('webservers', 'webservers_list', 'Webservers list'), make_url('webservers/virtualhost/'+str(self.webserver_id)), I18n.lang('webservers', 'websites', 'Websites'), make_url('webapps/'+str(self.data['virtualhost_id'])), I18n.lang('webservers', 'webapps', 'Webapps'))
self.arr_form=OrderedDict()
self.arr_form['webapp_id']=coreforms.HiddenForm('webapp_id', '')
self.arr_form['webapp_id'].required=True
self.arr_form['webapp']=coreforms.BaseForm('webapp', '')
self.arr_form['webapp'].required=True
self.arr_form['webapp'].label=I18n.lang('webapps', 'put_the_webapp_name_for_delete', 'Fill with the webapp name for delete')
self.delete_task=True
self.code_app=''
def form(self, t, yes_error=False, pass_values=False, values={}):
#Here load the form for it task
webapp=request.args.get('webapp', '')
#webapp_name=scripts_lists[webapp][0]
webapp_id=request.args.get('webapp_id', '0')
with self.connection.query('select * from webapp where id=%s', [webapp_id]) as cursor:
arr_webapp=cursor.fetchone()
webapp_name=arr_webapp['app_name']
self.arr_form['webapp_id'].default_value=webapp_id
return '<h2>'+I18n.lang('webservers', 'delete_webapp', 'Delete webapp')+' '+webapp_name+'</h2>'+show_form(values, self.arr_form, t, yes_error, pass_values)
def check_form(self, post):
self.data['webapp_id']=post.get('webapp_id', '0')
self.data['webapp']=post.get('webapp', '')
if self.data['webapp_id']=='':
return False
with self.connection.query('select webapp.*, virtualhost.* from webapp, virtualhost WHERE webapp.virtualhost_id=virtualhost.id AND webapp.id=%s', [self.data['webapp_id']]) as cursor:
arr_webapp=cursor.fetchone()
if not arr_webapp:
return False
if arr_webapp['app_name']!=self.data['webapp']:
self.arr_form['webapp'].error=True
self.arr_form['webapp'].txt_error='Invalid webapp name.'
return False
return True
def pre_task(self):
#self.commands_to_execute.append(['modules/webservers/scripts/install_mariadb.py', '--password=%s' % self.extra_data['mysql_password']])
if not super().pre_task():
return False
#ython3 delete_wordpress.py --user=developer --home_user /home/developer/sites/prueba.cuchulu.com/htdocs/ --mysql_host=localhost --mysql_db=wp_db
#if self.data['webapp']=='wordpress':
#elect webapp.*, virtualhost.* from webapp, virtualhost WHERE webapp.virtualhost_id=virtualhost.id AND webapp.id=43
with self.connection.query('select webapp.*, virtualhost.* from webapp, virtualhost WHERE webapp.virtualhost_id=virtualhost.id AND webapp.id=%s', [self.data['webapp_id']]) as cursor:
arr_webapp=cursor.fetchone()
if not arr_webapp:
self.logtask.insert({'status':1, 'progress': 100, 'error': 1, 'task_id': self.id, 'server': self.server, 'message': 'Error: webapp not exists'})
return False
self.code_app=arr_webapp['code']
self.files.append(['modules/apache/scripts/webapps/delete_'+arr_webapp['app_type']+'.py', 0o700])
self.files.append(['modules/apache/scripts/webapps/delete_app_apache.py', 0o700])
home_user=arr_webapp['home']+'/htdocs/'
if arr_webapp['path']!='/':
home_user=arr_webapp['home']+arr_webapp['path']
db_opts=''
virtualhost_id=arr_webapp['virtualhost_id']
self.data['virtualhost_id']=virtualhost_id
data=json.loads(arr_webapp['data'])
if 'mysql_host' in data:
db_opts='--mysql_host=%s --mysql_db=%s' % (data['mysql_host'], data['mysql_db'])
#python3 delete_app_apache.py --domain prueba.cuchulu.com --webapp wordpress
self.commands_to_execute.append(['modules/apache/scripts/webapps/delete_'+arr_webapp['app_type']+'.py', '--user=%s --home_user=%s %s' % (arr_webapp['username'], home_user, db_opts)])
self.commands_to_execute.append(['modules/apache/scripts/webapps/delete_app_apache.py', '--domain={} --webapp={}'.format(arr_webapp['domain'], arr_webapp['app_name'])])
return True
def post_task(self):
#virtualhost=VirtualHost(self.connection)
#virtualhost.safe_query()
#if not virtualhost.insert({'virtualhost_id': int(self.data['virtualhost_id']), 'app_name': 'wordpress', 'path': self.data['path']}):
# return False
#print(virtualhost.show_errors())
#virtua
self.connection.query('delete from webapp where id=%s', [self.data['webapp_id']])
if self.code_app!='':
self.connection.query('delete from updateserverscripts where code=%s', [self.code_app])
return True