Skip to content

Commit 237bfa8

Browse files
committed
fixes
1 parent fd43f3f commit 237bfa8

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

bota/src/bota/apache_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import re
3+
def clean_conf(apache_conf, root_path):
4+
apache_conf = re.sub(r'ProxyPass\s+' + re.escape(root_path) + r'\s+.*\n', '', apache_conf)
5+
apache_conf = re.sub(r'ProxyPassReverse\s+' + re.escape(root_path) + r'\s+.*\n', '', apache_conf)
6+
# return apache_conf
7+
return re.sub(r'[ \t]+</VirtualHost>', '</VirtualHost>', apache_conf)
8+
9+
def collapse_empty_lines(conf: str) -> str:
10+
# Replace multiple consecutive newlines (with optional whitespace) with a single newline
11+
return re.sub(r'\n\s*\n+', '\n\n', conf.strip()) + '\n'
12+
13+
def sub_at_start(apache_conf, root_path, api_target):
14+
return re.sub(
15+
r'(<VirtualHost\b.*?>)',
16+
f'\\1\n ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}',
17+
apache_conf,
18+
count=1
19+
)
20+
21+
def sub_at_end(apache_conf, root_path, api_target):
22+
return re.sub(r'</VirtualHost>', f' ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}\n</VirtualHost>', apache_conf)
23+
24+
def make_apache_content(apache_conf, root_path, api_target):
25+
# Remove existing ProxyPass and ProxyPassReverse directives for the root_path
26+
apache_conf = clean_conf(apache_conf, root_path)
27+
28+
# Add new ProxyPass and ProxyPassReverse directives based on the root_path
29+
if root_path == "/":
30+
return collapse_empty_lines(sub_at_end(apache_conf, root_path, api_target))
31+
else:
32+
# Add new directives right after the VirtualHost opening tag
33+
return collapse_empty_lines(sub_at_start(apache_conf, root_path, api_target))

bota/src/bota/vm.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import requests
66
from requests.exceptions import ReadTimeout
77
import traceback
8+
9+
from .apache_utils import make_apache_content
810
def find_ip(attempts=5, proxy=None) -> str:
911
"""Finds the public IP address of the current connection."""
1012
url = 'https://checkip.amazonaws.com/'
@@ -603,40 +605,6 @@ def read_file(path):
603605
def read_conf():
604606
return read_file("/etc/apache2/sites-available/000-default.conf")
605607

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 collapse_empty_lines(conf: str) -> str:
614-
# Replace multiple consecutive newlines (with optional whitespace) with a single newline
615-
return re.sub(r'\n\s*\n+', '\n\n', conf.strip()) + '\n'
616-
617-
def sub_at_start(apache_conf, root_path, api_target):
618-
return re.sub(
619-
r'(<VirtualHost\b.*?>)',
620-
f'\\1\n ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}',
621-
apache_conf,
622-
count=1
623-
)
624-
625-
626-
def sub_at_end(apache_conf, root_path, api_target):
627-
return re.sub(r'</VirtualHost>', f' ProxyPass {root_path} {api_target}\n ProxyPassReverse {root_path} {api_target}\n</VirtualHost>', apache_conf)
628-
629-
def make_apache_content(apache_conf, root_path, api_target):
630-
# Remove existing ProxyPass and ProxyPassReverse directives for the root_path
631-
apache_conf = clean_conf(apache_conf, root_path)
632-
633-
# Add new ProxyPass and ProxyPassReverse directives based on the root_path
634-
if root_path == "/":
635-
return collapse_empty_lines(sub_at_end(apache_conf, root_path, api_target))
636-
else:
637-
# Add new directives right after the VirtualHost opening tag
638-
return collapse_empty_lines(sub_at_start(apache_conf, root_path, api_target))
639-
640608
def setup_apache_load_balancer_desktop_app(port, api_base_path):
641609
root_path = api_base_path or '/'
642610
api_target = f"http://127.0.0.1:{port}{root_path}"

0 commit comments

Comments
 (0)