mirror of
https://github.com/KrystianD/mikrotik_configurator.git
synced 2025-07-28 14:54:29 +02:00
initial commit
This commit is contained in:
commit
cb6a0f8d27
31 changed files with 880 additions and 0 deletions
89
mikrotik_configurator/generator.py
Normal file
89
mikrotik_configurator/generator.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
import os
|
||||
from textwrap import indent
|
||||
from typing import Dict, List
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
from utils import read_text_file
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
cleanups = []
|
||||
|
||||
|
||||
def escape_for_mikrotik(cnt):
|
||||
return cnt \
|
||||
.replace('\\', '\\\\') \
|
||||
.replace("\t", "\\t") \
|
||||
.replace("\r", "\\r") \
|
||||
.replace("\n", "\\n") \
|
||||
.replace('"', '\\"')
|
||||
|
||||
|
||||
def load_file(path, name):
|
||||
_, ext = os.path.splitext(name)
|
||||
assert ext == ".txt"
|
||||
file_cnt = escape_for_mikrotik(read_text_file(os.path.expanduser(path)).strip())
|
||||
esc = escape_for_mikrotik(file_cnt)
|
||||
|
||||
cnt = f"""
|
||||
:execute script=":put \\"{esc}\\"" file="{name}"
|
||||
:delay 1000ms
|
||||
"""
|
||||
return cnt
|
||||
|
||||
|
||||
def generate_catch_block(body):
|
||||
return f""":do {{
|
||||
{indent(body.strip(), " ")}
|
||||
}} on-error={{}}
|
||||
"""
|
||||
|
||||
|
||||
def register_cleanup(caller):
|
||||
body = caller()
|
||||
|
||||
body = generate_catch_block(body)
|
||||
|
||||
cleanups.insert(0, body)
|
||||
return ""
|
||||
|
||||
|
||||
def escape_string(caller):
|
||||
body = caller().strip()
|
||||
return f'"{escape_for_mikrotik(body)}"'
|
||||
|
||||
|
||||
def rollback_delete_chain(name):
|
||||
body = generate_catch_block(f"""
|
||||
/ip firewall filter remove [find chain="{name}"]
|
||||
/ip firewall filter remove [find jump-target="{name}"]
|
||||
/ip firewall nat remove [find chain="{name}"]
|
||||
/ip firewall nat remove [find jump-target="{name}"]
|
||||
/ip firewall mangle remove [find chain="{name}"]
|
||||
/ip firewall mangle remove [find jump-target="{name}"]
|
||||
""")
|
||||
cleanups.insert(0, body)
|
||||
return ""
|
||||
|
||||
|
||||
def render_file(path: str, include_dirs: List[str], variables: Dict[str, str]):
|
||||
global cleanups
|
||||
cleanups = []
|
||||
|
||||
env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
os.path.join("."),
|
||||
*include_dirs,
|
||||
]),
|
||||
)
|
||||
env.globals['register_cleanup'] = register_cleanup
|
||||
env.globals['escape_string'] = escape_string
|
||||
env.globals['rollback_delete_chain'] = rollback_delete_chain
|
||||
env.globals = {**env.globals, **variables}
|
||||
|
||||
content = env.get_template(os.path.basename(path))
|
||||
content = content.render(load_file=load_file)
|
||||
|
||||
content = "\n".join(cleanups) + "\n\n" + content
|
||||
return content
|
Loading…
Add table
Add a link
Reference in a new issue