FIX: #0 Make Nginx config modifier robust using brace matching
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 55s

This commit is contained in:
vickytechkey 2026-08-19 22:06:29 +05:30
parent 846240a5e7
commit faf205b004

View file

@ -13,43 +13,36 @@ try:
except Exception as e:
print(f"Symlink creation message: {e}")
def remove_location_block(content, path):
search_str = f"location {path}"
start_idx = content.find(search_str)
if start_idx == -1:
return content
brace_start = content.find("{", start_idx)
if brace_start == -1:
return content
count = 1
i = brace_start + 1
while i < len(content) and count > 0:
if content[i] == '{':
count += 1
elif content[i] == '}':
count -= 1
i += 1
return content[:start_idx] + content[i:]
filepath = '/etc/nginx/sites-available/default'
if os.path.exists(filepath):
content = open(filepath).read()
# Remove old alias block if it exists
old_alias_block = r''' location ^~ /dolibarrbeta {
alias /home/ubuntu/dolibarrbeta/htdocs;
index index.php;
try_files $uri $uri/ /dolibarrbeta/index.php?$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
}'''
content = content.replace(old_alias_block, '')
# Remove old root-based block without timeout if it exists
old_root_block = r"""location /dolibarrbeta {
root /var/www/html;
index index.php;
try_files $uri $uri/ /dolibarrbeta/index.php?$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
}
"""
content = content.replace(old_root_block, '')
# Strip any existing /dolibarrbeta block
content = remove_location_block(content, '/dolibarrbeta')
# Insert the new correct root-based location block with increased timeout
if 'location /dolibarrbeta' not in content:
idx = content.find('location / {')
if idx != -1:
block = r"""location /dolibarrbeta {
@ -71,7 +64,5 @@ if os.path.exists(filepath):
print("Nginx config generated at /tmp/default")
else:
print("Error: Could not find 'location / {' in Nginx config")
else:
print("Dolibarrbeta location block already configured in Nginx")
else:
print(f"Error: {filepath} not found")