Add helper files
This commit is contained in:
parent
8a1ee721f3
commit
90baaa8d03
4 changed files with 119 additions and 0 deletions
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
19
src/settings.py
Normal file
19
src/settings.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
""" General settings """
|
||||
|
||||
# Prefix to all the networks and containers created
|
||||
PREFIX = "testnw"
|
||||
|
||||
# Container base installation path -- will be overlaid
|
||||
CONTAINER_BASE_ROOT = "/var/lib/machines/lxc-base-" + PREFIX
|
||||
|
||||
# Overlayfs mount dir
|
||||
OVERLAYFS_MOUNT_DIR = "/tmp/{}-overlays/".format(PREFIX)
|
||||
|
||||
# The ID of the whole generated network -- below 0xff
|
||||
NETWORK_ID = 132
|
||||
|
||||
# IPv4 /16 range
|
||||
IPV4_RANGE = "10.{}".format(NETWORK_ID)
|
||||
|
||||
# IPv6 /32 range
|
||||
IPV6_RANGE = "fd80:ba{:02x}".format(NETWORK_ID)
|
84
src/util.py
Normal file
84
src/util.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
""" Various utils """
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
class NumberedClass:
|
||||
""" A class that counts its current instance number """
|
||||
|
||||
next_id = 0
|
||||
|
||||
@classmethod
|
||||
def get_id(cls):
|
||||
out = cls.next_id
|
||||
cls.next_id += 1
|
||||
return out
|
||||
|
||||
def __init__(self):
|
||||
self.id = self.get_id()
|
||||
|
||||
|
||||
class MACAddress:
|
||||
""" A MAC address for a NIC or bridge """
|
||||
|
||||
def __init__(self, link_id, dev_id):
|
||||
""" link_id: id of the current link
|
||||
dev_id: id of the current NIC. If the device is a bridge, use None
|
||||
"""
|
||||
if not dev_id:
|
||||
dev_id = 0xFF
|
||||
|
||||
self.link_id = link_id
|
||||
self.dev_id = dev_id
|
||||
|
||||
def __str__(self):
|
||||
return "52:54:00:{nwid:02x}:{link_id:02x}:{dev_id:02x}".format(
|
||||
nwid=settings.NETWORK_ID, link_id=self.link_id, dev_id=self.dev_id
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return str(self)
|
||||
|
||||
|
||||
class Addrv4:
|
||||
""" An address in IPv4 """
|
||||
|
||||
def __init__(self, link_id, dev_id):
|
||||
""" link_id: id of the current link
|
||||
dev_id: id of the current NIC. Use None to get the base address
|
||||
"""
|
||||
if not dev_id:
|
||||
dev_id = 0
|
||||
|
||||
self.link_id = link_id
|
||||
self.dev_id = dev_id
|
||||
self.netmask = "255.255.255.0"
|
||||
self.prefix = 24
|
||||
|
||||
def __str__(self):
|
||||
return "{base_range}.{link_id}.{dev_id}".format(
|
||||
base_range=settings.IPV4_RANGE, link_id=self.link_id, dev_id=self.dev_id
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return str(self)
|
||||
|
||||
|
||||
class Addrv6:
|
||||
""" An address in IPv6 """
|
||||
|
||||
def __init__(self, link_id, dev_id):
|
||||
""" link_id: id of the current link
|
||||
dev_id: id of the current NIC. Use None to get the base address
|
||||
"""
|
||||
if not dev_id:
|
||||
dev_id = 0
|
||||
|
||||
self.link_id = link_id
|
||||
self.dev_id = dev_id
|
||||
self.prefix = 64
|
||||
|
||||
def __str__(self):
|
||||
return "{base_range}:{link_id:04x}::{dev_id:04x}".format(
|
||||
base_range=settings.IPV6_RANGE, link_id=self.link_id, dev_id=self.dev_id
|
||||
)
|
16
src/xml_template.py
Normal file
16
src/xml_template.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
""" Reads an XML template from a file """
|
||||
|
||||
import os
|
||||
|
||||
|
||||
class XMLTemplate:
|
||||
""" Reads and instanciates a template from a file """
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
|
||||
with open(self.path, "r") as handle:
|
||||
self.template_str = handle.read()
|
||||
|
||||
def inst(self, *args, **kwargs):
|
||||
""" instanciates the template """
|
||||
return self.template_str.format(*args, **kwargs)
|
Loading…
Reference in a new issue