summaryrefslogtreecommitdiff
path: root/pykolab/confmgmt/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'pykolab/confmgmt/__init__.py')
-rw-r--r--pykolab/confmgmt/__init__.py333
1 files changed, 333 insertions, 0 deletions
diff --git a/pykolab/confmgmt/__init__.py b/pykolab/confmgmt/__init__.py
new file mode 100644
index 0000000..8958ead
--- /dev/null
+++ b/pykolab/confmgmt/__init__.py
@@ -0,0 +1,333 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
+#
+# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 3 or, at your option, any later version
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+import pykolab
+
+log = pykolab.getLogger('pykolab.confmgmt')
+
+from pykolab.confmgmt.db import get_db
+
+from pykolab.confmgmt.model import Environment
+from pykolab.confmgmt.model import File
+from pykolab.confmgmt.model import Node
+from pykolab.confmgmt.model import Package
+from pykolab.confmgmt.model import Role
+from pykolab.confmgmt.model import Service
+from pykolab.confmgmt.model import Setting
+
+def add_environment(name=None):
+ db = get_db()
+
+ if not name == None:
+ environment = db.query(Environment).filter_by(name=name).first()
+ if not environment == None:
+ return environment
+
+ environments = db.query(Environment).all()
+
+ if len(environments) == 0 and name == None:
+ return add_environment('development')
+ else:
+ db.add(Environment(name=name))
+ db.commit()
+
+ environment = db.query(Environment).filter_by(name=name).first()
+
+ return environment
+
+def add_file(file_path, tech=None):
+ log.debug("Adding file %r" % (file_path))
+ db = get_db()
+
+ _file = db.query(File).filter_by(path=file_path).first()
+
+ if _file == None:
+ db.add(File(path=file_path, tech=tech))
+ db.commit()
+
+ _file = db.query(File).filter_by(path=file_path).first()
+
+ return _file
+
+def add_file_setting(_file, setting):
+ db = get_db()
+ _file.settings.append(setting)
+ db.commit()
+
+def add_node(node_fqdn):
+ log.debug("Adding node %r" % (node_fqdn))
+ db = get_db()
+ node = db.query(Node).filter_by(fqdn=node_fqdn).first()
+
+ if node == None:
+ db.add(Node(fqdn=node_fqdn))
+ db.commit()
+
+ node = db.query(Node).filter_by(fqdn=node_fqdn).first()
+
+ node.environment = add_environment()
+
+ db.commit()
+
+ return node
+
+def add_node_role(node, role):
+ log.debug("Adding node %r role %r" % (node.fqdn, role.name))
+ db = get_db()
+ node.roles.append(role)
+ db.commit()
+
+def add_package():
+ pass
+
+def add_package_file():
+ pass
+
+def add_package_service():
+ pass
+
+def add_role(role):
+ log.debug("Adding role %r" % (role))
+ db = get_db()
+
+ _role = db.query(Role).filter_by(name=role).first()
+ if _role == None:
+ db.add(Role(name=role))
+ db.commit()
+
+ _role = db.query(Role).filter_by(name=role).first()
+
+ return _role
+
+def add_role_service(_role,_service):
+ log.debug("Adding role %r service %r" % (_role.name, _service.name))
+ db = get_db()
+
+ _role.services.append(_service)
+
+ db.commit()
+
+def add_role_setting():
+ pass
+
+def add_service(service):
+ db = get_db()
+
+ _service = db.query(Service).filter_by(name=service).first()
+
+ if _service == None:
+ db.add(Service(name=service))
+ db.commit()
+
+ _service = db.query(Service).filter_by(name=service).first()
+
+ return _service
+
+def add_service_file(service, _file):
+ db = get_db()
+ service.files.append(_file)
+ db.commit()
+
+def add_setting(key, value, function):
+ db = get_db()
+
+ _setting = db.query(Setting).filter_by(key=key, value=value, function=function).first()
+
+ if _setting == None:
+ db.add(Setting(key=key, value=value, function=function))
+ db.commit()
+
+ _setting = db.query(Setting).filter_by(key=key, value=value, function=function).first()
+
+ return _setting
+
+def _add_setting(file_path, key, value=None, function=None, service_name=None, role_name=None, tech=None):
+ if not service_name == None:
+ log.debug(_("Searching for service with name %s") % (service_name), level=8)
+ service = add_service(service_name)
+ if not service == None:
+ log.debug(_("Found service %s") % (service_name), level=8)
+ else:
+ service = None
+
+ if not role_name == None:
+ log.debug(_("Searching for role with name %s") % (role_name), level=8)
+ role = add_role(role_name)
+ if not role == None:
+ log.debug(_("Found role %s") % (role_name), level=8)
+ else:
+ role = None
+
+ log.debug(_("Searching for file with path %s") % (file_path), level=8)
+ _file = add_file(file_path, tech)
+
+ if not service_name == None:
+ log.debug(_("Adding file with path %s to service with name %s") % (file_path, service_name), level=8)
+ add_service_file(service, _file)
+
+ log.debug(_("Adding setting with key %s (%r, %r)") % (key, value, function), level=8)
+ setting = add_setting(key=key, value=value, function=function)
+
+ log.debug(_("Adding setting %s to file with path %s") % (key, file_path), level=8)
+ add_file_setting(_file, setting)
+
+ if not role == None:
+ log.debug(_("Adding role with name %s to setting with key %s") %(role_name, key), level=8)
+ setting.roles.append(role)
+
+def commit():
+ db = get_db()
+ db.commit()
+
+def get_environment(name):
+ """
+ Return the environment record for the environment with name 'name'.
+ """
+ db = get_db()
+ environment = db.query(Environment).filter_by(name=name).first()
+ return environment
+
+def get_file(path):
+ """
+ Return a file record for the file with path 'path'.
+ """
+ db = get_db()
+ _file = db.query(File).filter_by(path=path).first()
+ return _file
+
+def get_node(fqdn):
+ """
+ Return a node record for the node with FQDN 'fqdn'.
+ """
+ db = get_db()
+ node = db.query(Node).filter_by(fqdn=fqdn).first()
+ return node
+
+def get_node_files(fqdn):
+ """
+ Get a list of file records that are relevant for the node with FQDN 'fqdn'.
+ """
+ files = []
+
+ node = get_node(fqdn)
+
+ if node == None:
+ return files
+
+ for role in node.roles:
+ for service in role.services:
+ for _file in service.files:
+ _settings = []
+
+ for setting in _file.settings:
+ if len(setting.roles) > 0:
+ if (len([needle.name for needle in setting.roles if needle.name in [haystack.name for haystack in node.roles]]) > 0):
+ _settings.append(setting.key)
+ else:
+ _settings.append(setting.key)
+
+ if len(_settings) > 0:
+ files.append(_file)
+
+ return files
+
+def get_role(name):
+ """
+ Return a role record for the role with name 'name'.
+ """
+ db = get_db()
+ role = db.query(Role).filter_by(name=name).first()
+ return role
+
+def get_service(name):
+ """
+ Return the service record for the service with name 'name'.
+ """
+ db = get_db()
+ service = db.query(Service).filter_by(name=name).first()
+ return service
+
+def get_setting(key, path, roles=[]):
+ """
+ Return the setting record for setting 'key' in file 'file_path'.
+ """
+ db = get_db()
+ _file = db.query(File).filter_by(path=path).first()
+ setting = db.query(Setting).filter_by(key=key).filter(Setting.files.contains(_file)).first()
+ return setting
+
+def list_environments():
+ """
+ List environments.
+ """
+ db = get_db()
+ environments = db.query(Environment).all()
+ return environments
+
+def list_files():
+ """
+ List files.
+ """
+ db = get_db()
+ files = db.query(File).all()
+ return files
+
+def list_nodes():
+ """
+ List nodes.
+ """
+ db = get_db()
+ nodes = db.query(Node).all()
+ return nodes
+
+def list_roles():
+ """
+ List roles.
+ """
+ db = get_db()
+ roles = db.query(Role).all()
+ return roles
+
+def list_services():
+ """
+ List services.
+ """
+ db = get_db()
+ services = db.query(Service).all()
+ return services
+
+def remove_environment(environment):
+ db = get_db()
+
+ db.delete(environment)
+ db.commit()
+
+def remove_node_role(node, role):
+ log.debug("Removing node %r role %r" % (node.fqdn, role.name))
+ db = get_db()
+ node.roles.pop(node.roles.index(role))
+ db.commit()
+
+def remove_role(role):
+ db = get_db()
+
+ db.delete(role)
+ db.commit()
+