summaryrefslogtreecommitdiff
path: root/pykolab/confmgmt/db.py
diff options
context:
space:
mode:
Diffstat (limited to 'pykolab/confmgmt/db.py')
-rw-r--r--pykolab/confmgmt/db.py140
1 files changed, 140 insertions, 0 deletions
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
+