Commit c44a9e73 authored by Xavier Thompson's avatar Xavier Thompson

slapformat: WIP: extend and improve format

parent e03a89b6
......@@ -252,12 +252,22 @@ class Computer(object):
pass
def format(self):
# Software root path
ensureDir(self.conf.software_root, 0o755)
# Software user
self.user.createUser()
uid, gid = self.user.getIds()
os.chown(self.conf.software_root, uid, gid)
# Non local bind for IPv6 ranges
if self.conf.ipv6_range or any(p.ipv6_range for p in self.partitions):
self.interface.enableIPv6NonLocalBind()
# Computer address
self.interface.addAddress(self.address, self.reference)
# Instance root path
ensureDir(self.conf.instance_root, 0o755)
# Format each partitions
for p in self.partitions:
p.format()
p.format(self.interface)
def update(self):
pass
......@@ -332,24 +342,24 @@ class Interface(object):
if not 'dev lo' in result:
call(['ip', '-6', 'route', 'add', 'local', network, 'dev', 'lo'])
def addAddress(self, ip, interface=None):
def addAddress(self, ip, reason, interface=None):
interface = interface or getattr(self, 'ipv%d_interface' % ip.version)
af = {4: AF_INET, 6: AF_INET6}[ip.version]
address = str(ip.ip)
for iface in netifaces.interfaces():
if iface != interface:
ifaddresses = netifaces.ifaddresses(iface).get(af, ())
if ip.ip in (q['addr'].split('%')[0] for q in ifaddresses):
if address in (q['addr'].split('%')[0] for q in ifaddresses):
self.conf.error(
"Cannot add address %s to %s as it already exists on %s",
ip, interface, iface
"Cannot add address %s (%s) to %s because it already exists on %s",
ip, reason, interface, iface
)
ifaddresses = netifaces.ifaddresses(interface).get(af, ())
if not ip.ip in (q['addr'].split('%')[0] for q in ifaddresses):
if not address in (q['addr'].split('%')[0] for q in ifaddresses):
call(['ip', 'addr', 'add', str(ip), 'dev', interface])
class Partition(object):
interface : Interface
reference : str
index: int
path : str
......@@ -363,10 +373,9 @@ class Partition(object):
def __init__(self, index, computer, definition=None):
i = str(index)
conf = computer.conf
interface = computer.interface
section = definition['partition_' + i]
options = defaultdict(type(None), section, **definition['default'])
# Interface
self.interface = interface = computer.interface
# Reference, path & user
self.reference = options['pathname'] or conf.partition_base_name + i
self.path = os.path.join(conf.instance_root, self.reference)
......@@ -389,13 +398,13 @@ class Partition(object):
# Tap & Tun
# XXX
def format(self):
def format(self, interface):
self.user.createUser()
self.createPath()
for ip in self.ipv4_list:
self.interface.addAddress(ip)
interface.addAddress(ip, self.reference)
for ip in self.ipv6_list:
self.interface.addAddress(ip)
interface.addAddress(ip, self.reference)
def createPath(self):
if not os.path.exists(self.path):
......
Markdown is supported
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