Verified Commit 05676899 authored by Jan Miksik's avatar Jan Miksik
Browse files

feature/lxd: added network to container

added property of network as class to conatiner instances to be
more easily available for usage.
parent 118ade71
......@@ -191,7 +191,7 @@ class Shell(Cli):
def txt_write(self, path, content):
"""Write text file via shell.
Note that parent directory has to exist and any file will be rewritten.
path: path to be written to
......@@ -215,7 +215,7 @@ class Shell(Cli):
def bin_write(self, path, content):
"""Write given bytes to binary file in path.
Note that parent directory has to exist and any file will be rewritten.
path: path to file to be written.
......@@ -4,13 +4,16 @@ import os
import typing
import logging
import pexpect
import itertools
import functools
import typing
import warnings
import ipaddress
from .. import cli
from .connection import LXDConnection
from .image import Image
from .device import Device
from .exceptions import LXDDeviceError
from .network import NetworkInterface
logger = logging.getLogger(__package__)
......@@ -28,6 +31,7 @@ class Container:
self._override_wants_internet = internet
self._strict = strict
self._devices = dict()
self._network = None
self._image = image if isinstance(image, Image) else Image(lxd_connection, image)
self._logger = logging.getLogger(f"{__package__}[{}]")
......@@ -68,6 +72,8 @@ class Container:
}, wait=True)
# Added LXD network class
self._network = NetworkInterface(self)
logger.debug("Container prepared: %s",
def _container_name(self, prefix="nsfarm"):
......@@ -100,6 +106,19 @@ class Container:
pexp.logfile_read = cli.PexpectLogging(logging.getLogger( + str(command)))
return pexp
def get_ip(self,
interfaces: typing.Union[list, tuple] = None,
versions: typing.Union[list, tuple] = frozenset([4, 6])) -> list:
"""returns list of ipaddress.IP#Interface filtered according to parameters.
ips = []
ifs_dict =
for iface in interfaces if interfaces else
for address in ifs_dict[iface]:
if address.version in versions:
return ips
def __enter__(self):
return self
......@@ -135,3 +154,9 @@ class Container:
"""Dict of passed devices from host.
return self._devices
def network(self) -> dict:
"""network representation, that represents all data connections.
return self._network
import ipaddress
class NetworkInterface():
"""Interface representing network interfaces of LXD container.
Following values from lxd network dict are not implemented: counters, type, mtus, state
def __init__(self, container):
self._container = container
def _network(self):
return self._container.lxd_container.state().network
def hwaddr(self):
"""returns hardware address/mac of interfaces in a dictionary.
return {interface: self._network[interface]['hwaddr'] for interface in self._network}
def hostname(self):
"""returns hostname of interfaces in a dictionary.
return {interface: self._network[interface]['host_name'] for interface in self._network}
def addresses(self):
"""returns ip addresses of interfaces in a dictionary of lists, containing ipaddress.IPvXAddresses
interface_addrs = {}
for interface in self._network:
interface_addrs[interface] = []
for address in self._network[interface]['addresses']:
# contains list of dictionaries
return interface_addrs
def interfaces(self):
"""returns list of available interfaces
return self._network.keys()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment