diff --git a/scripts/webapps/delete_app_apache.py b/scripts/webapps/delete_app_apache.py new file mode 100644 index 0000000..31be715 --- /dev/null +++ b/scripts/webapps/delete_app_apache.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 -u + +import argparse +import os +#from pastafariutils.unix import add_user, del_user +from pathlib import Path +from subprocess import call, DEVNULL +import json +import time +import shutil +import pwd +import distro +import subprocess +import re +import sys + + +parser=argparse.ArgumentParser(prog='delete_wordpress.py', description='A tool for delete wordpress') + +parser.add_argument('--domain', help='Domain to delete', required=True) +parser.add_argument('--webapp', help='Webapp config to delete', required=True) + +args=parser.parse_args() + +apache_cmd='apache2' + +apachectl='apache2ctl' + +linux_distro=distro.id() + +if linux_distro!='debian' and linux_distro!='ubuntu': + apache_cmd='httpd' + apachectl='apachectl' + +print('Deleting the apache configuration of webapp..') + +# /etc/httpd/vhosts.d/php/prueba.cuchulu.com-php.conf +""" +apache_php='/etc/{}/vhosts.d/php/{}-php.conf'.format(apache_cmd, args.domain) + +if os.path.isfile(apache_php): + if subprocess.call("sudo rm %s" % (apache_php), shell=True) > 0: + print('Error: cannot delete php config %s' % apache_php) + sys.exit(1) +""" + +# /etc/httpd/vhosts.d/extra/prueba.cuchulu.com-wordpress.conf + +apache_webapp='/etc/{}/vhosts.d/extra/{}-{}.conf'.format(apache_cmd, args.domain, args.webapp) + +if os.path.isfile(apache_webapp): + if subprocess.call("sudo rm %s" % (apache_webapp), shell=True) > 0: + print('Error: cannot delete webapp config %s' % apache_webapp) + sys.exit(1) + +if subprocess.call("sudo systemctl restart %s" % (apache_cmd), shell=True) > 0: + print('Error: cannot restart Apache server') + sys.exit(1) + +print('Deleted the apache configuration of webapp successfully..') diff --git a/scripts/webapps/delete_wordpress.py b/scripts/webapps/delete_wordpress.py new file mode 100644 index 0000000..4c6adc3 --- /dev/null +++ b/scripts/webapps/delete_wordpress.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 -u + +import argparse +import os +#from pastafariutils.unix import add_user, del_user +from pathlib import Path +from subprocess import call, DEVNULL +import json +import time +import shutil +import pwd +import distro +import subprocess +import re +import sys + + +parser=argparse.ArgumentParser(prog='delete_wordpress.py', description='A tool for delete wordpress') + +parser.add_argument('--home_user', help='Home where wordpress resides', required=True) +parser.add_argument('--user', help='The user', required=True) +parser.add_argument('--mysql_host', help='The host of mysql', required=True) +parser.add_argument('--mysql_db', help='The db to clean', required=True) + +print('Deleting Wordpress..') + +args=parser.parse_args() + +home_user=args.home_user + +if home_user.find('/htdocs/', -8)!=-1: + + if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s && mkdir %s'" % (args.user, args.home_user, args.home_user), shell=True) > 0: + print('Error: cannot delete %s' % args.home_user) + sys.exit(1) + +else: + + if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (args.user, args.home_user), shell=True) > 0: + print('Error: cannot delete %s' % args.home_user) + sys.exit(1) + +print('Deleting Database if in localhost..') + +if args.mysql_host=='127.0.0.1' or args.mysql_host=='localhost': + + if subprocess.call("sudo mysql --execute=\"drop database %s\"" % (args.mysql_db), shell=True) > 0: + print('Error: cannot delete database %s' % args.mysql_db) + sys.exit(1) + +else: + + print('I cannot delete db because are not in the same server, please, delete manually the db.') + +print('Deleted Wordpress successfully..') diff --git a/tasks/apache/scripts/delete_app.py b/tasks/apache/scripts/delete_app.py new file mode 100644 index 0000000..f4f21bb --- /dev/null +++ b/tasks/apache/scripts/delete_app.py @@ -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='

{} >> {} >> {}'.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 '

'+I18n.lang('webservers', 'delete_webapp', 'Delete webapp')+' '+webapp_name+'

'+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 diff --git a/templates/admin/webapps.phtml b/templates/admin/webapps.phtml index 5ac3c66..4562498 100644 --- a/templates/admin/webapps.phtml +++ b/templates/admin/webapps.phtml @@ -31,7 +31,7 @@ ids=json.dumps([server_id]) - +