From ffa0927119374532eda8a576438740039e7b42f5 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sat, 4 Apr 2020 12:21:25 +0200 Subject: [PATCH] Add net.get_ip(container_name) --- usr/lib/python3.8/spoc/container.py | 6 +++--- usr/lib/python3.8/spoc/{network.py => net.py} | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) rename usr/lib/python3.8/spoc/{network.py => net.py} (90%) diff --git a/usr/lib/python3.8/spoc/container.py b/usr/lib/python3.8/spoc/container.py index 7e51750..62579e7 100644 --- a/usr/lib/python3.8/spoc/container.py +++ b/usr/lib/python3.8/spoc/container.py @@ -9,7 +9,7 @@ import subprocess import time from concurrent.futures import ThreadPoolExecutor -from . import config, network, repo_local, templates +from . import config, net, repo_local, templates from .depsolver import DepSolver from .exceptions import InvalidContainerStateError @@ -131,7 +131,7 @@ class Container: cmd = self.cmd if self.cmd else '/sbin/init' cwd = self.cwd if self.cwd else '/' halt = self.halt if self.halt else 'SIGINT' - ip_address, ip_netmask, ip_gateway = network.request_ip(self.name) + ip_address, ip_netmask, ip_gateway = net.request_ip(self.name) # Write LXC configuration file with open(self.config_path, 'w') as f: f.write(templates.LXC_CONTAINER_TEMPLATE.format(name=self.name, ip_address=ip_address, ip_netmask=ip_netmask, ip_gateway=ip_gateway, rootfs=self.rootfs_path, hosts=config.HOSTS_FILE, mounts=mounts, env=env, uid=uid, gid=gid, cmd=cmd, cwd=cwd, halt=halt, log=self.log_path)) @@ -149,7 +149,7 @@ class Container: except FileNotFoundError: pass # Release the IP address from global hosts configuration - network.release_ip(self.name) + net.release_ip(self.name) def start(self): # Start the container including its dependencies diff --git a/usr/lib/python3.8/spoc/network.py b/usr/lib/python3.8/spoc/net.py similarity index 90% rename from usr/lib/python3.8/spoc/network.py rename to usr/lib/python3.8/spoc/net.py index bf7aab4..dfb905a 100644 --- a/usr/lib/python3.8/spoc/network.py +++ b/usr/lib/python3.8/spoc/net.py @@ -37,7 +37,7 @@ def save_leases(): # write all IP-hostname pairs to the global hosts file global mtime with open(config.HOSTS_FILE, 'w') as f: - for ip, hostname in sorted(leases.items(), key=lambda lease: socket.inet_aton(lease[0])): + for ip,hostname in sorted(leases.items(), key=lambda lease: socket.inet_aton(lease[0])): f.write(f'{ip} {hostname}\n') mtime = os.stat(config.HOSTS_FILE).st_mtime @@ -50,6 +50,13 @@ def get_bridge_interface(): netmask = socket.inet_ntoa(fcntl.ioctl(sock.fileno(), IOCTL_SIOCGIFNETMASK, packed_ifname)[20:24]) return ipaddress.IPv4Interface(f'{ip}/{netmask}') +def get_ip(container_name): + load_leases() + for ip,hostname in leases.items(): + if hostname == container_name: + return ip + return None + def request_ip(container_name): # Find if and IP hasn't been leased for the hostname interface = get_bridge_interface()