wip-python -> Python rewriting + better logs + backup + update at startup #3
@@ -23,6 +23,7 @@
 | 
				
			|||||||
- Updates the disallowed_clients section in the AdGuard Home config.
 | 
					- Updates the disallowed_clients section in the AdGuard Home config.
 | 
				
			||||||
- Configurable update frequency via cron expression environment variable.
 | 
					- Configurable update frequency via cron expression environment variable.
 | 
				
			||||||
- Automatically restarts the AdGuard Home container after updates via Docker socket proxy.
 | 
					- Automatically restarts the AdGuard Home container after updates via Docker socket proxy.
 | 
				
			||||||
 | 
					- Backup `AdguardHome.yaml` at first startup, then create a second backup at each update.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Environment Variables
 | 
					## Environment Variables
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,46 +61,46 @@ def read_manual_ips():
 | 
				
			|||||||
        return []
 | 
					        return []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_yaml_with_ips(ips):
 | 
					def update_yaml_with_ips(ips):
 | 
				
			||||||
    # Read original lines
 | 
					 | 
				
			||||||
    with ADGUARD_YAML.open() as f:
 | 
					 | 
				
			||||||
        lines = f.readlines()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    output_lines = []
 | 
					    output_lines = []
 | 
				
			||||||
    inside_disallowed = False
 | 
					    inside_disallowed = False
 | 
				
			||||||
    disallowed_indent = ""
 | 
					    disallowed_indent = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    formatted_ips = []  # We'll prepare after knowing indent
 | 
					    with ADGUARD_YAML.open() as f:
 | 
				
			||||||
 | 
					        lines = f.readlines()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for i, line in enumerate(lines):
 | 
					    for line in lines:
 | 
				
			||||||
        stripped = line.lstrip()
 | 
					        stripped = line.lstrip()
 | 
				
			||||||
        indent = line[:len(line) - len(stripped)]
 | 
					        indent = line[:len(line) - len(stripped)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if stripped.startswith("disallowed_clients:"):
 | 
					        if stripped.startswith("disallowed_clients:"):
 | 
				
			||||||
 | 
					            # Capture the indentation of the disallowed_clients key
 | 
				
			||||||
            disallowed_indent = indent
 | 
					            disallowed_indent = indent
 | 
				
			||||||
            # Write the disallowed_clients line with original indent
 | 
					 | 
				
			||||||
            output_lines.append(line.rstrip("\n"))
 | 
					 | 
				
			||||||
            inside_disallowed = True
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Prepare formatted IPs with indentation plus 2 spaces (YAML block indent)
 | 
					            # Replace entire line with just 'disallowed_clients:' (remove any [])
 | 
				
			||||||
 | 
					            output_lines.append(f"{disallowed_indent}disallowed_clients:")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Add all IPs indented 2 spaces more than disallowed_clients
 | 
				
			||||||
            formatted_ips = [f"{disallowed_indent}  - {ip}" for ip in ips]
 | 
					            formatted_ips = [f"{disallowed_indent}  - {ip}" for ip in ips]
 | 
				
			||||||
 | 
					 | 
				
			||||||
            # Immediately add IP list here (replace old block)
 | 
					 | 
				
			||||||
            output_lines.extend(formatted_ips)
 | 
					            output_lines.extend(formatted_ips)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            inside_disallowed = True
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if inside_disallowed:
 | 
					        if inside_disallowed:
 | 
				
			||||||
            # Detect if the current line is out of the disallowed_clients block by checking indentation
 | 
					            # We skip all old lines inside disallowed_clients block.
 | 
				
			||||||
            # If line is empty or less indented, disallowed block ended
 | 
					            # The block ends when we find a line with indentation
 | 
				
			||||||
            if (line.strip() == "") or (len(line) - len(line.lstrip()) <= len(disallowed_indent)):
 | 
					            # less than or equal to disallowed_indent but not the key line itself.
 | 
				
			||||||
 | 
					            # To detect end of block, compare indent length:
 | 
				
			||||||
 | 
					            if len(indent) <= len(disallowed_indent) and stripped != "":
 | 
				
			||||||
                inside_disallowed = False
 | 
					                inside_disallowed = False
 | 
				
			||||||
                output_lines.append(line.rstrip("\n"))
 | 
					                output_lines.append(line.rstrip("\n"))
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                # Skip lines inside disallowed_clients block (already replaced)
 | 
					                # skip this line (old disallowed_clients content)
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            output_lines.append(line.rstrip("\n"))
 | 
					            output_lines.append(line.rstrip("\n"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Write to temporary YAML
 | 
					    # Write temp file in same directory to avoid cross-device rename errors
 | 
				
			||||||
    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")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user