Commit 35b9ed2c authored by Antoine Catton's avatar Antoine Catton

Update container configuration during preparation

parent 9ff3e2af
# -*- coding: utf-8 -*-
import collections
import StringIO
class LXCConfig(object):
"""LXC Configuration file parser
"""
class _Sub(object):
"""Subobject to go throught
LXC COnfiguration"""
def __init__(self, parent, namespace):
self._parent = parent
self._namespace = namespace
def __getattr__(self, name):
if name.startswith('_'):
return self.__dict__[name]
return self._parent._get('%s.%s' % (self._namespace,
name))
def __setattr__(self, name, value):
if name.startswith('_'):
self.__dict__[name] = value
else:
self._parent._set('%s.%s' % (self._namespace, name),
value)
def __init__(self, filename):
"""LXCConfig init method. filename should be a string
of the path to the filename.
"""
self._filename = filename
with open(filename, 'r') as lxcconf_file:
self._values = self._load(lxcconf_file.read())
def __getattr__(self, name):
return self._get(name)
def __setattr__(self, name, value):
if name.startswith('_'):
self.__dict__[name] = value
else:
self._set(name, value)
def _load(self, config_string):
result = collections.OrderedDict()
for line in config_string.split('\n'):
if not line.strip().startswith('#') and line.strip() != '':
if '=' not in line:
raise ValueError("Not a valid LXCFile")
key, value = [i.strip() for i in line.split('=', 1)]
if key in result:
if isinstance(result[key], basestring):
result[key] = [result[key], value]
else:
result[key].append(value)
else:
result[key] = value
return result
def _set(self, key, value):
self._values['lxc.%s' % key] = value
def _get(self, key):
try:
return self._values['lxc.%s' % key]
except KeyError:
return self._Sub(self, key)
def __str__(self):
result = StringIO.StringIO()
for key, value in self._values.iteritems():
if isinstance(value, basestring):
print >> result, key, '=', value
else:
for item in value:
print >> result, key, '=', item
return result.getvalue()
......@@ -6,6 +6,8 @@ import subprocess
import lockfile
from .config import LXCConfig
class SlapContainerError(Exception):
"""This exception is thrown, if there is
any failure during slapcontainer preparation,
......@@ -82,7 +84,21 @@ def stop(sr_directory, partition_path, conf):
def create(sr_directory, partition_path, conf):
lxc_debian = os.path.join(sr_directory,
'parts/lxc/lib/lxc/templates/lxc-debian')
return call([lxc_debian, '-p', partition_path])
call([lxc_debian, '-p', partition_path])
lxc_filename = os.path.join(partition_path, 'config')
lxc = LXCConfig(lxc_filename)
lxc.utsname = os.path.basename(partition_path)
lxc.network.vlan.type = 'vlan'
lxc.network.link = conf.get('information', 'interface')
lxc.network.name = 'eth0'
# XXX: Hardcoded netmasks
lxc.network.ipv4 = '%s/32' % conf.get('information', 'ipv4')
lxc.network.ipv6 = '%s/128' % conf.get('information', 'ipv6')
lxc.network.flags = 'up'
with open(lxc_filename, 'w') as lxc_file:
lxc_file.write(str(lxc))
def destroy(partition_path, conf):
......
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