author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Tue, 28 Jul 2009 21:26:46 +0200 | |
changeset 2539 | 0f26a76b0348 |
parent 2476 | 1294a6bdf3bf |
child 2614 | 351f1fcfa53c |
permissions | -rw-r--r-- |
358
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
1 |
.. -*- coding: utf-8 -*- |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
2 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
3 |
Cook book |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
4 |
========= |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
5 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
6 |
We gathered together some of our tricks and scripts that could make |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
7 |
life easier. |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
8 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
9 |
|
2175
16d3c37c5d28
[doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2172
diff
changeset
|
10 |
* How to import LDAP users in *CubicWeb*? |
358
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
11 |
|
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
12 |
[XXX distribute this script with cubicweb instead] |
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
13 |
|
1463
136756fff6fb
[doc] s/usefull/useful/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1444
diff
changeset
|
14 |
Here is a very useful script which enables you to import LDAP users |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2175
diff
changeset
|
15 |
into your *CubicWeb* instance by running the following: :: |
358
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
16 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
17 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
18 |
import os |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
19 |
import pwd |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
20 |
import sys |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
21 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
22 |
from logilab.common.db import get_connection |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
23 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
24 |
def getlogin(): |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
25 |
"""avoid usinng os.getlogin() because of strange tty / stdin problems |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
26 |
(man 3 getlogin) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
27 |
Another solution would be to use $LOGNAME, $USER or $USERNAME |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
28 |
""" |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
29 |
return pwd.getpwuid(os.getuid())[0] |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
30 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
31 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
32 |
try: |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
33 |
database = sys.argv[1] |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
34 |
except IndexError: |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
35 |
print 'USAGE: python ldap2system.py <database>' |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
36 |
sys.exit(1) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
37 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
38 |
if raw_input('update %s db ? [y/n]: ' % database).strip().lower().startswith('y'): |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
39 |
cnx = get_connection(user=getlogin(), database=database) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
40 |
cursor = cnx.cursor() |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
41 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
42 |
insert = ('INSERT INTO euser (creation_date, eid, modification_date, login, firstname, surname, last_login_time, upassword) ' |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
43 |
"VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, %(firstname)s, %(surname)s, %(mtime)s, './fqEz5LeZnT6');") |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
44 |
update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;" |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
45 |
cursor.execute("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'") |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
46 |
for eid, type, source, extid, mtime in cursor.fetchall(): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1198
diff
changeset
|
47 |
if type != 'CWUser': |
358
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
48 |
print "don't know what to do with entity type", type |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
49 |
continue |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
50 |
if source != 'ldapuser': |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
51 |
print "don't know what to do with source type", source |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
52 |
continue |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
53 |
ldapinfos = dict(x.strip().split('=') for x in extid.split(',')) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
54 |
login = ldapinfos['uid'] |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
55 |
firstname = ldapinfos['uid'][0].upper() |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
56 |
surname = ldapinfos['uid'][1:].capitalize() |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
57 |
if login != 'jcuissinat': |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
58 |
args = dict(eid=eid, type=type, source=source, login=login, |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
59 |
firstname=firstname, surname=surname, mtime=mtime) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
60 |
print args |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
61 |
cursor.execute(insert, args) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
62 |
cursor.execute(update, args) |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
63 |
|
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
64 |
cnx.commit() |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
65 |
cnx.close() |
e7347a1e3659
[doc] Added a cook book as advised and moved ldap import scripts in the cook book. Started a howto about configuring LDAP but it will have to be reviewed by experts.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff
changeset
|
66 |
|
1675
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
67 |
|
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
68 |
* How to load data from a script? |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
69 |
|
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
70 |
The following script aims at loading data within a script assuming pyro-nsd is |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2175
diff
changeset
|
71 |
running and your instance is configured with ``pyro-server=yes``, otherwise |
1675
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
72 |
you would not be able to use dbapi. :: |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
73 |
|
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
74 |
from cubicweb import dbapi |
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1808
diff
changeset
|
75 |
|
1675
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
76 |
cnx = dbapi.connection(database='instance-id', user='admin', password='admin') |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
77 |
cur = cnx.cursor() |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
78 |
for name in ('Personal', 'Professional', 'Computers'): |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
79 |
cur.execute('INSERT Blog B: B name %s', name) |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
80 |
cnx.commit() |
26f9d2a1a553
[doc] Add script to load data with dbapi using the setting pyro-server=yes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1463
diff
changeset
|
81 |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1808
diff
changeset
|
82 |