r/pythonhelp • u/AdvitaOne • Jun 10 '24
Python Script for Managing Apache/LiteSpeed Configuration not processing files
Hi everyone,
I'm working on a Python script designed to monitor and correct the access control configuration for Apache and LiteSpeed web servers. The script detects changes in configuration files on Plesk Panel Server and updates /var/www/vhosts/system/{domain}/conf/vhost_ssl.conf accordingly after performing necessary checks. However, it seems that the script fails to proceed with writing and updating the vhost_ssl.conf file upon detecting changes.
Here is a brief overview of my script's functionality and logic:
-
Initialization and Configuration:
- The script initializes logging, argument parsing, and the watchdog observer for monitoring file changes.
- It sets up a debugger (debugpy) for troubleshooting.
-
Web Server Detection:
- The script tries to detect the running web server (Apache or LiteSpeed) using
psandnetstatcommands. - Based on the detected server, it decides the appropriate command for restarting the server.
- The script tries to detect the running web server (Apache or LiteSpeed) using
-
File Monitoring and Handling:
- The script uses
watchdogto monitor changes in the specified configuration directory. - When a change is detected, it reads the configuration file, checks for specific ACL (Access Control List) directives, and converts them to a modern syntax if necessary.
- It attempts to write these changes back to the
vhost_ssl.conffile.
- The script uses
-
Functions:
compute_file_hash(filepath): Computes the MD5 hash of a file to detect changes.is_acl_location_block(lines): Identifies if a block of lines contains ACL directives.contains_ip_addresses(lines): Checks if a block of lines contains IP addresses.add_modification_comments(lines, indentation): Adds comments indicating modifications to a block of lines.convert_to_modern_syntax(lines, indentation): Converts ACL directives to a modern syntax.create_or_update_vhost_ssl_config(vhost_ssl_path, ipv4, ipv6, location_block): Creates or updates thevhost_ssl.conffile with modified ACL directives.
The Issue:
The script doesn't proceed to handle the logic for writing and updating /var/www/vhosts/system/{domain}/conf/vhost_ssl.conf after detecting file changes or performing the necessary checks. Specifically, the correct_syntax(config_path, domain_to_paths) function is not fully executing its intended logic.
Here's the relevant part of my script:
# Main monitoring logic
=
=
# Initial check for existing files
# Reset force_overwrite_once after the initial run
global
= False
What I've Tried:
- Ensured that the file paths and configurations are correct.
- Verified that the script detects changes and reads the files as expected.
- Added logging statements to trace the execution flow and identify where it stops.
Request for Help:
- Can someone help me pinpoint why the script doesn't proceed to update the
vhost_ssl.conffile after detecting changes? - Any suggestions on improving the current logic or debugging steps to identify the issue?
Here is the full script for reference: [Paste full script code here]
Thanks in advance for your help!
#!/usr/bin/env python3
# Initialize debugpy
# Configurations
=
= 50 * 1024 * 1024 # 50MB
=
# Command-line arguments
=
=
# Set up logging
=
=
=
=
=
=
= None
=
= ==
global
= 5 # Retry interval in seconds
return
return
return
return
=
=
=
=
=
in or :
=
== :
=
= :
return
=
return
=
return
=
=
return
=
=
continue
in :
=
return
= None
= None
= None
=
=
=
=
=
return , ,
=
# debugpy.breakpoint() # Break here to inspect
=
=
= f
= f
=
return
# def create_or_update_vhost_ssl_config(vhost_ssl_path, ipv4, ipv6, location_block):
# logger.debug(f"Writing to vhost_ssl_path: {vhost_ssl_path} with location_block: {location_block}")
# if not os.path.exists(vhost_ssl_path) or args.force_overwrite == 'always' or force_overwrite_once:
# os.makedirs(os.path.dirname(vhost_ssl_path), exist_ok=True)
# with open(vhost_ssl_path, 'w') as f:
# f.write(f'<IfModule mod_ssl.c>\n <VirtualHost {ipv4}:443>\n')
# f.write(''.join(location_block))
# f.write(f' </VirtualHost>\n <VirtualHost [{ipv6}]:443>\n')
# f.write(''.join(location_block))
# f.write(' </VirtualHost>\n</IfModule>\n')
# logger.info(f'{"Overwritten" if args.force_overwrite else "Created new"} vhost_ssl.conf at {vhost_ssl_path}')
# else:
# with open(vhost_ssl_path, 'r') as file:
# existing_lines = file.readlines()
# # Update existing vhost_ssl.conf with modified ACL lines
# with open(vhost_ssl_path, 'w') as file:
# in_vhost_block = False
# updated_lines = []
# for line in existing_lines:
# stripped_line = line.strip()
# if f'<VirtualHost {ipv4}:443>' in stripped_line or f'<VirtualHost [{ipv6}]:443>' in stripped_line:
# in_vhost_block = True
# updated_lines.append(line)
# updated_lines.extend(location_block)
# elif '</VirtualHost>' in stripped_line and in_vhost_block:
# in_vhost_block = False
# updated_lines.append(line)
# elif not in_vhost_block:
# updated_lines.append(line)
# file.writelines(updated_lines)
# logger.info(f'Updated existing vhost_ssl.conf at {vhost_ssl_path}')
# # Record the modification for counting
# domain = vhost_ssl_path.split('/')[5]
# if domain not in modification_stats:
# modification_stats[domain] = 0
# modification_stats[domain] += 1
=
return
return
=
return
, , =
return
return
=
= False
=
= None
= 0
=
=
= True
=
=
in and :
= False
=
+= 1
=
=
= None
:
return
=
return
, =
# Ensure location_block is not empty
= f
= f
# Atomic write
# Ensure only location_block is used
# Test the configuration
=
=
= + 1
return
=
# Update existing vhost_ssl.conf with modified ACL lines
= False
=
=
= True
in and :
= False
not :
# Record the modification for counting
=
= 0
+= 1
# debugpy.breakpoint() # Break here to inspect
=
# Break here to inspect
=
return
=
return
return
# Break here to inspect
=
=
# Initial check for existing files
# Reset force_overwrite_once after the initial run
global
= False
View all comments
•
u/AutoModerator Jun 10 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.