Add net.get_ip(container_name)

This commit is contained in:
Disassembler 2020-04-04 12:21:25 +02:00
parent 55ee03079f
commit ffa0927119
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

@ -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()