Skip to content

Commit b2173b6

Browse files
committed
fixes
1 parent 2dca46b commit b2173b6

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

bota/src/bota/vm.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,40 @@ def read_file(path):
603603
def read_conf():
604604
return read_file("/etc/apache2/sites-available/000-default.conf")
605605

606+
import re
607+
def clean_conf(apache_conf, root_path):
608+
apache_conf = re.sub(r'ProxyPass\s+' + re.escape(root_path) + r'\s+.*\n', '', apache_conf)
609+
apache_conf = re.sub(r'ProxyPassReverse\s+' + re.escape(root_path) + r'\s+.*\n', '', apache_conf)
610+
# return apache_conf
611+
return re.sub(r'[ \t]+</VirtualHost>', '</VirtualHost>', apache_conf)
612+
613+
def sub_at_start(apache_conf, root_path, api_target):
614+
return re.sub(
615+
r'(<VirtualHost\b.*?>)',
616+
f'\\1\n ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}',
617+
apache_conf,
618+
count=1
619+
)
606620

607-
def make_apache_content():
608-
return read_file("/etc/apache2/sites-available/000-default.conf")
621+
622+
def sub_at_end(apache_conf, root_path, api_target):
623+
return re.sub(r'</VirtualHost>', f' ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}\n</VirtualHost>', apache_conf)
624+
625+
def make_apache_content(apache_conf, root_path, api_target):
626+
# Remove existing ProxyPass and ProxyPassReverse directives for the root_path
627+
apache_conf = clean_conf(apache_conf, root_path)
628+
629+
# Add new ProxyPass and ProxyPassReverse directives based on the root_path
630+
if root_path == "/":
631+
return remove_empty_lines(sub_at_end(apache_conf, root_path, api_target))
632+
else:
633+
# Add new directives right after the VirtualHost opening tag
634+
return remove_empty_lines(sub_at_start(apache_conf, root_path, api_target))
609635

610636
def setup_apache_load_balancer_desktop_app(port, api_base_path):
611637
root_path = api_base_path or '/'
612-
apache_conf = f"""<VirtualHost *:80>
613-
ServerAdmin webmaster@localhost
614-
DocumentRoot /var/www/html
615-
ErrorLog ${{APACHE_LOG_DIR}}/error.log
616-
CustomLog ${{APACHE_LOG_DIR}}/access.log combined
617-
ProxyPass {root_path} http://127.0.0.1:{port}{root_path}
618-
ProxyPassReverse {root_path} http://127.0.0.1:{port}{root_path}
619-
</VirtualHost>"""
638+
api_target = f"http://127.0.0.1:{port}{root_path}"
639+
apache_conf = make_apache_content(read_conf(), root_path, api_target)
620640
write_file_sudo(apache_conf, "/etc/apache2/sites-available/000-default.conf")
621641

622642
def validate_url(url):

0 commit comments

Comments
 (0)