Added delete flask webapp

This commit is contained in:
absurdo 2023-11-29 23:53:11 +01:00
parent 4f7d88f995
commit 90990ab1c0
10 changed files with 969 additions and 2 deletions

View file

@ -48,10 +48,26 @@ if os.path.isfile(apache_php):
apache_webapp='/etc/{}/vhosts.d/extra/{}-{}.conf'.format(apache_cmd, args.domain, args.webapp)
apache_sub0_webapp='/etc/{}/vhosts.d/extra/{}-000-{}.conf'.format(apache_cmd, args.domain, args.webapp)
apache_sub1_webapp='/etc/{}/vhosts.d/extra/{}-001-{}.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 os.path.isfile(apache_sub0_webapp):
if subprocess.call("sudo rm %s" % (apache_sub0_webapp), shell=True) > 0:
print('Error: cannot delete webapp config %s' % apache_sub0_webapp)
sys.exit(1)
if os.path.isfile(apache_sub1_webapp):
if subprocess.call("sudo rm %s" % (apache_sub1_webapp), shell=True) > 0:
print('Error: cannot delete webapp config %s' % apache_sub1_webapp)
sys.exit(1)
if subprocess.call("sudo systemctl restart %s" % (apache_cmd), shell=True) > 0:
print('Error: cannot restart Apache server')

View file

@ -0,0 +1,49 @@
#!/usr/bin/python3 -u
import argparse
import os
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_flask.py', description='A tool for delete flask')
parser.add_argument('--home_user', help='Home where flask resides', required=True)
parser.add_argument('--user', help='The user', required=True)
print('Deleting Flask App..')
args=parser.parse_args()
home_user=args.home_user
if home_user=='/':
print('What are you doing?')
sys.exit(1)
name_service=os.path.basename(home_user[:-1]).strip()
#d=os.path.basename(p[:-1].replace(f, '')[:-1])
domain=os.path.basename(home_user[:-1].replace(name_service, '')[:-1]).strip()
#print(domain)
if subprocess.call('sudo systemctl stop %s-%s.service && sudo rm /etc/systemd/system/%s-%s.service' % (domain, name_service, domain, name_service), shell=True) > 0:
print('Error: cannot delete app service in systemd...')
sys.exit(1)
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('Deleted Flask app successfully...')

View file

@ -0,0 +1,188 @@
#!/usr/bin/python3 -u
import sys, os
import subprocess
import argparse
import platform
import shutil
import pathlib
import distro
import pwd
import getpass
import re
#import pymysql.cursors
#pymysql.install_as_MySQLdb
pyv=platform.python_version_tuple()
if pyv[0]!='3':
print('Need python 3 for execute this script')
sys.exit(1)
parser = argparse.ArgumentParser(description='Script for create a flask site.')
parser.add_argument('--domain', help='The domain where is the site', required=True)
parser.add_argument('--home_user', help='The name of the new user', required=True)
parser.add_argument('--git_url', help='Url for download the last tag of app, can be used for update app', required=True)
parser.add_argument('--user', help='The name of the domain user', required=True)
parser.add_argument('--dependencies', help='The dependencies of the flask app separated by commas', required=True)
parser.add_argument('--app_flask', help='The string type app:app for load in gunicorn', required=True)
#parser.add_argument('--port', help='The port of the gunicorn application', required=True)
parser.add_argument('--path', help='The path of the flask install')
args = parser.parse_args()
linux_distro=distro.id()
home_user=args.home_user
if home_user.find('/', -1)==-1:
print("Error: you need backslash in home_user option")
exit(1)
user=args.user
apache_cmd='apache2'
apachectl='apache2ctl'
if linux_distro!='debian' and linux_distro!='ubuntu':
apache_cmd='httpd'
apachectl='apachectl'
if linux_distro=='rocky' or linux_distro=='fedora':
apache_group='apache'
if linux_distro=='arch':
apache_group='http'
system_user=getpass.getuser()
if os.path.isdir(home_user):
print("Cleaning %s directory" % home_user)
if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (user, home_user), shell=True) > 0:
print('Error: cannot clean directory {}'.format(home_user))
sys.exit(1)
"""
if linux_distro=='debian' or linux_distro=='ubuntu':
#libapache2-mod-wsgi-py3/
if subprocess.call("sudo apt-get update && sudo apt-get upgrade && sudo apt-get install libapache2-mod-wsgi-py3", shell=True) > 0:
print('Error: cannot install mod_wsgi for apache')
sys.exit(1)
"""
print("Creating virtualenv for the flask app...")
if subprocess.call("sudo su %s -s /bin/bash -c 'mkdir -p %s && python3 -m venv %s/venv'" % (user, home_user, home_user), shell=True) > 0:
print('Error: cannot install python virtualenv in {}'.format(home_user))
sys.exit(1)
dependencies=args.dependencies
if subprocess.call("sudo su %s -s /bin/bash -c '%s/venv/bin/pip install %s flask gunicorn'" % (user, home_user, dependencies.replace(',', ' ')), shell=True) > 0:
print('Error: cannot install python virtualenv dependencies in {}'.format(home_user))
sys.exit(1)
num_proc=str(os.cpu_count()*2+1)
name=os.path.basename(home_user[:-1]).strip()
#port=int(args.port)
if not re.match('\w+:\w+', args.app_flask):
print('Error: define the app_flask with app:app format')
sys.exit(1)
path='/'
if args.path:
if args.path.find('/', -1)==-1:
print("Error: you need backslash in path option")
exit(1)
path=args.path[:-1]
if path=='':
path='/'
print("Creating the service for flask app with gunicorn...")
with open('./%s-%s.service' % (args.domain, name), 'w') as f:
f.write("# Save it in /etc/systemd/system/%s-%s.service\n" % (args.domain, name))
f.write("[Unit]\n")
f.write("Description=App %s\n" % name)
f.write("After=syslog.target\n")
f.write("After=network.target\n")
f.write("[Service]\n")
f.write("Type=simple\n")
f.write("User=%s\n" % user)
f.write("Group=%s\n" % user)
f.write("WorkingDirectory=%s/app\n" % home_user)
f.write("ExecStart=%svenv/bin/gunicorn -w%s -b unix:%sgunicorn.sock --timeout 300 --error-logfile %s%s-error.log --access-logfile %s%s-access.log --reload %s\n" % (home_user, num_proc, home_user, home_user, name, home_user, name, args.app_flask))
f.write("Restart=always\n")
f.write("Environment=USER=%s HOME=/home/%s\n" % (user, user))
f.write("[Install]\n")
f.write("WantedBy=multi-user.target\n")
print("Downloading application using git...")
if subprocess.call('sudo cp ./%s-%s.service /etc/systemd/system' % (args.domain, name), shell=True) > 0:
print('Error: cannot install app service in systemd...')
sys.exit(1)
if subprocess.call("sudo su %s -s /bin/bash -c 'git clone %s %sapp && cd %sapp && export GTAG=$(git describe --tags `git rev-list --tags --max-count=1`) && git checkout $GTAG -b latest-$GTAG'" % (user, args.git_url, home_user, home_user), shell=True) > 0:
print('Error: cannot download git packages in {}app'.format(home_user))
sys.exit(1)
if path=='/':
base_name_file='{}-001-{}.conf'.format(args.domain, name)
else:
base_name_file='{}-000-{}.conf'.format(args.domain, name)
name_file='/home/{}/{}'.format(system_user, base_name_file)
with open(name_file, 'w') as f:
if path=='/':
f.write("<Location %s.well-known/acme-challenge/>\n" % path)
f.write("ProxyPass !\n")
f.write("</Location>\n")
f.write("<Location %s>\n" % path)
#ProxyPass unix:/home/root/flask_rest/flaskrest.sock|http://127.0.0.1/
f.write("ProxyPass unix:%sgunicorn.sock|http://127.0.0.1%s\n" % (home_user, path))
f.write("ProxyPassReverse unix:%sgunicorn.sock|http://127.0.0.1%s\n" % (home_user, path))
f.write("</Location>\n")
print('Updating apache configuration for wordpress outside of htdocs...')
if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0:
print('Error')
sys.exit(1)
print("Preparing apache for use gunicorn...")
if linux_distro=='debian' or linux_distro=='ubuntu':
if subprocess.call('sudo a2enmod proxy_http', shell=True) > 0:
print("Error: cannot enable proxy_http in apache in debian/ubuntu distro")
sys.exit(1)
service=args.domain+'-'+name
if subprocess.call("sudo systemctl enable %s && sudo systemctl start %s && sudo systemctl restart %s" % (service, service, apache_cmd), shell=True) > 0:
print("Error: cannot update and restart apache")
sys.exit(1)
print("Flask app install successfully!")

View file

@ -0,0 +1,42 @@
#!/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_phpmyadmin.py', description='A tool for delete phpmyadmin')
parser.add_argument('--home_user', help='Home where phpmyadmin resides', required=True)
parser.add_argument('--user', help='The user', required=True)
print('Deleting PHPMyAdmin..')
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('Deleted PHPMyAdmin successfully...')

View file

@ -0,0 +1,205 @@
#!/usr/bin/python3 -u
import sys, os
import subprocess
import argparse
import platform
import shutil
import pathlib
import distro
import pwd
import getpass
#import pymysql.cursors
#pymysql.install_as_MySQLdb
pyv=platform.python_version_tuple()
if pyv[0]!='3':
print('Need python 3 for execute this script')
sys.exit(1)
parser = argparse.ArgumentParser(description='Script for create a new wordpress site.')
parser.add_argument('--domain', help='The domain where is the site', required=True)
parser.add_argument('--home_user', help='The name of the new user', required=True)
parser.add_argument('--user', help='The name of the new user', required=True)
parser.add_argument('--path', help='The path of the wordpress install')
parser.add_argument('--server_mysql', help='Server of MySQL database', required=True)
parser.add_argument('--port_mysql', help='The port of the MySQL server', required=True, type=int)
parser.add_argument('--php_version', help='PHP version', required=True)
args = parser.parse_args()
php_version=args.php_version
php_command='/usr/bin/php'+str(php_version)
linux_distro=distro.id()
if linux_distro=='arch':
php_command='/usr/bin/php-legacy'
system_user=getpass.getuser()
apache_cmd='apache2'
apachectl='apache2ctl'
if linux_distro!='debian' and linux_distro!='ubuntu':
apache_cmd='httpd'
apachectl='apachectl'
if linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux' or linux_distro=='centos':
apache_group='apache'
php_command=php_command.replace('.', '')
if linux_distro=='arch':
apache_group='http'
home_user=args.home_user
if home_user.find('/', -1)==-1:
print("Error: you need backslash in home_user option")
exit(1)
home_user_default=home_user.replace('/htdocs/', '')
stat_group=os.stat(home_user)
uid=stat_group.st_uid
user=pwd.getpwuid(uid)[0]
if args.path and args.path!='/':
home_user=args.home_user.replace('/htdocs/', args.path)
"""
if not os.path.isdir(home_user):
#os.mkdir(home_user)
if subprocess.call("sudo su %s -s /bin/bash -c 'mkdir -p %s'" % (user, home_user), shell=True) > 0:
print("Error, cannot create phpmyadmin folder sudo su %s -s /bin/bash -c 'mkdir -p %s'" % (args.user, home_user)+"\n")
sys.exit(1)
else:
print("Error, exists a directory with same name, i cannot install phpmyadmin in this folder")
sys.exit(1)
"""
#os.chdir(home_user)
else:
home_user=args.home_user
phpmyadmin_dir=args.home_user.replace('/htdocs/', '/phpmyadmin/')
if os.path.isdir(phpmyadmin_dir):
print('Cleaning directory %s because is not empty, probably by an error in other install' % phpmyadmin_dir)
if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (user, phpmyadmin_dir), shell=True) > 0:
print('Error: cannot clean to {}\n'.format(phpmyadmin_dir))
sys.exit(1)
#os.chdir(home_user)
#print(home_user)
#sys.exit(0)
# Php-fpm for user
#if not os.path.isfile('/etc/')
#shutil.chown('/var/www/sites/'+args.domain, 'hosting', 'hosting')
#Dash, the default debian stretch shell, don't support <<<
#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password password your_password'
#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password_again password your_password'
print('Installing phpmyadmin...')
# composer create-project phpmyadmin/phpmyadmin --repository-url=https://www.phpmyadmin.net/packages.json --no-dev --working-dir={}
ret_install=subprocess.call("sudo su %s -s /bin/bash -c '%s /usr/local/bin/composer create-project phpmyadmin/phpmyadmin --repository-url=https://www.phpmyadmin.net/packages.json --no-dev --working-dir=%s'" % (user, php_command, home_user_default), shell=True)
if ret_install > 0 and ret_install!=5:
print('Error: cannot install phpmyadmin with composer in {} with code {}\n'.format(home_user, ret_install))
sys.exit(1)
if ret_install==5:
print('Updating phpmyadmin using composer...\n')
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && %s /usr/local/bin/composer update'" % (user,home_user_default+'/phpmyadmin/', php_command), shell=True) > 0:
print('Error: cannot update phpmyadmin with composer in {}\n'.format(home_user))
sys.exit(1)
if home_user.find('/htdocs/', -8)!=-1:
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && mv htdocs htdocs_old && mv phpmyadmin htdocs'" % (user, home_user_default), shell=True) > 0:
print('Error: cannot move phpmyadmin to {}\n'.format(home_user))
sys.exit(1)
pass
else:
if args.path!='/phpmyadmin/':
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && mv phpmyadmin %s'" % (user, home_user_default, home_user), shell=True) > 0:
print('Error: cannot move phpmyadmin to {}\n'.format(home_user))
sys.exit(1)
pass
if args.server_mysql:
#echo "\$cfg['Servers'][\$i]['host'] = '$MYSQL_HOST';" >> config.inc.php
# $cfg['Servers'][$i]['host'] = '%s';
config_servers='';
if subprocess.call("sudo su {} -s /bin/bash -c 'cp {}/config.sample.inc.php {}/config.inc.php && echo \"\$cfg[\\\"Servers\\\"][\$i][\\\"host\\\"] = \\\"{}:{}\\\";\" >> {}/config.inc.php'".format(user, home_user, home_user, args.server_mysql, args.port_mysql, home_user), shell=True) > 0:
print('Error: cannot update config of phpmyadmin {}\n'.format(home_user))
sys.exit(1)
"""
if subprocess.call("sudo mv %s %s & rmdir %s" % (home_user+'/phpmyadmin/*', home_user), shell=True) > 0:
print('Error: cannot move phpmyadmin to {}'.format(home_user))
sys.exit(1)
"""
if args.path!='/':
alias_apache=" Alias {} {} \n\
<Directory {}> \n\
Options FollowSymLinks MultiViews \n\
AllowOverride All \n\
Require all granted \n\
</Directory> \n\
<Location {}>\n\
ProxyPass ! \n\
</Location> \n\
".format(args.path[:-1], home_user, home_user, args.path[:-1])
base_name_file='{}-{}.conf'.format(args.domain, os.path.basename(args.path[:1]))
name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, os.path.basename(args.path[:1]))
with open(name_file, 'w') as f:
f.write(alias_apache)
print('Updating apache configuration for wordpress outside of htdocs...\n')
if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0:
print('Error')
sys.exit(1)
print('Restarting apache...')
if subprocess.call('sudo '+apachectl+' configtest && sudo systemctl restart '+apache_cmd, shell=True) > 0:
subprocess.call('sudo rm /etc/{}/vhosts.d/extra/{}'.format(apache_cmd, base_name_file), shell=True)
print('Error: Error in configtest\n')
sys.exit(1)
print('phpmyadmin installed!')

View file

@ -0,0 +1,53 @@
#!/opt/pythonenv/bin/python3 -u
import sys, os
import subprocess
import argparse
import platform
import shutil
import pathlib
import distro
import pwd
import getpass
import json
#import pymysql.cursors
#pymysql.install_as_MySQLdb
pyv=platform.python_version_tuple()
if pyv[0]!='3':
print('Need python 3 for execute this script')
sys.exit(1)
parser = argparse.ArgumentParser(description='Script for update phpmyadmin site.')
parser.add_argument('--home_user', help='The path where phpmyadmin resides', required=True)
parser.add_argument('--user', help='The name of the new user', required=True)
args = parser.parse_args()
home_user=args.home_user
user=args.user
json_return={}
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && composer update'" % (user, home_user), shell=True) > 0:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot update phpmyadmin'
print(json.dumps(json_return))
exit(1)
else:
json_return['error']=0
json_return['status']=0
json_return['progress']=100
json_return['message']='PHPMyAdmin updated...'
print(json.dumps(json_return))