diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 580922f..9f05c80 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,12 +1,29 @@ - + + + + + + + + + + + + + + @@ -14,7 +31,9 @@ - - - - - file://$PROJECT_DIR$/main.py - 8 - - - + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py deleted file mode 100644 index 5596b44..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press Shift+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/resources/lxc/0000/config.json b/resources/lxc/0000/config.json new file mode 100644 index 0000000..85a0e68 --- /dev/null +++ b/resources/lxc/0000/config.json @@ -0,0 +1,26 @@ +{ + "lxc_id": "0000", + "lxc_hostname": "test", + "os": "alpine", + "release": "3.17", + "resources": { + "cpu": "1", + "memory": "512", + "disk": "10", + "storage": "local-lvm" + }, + "network": { + "bridge": "vmbr0", + "ipv4": "dhcp", + "ipv6": "", + "mac": "00:00:00:00:00:00", + "gateway": "", + "vlan": "" + }, + "options": { + "privileged": "false", + "start_on_boot": "false", + "password": "qwertz1234", + "ssh": false + } +} \ No newline at end of file diff --git a/resources/qemu/id/0000/config.json b/resources/qemu/id/0000/config.json new file mode 100644 index 0000000..e69de29 diff --git a/run.py b/run.py new file mode 100644 index 0000000..e3eb317 --- /dev/null +++ b/run.py @@ -0,0 +1,7 @@ +import logging + +from src import main + +if __name__ == '__main__': + logging.getLogger().setLevel(logging.INFO) + main.run() diff --git a/src/get_path_file.py b/src/get_path_file.py new file mode 100644 index 0000000..5a128df --- /dev/null +++ b/src/get_path_file.py @@ -0,0 +1,3 @@ +import pathlib + +project_path = pathlib.Path(__file__).parent.parent.resolve() diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..8a6641a --- /dev/null +++ b/src/main.py @@ -0,0 +1,29 @@ +import logging +import os + +from src.get_path_file import project_path +from src.utils.lxc_utils import load_lxc, get_all_lxcs + + +def run(): + # Read all files in the resources directory + resources = os.listdir(project_path / "resources") + logging.info(f"Resources found: {resources}") + + # Go through each LXC file + for resource in resources: + if resource == "lxc": + logging.info("LXC folder found") + + # Read all files in the LXC directory + lxc_folders = os.listdir(project_path / "resources" / "lxc") + for lxc_folder in lxc_folders: + lxc_file = os.path.join(project_path / "resources" / "lxc", lxc_folder, "config.json") + logging.info(f"Reading LXC ID {lxc_file}") + + # Open the file + with open(lxc_file, "r") as file: + # Load the LXC + load_lxc(file.read()) + + print(get_all_lxcs()) diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/lxc_utils.py b/src/utils/lxc_utils.py new file mode 100644 index 0000000..41d1548 --- /dev/null +++ b/src/utils/lxc_utils.py @@ -0,0 +1,179 @@ +import json + +lxcs = [] + + +def get_all_lxcs(): + """ + Get all LXCs + :return: list of all lxcs + """ + return lxcs + + +def get_lxc(id): + """ + Get LXC by ID + :param id: lxc id + :return: lxc object + """ + for lxc in lxcs: + if lxc.get_lxc_id() == id: + return lxc + + return None + + +def load_lxc(file): + """ + Load LXC from JSON file + + :param file: json config file + :return: + """ + # Load JSON data + data = json.loads(file) + + # Extract values from JSON + lxc_id = data["lxc_id"] + lxc_hostname = data["lxc_hostname"] + disk_size = data["resources"]["disk"] + core_count = data["resources"]["cpu"] + ram_size = data["resources"]["memory"] + bridge = data["network"]["bridge"] + ipv4 = data["network"]["ipv4"] + gateway = data["network"]["gateway"] + disable_ipv6 = "yes" if data["network"]["ipv6"] == "" else "no" + mac = data["network"]["mac"] + vlan = data["network"]["vlan"] + ssh = "yes" if data["options"]["ssh"] else "no" + + # Create LXC object + lxc = LXC( + lxc_id=lxc_id, + lxc_hostname=lxc_hostname, + disk_size=disk_size, + core_count=core_count, + ram_size=ram_size, + bridge=bridge, + ipv4=ipv4, + gateway=gateway, + disable_ipv6=disable_ipv6, + mac=mac, + vlan=vlan, + ssh=ssh + ) + + lxcs.append(lxc) + + +class LXC: + """ + LXC class + """ + + def __init__(self, lxc_id, lxc_hostname, disk_size, core_count, ram_size, bridge, ipv4, gateway, disable_ipv6, mac, + vlan, ssh): + self.lxc_id = lxc_id + self.lxc_hostname = lxc_hostname + self.disk_size = disk_size + self.core_count = core_count + self.ram_size = ram_size + self.bridge = bridge + self.ipv4 = ipv4 + self.gateway = gateway + self.disable_ipv6 = disable_ipv6 + self.mac = mac + self.vlan = vlan + self.ssh = ssh + + def __str__(self): + return f"LXC {self.lxc_id} ({self.lxc_hostname})" + + def __repr__(self): + return f"LXC {self.lxc_id} ({self.lxc_hostname})" + + def __eq__(self, other): + return self.lxc_id == other.lxc_id + + def __hash__(self): + return hash(self.lxc_id) + + def get_lxc_id(self): + return self.lxc_id + + def get_lxc_hostname(self): + return self.lxc_hostname + + def get_disk_size(self): + return self.disk_size + + def get_core_count(self): + return self.core_count + + def get_ram_size(self): + return self.ram_size + + def get_bridge(self): + return self.bridge + + def get_ipv4(self): + return self.ipv4 + + def get_gateway(self): + return self.gateway + + def get_disable_ipv6(self): + return self.disable_ipv6 + + def get_mac(self): + return self.mac + + def get_vlan(self): + return self.vlan + + def get_ssh(self): + return self.ssh + + def get_tteck_env_variables(self): + """ + Get TTECK environment variables to run scripts silently + :return: environment variables + """ + + env_template = '''CT_TYPE="1" + PW="" + CT_ID={lxc_id} + HN={lxc_hostname} + DISK_SIZE="{disk_size}" + CORE_COUNT="{core_count}" + RAM_SIZE="{ram_size}" + BRG="{bridge}" + NET="{ipv4}" + GATE="{gateway}" + DISABLEIP6="{disable_ipv6}" + MTU="" + SD="" + NS="" + MAC="{mac}" + VLAN="{vlan}" + SSH="{ssh}" + VERB="no"''' + + # Format the environment variables template + env_variables = env_template.format( + lxc_id=self.lxc_id, + lxc_hostname=self.lxc_hostname, + disk_size=self.disk_size, + core_count=self.core_count, + ram_size=self.ram_size, + bridge=self.bridge, + ipv4=self.ipv4, + gateway=self.gateway, + disable_ipv6=self.disable_ipv6, + mac=self.mac, + vlan=self.vlan, + ssh=self.ssh + ) + + return env_variables