summaryrefslogtreecommitdiff
path: root/pykolab/confmgmt/db.py
blob: e42bcaec0efdce66fd0fd043553bfe517d320add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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