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