mirror of
https://github.com/MAGICGrants/truenas-apps.git
synced 2026-01-09 20:47:58 -05:00
remove unsued stuff
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
from . import utils
|
||||
|
||||
|
||||
def envs(dev_env: dict, user_env: dict = None) -> list:
|
||||
tracked_env = {}
|
||||
envs = {}
|
||||
for key, value in dev_env.items():
|
||||
envs.update({key: value})
|
||||
tracked_env[key] = True
|
||||
|
||||
for key, value in user_env.items() if user_env else {}:
|
||||
if key in tracked_env:
|
||||
err = f"Key [{key}] is already defined by the application developer."
|
||||
err += " Please remove it from the user defined environment variables."
|
||||
raise utils.throw_error(err)
|
||||
|
||||
envs.update({key: value})
|
||||
|
||||
return envs
|
||||
@@ -1,73 +0,0 @@
|
||||
from . import utils
|
||||
from typing import Dict, Any, List
|
||||
|
||||
item_format = {
|
||||
"enabled": True,
|
||||
# The container port
|
||||
"container_port": 80,
|
||||
# The published port
|
||||
"host_port": 80,
|
||||
# The protocol
|
||||
"protocol": "tcp",
|
||||
# The mode
|
||||
"mode": "ingress",
|
||||
# The host IP
|
||||
"host_ip": "192.168.0.1",
|
||||
# The app_protocol
|
||||
"app_protocol": "http",
|
||||
# The container to use for the port
|
||||
"target": "container_name",
|
||||
}
|
||||
|
||||
|
||||
def get_selected_ports_for_container(container: str, values: Dict[str, Any] = {}) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Returns a list of ports to apply to the given container.
|
||||
"""
|
||||
if not values or not values.get("ports"):
|
||||
return []
|
||||
|
||||
ports = []
|
||||
valid_protocols = ["tcp", "udp"]
|
||||
valid_modes = ["ingress", "host"]
|
||||
req_keys = ["target", "host_port"]
|
||||
for item in values["ports"]:
|
||||
if not item or not item.get("enabled"):
|
||||
continue
|
||||
for key in req_keys:
|
||||
if not item.get(key):
|
||||
utils.throw_error(f"Expected [{key}] to be set for port [{item['name']}]")
|
||||
|
||||
if not isinstance(item["target"], str):
|
||||
utils.throw_error(f"Expected [target] to be a string for port [{item['name']}], got [{type(item['target'])}]")
|
||||
|
||||
if container != item["target"]:
|
||||
continue
|
||||
|
||||
port = {}
|
||||
if not item.get("host_port"):
|
||||
utils.throw_error(f"Expected [host_port] to be set for port [{item['name']}]")
|
||||
|
||||
port["published"] = utils.must_valid_port(item["host_port"])
|
||||
|
||||
if item.get("container_port"):
|
||||
port["target"] = utils.must_valid_port(item["container_port"])
|
||||
else: # If container port is not specified, use the same as host port
|
||||
port["target"] = port["published"]
|
||||
|
||||
if item.get("protocol"):
|
||||
if item["protocol"] not in valid_protocols:
|
||||
utils.throw_error(f"Expected [protocol] to be one of {valid_protocols}, got [{item['protocol']}]")
|
||||
port["protocol"] = item["protocol"]
|
||||
|
||||
if item.get("mode"):
|
||||
if item["mode"] not in valid_modes:
|
||||
utils.throw_error(f"Expected [mode] to be one of {valid_modes}, got [{item['mode']}]")
|
||||
port["mode"] = item["mode"]
|
||||
|
||||
if item.get("host_ip"):
|
||||
port["host_ip"] = utils.must_valid_ip(item["host_ip"], f"Expected [host_ip] to be a valid IP address, got [{item['host_ip']}]")
|
||||
|
||||
ports.append(port)
|
||||
|
||||
return ports
|
||||
@@ -1,7 +1,5 @@
|
||||
import ipaddress
|
||||
import secrets
|
||||
import sys
|
||||
import re
|
||||
|
||||
|
||||
class TemplateException(Exception):
|
||||
@@ -18,69 +16,3 @@ def throw_error(message):
|
||||
|
||||
def secure_string(length):
|
||||
return secrets.token_urlsafe(length)
|
||||
|
||||
|
||||
def must_valid_range(range: str, msg: str = "") -> str:
|
||||
"""
|
||||
Validates if the given range is a valid IP network range.
|
||||
If its valid, returns the range.
|
||||
If its not valid, throws an error.
|
||||
"""
|
||||
try:
|
||||
ipaddress.ip_network(range)
|
||||
return range
|
||||
except ValueError:
|
||||
throw_error(msg or f"Expected range [{range}] to be a valid IP network range")
|
||||
|
||||
|
||||
def must_valid_mac(mac: str, msg: str = "") -> str:
|
||||
"""
|
||||
Validates if the given MAC address is valid.
|
||||
If its valid, returns the MAC address.
|
||||
If its not valid, throws an error.
|
||||
"""
|
||||
re_mac_part = r"^[0-9a-fA-F]{2}$"
|
||||
parts = mac.split(":")
|
||||
if len(parts) != 6:
|
||||
throw_error(msg or f"Expected MAC address [{mac}] to be a valid MAC address")
|
||||
for part in parts:
|
||||
if not re.match(re_mac_part, part):
|
||||
throw_error(msg or f"Expected MAC address [{mac}] to be a valid MAC address")
|
||||
return mac
|
||||
|
||||
|
||||
def must_valid_ip(ip: str, msg: str = "") -> str:
|
||||
"""
|
||||
Validates if the given IP address is valid.
|
||||
If its valid, returns the IP address.
|
||||
If its not valid, throws an error.
|
||||
"""
|
||||
try:
|
||||
ipaddress.ip_address(ip)
|
||||
return ip
|
||||
except ValueError:
|
||||
throw_error(msg or f"Expected IP address [{ip}] to be a valid IP address")
|
||||
|
||||
|
||||
def must_valid_path(path: str) -> str:
|
||||
"""
|
||||
Validates if the given path is valid.
|
||||
If its valid, returns the path.
|
||||
If its not valid, throws an error.
|
||||
"""
|
||||
if not path.startswith("/"):
|
||||
throw_error(f"Expected path [{path}] to start with [/]")
|
||||
|
||||
return path
|
||||
|
||||
|
||||
def must_valid_port(port: int) -> int:
|
||||
"""
|
||||
Validates if the given port is valid.
|
||||
If its valid, returns the port.
|
||||
If its not valid, throws an error.
|
||||
"""
|
||||
if port < 1 or port > 65535:
|
||||
throw_error(f"Expected port [{port}] to be between 1 and 65535")
|
||||
|
||||
return port
|
||||
|
||||
Reference in New Issue
Block a user