add: start.py generate yaml from json

This commit is contained in:
Domingo Dirutigliano
2024-09-09 22:54:54 +02:00
parent b33b0b46d7
commit 35c6965072

146
start.py
View File

@@ -24,6 +24,33 @@ def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
def sep(): puts("-----------------------------------", is_bold=True) def sep(): puts("-----------------------------------", is_bold=True)
def dict_to_yaml(data, indent_spaces:int=4, base_indent:int=0, additional_spaces:int=0, add_text_on_dict:str|None=None):
yaml = ''
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
if isinstance(data, dict):
for key, value in data.items():
if not add_text_on_dict is None:
spaces_len = len(spaces)-len(add_text_on_dict)
spaces = (' '*max(spaces_len, 0))+add_text_on_dict
add_text_on_dict = None
if isinstance(value, dict) or isinstance(value, list):
yaml += f"{spaces}{key}:\n"
yaml += dict_to_yaml(value, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}{key}: {value}\n"
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
elif isinstance(data, list):
for item in data:
if isinstance(item, dict):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent, additional_spaces=additional_spaces+2, add_text_on_dict="- ")
elif isinstance(item, list):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}- {item}\n"
else:
yaml += f"{data}\n"
return yaml
def check_if_exists(program, get_output=False): def check_if_exists(program, get_output=False):
if get_output: if get_output:
return subprocess.getoutput(program) return subprocess.getoutput(program)
@@ -120,57 +147,78 @@ def write_compose(skip_password = True):
with open(g.composefile,"wt") as compose: with open(g.composefile,"wt") as compose:
if is_linux(): #Check if not is a wsl also if is_linux(): #Check if not is a wsl also
compose.write(f""" compose.write(dict_to_yaml({
services: "services": {
firewall: "firewall": {
restart: unless-stopped "restart": "unless-stopped",
container_name: firegex "container_name": "firegex",
{"build: ." if args.build else "image: ghcr.io/pwnzer0tt1/firegex"} "build" if args.build else "image": "." if args.build else "ghcr.io/pwnzer0tt1/firegex",
network_mode: "host" "network_mode": "host",
environment: "environment": [
- PORT={args.port} f"PORT={args.port}",
- NTHREADS={args.threads} f"NTHREADS={args.threads}",
{"- HEX_SET_PSW="+psw_set.encode().hex() if psw_set else ""} f"HEX_SET_PSW={psw_set.encode().hex()}" if psw_set else ""
volumes: ],
- firegex_data:/execute/db "volumes": [
- type: bind "firegex_data:/execute/db",
source: /proc/sys/net/ipv4/conf/all/route_localnet {
target: /sys_host/net.ipv4.conf.all.route_localnet "type": "bind",
- type: bind "source": "/proc/sys/net/ipv4/conf/all/route_localnet",
source: /proc/sys/net/ipv4/ip_forward "target": "/sys_host/net.ipv4.conf.all.route_localnet"
target: /sys_host/net.ipv4.ip_forward },
- type: bind {
source: /proc/sys/net/ipv4/conf/all/forwarding "type": "bind",
target: /sys_host/net.ipv4.conf.all.forwarding "source": "/proc/sys/net/ipv4/ip_forward",
- type: bind "target": "/sys_host/net.ipv4.ip_forward"
source: /proc/sys/net/ipv6/conf/all/forwarding },
target: /sys_host/net.ipv6.conf.all.forwarding {
cap_add: "type": "bind",
- NET_ADMIN "source": "/proc/sys/net/ipv4/conf/all/forwarding",
volumes: "target": "/sys_host/net.ipv4.conf.all.forwarding"
firegex_data: },
""") {
"type": "bind",
"source": "/proc/sys/net/ipv6/conf/all/forwarding",
"target": "/sys_host/net.ipv6.conf.all.forwarding"
}
],
"cap_add": [
"NET_ADMIN"
]
}
},
"volumes": {
"firegex_data": ""
}
}))
else: else:
compose.write(f""" compose.write(dict_to_yaml({
services: "services": {
firewall: "firewall": {
restart: unless-stopped "restart": "unless-stopped",
container_name: firegex "container_name": "firegex",
{"build: ." if args.build else "image: ghcr.io/pwnzer0tt1/firegex"} "build" if args.build else "image": "." if args.build else "ghcr.io/pwnzer0tt1/firegex",
ports: "ports": [
- {args.port}:{args.port} f"{args.port}:{args.port}"
environment: ],
- PORT={args.port} "environment": [
- NTHREADS={args.threads} f"PORT={args.port}",
{"- HEX_SET_PSW="+psw_set.encode().hex() if psw_set else ""} f"NTHREADS={args.threads}",
volumes: f"HEX_SET_PSW={psw_set.encode().hex()}" if psw_set else ""
- firegex_data:/execute/db ],
cap_add: "volumes": [
- NET_ADMIN "firegex_data:/execute/db"
volumes: ],
firegex_data: "cap_add": [
""") "NET_ADMIN"
]
}
},
"volumes": {
"firegex_data": ""
}
}))