aetherscale

[unmaintained] code for a cloud provider tutorial
Log | Files | Refs | README | LICENSE

commit 30cd86ab4eca59c4f25f0b0e05f22fbc2293c0b0
parent 44cd7625ec1e483c3a0296f370a5e21551310aac
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sat, 30 Jan 2021 17:13:04 +0100

store config files in vm and vpn subfolders

Diffstat:
Maetherscale/computing.py | 61++++++++++++++++++++++++++++++++++++++++++++-----------------
Maetherscale/vpn/tinc.py | 4+++-
2 files changed, 47 insertions(+), 18 deletions(-)

diff --git a/aetherscale/computing.py b/aetherscale/computing.py @@ -1,3 +1,4 @@ +import enum import logging import json import os @@ -62,22 +63,41 @@ def create_user_image(vm_id: str, image_name: str) -> Path: return user_image -def setup_script_path(tap_name: str) -> Path: - network_conf_dir = config.AETHERSCALE_CONFIG_DIR / 'networking' - return network_conf_dir / f'{tap_name}-setup.sh' +class ResourceType(enum.Enum): + VM = enum.auto() + VPN = enum.auto() -def teardown_script_path(tap_name: str) -> Path: - network_conf_dir = config.AETHERSCALE_CONFIG_DIR / 'networking' - return network_conf_dir / f'{tap_name}-teardown.sh' +def resource_config_path( + resource_type: ResourceType, resource_name: str) -> Path: + if resource_type == ResourceType.VM: + resource_folder = 'vm' + elif resource_type == ResourceType.VPN: + resource_folder = 'vpn' + else: + raise ValueError(f'Unknown resource type {resource_type}') + + return config.AETHERSCALE_CONFIG_DIR / resource_folder / resource_name + + +def setup_script_path(resource_folder: Path, tap_name: str) -> Path: + return resource_folder / f'{tap_name}-setup.sh' + +def teardown_script_path(resource_folder: Path, tap_name: str) -> Path: + return resource_folder / f'{tap_name}-teardown.sh' + + +def setup_tap_device( + resource_type: ResourceType, resource_name: str, + tap_name: str, bridge: str) -> Tuple[Path, Path]: + resource_folder = resource_config_path(resource_type, resource_name) -def setup_tap_device(tap_name: str, bridge: str) -> Tuple[Path, Path]: iproute = networking.Iproute2Network() iproute.tap_device(tap_name, config.USER, bridge) - setup_script = setup_script_path(tap_name) - teardown_script = teardown_script_path(tap_name) + setup_script = setup_script_path(resource_folder, tap_name) + teardown_script = teardown_script_path(resource_folder, tap_name) with open(setup_script, 'w') as f: f.write(iproute.setup_script()) @@ -181,9 +201,12 @@ class ComputingHandler: if 'vpn' in options: # TODO: Do we have to assign the VPN mac addr to the macvtap? vpn_tap_device = self._establish_vpn(options['vpn'], vm_id) - network_setup_scripts.append(setup_script_path(vpn_tap_device)) + + resource_folder = resource_config_path(ResourceType.VM, vm_id) + network_setup_scripts.append(setup_script_path( + resource_folder, vpn_tap_device)) network_teardown_scripts.append( - teardown_script_path(vpn_tap_device)) + teardown_script_path(resource_folder, vpn_tap_device)) mac_addr_vpn = networking.create_mac_address() logging.debug( @@ -198,7 +221,8 @@ class ComputingHandler: if 'public-ip' in options and options['public-ip']: mac_addr = networking.create_mac_address() - logging.debug(f'Assigning MAC address "{mac_addr}" to VM "{vm_id}"') + logging.debug( + f'Assigning MAC address "{mac_addr}" to VM "{vm_id}"') pub_tap_device = f'pub-{vm_id}' pubnet = runtime.QemuInterfaceConfig( @@ -208,7 +232,7 @@ class ComputingHandler: qemu_interfaces.append(pubnet) setup_script, teardown_script = setup_tap_device( - pub_tap_device, 'br0') + ResourceType.VM, vm_id, pub_tap_device, 'br0') network_setup_scripts.append(setup_script) network_teardown_scripts.append(teardown_script) @@ -294,8 +318,8 @@ class ComputingHandler: yield response def delete_vm(self, options: Dict[str, Any]) -> Iterator[Dict[str, str]]: - # TODO: Once all VMs of a VPN on a host have been deleted, we can delete - # the associated VPN + # TODO: Once all VMs of a VPN on a host have been deleted, we can + # delete the associated VPN try: vm_id = options['vm-id'] @@ -422,7 +446,9 @@ class ComputingHandler: # Create a new tap device for the VM to use associated_tap_device = 'vpn-' + vm_id - setup_tap_device(associated_tap_device, vpn.bridge_interface_name) + setup_tap_device( + ResourceType.VM, vm_id, + associated_tap_device, vpn.bridge_interface_name) logging.debug( f'Created TAP device {associated_tap_device} for VM {vm_id}') @@ -520,7 +546,8 @@ def callback(ch, method, properties, body, handler: ComputingHandler): resp_message = { 'execution-info': { 'status': 'error', - # TODO: Only ouput message if it is an exception generated by us + # TODO: Only ouput message if it is an exception generated + # by us 'reason': str(e), } } diff --git a/aetherscale/vpn/tinc.py b/aetherscale/vpn/tinc.py @@ -106,7 +106,9 @@ class TincVirtualNetwork(object): net_dir_quoted = shlex.quote(str(self._net_config_folder())) pidfile_quoted = shlex.quote(str(self.pidfile)) - network_conf_dir = config.AETHERSCALE_CONFIG_DIR / 'networking' + # TODO: Manage all paths through a central module responsible for + # path/files management + network_conf_dir = config.AETHERSCALE_CONFIG_DIR / 'vpn' / self.netname network_conf_dir.mkdir(parents=True, exist_ok=True) setup_file = network_conf_dir / f'network-{self.netname}-setup.sh' teardown_file = network_conf_dir / f'network-{self.netname}-teardown.sh'