Add net.get_ip(container_name)
This commit is contained in:
parent
55ee03079f
commit
ffa0927119
@ -9,7 +9,7 @@ import subprocess
|
|||||||
import time
|
import time
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
from . import config, network, repo_local, templates
|
from . import config, net, repo_local, templates
|
||||||
from .depsolver import DepSolver
|
from .depsolver import DepSolver
|
||||||
from .exceptions import InvalidContainerStateError
|
from .exceptions import InvalidContainerStateError
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ class Container:
|
|||||||
cmd = self.cmd if self.cmd else '/sbin/init'
|
cmd = self.cmd if self.cmd else '/sbin/init'
|
||||||
cwd = self.cwd if self.cwd else '/'
|
cwd = self.cwd if self.cwd else '/'
|
||||||
halt = self.halt if self.halt else 'SIGINT'
|
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
|
# Write LXC configuration file
|
||||||
with open(self.config_path, 'w') as f:
|
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))
|
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:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
# Release the IP address from global hosts configuration
|
# Release the IP address from global hosts configuration
|
||||||
network.release_ip(self.name)
|
net.release_ip(self.name)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
# Start the container including its dependencies
|
# Start the container including its dependencies
|
||||||
|
@ -37,7 +37,7 @@ def save_leases():
|
|||||||
# write all IP-hostname pairs to the global hosts file
|
# write all IP-hostname pairs to the global hosts file
|
||||||
global mtime
|
global mtime
|
||||||
with open(config.HOSTS_FILE, 'w') as f:
|
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')
|
f.write(f'{ip} {hostname}\n')
|
||||||
mtime = os.stat(config.HOSTS_FILE).st_mtime
|
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])
|
netmask = socket.inet_ntoa(fcntl.ioctl(sock.fileno(), IOCTL_SIOCGIFNETMASK, packed_ifname)[20:24])
|
||||||
return ipaddress.IPv4Interface(f'{ip}/{netmask}')
|
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):
|
def request_ip(container_name):
|
||||||
# Find if and IP hasn't been leased for the hostname
|
# Find if and IP hasn't been leased for the hostname
|
||||||
interface = get_bridge_interface()
|
interface = get_bridge_interface()
|
Loading…
Reference in New Issue
Block a user