summaryrefslogtreecommitdiff
path: root/reconfigure/configs
diff options
context:
space:
mode:
Diffstat (limited to 'reconfigure/configs')
-rw-r--r--reconfigure/configs/__init__.py46
-rw-r--r--reconfigure/configs/ajenti.py14
-rw-r--r--reconfigure/configs/base.py71
-rw-r--r--reconfigure/configs/bind9.py19
-rw-r--r--reconfigure/configs/crontab.py14
-rw-r--r--reconfigure/configs/ctdb.py44
-rw-r--r--reconfigure/configs/dhcpd.py17
-rw-r--r--reconfigure/configs/exports.py17
-rw-r--r--reconfigure/configs/fstab.py17
-rw-r--r--reconfigure/configs/group.py18
-rw-r--r--reconfigure/configs/hosts.py17
-rw-r--r--reconfigure/configs/iptables.py17
-rw-r--r--reconfigure/configs/netatalk.py18
-rw-r--r--reconfigure/configs/nsd.py17
-rw-r--r--reconfigure/configs/passwd.py18
-rw-r--r--reconfigure/configs/resolv.py18
-rw-r--r--reconfigure/configs/samba.py14
-rw-r--r--reconfigure/configs/squid.py14
-rw-r--r--reconfigure/configs/supervisor.py20
19 files changed, 430 insertions, 0 deletions
diff --git a/reconfigure/configs/__init__.py b/reconfigure/configs/__init__.py
new file mode 100644
index 0000000..6954547
--- /dev/null
+++ b/reconfigure/configs/__init__.py
@@ -0,0 +1,46 @@
+"""
+Configs are ready-to-use objects that link together Parsers, Includers and Builders to provide direct conversion between config files and Data tree.
+"""
+
+from base import Reconfig
+from ajenti import AjentiConfig
+from bind9 import BIND9Config
+from crontab import CrontabConfig
+from ctdb import CTDBConfig, CTDBNodesConfig, CTDBPublicAddressesConfig
+from dhcpd import DHCPDConfig
+from exports import ExportsConfig
+from fstab import FSTabConfig
+from group import GroupConfig
+from hosts import HostsConfig
+from iptables import IPTablesConfig
+from netatalk import NetatalkConfig
+from nsd import NSDConfig
+from passwd import PasswdConfig
+from resolv import ResolvConfig
+from samba import SambaConfig
+from squid import SquidConfig
+from supervisor import SupervisorConfig
+
+
+__all__ = [
+ 'Reconfig',
+ 'AjentiConfig',
+ 'BIND9Config',
+ 'CrontabConfig',
+ 'CTDBConfig',
+ 'CTDBNodesConfig',
+ 'CTDBPublicAddressesConfig',
+ 'DHCPDConfig',
+ 'ExportsConfig',
+ 'FSTabConfig',
+ 'GroupConfig',
+ 'HostsConfig',
+ 'IPTablesConfig',
+ 'NetatalkConfig',
+ 'NSDConfig',
+ 'PasswdConfig',
+ 'ResolvConfig',
+ 'SambaConfig',
+ 'SquidConfig',
+ 'SupervisorConfig',
+]
diff --git a/reconfigure/configs/ajenti.py b/reconfigure/configs/ajenti.py
new file mode 100644
index 0000000..1ecf9ab
--- /dev/null
+++ b/reconfigure/configs/ajenti.py
@@ -0,0 +1,14 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import JsonParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.ajenti import AjentiData
+
+
+class AjentiConfig (Reconfig):
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': JsonParser(),
+ 'builder': BoundBuilder(AjentiData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/base.py b/reconfigure/configs/base.py
new file mode 100644
index 0000000..0753246
--- /dev/null
+++ b/reconfigure/configs/base.py
@@ -0,0 +1,71 @@
+import chardet
+
+
+class Reconfig (object):
+ """
+ Basic config class. Derivatives normally only need to override the constructor.
+
+ Config data is loaded either from ``path`` or from ``content``
+
+ :param parser: overrides the Parser instance
+ :param includer: overrides the Includer instance
+ :param builder: overrides the Builder instance
+ :param path: config file path. Not compatible with ``content``
+ :param content: config file content. Not compatible with ``path``
+ """
+
+ def __init__(self, parser=None, includer=None, builder=None, path=None, content=None):
+ self.parser = parser
+ self.builder = builder
+ self.includer = includer
+ if self.includer is not None:
+ if not self.includer.parser:
+ self.includer.parser = self.parser
+ if path:
+ self.origin = path
+ self.content = None
+ else:
+ self.origin = None
+ self.content = content
+
+ def load(self):
+ """
+ Loads the config data, parses and builds it. Sets ``tree`` attribute to point to Data tree.
+ """
+ if self.origin:
+ self.content = open(self.origin, 'r').read()
+
+ try:
+ self.content = self.content.decode('utf8')
+ self.encoding = 'utf8'
+ except UnicodeDecodeError:
+ self.encoding = chardet.detect(self.content)['encoding']
+ self.content = self.content.decode(self.encoding)
+
+ self.nodetree = self.parser.parse(self.content)
+ if self.includer is not None:
+ self.nodetree = self.includer.compose(self.origin, self.nodetree)
+ if self.builder is not None:
+ self.tree = self.builder.build(self.nodetree)
+ return self
+
+ def save(self):
+ """
+ Unbuilds, stringifies and saves the config. If the config was loaded from string, returns ``{ origin: data }`` dict
+ """
+ tree = self.tree
+ if self.builder is not None:
+ nodetree = self.builder.unbuild(tree) or self.nodetree
+ if self.includer is not None:
+ nodetree = self.includer.decompose(nodetree)
+ else:
+ nodetree = {self.origin: nodetree}
+
+ result = {}
+ for k in nodetree:
+ result[k or self.origin] = self.parser.stringify(nodetree[k]).encode(self.encoding)
+
+ if self.origin is not None:
+ for k in result:
+ open(k, 'w').write(result[k])
+ return result
diff --git a/reconfigure/configs/bind9.py b/reconfigure/configs/bind9.py
new file mode 100644
index 0000000..8bf38a8
--- /dev/null
+++ b/reconfigure/configs/bind9.py
@@ -0,0 +1,19 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import BIND9Parser
+from reconfigure.includers import BIND9Includer
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.bind9 import BIND9Data
+
+
+class BIND9Config (Reconfig):
+ """
+ ``named.conf``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': BIND9Parser(),
+ 'includer': BIND9Includer(),
+ 'builder': BoundBuilder(BIND9Data),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/crontab.py b/reconfigure/configs/crontab.py
new file mode 100644
index 0000000..3d7e21a
--- /dev/null
+++ b/reconfigure/configs/crontab.py
@@ -0,0 +1,14 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import CrontabParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.crontab import CrontabData
+
+
+class CrontabConfig (Reconfig):
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': CrontabParser(),
+ 'builder': BoundBuilder(CrontabData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/ctdb.py b/reconfigure/configs/ctdb.py
new file mode 100644
index 0000000..491ac28
--- /dev/null
+++ b/reconfigure/configs/ctdb.py
@@ -0,0 +1,44 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import IniFileParser
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.ctdb import CTDBData, NodesData, PublicAddressesData
+
+
+class CTDBConfig (Reconfig):
+ """
+ ``CTDB main config``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': IniFileParser(sectionless=True),
+ 'builder': BoundBuilder(CTDBData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
+
+
+class CTDBNodesConfig (Reconfig):
+ """
+ ``CTDB node list file``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(),
+ 'builder': BoundBuilder(NodesData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
+
+
+class CTDBPublicAddressesConfig (Reconfig):
+ """
+ ``CTDB public address list file``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(),
+ 'builder': BoundBuilder(PublicAddressesData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/dhcpd.py b/reconfigure/configs/dhcpd.py
new file mode 100644
index 0000000..0f52986
--- /dev/null
+++ b/reconfigure/configs/dhcpd.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import NginxParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.dhcpd import DHCPDData
+
+
+class DHCPDConfig (Reconfig):
+ """
+ ``DHCPD``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': NginxParser(),
+ 'builder': BoundBuilder(DHCPDData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/exports.py b/reconfigure/configs/exports.py
new file mode 100644
index 0000000..3f0296a
--- /dev/null
+++ b/reconfigure/configs/exports.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import ExportsParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.exports import ExportsData
+
+
+class ExportsConfig (Reconfig):
+ """
+ ``/etc/fstab``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': ExportsParser(),
+ 'builder': BoundBuilder(ExportsData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/fstab.py b/reconfigure/configs/fstab.py
new file mode 100644
index 0000000..69f21fe
--- /dev/null
+++ b/reconfigure/configs/fstab.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.fstab import FSTabData
+
+
+class FSTabConfig (Reconfig):
+ """
+ ``/etc/fstab``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(),
+ 'builder': BoundBuilder(FSTabData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/group.py b/reconfigure/configs/group.py
new file mode 100644
index 0000000..e769a04
--- /dev/null
+++ b/reconfigure/configs/group.py
@@ -0,0 +1,18 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.group import GroupsData
+
+
+class GroupConfig (Reconfig):
+ """
+ ``/etc/group``
+ """
+
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(separator=':'),
+ 'builder': BoundBuilder(GroupsData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/hosts.py b/reconfigure/configs/hosts.py
new file mode 100644
index 0000000..fbb1c3c
--- /dev/null
+++ b/reconfigure/configs/hosts.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.hosts import HostsData
+
+
+class HostsConfig (Reconfig):
+ """
+ ``/etc/hosts``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(),
+ 'builder': BoundBuilder(HostsData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/iptables.py b/reconfigure/configs/iptables.py
new file mode 100644
index 0000000..8a26211
--- /dev/null
+++ b/reconfigure/configs/iptables.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import IPTablesParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.iptables import IPTablesData
+
+
+class IPTablesConfig (Reconfig):
+ """
+ ``iptables-save`` and ``iptables-restore``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': IPTablesParser(),
+ 'builder': BoundBuilder(IPTablesData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/netatalk.py b/reconfigure/configs/netatalk.py
new file mode 100644
index 0000000..6db2d21
--- /dev/null
+++ b/reconfigure/configs/netatalk.py
@@ -0,0 +1,18 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import IniFileParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.netatalk import NetatalkData
+
+
+class NetatalkConfig (Reconfig):
+ """
+ Netatalk afp.conf
+ """
+
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': IniFileParser(),
+ 'builder': BoundBuilder(NetatalkData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/nsd.py b/reconfigure/configs/nsd.py
new file mode 100644
index 0000000..a313feb
--- /dev/null
+++ b/reconfigure/configs/nsd.py
@@ -0,0 +1,17 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import NSDParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.nsd import NSDData
+
+
+class NSDConfig (Reconfig):
+ """
+ ``NSD DNS server nsd.conf``
+ """
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': NSDParser(),
+ 'builder': BoundBuilder(NSDData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/passwd.py b/reconfigure/configs/passwd.py
new file mode 100644
index 0000000..7fb1e0c
--- /dev/null
+++ b/reconfigure/configs/passwd.py
@@ -0,0 +1,18 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.passwd import PasswdData
+
+
+class PasswdConfig (Reconfig):
+ """
+ ``/etc/passwd``
+ """
+
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(separator=':'),
+ 'builder': BoundBuilder(PasswdData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/resolv.py b/reconfigure/configs/resolv.py
new file mode 100644
index 0000000..41802b9
--- /dev/null
+++ b/reconfigure/configs/resolv.py
@@ -0,0 +1,18 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SSVParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.resolv import ResolvData
+
+
+class ResolvConfig (Reconfig):
+ """
+ ``/etc/resolv.conf``
+ """
+
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SSVParser(maxsplit=1),
+ 'builder': BoundBuilder(ResolvData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/samba.py b/reconfigure/configs/samba.py
new file mode 100644
index 0000000..32eec80
--- /dev/null
+++ b/reconfigure/configs/samba.py
@@ -0,0 +1,14 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import IniFileParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.samba import SambaData
+
+
+class SambaConfig (Reconfig):
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': IniFileParser(),
+ 'builder': BoundBuilder(SambaData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/squid.py b/reconfigure/configs/squid.py
new file mode 100644
index 0000000..bfd1642
--- /dev/null
+++ b/reconfigure/configs/squid.py
@@ -0,0 +1,14 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import SquidParser
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.squid import SquidData
+
+
+class SquidConfig (Reconfig):
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': SquidParser(),
+ 'builder': BoundBuilder(SquidData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)
diff --git a/reconfigure/configs/supervisor.py b/reconfigure/configs/supervisor.py
new file mode 100644
index 0000000..ef0fc95
--- /dev/null
+++ b/reconfigure/configs/supervisor.py
@@ -0,0 +1,20 @@
+from reconfigure.configs.base import Reconfig
+from reconfigure.parsers import IniFileParser
+from reconfigure.includers import SupervisorIncluder
+from reconfigure.builders import BoundBuilder
+from reconfigure.items.supervisor import SupervisorData
+
+
+class SupervisorConfig (Reconfig):
+ """
+ ``/etc/supervisor/supervisord.conf``
+ """
+
+ def __init__(self, **kwargs):
+ k = {
+ 'parser': IniFileParser(),
+ 'includer': SupervisorIncluder(),
+ 'builder': BoundBuilder(SupervisorData),
+ }
+ k.update(kwargs)
+ Reconfig.__init__(self, **k)