diff options
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -149,6 +149,8 @@ class Network: self.zones = {} self.subnet_manager = None self.latency_off = Latency(0) + self.host_ip = None + self.host_link = None def set_subnet_manager(self, subnet): self.subnet_manager = SubnetManager(subnet) @@ -184,14 +186,17 @@ class Network: else: zone.ip = self.subnet_manager.next_local() self.subnet_manager.next_zone() + if not self.host_ip: + self.host_ip = self.subnet_manager.next_local() def __str__(self): return f'Network{{subnet_manager: {self.subnet_manager}, zones: {list(self.zones.values())}, latency_offset: {self.latency_off}}}' class NamespaceManager: def __init__(self): - self.namespaces = set() + self.namespaces = set(["unconfined"]) self.prefixlen = 0 + net.ns.name_unconfined() def make_namespace(self, name): if not name in self.namespaces: @@ -202,7 +207,7 @@ class NamespaceManager: self.make_namespace(namespace) net.create_bridge(name, namespace, ports) - def make_veth(self, name1, name2, space1, space2, ip, link): + def make_veth(self, name1, name2, space1, space2, ip, link=None): self.make_namespace(space1) self.make_namespace(space2) net.create_veth(name1, space1, name2, space2, ip, self.prefixlen, link) @@ -210,7 +215,8 @@ class NamespaceManager: def build_network(self, network): self.prefixlen = network.subnet_manager.prefix netns = "testnet-core" - ports = [] + self.make_veth("veth-testnet", "unconfined", "unconfined", netns, network.host_ip, network.host_link) + ports = ["unconfined"] for zone in network.zones.values(): if zone.is_zone(): self.build_zone(zone) @@ -244,6 +250,10 @@ def parse(yaml): latency_offset = global_conf.get("latency-offset", 0) network = Network() + if upstream := global_conf.get("upstream"): + network.host_ip = upstream.get("ip") + if host_link:= upstream.get("conn"): + network.host_link = LinkInfo(latency_offset=latency_offset, **host_link) network.set_subnet_manager(subnet) network.set_latency_offset(latency_offset) for server in server_list: @@ -302,6 +312,7 @@ def runall(cmd): config = yaml.safe_load(file) zones = parse(config).zones + number = 1 for zone in zones.values(): if zone.is_zone(): for server in zone.servers.values(): @@ -309,19 +320,25 @@ def runall(cmd): env["ZONE"] = zone.name env["HOST"] = server.name env["IP"] = str(server.ip) + env["ID"] = str(number) name = f'testnet-{zone.name}-{server.name}' net.ns.run(name, cmd, env) + number +=1 else: env = os.environ.copy() + env["ZONE"] = "" env["HOST"] = zone.name env["IP"] = str(zone.ip) + env["ID"] = str(number) name = f'testnet-{zone.name}-{zone.name}' net.ns.run(name, cmd, env) - + first = False + number +=1 def destroy(): for ns in net.ns.list(): net.ns.kill(ns) + net.ns.forget("unconfined") os.remove(".current_state.yml") if __name__ == "__main__": |