summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-12-14 10:57:17 +0000
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-12-14 12:31:26 +0000
commitb5f1f9fc37a6a60e159151ff5e5e81b26bfaf761 (patch)
treeb979194863bdfa1dfdf902e6504da5858f85e3ef
parent38c2bf2ba44e48d5297c6bdf052c1cdfc50bcb9b (diff)
downloadpykolab-b5f1f9fc37a6a60e159151ff5e5e81b26bfaf761.tar.gz
Add confmgmt module, model and database
-rw-r--r--pykolab/confmgmt/__init__.py333
-rw-r--r--pykolab/confmgmt/db.py140
-rw-r--r--pykolab/confmgmt/model.py545
3 files changed, 1018 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()
+
diff --git a/pykolab/confmgmt/db.py b/pykolab/confmgmt/db.py
new file mode 100644
index 0000000..e42bcae
--- /dev/null
+++ b/pykolab/confmgmt/db.py
@@ -0,0 +1,140 @@
+# -*- 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.
+#
+
+from sqlalchemy import create_engine
+
+try:
+ from sqlalchemy.orm import sessionmaker
+except:
+ from sqlalchemy.orm import create_session
+
+from model import Base
+from model import Environment
+from model import File
+from model import Node
+from model import Package
+from model import Role
+from model import Service
+from model import Setting
+#from confmgmt.model import Task
+
+import pykolab
+from pykolab.auth import Auth
+from pykolab.translate import _
+
+log = pykolab.getLogger('pykolab.confmgmt')
+conf = pykolab.getConf()
+
+db = None
+
+def get_db():
+ global db
+
+ if db == None:
+ return init_db()
+
+ return db
+
+def init_db():
+ """
+ Returns a SQLAlchemy Session() instance.
+ """
+ global db
+
+ db_uri = "mysql://root:Welcome2KolabSystems@localhost/kolab"
+
+ if not db_uri == None:
+ echo = conf.debuglevel > 8
+ engine = create_engine(db_uri, echo=echo)
+
+ if conf.debuglevel > 8:
+ log.info(_("Dropping all tables..."))
+ Base.metadata.drop_all(engine)
+
+ log.info(_("Creating the necessary tables..."))
+ Base.metadata.create_all(engine)
+
+ Session = sessionmaker(bind=engine)
+ db = Session()
+
+ if db == None:
+ log.error(_("No database available"))
+
+ return db
+
+def ldap_server_hostname_from_uri():
+ ldap_uri = conf.get('ldap', 'ldap_uri')
+ hostname = None
+ port = None
+
+ from urlparse import urlparse
+
+ result = urlparse(ldap_uri)
+
+ if hasattr(result, 'hostname'):
+ hostname = result.hostname
+ else:
+ scheme = ldap_uri.split(':')[0]
+ (hostname, port) = ldap_uri.split('/')[2].split(':')
+
+ return hostname
+
+def list_domains():
+ domains = []
+ print "Listing domains"
+ auth = Auth()
+ auth.connect()
+ _domains = auth.list_domains()
+ print "Domains:", _domains
+ for domain,domain_aliases in _domains:
+ domains.append(domain)
+ domains.extend(domain_aliases)
+
+ return domains
+
+def list_nodes_by_role(role):
+ print "Executing list_nodes_by_role() for role", role
+ role = db.query(Role).filter_by(name=role).first()
+
+ nodes = []
+
+ for node in role.nodes:
+ if not node.fqdn in nodes:
+ nodes.append(node.fqdn)
+
+ return nodes
+
+def list_users_by_role(role, result_attr):
+ print "Executing list_users_by_role() for role", role
+ auth = Auth()
+ auth.connect()
+ user_dns = auth.search_entry_by_attribute('nsroledn', 'cn=%s,%s' % (role,conf.get('ldap','base_dn')))
+
+ print user_dns
+
+ user_login_names = []
+
+ for user_dn in user_dns:
+ user_login_names.append(auth.get_user_attribute(None, {'dn': user_dn}, result_attr))
+
+ print "User login names:", user_login_names
+
+ return user_login_names
+
diff --git a/pykolab/confmgmt/model.py b/pykolab/confmgmt/model.py
new file mode 100644
index 0000000..3652d8d
--- /dev/null
+++ b/pykolab/confmgmt/model.py
@@ -0,0 +1,545 @@
+# -*- 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 datetime
+
+import sqlalchemy
+
+from sqlalchemy import Column
+from sqlalchemy import MetaData
+from sqlalchemy import Table
+
+from sqlalchemy import DateTime
+from sqlalchemy import ForeignKey
+from sqlalchemy import Integer
+from sqlalchemy import String
+from sqlalchemy import Text
+
+from sqlalchemy.interfaces import PoolListener
+
+from sqlalchemy.orm import mapper
+
+try:
+ from sqlalchemy.orm import relationship
+except:
+ from sqlalchemy.orm import relation as relationship
+
+from sqlalchemy.ext.declarative import declarative_base
+
+from sqlalchemy.schema import Index
+from sqlalchemy.schema import UniqueConstraint
+
+import pykolab
+from pykolab.translate import _
+
+log = pykolab.getLogger('pykolab.confmgmt')
+conf = pykolab.getConf()
+
+Base = declarative_base()
+
+# A single file has multiple settings.
+file_settings_table = Table(
+ 'file_settings', Base.metadata,
+ Column('file_id', Integer, ForeignKey('file.id')),
+ Column('setting_id', Integer, ForeignKey('setting.id'))
+ )
+
+# A node has multiple roles.
+node_roles_table = Table(
+ 'node_roles', Base.metadata,
+ Column('node_id', Integer, ForeignKey('node.id')),
+ Column('role_id', Integer, ForeignKey('role.id'))
+ )
+
+# A package ships multiple files, and each file belongs to one package (at the
+# most).
+package_files_table = Table(
+ 'package_files', Base.metadata,
+ Column('package_id', Integer, ForeignKey('package.id')),
+ Column('file_id', Integer, ForeignKey('file.id'))
+ )
+
+package_services_table = Table(
+ 'package_services', Base.metadata,
+ Column('package_id', Integer, ForeignKey('package.id')),
+ Column('service_id', Integer, ForeignKey('service.id'))
+ )
+
+# Each role implies running one or more services.
+role_services_table = Table(
+ 'role_services', Base.metadata,
+ Column('role_id', Integer, ForeignKey('role.id')),
+ Column('service_id', Integer, ForeignKey('service.id'))
+ )
+
+role_settings_table = Table(
+ 'role_settings', Base.metadata,
+ Column('role_id', Integer, ForeignKey('role.id')),
+ Column('setting_id', Integer, ForeignKey('setting.id'))
+ )
+
+service_files_table = Table(
+ 'service_files', Base.metadata,
+ Column('service_id', Integer, ForeignKey('service.id')),
+ Column('file_id', Integer, ForeignKey('file.id'))
+ )
+
+class Environment(Base):
+ __tablename__ = 'environment'
+
+ id = Column(Integer, primary_key=True)
+ # TODO: unique constraint
+ name = Column(String(16), nullable=False)
+ description = Column(String(128), nullable=True)
+
+ # One to many
+ nodes = relationship(
+ 'Node',
+ backref='environment'
+ )
+
+class File(Base):
+ __tablename__ = 'file'
+
+ id = Column(Integer, primary_key=True)
+ path = Column(Text, nullable=False)
+ tech = Column(String(7), nullable=False, default='augeas')
+
+ package = relationship(
+ 'Package',
+ secondary=package_files_table
+ )
+
+ services = relationship(
+ 'Service',
+ secondary=service_files_table
+ )
+
+ settings = relationship(
+ 'Setting',
+ secondary=file_settings_table
+ )
+
+class Node(Base):
+ __tablename__ = 'node'
+
+ id = Column(Integer, primary_key=True)
+ fqdn = Column(String(128), nullable=False)
+
+ # One node can only belong to one environment at a time
+ environment_id = Column(Integer, ForeignKey('environment.id'))
+
+ roles = relationship(
+ 'Role',
+ secondary=node_roles_table
+ )
+
+class Package(Base):
+ __tablename__ = 'package'
+
+ id = Column(Integer, primary_key=True)
+ name = Column(String(256), nullable=False)
+
+ files = relationship(
+ 'File',
+ secondary=package_files_table
+ )
+
+ services = relationship(
+ 'Service',
+ secondary=package_services_table
+ )
+
+class Role(Base):
+ __tablename__ = 'role'
+
+ id = Column(Integer, primary_key=True)
+ name = Column(String(128), nullable=False)
+
+ nodes = relationship(
+ 'Node',
+ secondary=node_roles_table
+ )
+
+ services = relationship(
+ 'Service',
+ secondary=role_services_table
+ )
+
+ settings = relationship(
+ 'Setting',
+ secondary=role_settings_table
+ )
+
+class Service(Base):
+ __tablename__ = 'service'
+
+ id = Column(Integer, primary_key=True)
+ name = Column(String(128), nullable=False)
+
+ files = relationship(
+ 'File',
+ secondary=service_files_table
+ )
+
+ package = relationship(
+ 'Package',
+ secondary=package_services_table
+ )
+
+ roles = relationship(
+ 'Role',
+ secondary=role_services_table
+ )
+
+class Setting(Base):
+ __tablename__ = 'setting'
+
+ id = Column(Integer, primary_key=True)
+ key = Column(String(128), nullable=False)
+
+ environment_id = Column(Integer, ForeignKey('environment.id'))
+
+ value = Column(Text, nullable=True)
+ function = Column(Text, nullable=True)
+
+ files = relationship(
+ 'File',
+ secondary=file_settings_table
+ )
+
+ roles = relationship(
+ 'Role',
+ secondary=role_settings_table
+ )
+
+def list_nodes_by_role(role):
+ role = db.query(Role).filter_by(name=role).first()
+
+ nodes = []
+
+ for node in role.nodes:
+ if not node.fqdn in nodes:
+ nodes.append(node.fqdn)
+
+ return nodes
+
+def list_users_by_role(role, result_attr):
+ auth = pykolab.auth
+ auth.connect()
+ user_dns = auth.search_users('nsrole', 'cn=%s,dc=klab,dc=cc' % (role), base_dn="dc=klab,dc=cc")
+
+ user_login_names = []
+
+ for user_dn in user_dns:
+ user_login_names.append(auth.get_user_attribute(None, {'dn': user_dn}, result_attr))
+
+ return user_login_names
+
+if __name__ == "__main__":
+ db = init_db()
+
+ if conf.debuglevel > 8:
+ environment = db.query(Environment).all()
+
+ if len(environment) == 0:
+ db.add(Environment(name='development'))
+ db.commit()
+
+ environment = db.query(Environment).filter_by(name='development').first()
+
+ # Add a series of nodes.
+ for node_fqdn in PRETEND_TO_HAVE_NODES:
+ 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 = environment
+
+ db.commit()
+
+ roles = {
+ 'mta-internal': {
+ 'services': [
+ 'postfix'
+ ]
+ },
+ 'mta-external': {
+ 'services': [
+ 'postfix'
+ ]
+ },
+ 'mta-backend': {
+ 'services': [
+ 'postfix'
+ ]
+ },
+ 'mta-content-filter': {
+ 'services': [
+ 'postfix'
+ ]
+ },
+ 'content-filter': {
+ 'services': [
+ 'postfix'
+ ]
+ },
+ 'imap-server-backend': {
+ 'services': [
+ 'postfix',
+ 'cyrus-imapd'
+ ]
+ },
+ 'imap-server-frontend': {
+ 'services': [
+ 'postfix',
+ 'cyrus-imapd'
+ ]
+ }
+ }
+
+ for role in roles.keys():
+ _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()
+
+ if isinstance(roles[role], dict):
+ if roles[role].has_key('services'):
+ for service in roles[role]['services']:
+ _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()
+
+ _role.services.append(_service)
+
+ db.commit()
+
+ for node_fqdn in PRETEND_TO_HAVE_NODES:
+ node = db.query(Node).filter_by(fqdn=node_fqdn).first()
+ if node.fqdn.startswith(role):
+ node.roles.append(_role)
+ db.commit()
+
+ db.commit()
+
+ # Add /etc/imapd.conf for cyrus-imapd service
+ service = db.query(Service).filter_by(name='cyrus-imapd').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ if file == None:
+ db.add(File(path='/etc/imapd.conf'))
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ service.files.append(file)
+ db.commit()
+
+ # Add /etc/cyrus.conf for cyrus-imapd service
+ service = db.query(Service).filter_by(name='cyrus-imapd').first()
+ file = db.query(File).filter_by(path='/etc/cyrus.conf').first()
+ if file == None:
+ db.add(File(path='/etc/cyrus.conf'))
+ file = db.query(File).filter_by(path='/etc/cyrus.conf').first()
+ service.files.append(file)
+ db.commit()
+
+ # Set the service for the roles.
+ service = db.query(Service).filter_by(name='cyrus-imapd').first()
+ for role in db.query(Role).filter(
+ Role.name.in_(['imap-server-backend', 'imap-server-frontend'])
+ ).all():
+
+ role.services.append(service)
+ db.commit()
+
+ # Add setting 'admins' for file '/etc/imapd.conf' (no role)
+ setting = db.query(Setting).filter_by(key='admins').first()
+ if setting == None:
+ db.add(
+ Setting(
+ key='admins',
+ function="list_users_by_role('cyrus-admin', 'uid')"
+ )
+ )
+
+ db.commit()
+ setting = db.query(Setting).filter_by(key='admins').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ file.settings.append(setting)
+ db.commit()
+
+ # Add setting 'proxyservers' for file '/etc/imapd.conf' (role:
+ # imap-server-backend)
+ setting = db.query(Setting).filter_by(key='proxyservers').first()
+ if setting == None:
+ db.add(Setting(key='proxyservers', function="list_users_by_role('cyrus-proxyserver', 'uid')"))
+ db.commit()
+ setting = db.query(Setting).filter_by(key='proxyservers').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ file.settings.append(setting)
+ db.commit()
+
+ role = db.query(Role).filter_by(name='imap-server-backend').all()
+ setting.roles = role
+ db.commit()
+
+ # Add setting 'proxy_authname' for file '/etc/imapd.conf' (role:
+ # imap-server-frontend)
+ setting = db.query(Setting).filter_by(key='proxy_authname').first()
+ if setting == None:
+ # TODO: Somehow ensure that the value here is returned by the
+ # function for proxyservers as well!
+ db.add(Setting(key='proxy_authname', value='cyrus-murder'))
+ db.commit()
+ setting = db.query(Setting).filter_by(key='proxy_authname').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ file.settings.append(setting)
+ db.commit()
+
+ role = db.query(Role).filter_by(name='imap-server-frontend').all()
+ setting.roles = role
+ db.commit()
+
+ # Add setting 'proxy_password' for file '/etc/imapd.conf' (role:
+ # imap-server-frontend)
+ setting = db.query(Setting).filter_by(key='proxy_password').first()
+ if setting == None:
+ db.add(Setting(key='proxy_password', value='V3ryS3cr3t'))
+ db.commit()
+ setting = db.query(Setting).filter_by(key='proxy_password').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ file.settings.append(setting)
+ db.commit()
+
+ role = db.query(Role).filter_by(name='imap-server-frontend').all()
+ setting.roles = role
+ db.commit()
+
+ # Add setting 'serverlist' for file '/etc/imapd.conf' (role:
+ # imap-server-frontend)
+ setting = db.query(Setting).filter_by(key='serverlist').first()
+ if setting == None:
+ db.add(Setting(key='serverlist', function="list_nodes_by_role('imap-server-backend')"))
+ db.commit()
+ setting = db.query(Setting).filter_by(key='serverlist').first()
+ file = db.query(File).filter_by(path='/etc/imapd.conf').first()
+ file.settings.append(setting)
+ db.commit()
+
+ role = db.query(Role).filter_by(name='imap-server-frontend').all()
+ setting.roles = role
+ db.commit()
+
+ for node_fqdn in PRETEND_TO_HAVE_NODES:
+ print "Running for NODE FQDN %s" % (node_fqdn)
+
+ mynode = db.query(Node).filter_by(fqdn=node_fqdn).first()
+ myroles = mynode.roles
+
+ myservices = []
+ myfiles = []
+ mysettings = []
+
+ for myrole in myroles:
+ print "%-2s %s" % ('-', myrole.name)
+ print "role(%s) settings:" % (myrole.name), ', '.join([x.key for x in myrole.settings])
+
+ for _myservice in myrole.services:
+ print "role(%s) service(%s) files:" % (myrole.name, _myservice.name), ', '.join([x.path for x in _myservice.files])
+ if not _myservice in myservices:
+ myservices.append(_myservice)
+
+ for _myfile in _myservice.files:
+ print "role(%s) service(%s) file(%s) settings:" % (myrole.name,_myservice.name,_myfile.path), ', '.join([x.key for x in _myfile.settings])
+
+ if not _myfile in myfiles:
+ myfiles.append(_myfile)
+
+ for _mysetting in _myfile.settings:
+ if not _mysetting in mysettings:
+ if not _mysetting.roles == []:
+ for _role in _mysetting.roles:
+ if _role in myroles:
+ mysettings.append(_mysetting)
+ else:
+ mysettings.append(_mysetting)
+
+ for _mysetting in myrole.settings:
+ if not _mysetting in mysettings:
+ mysettings.append(_mysetting)
+
+ print "\nMy services:"
+
+ for myservice in myservices:
+ print "%-2s %s" % ('-', myservice.name)
+
+ print "\nMy files:"
+
+ for myfile in myfiles:
+ print "%-2s %s" % ('-', myfile.path)
+ if len(myfile.settings) > 0:
+ print "%-4s %s" % ('', "Related settings:")
+
+ for _mysetting in myfile.settings:
+ if len(_mysetting.roles) > 0:
+ is_my_role_too = False
+ for _role in _mysetting.roles:
+ if _role in myroles:
+ is_my_role_too = True
+
+ if not is_my_role_too:
+ continue
+
+ print "%-4s %s" % ('', _mysetting.key)
+
+ myaugeas = Augeas(flags=Augeas.SAVE_NEWFILE)
+
+ augeas_setting_path = '/files/%s/%s' % (myfile.path,_mysetting.key)
+
+ print "Using augeas setting path: %s" % (augeas_setting_path)
+
+ current_setting = myaugeas.get(augeas_setting_path)
+
+ print "Current setting:", current_setting
+
+ if not _mysetting.function == None:
+ exec("retval = %s" % (_mysetting.function))
+ new_setting = ' '.join(retval)
+ elif not _mysetting.value == None:
+ new_setting = _mysetting.value
+ else:
+ print "ERROR: No value nor function for setting %s" % (_mysetting.key)
+ continue
+
+ print "New setting:", new_setting
+
+ myaugeas.set(augeas_setting_path, new_setting)
+
+ myaugeas.save()
+
+ utils.ask_confirmation("Does this look alright?")