#!/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('--url', help='Url for the http service to proxy', required=True)
parser.add_argument('--user', help='The name of the domain user', 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' or linux_distro=='almalinux':
apache_group='apache'
if linux_distro=='arch':
apache_group='http'
system_user=getpass.getuser()
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='/'
if path=='/':
name='proxy'
base_name_file='{}-001-{}.conf'.format(args.domain, name)
name_file='/home/{}/{}-001-{}.conf'.format(system_user, args.domain, name)
else:
name=os.path.basename(home_user[:-1]).strip()
base_name_file='{}-000-{}.conf'.format(args.domain, name)
name_file='/home/{}/{}-000-{}.conf'.format(system_user, args.domain, name)
with open(name_file, 'w') as f:
f.write("ProxyPreserveHost On\n")
f.write("ProxyRequests Off\n")
f.write("AllowEncodedSlashes NoDecode\n")
f.write("\n" % path)
#ProxyPass unix:/home/root/flask_rest/flaskrest.sock|http://127.0.0.1/
f.write("ProxyPass {} nocanon\n".format(args.url))
#f.write("ProxyPassReverse {}\n" .format(args.url))
if path!='/':
f.write("RequestHeader set SCRIPT_NAME %s\n" % path)
f.write("RequestHeader set X-Forwarded-Proto http\n")
f.write("RequestHeader set X-Forwarded-Prefix %s\n" % path)
f.write("\n")
if path=='/':
f.write("\n")
f.write("ProxyPass !\n")
f.write("\n")
"""
if path=='/':
f.write("ProxyPreserveHost On\n")
f.write("ProxyRequests off\n")
f.write("AllowEncodedSlashes NoDecode\n")
f.write("ProxyPass %s.well-known/acme-challenge/ !\n" % path)
f.write("ProxyPass / {}/ nocanon\n".format(args.url))
else:
f.write("AllowEncodedSlashes NoDecode\n")
f.write("ProxyPass {} {} nocanon\n".format(path, args.url))
"""
print('Updating apache configuration for application 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 proxy http...")
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 restart %s" % (apache_cmd), shell=True) > 0:
print("Error: cannot update and restart apache")
sys.exit(1)
print("Proxy app install successfully!")