wip-python -> Python rewriting + better logs + backup + update at startup #3
@@ -61,35 +61,53 @@ def read_manual_ips():
 | 
				
			|||||||
        return []
 | 
					        return []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_yaml_with_ips(ips):
 | 
					def update_yaml_with_ips(ips):
 | 
				
			||||||
    # Format IPs for YAML list (4 spaces indent + dash)
 | 
					    # Read original lines
 | 
				
			||||||
    formatted_ips = [f"    - {ip}" for ip in ips]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    inside_disallowed = False
 | 
					 | 
				
			||||||
    output_lines = []
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    with ADGUARD_YAML.open() as f:
 | 
					    with ADGUARD_YAML.open() as f:
 | 
				
			||||||
        for line in f:
 | 
					        lines = f.readlines()
 | 
				
			||||||
            if line.strip().startswith("disallowed_clients:"):
 | 
					
 | 
				
			||||||
                # Replace existing disallowed_clients block
 | 
					    output_lines = []
 | 
				
			||||||
                output_lines.append("disallowed_clients:")
 | 
					    inside_disallowed = False
 | 
				
			||||||
                output_lines.extend(formatted_ips)
 | 
					    disallowed_indent = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    formatted_ips = []  # We'll prepare after knowing indent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for i, line in enumerate(lines):
 | 
				
			||||||
 | 
					        stripped = line.lstrip()
 | 
				
			||||||
 | 
					        indent = line[:len(line) - len(stripped)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if stripped.startswith("disallowed_clients:"):
 | 
				
			||||||
 | 
					            disallowed_indent = indent
 | 
				
			||||||
 | 
					            # Write the disallowed_clients line with original indent
 | 
				
			||||||
 | 
					            output_lines.append(line.rstrip("\n"))
 | 
				
			||||||
            inside_disallowed = True
 | 
					            inside_disallowed = True
 | 
				
			||||||
            elif inside_disallowed:
 | 
					
 | 
				
			||||||
                # Skip old lines under disallowed_clients block
 | 
					            # Prepare formatted IPs with indentation plus 2 spaces (YAML block indent)
 | 
				
			||||||
                if line.startswith("  ") and not line.startswith("    -"):
 | 
					            formatted_ips = [f"{disallowed_indent}  - {ip}" for ip in ips]
 | 
				
			||||||
                    # New section, disallowed_clients block ended
 | 
					
 | 
				
			||||||
 | 
					            # Immediately add IP list here (replace old block)
 | 
				
			||||||
 | 
					            output_lines.extend(formatted_ips)
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if inside_disallowed:
 | 
				
			||||||
 | 
					            # Detect if the current line is out of the disallowed_clients block by checking indentation
 | 
				
			||||||
 | 
					            # If line is empty or less indented, disallowed block ended
 | 
				
			||||||
 | 
					            if (line.strip() == "") or (len(line) - len(line.lstrip()) <= len(disallowed_indent)):
 | 
				
			||||||
                inside_disallowed = False
 | 
					                inside_disallowed = False
 | 
				
			||||||
                output_lines.append(line.rstrip("\n"))
 | 
					                output_lines.append(line.rstrip("\n"))
 | 
				
			||||||
                # else skip line
 | 
					            else:
 | 
				
			||||||
 | 
					                # Skip lines inside disallowed_clients block (already replaced)
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            output_lines.append(line.rstrip("\n"))
 | 
					            output_lines.append(line.rstrip("\n"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Write to temporary YAML
 | 
				
			||||||
    with TMP_YAML.open("w") as f:
 | 
					    with TMP_YAML.open("w") as f:
 | 
				
			||||||
        f.write("\n".join(output_lines) + "\n")
 | 
					        f.write("\n".join(output_lines) + "\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TMP_YAML.replace(ADGUARD_YAML)
 | 
					    TMP_YAML.replace(ADGUARD_YAML)
 | 
				
			||||||
    logging.info(f"Updated {ADGUARD_YAML} with new disallowed clients list.")
 | 
					    logging.info(f"Updated {ADGUARD_YAML} with new disallowed clients list.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def restart_adguard_container():
 | 
					def restart_adguard_container():
 | 
				
			||||||
    docker_api_url = os.getenv("DOCKER_API_URL", "http://socket-proxy-adguard:2375")
 | 
					    docker_api_url = os.getenv("DOCKER_API_URL", "http://socket-proxy-adguard:2375")
 | 
				
			||||||
    container_name = os.getenv("ADGUARD_CONTAINER_NAME", "adguardhome")
 | 
					    container_name = os.getenv("ADGUARD_CONTAINER_NAME", "adguardhome")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user