doc/book/en/annexes/cookbook.rst
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Sun, 16 Aug 2009 20:27:28 +0200
changeset 2867 e8581a4f1bae
parent 2614 351f1fcfa53c
permissions -rw-r--r--
backport stable branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2614
351f1fcfa53c F [doc] mark code block as python source in cookbook
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
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
2614
351f1fcfa53c F [doc] mark code block as python source in cookbook
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
    17
.. sourcecode:: python
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
    18
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 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
    20
    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
    21
    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
    22
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
    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
    24
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
    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
    26
        """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
    27
        (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
    28
        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
    29
        """
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
        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
    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
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
    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
    34
        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
    35
    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
    36
        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
    37
        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
    38
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
    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
    40
        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
    41
        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
    42
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
        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
    44
                  "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
    45
        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
    46
        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
    47
        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
    48
            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
    49
                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
    50
                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
    51
            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
    52
                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
    53
                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
    54
            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
    55
            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
    56
            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
    57
            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
    58
            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
    59
                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
    60
                            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
    61
                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
    62
                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
    63
                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
    64
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.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
    66
        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
    67
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
    68
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
* 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
    70
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
    71
  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
    72
  running and your instance is configured with ``pyro-server=yes``, otherwise
2614
351f1fcfa53c F [doc] mark code block as python source in cookbook
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
    73
  you would not be able to use dbapi.
351f1fcfa53c F [doc] mark code block as python source in cookbook
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
    74
351f1fcfa53c F [doc] mark code block as python source in cookbook
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
    75
.. sourcecode:: python
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
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
    from cubicweb import dbapi
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1808
diff changeset
    78
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
    79
    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
    80
    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
    81
    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
    82
        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
    83
    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
    84
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1808
diff changeset
    85