doc/book/en/annexes/cookbook.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 24 Jul 2009 14:36:38 +0200
changeset 2477 7baebaf44fc2
parent 2476 1294a6bdf3bf
child 2539 0f26a76b0348
permissions -rw-r--r--
application -> instance where it makes sense
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
1463
136756fff6fb [doc] s/usefull/useful/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1444
diff changeset
    12
  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
    13
  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
    14
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
    15
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
    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
    17
    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
    18
    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
    19
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
    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
    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
    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
    23
        """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
    24
        (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
    25
        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
    26
        """
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
        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
    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
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
    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
    31
        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
    32
    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
    33
        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
    34
        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
    35
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
    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
    37
        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
    38
        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
    39
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
        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
    41
                  "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
    42
        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
    43
        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
    44
        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
    45
            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
    46
                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
    47
                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
    48
            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
    49
                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
    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
            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
    52
            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
    53
            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
    54
            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
    55
            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
    56
                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
    57
                            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
    58
                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
    59
                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
    60
                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
    61
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
        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
    63
        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
    64
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
    65
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
    66
* 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
    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
  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
    69
  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
    70
  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
    71
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
    from cubicweb import dbapi
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1808
diff changeset
    73
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
    74
    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
    75
    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
    76
    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
    77
        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
    78
    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
    79
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1808
diff changeset
    80