server/serverctl.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 28 Mar 2013 19:24:46 +0100
changeset 10123 c390c444de06
parent 10077 1a28c0b0a0b4
child 10124 153c969b3a05
permissions -rw-r--r--
[server] refactor cleanup() code used by "cubicweb-ctl delete" Try to make each logical step (i.e. "delete database" and "delete user") a bit more visible and explicit. This is a preparatory work to easily plug the "delete namespace" feature.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9797
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5410
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5967
8deedfeb7846 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
    18
"""cubicweb-ctl commands and command handlers specific to the repository"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    20
__docformat__ = 'restructuredtext en'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
4718
3dc3ad02d091 avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4670
diff changeset
    22
# *ctl module should limit the number of import to be imported as quickly as
3dc3ad02d091 avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4670
diff changeset
    23
# possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
3dc3ad02d091 avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4670
diff changeset
    24
# completion). So import locally in command helpers.
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
    25
import sys
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
import os
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
    27
from contextlib import contextmanager
7457
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
    28
import logging
8233
9b9180969892 [c-c] use subprocess instead of os.system to start repository
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7815
diff changeset
    29
import subprocess
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
6867
f691757792f9 [c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6863
diff changeset
    31
from logilab.common import nullobject
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
    32
from logilab.common.configuration import Configuration, merge_options
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
    33
from logilab.common.shellutils import ASK, generate_password
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
    35
from logilab.database import get_db_helper, get_connection
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
    36
2790
968108e16066 move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2711
diff changeset
    37
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
968108e16066 move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2711
diff changeset
    38
from cubicweb.toolsutils import Command, CommandHandler, underline_title
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
    39
from cubicweb.cwctl import CWCTL, check_options_consistency, ConfigureInstanceCommand
2105
92ea410806fe refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1980
diff changeset
    40
from cubicweb.server import SOURCE_TYPES
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
    41
from cubicweb.server.repository import Repository
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
    42
from cubicweb.server.serverconfig import (
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
    43
    USER_OPTIONS, ServerConfiguration, SourceConfiguration,
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
    44
    ask_source_config, generate_source_config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
# utility functions ###########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    48
def source_cnx(source, dbname=None, special_privs=False, interactive=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    """open and return a connection to the system database defined in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    given server.serverconfig
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    from getpass import getpass
4218
deb2dcffb301 [cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 4212
diff changeset
    53
    dbhost = source.get('db-host')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    if dbname is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        dbname = source['db-name']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    driver = source['db-driver']
6060
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    57
    dbhelper = get_db_helper(driver)
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    58
    if interactive:
5754
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
    59
        print '-> connecting to %s database' % driver,
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
    60
        if dbhost:
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
    61
            print '%s@%s' % (dbname, dbhost),
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
    62
        else:
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
    63
            print dbname,
6060
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    64
    if dbhelper.users_support:
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    65
        if not interactive or (not special_privs and source.get('db-user')):
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    66
            user = source.get('db-user', os.environ.get('USER', ''))
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    67
            if interactive:
6060
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    68
                print 'as', user
6860
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
    69
            password = source.get('db-password')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        else:
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
    71
            print
6060
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    72
            if special_privs:
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    73
                print 'WARNING'
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    74
                print ('the user will need the following special access rights '
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    75
                       'on the database:')
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    76
                print special_privs
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    77
                print
6072
9d83b214066a [db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6060
diff changeset
    78
            default_user = source.get('db-user', os.environ.get('USER', ''))
9d83b214066a [db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6060
diff changeset
    79
            user = raw_input('Connect as user ? [%r]: ' % default_user)
6860
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
    80
            user = user.strip() or default_user
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
    81
            if user == source.get('db-user'):
6939
8fa55cf2a8cb [c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6867
diff changeset
    82
                password = source.get('db-password')
6072
9d83b214066a [db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6060
diff changeset
    83
            else:
9d83b214066a [db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6060
diff changeset
    84
                password = getpass('password: ')
6060
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    85
    else:
353bec342e2a [db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5990
diff changeset
    86
        user = password = None
4180
9fb0d06926cc more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4177
diff changeset
    87
    extra_args = source.get('db-extra-arguments')
9fb0d06926cc more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4177
diff changeset
    88
    extra = extra_args and {'extra_args': extra_args} or {}
4837
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    89
    cnx = get_connection(driver, dbhost, dbname, user, password=password,
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    90
                         port=source.get('db-port'),
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    91
                         **extra)
4845
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    92
    try:
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    93
        cnx.logged_user = user
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    94
    except AttributeError:
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    95
        # C object, __slots__
4848
41f84eea63c9 rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4845
diff changeset
    96
        from logilab.database import _SimpleConnectionWrapper
4845
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    97
        cnx = _SimpleConnectionWrapper(cnx)
dc351b96f596 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831 4837
diff changeset
    98
        cnx.logged_user = user
4837
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    99
    return cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
136
ff51a18c66a3 ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   101
def system_source_cnx(source, dbms_system_base=False,
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   102
                      special_privs='CREATE/DROP DATABASE', interactive=True):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   103
    """shortcut to get a connextion to the instance system database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    defined in the given config. If <dbms_system_base> is True,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    connect to the dbms system database instead (for task such as
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   106
    create/drop the instance database)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    if dbms_system_base:
4831
c5aec27c1bf7 [repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   109
        system_db = get_db_helper(source['db-driver']).system_database()
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   110
        return source_cnx(source, system_db, special_privs=special_privs,
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   111
                          interactive=interactive)
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   112
    return source_cnx(source, special_privs=special_privs,
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   113
                      interactive=interactive)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   115
def _db_sys_cnx(source, special_privs, interactive=True):
6860
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
   116
    """return a connection on the RDMS system table (to create/drop a user or a
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
   117
    database)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    """
2417
18a14c23413c should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2107
diff changeset
   119
    import logilab.common as lgp
18a14c23413c should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2107
diff changeset
   120
    lgp.USE_MX_DATETIME = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    driver = source['db-driver']
4831
c5aec27c1bf7 [repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   122
    helper = get_db_helper(driver)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
    # connect on the dbms system base to create our base
6860
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
   124
    cnx = system_source_cnx(source, True, special_privs=special_privs,
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   125
                            interactive=interactive)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    # disable autocommit (isolation_level(1)) because DROP and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
    # CREATE DATABASE can't be executed in a transaction
7577
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7569
diff changeset
   128
    set_isolation_level = getattr(cnx, 'set_isolation_level', None)
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7569
diff changeset
   129
    if set_isolation_level is not None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        # set_isolation_level() is psycopg specific
7577
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7569
diff changeset
   131
        set_isolation_level(0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
    return cnx
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   133
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
def repo_cnx(config):
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   135
    """return a in-memory repository and a repoapi connection to it"""
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   136
    from cubicweb import repoapi
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
    from cubicweb.server.utils import manager_userpasswd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
    try:
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   139
        login = config.default_admin_config['login']
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   140
        pwd = config.default_admin_config['password']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        login, pwd = manager_userpasswd()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    while True:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
        try:
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   145
            repo = repoapi.get_repository(config=config)
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   146
            cnx = repoapi.connect(repo, login, password=pwd)
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   147
            return repo, cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        except AuthenticationError:
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2107
diff changeset
   149
            print '-> Error: wrong user/password.'
1682
36bd5cba09de must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1469
diff changeset
   150
            # reset cubes else we'll have an assertion error on next retry
36bd5cba09de must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1469
diff changeset
   151
            config._cubes = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        login, pwd = manager_userpasswd()
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   153
3904
dc7d315165fc default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3893
diff changeset
   154
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
# repository specific command handlers ########################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
class RepositoryCreateHandler(CommandHandler):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
    cmdname = 'create'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
    cfgname = 'repository'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   161
    def bootstrap(self, cubes, automatic=False, inputlevel=0):
5967
8deedfeb7846 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   162
        """create an instance by copying files from the given cube and by asking
8deedfeb7846 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   163
        information necessary to build required configuration files
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
        config = self.config
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   166
        if not automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   167
            print underline_title('Configuring the repository')
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   168
            config.input_config('email', inputlevel)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   169
            # ask for pyro configuration if pyro is activated and we're not
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   170
            # using a all-in-one config, in which case this is done by the web
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   171
            # side command handler
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   172
            if config.pyro_enabled() and config.name != 'all-in-one':
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   173
                config.input_config('pyro', inputlevel)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   174
            print '\n'+underline_title('Configuring the sources')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        sourcesfile = config.sources_file()
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   176
        # hack to make Method('default_instance_id') usable in db option defs
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   177
        # (in native.py)
6299
51a9494b5efb [sqlite] quick hacks to fix #1251873 and #1251874
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6184
diff changeset
   178
        sconfig = SourceConfiguration(config,
3904
dc7d315165fc default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3893
diff changeset
   179
                                      options=SOURCE_TYPES['native'].options)
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   180
        if not automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   181
            sconfig.input_config(inputlevel=inputlevel)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   182
            print
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        sourcescfg = {'system': sconfig}
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   184
        if automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   185
            # XXX modify a copy
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   186
            password = generate_password()
7155
4bab50b02927 [cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7140
diff changeset
   187
            print '-> set administrator account to admin / %s' % password
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   188
            USER_OPTIONS[1][1]['default'] = password
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   189
            sconfig = Configuration(options=USER_OPTIONS)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   190
        else:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   191
            sconfig = Configuration(options=USER_OPTIONS)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   192
            sconfig.input_config(inputlevel=inputlevel)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        sourcescfg['admin'] = sconfig
2105
92ea410806fe refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1980
diff changeset
   194
        config.write_sources_file(sourcescfg)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        # remember selected cubes for later initialization of the database
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        config.write_bootstrap_cubes_file(cubes)
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   197
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   198
    def postcreate(self, automatic=False, inputlevel=0):
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   199
        if automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   200
            CWCTL.run(['db-create', '--automatic', self.config.appid])
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   201
        elif ASK.confirm('Run db-create to create the system database ?'):
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   202
            CWCTL.run(['db-create', '--config-level', str(inputlevel),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   203
                       self.config.appid])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        else:
2395
e3093fc12a00 [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2394
diff changeset
   205
            print ('-> nevermind, you can do it later with '
e3093fc12a00 [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2394
diff changeset
   206
                   '"cubicweb-ctl db-create %s".' % self.config.appid)
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   207
6867
f691757792f9 [c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6863
diff changeset
   208
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   209
@contextmanager
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   210
def db_sys_transaction(source, privilege):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   211
    """Open a transaction to the system database"""
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   212
    cnx = _db_sys_cnx(source, privilege)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   213
    cursor = cnx.cursor()
6867
f691757792f9 [c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6863
diff changeset
   214
    try:
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   215
        yield cursor
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   216
    except:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   217
        cnx.rollback()
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   218
        cnx.close()
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   219
        raise
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   220
    else:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   221
        cnx.commit()
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   222
        cnx.close()
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   223
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
class RepositoryDeleteHandler(CommandHandler):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
    cmdname = 'delete'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    cfgname = 'repository'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   229
    def _drop_database(self, source):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   230
        dbname = source['db-name']
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   231
        if source['db-driver'] == 'sqlite':
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   232
            print 'deleting database file %(db-name)s' % source
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   233
            os.unlink(source['db-name'])
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   234
            print '-> database %(db-name)s dropped.' % source
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   235
        else:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   236
            helper = get_db_helper(source['db-driver'])
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   237
            with db_sys_transaction(source, privilege='DROP DATABASE') as cursor:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   238
                print 'dropping database %(db-name)s' % source
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   239
                cursor.execute('DROP DATABASE "%(db-name)s"' % source)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   240
                print '-> database %(db-name)s dropped.' % source
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   241
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   242
    def _drop_user(self, source):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   243
        user = source['db-user'] or None
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   244
        if user is not None:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   245
            with db_sys_transaction(source, privilege='DROP USER') as cursor:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   246
                print 'dropping user %s' % user
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   247
                cursor.execute('DROP USER %s' % user)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   248
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   249
    def _cleanup_steps(self, source):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   250
        # 1/ delete database
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   251
        yield ('Delete database "%(db-name)s"' % source,
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   252
               self._drop_database, True)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   253
        # 2/ delete user
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   254
        helper = get_db_helper(source['db-driver'])
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   255
        if source['db-user'] and helper.users_support:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   256
            # XXX should check we are not connected as user
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   257
            yield ('Delete user "%(db-user)s"' % source,
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   258
                   self._drop_user, False)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   259
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    def cleanup(self):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   261
        """remove instance's configuration and database"""
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   262
        source = self.config.system_source_config
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   263
        for msg, step, default in self._cleanup_steps(source):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   264
            if ASK.confirm(msg, default_is_yes=default):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   265
                try:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   266
                    step(source)
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   267
                except Exception as exc:
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   268
                    print 'ERROR', exc
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   269
                    if ASK.confirm('An error occurred. Continue anyway?',
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   270
                                   default_is_yes=False):
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   271
                        continue
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   272
                    raise ExecutionError(str(exc))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   274
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
class RepositoryStartHandler(CommandHandler):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
    cmdname = 'start'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
    cfgname = 'repository'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   279
    def start_server(self, config):
8233
9b9180969892 [c-c] use subprocess instead of os.system to start repository
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7815
diff changeset
   280
        command = ['cubicweb-ctl', 'start-repository']
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   281
        if config.debugmode:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
            command.append('--debug')
8233
9b9180969892 [c-c] use subprocess instead of os.system to start repository
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7815
diff changeset
   283
        command.append('--loglevel')
9b9180969892 [c-c] use subprocess instead of os.system to start repository
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7815
diff changeset
   284
        command.append(config['log-threshold'].lower())
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   285
        command.append(config.appid)
8233
9b9180969892 [c-c] use subprocess instead of os.system to start repository
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7815
diff changeset
   286
        subprocess.call(command)
6778
7dd4835d5198 properly tell we've started an instance (close #1391262)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6700
diff changeset
   287
        return 1
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   288
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
class RepositoryStopHandler(CommandHandler):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
    cmdname = 'stop'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
    cfgname = 'repository'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
    def poststop(self):
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   295
        """if pyro is enabled, ensure the repository is correctly unregistered
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
        if self.config.pyro_enabled():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
            from cubicweb.server.repository import pyro_unregister
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
            pyro_unregister(self.config)
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   300
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
# repository specific commands ################################################
3954
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   303
5754
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   304
def createdb(helper, source, dbcnx, cursor, **kwargs):
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   305
    if dbcnx.logged_user != source['db-user']:
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   306
        helper.create_database(cursor, source['db-name'], source['db-user'],
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   307
                               source['db-encoding'], **kwargs)
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   308
    else:
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   309
        helper.create_database(cursor, source['db-name'],
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   310
                               dbencoding=source['db-encoding'], **kwargs)
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   311
10123
c390c444de06 [server] refactor cleanup() code used by "cubicweb-ctl delete"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 10077
diff changeset
   312
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   313
class CreateInstanceDBCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   314
    """Create the system database of an instance (run after 'create').
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   315
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
    You will be prompted for a login / password to use to connect to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
    the system database.  The given user should have almost all rights
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   318
    on the database (ie a super user on the DBMS allowed to create
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
    database, users, languages...).
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   321
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   322
      the identifier of the instance to initialize.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
    name = 'db-create'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   325
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   326
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
    options = (
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   328
        ('automatic',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   329
         {'short': 'a', 'action' : 'store_true',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   330
          'default': False,
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   331
          'help': 'automatic mode: never ask and use default answer to every '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   332
          'question. this may require that your login match a database super '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   333
          'user (allowed to create database & all).',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   334
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   335
        ('config-level',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   336
         {'short': 'l', 'type' : 'int', 'metavar': '<level>',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   337
          'default': 0,
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   338
          'help': 'configuration level (0..2): 0 will ask for essential '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   339
          'configuration parameters only while 2 will ask for all parameters',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   340
          }),
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   341
        ('create-db',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   342
         {'short': 'c', 'type': 'yn', 'metavar': '<y or n>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
          'default': True,
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   344
          'help': 'create the database (yes by default)'
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7061
diff changeset
   345
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
        )
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   347
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
        """run the command with its specific arguments"""
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   350
        check_options_consistency(self.config)
2969
d95f23a0fc3b F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2851
diff changeset
   351
        automatic = self.get('automatic')
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   352
        appid = args.pop()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
        config = ServerConfiguration.config_for(appid)
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   354
        source = config.system_source_config
4197
5446c74189b6 quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4180
diff changeset
   355
        dbname = source['db-name']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
        driver = source['db-driver']
4831
c5aec27c1bf7 [repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   357
        helper = get_db_helper(driver)
4197
5446c74189b6 quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4180
diff changeset
   358
        if driver == 'sqlite':
5585
e1cbf6b304ea [sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5553
diff changeset
   359
            if os.path.exists(dbname) and (
e1cbf6b304ea [sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5553
diff changeset
   360
                automatic or
e1cbf6b304ea [sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5553
diff changeset
   361
                ASK.confirm('Database %s already exists. Drop it?' % dbname)):
4197
5446c74189b6 quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4180
diff changeset
   362
                os.unlink(dbname)
5754
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   363
        elif self.config.create_db:
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   364
            print '\n'+underline_title('Creating the system database')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
            # connect on the dbms system base to create our base
6939
8fa55cf2a8cb [c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6867
diff changeset
   366
            dbcnx = _db_sys_cnx(source, 'CREATE/DROP DATABASE and / or USER',
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   367
                                interactive=not automatic)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
            cursor = dbcnx.cursor()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
                if helper.users_support:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
                    user = source['db-user']
2969
d95f23a0fc3b F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2851
diff changeset
   372
                    if not helper.user_exists(cursor, user) and (automatic or \
d95f23a0fc3b F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2851
diff changeset
   373
                           ASK.confirm('Create db user %s ?' % user, default_is_yes=False)):
6939
8fa55cf2a8cb [c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6867
diff changeset
   374
                        helper.create_user(source['db-user'], source.get('db-password'))
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2107
diff changeset
   375
                        print '-> user %s created.' % user
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
                if dbname in helper.list_databases(cursor):
2969
d95f23a0fc3b F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2851
diff changeset
   377
                    if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
7564
1d64c8d33156 [server] "overrule" case insensitivity of database name (closes: #611294)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7342
diff changeset
   378
                        cursor.execute('DROP DATABASE "%s"' % dbname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
                    else:
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   380
                        print ('you may want to run "cubicweb-ctl db-init '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   381
                               '--drop %s" manually to continue.' % config.appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
                        return
5754
51179e0bb250 [test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5696
diff changeset
   383
                createdb(helper, source, dbcnx, cursor)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
                dbcnx.commit()
4197
5446c74189b6 quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4180
diff changeset
   385
                print '-> database %s created.' % dbname
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7764
diff changeset
   386
            except BaseException:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
                dbcnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
                raise
6860
a0dbf6135c72 [db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6779
diff changeset
   389
        cnx = system_source_cnx(source, special_privs='CREATE LANGUAGE',
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   390
                                interactive=not automatic)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
        cursor = cnx.cursor()
4886
18a1735c283c [db creation] fix some bugs introduced by the migration to logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4848
diff changeset
   392
        helper.init_fti_extensions(cursor)
7479
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   393
        cnx.commit()
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   394
        # postgres specific stuff
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        if driver == 'postgres':
7479
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   396
            # install plpythonu/plpgsql languages
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   397
            langs = ('plpythonu', 'plpgsql')
3115
29262ba01464 minimal steps to have cw running on windows
Aurélien Campéas
parents: 2969
diff changeset
   398
            for extlang in langs:
7479
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   399
                if automatic or ASK.confirm('Create language %s ?' % extlang):
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   400
                    try:
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   401
                        helper.create_language(cursor, extlang)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   402
                    except Exception as exc:
7479
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   403
                        print '-> ERROR:', exc
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   404
                        print '-> could not create language %s, some stored procedures might be unusable' % extlang
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   405
                        cnx.rollback()
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   406
                    else:
b0603fc4ed6d [sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7457
diff changeset
   407
                        cnx.commit()
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   408
        print '-> database for instance %s created and necessary extensions installed.' % appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
        print
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   410
        if automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   411
            CWCTL.run(['db-init', '--automatic', '--config-level', '0',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   412
                       config.appid])
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   413
        elif ASK.confirm('Run db-init to initialize the system database ?'):
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   414
            CWCTL.run(['db-init', '--config-level',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   415
                       str(self.config.config_level), config.appid])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
        else:
2395
e3093fc12a00 [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2394
diff changeset
   417
            print ('-> nevermind, you can do it later with '
3893
74f216b69628 fix buglet
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3835
diff changeset
   418
                   '"cubicweb-ctl db-init %s".' % config.appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   420
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   421
class InitInstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   422
    """Initialize the system database of an instance (run after 'db-create').
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   423
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   424
    Notice this will be done using user specified in the sources files, so this
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   425
    user should have the create tables grant permissions on the database.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   427
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   428
      the identifier of the instance to initialize.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
    name = 'db-init'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   431
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   432
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
    options = (
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   434
        ('automatic',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   435
         {'short': 'a', 'action' : 'store_true',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   436
          'default': False,
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   437
          'help': 'automatic mode: never ask and use default answer to every '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   438
          'question.',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   439
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   440
        ('config-level',
9922
fc02ba8654a7 [serverctl] fix default value for db-init config-level
Julien Cristau <julien.cristau@logilab.fr>
parents: 9303
diff changeset
   441
         {'short': 'l', 'type': 'int', 'default': 0,
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   442
          'help': 'level threshold for questions asked when configuring '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   443
          'another source'
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   444
          }),
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   445
        ('drop',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   446
         {'short': 'd', 'action': 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
          'default': False,
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   448
          'help': 'insert drop statements to remove previously existant '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   449
          'tables, indexes... (no by default)'
7030
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   450
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
    def run(self, args):
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   454
        check_options_consistency(self.config)
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   455
        print '\n'+underline_title('Initializing the system database')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
        from cubicweb.server import init_repository
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   457
        appid = args[0]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
        config = ServerConfiguration.config_for(appid)
3954
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   459
        try:
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   460
            system = config.system_source_config
8669
62213a34726e [db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
   461
            extra_args = system.get('db-extra-arguments')
4180
9fb0d06926cc more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4177
diff changeset
   462
            extra = extra_args and {'extra_args': extra_args} or {}
3954
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   463
            get_connection(
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   464
                system['db-driver'], database=system['db-name'],
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   465
                host=system.get('db-host'), port=system.get('db-port'),
8434
39c5bb4dcc59 [ldapfeed] do not crash on ldap user deletion + pull + already deactivated users, cleanups (closes #2392933)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8376
diff changeset
   466
                user=system.get('db-user') or '', password=system.get('db-password') or '',
4180
9fb0d06926cc more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4177
diff changeset
   467
                **extra)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   468
        except Exception as ex:
3954
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   469
            raise ConfigurationError(
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   470
                'You seem to have provided wrong connection information in '\
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   471
                'the %s file. Resolve this first (error: %s).'
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   472
                % (config.sources_file(), str(ex).strip()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
        init_repository(config, drop=self.config.drop)
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   474
        if not self.config.automatic:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   475
            while ASK.confirm('Enter another source ?', default_is_yes=False):
9759
846bc9227394 [serverctl] rename 'add-source' to 'source-add' for consistency's sake (closes #3732169)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9577
diff changeset
   476
                CWCTL.run(['source-add', '--config-level',
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7121
diff changeset
   477
                           str(self.config.config_level), config.appid])
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   478
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   479
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   480
class AddSourceCommand(Command):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   481
    """Add a data source to an instance.
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   482
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   483
    <instance>
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   484
      the identifier of the instance to initialize.
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   485
    """
9759
846bc9227394 [serverctl] rename 'add-source' to 'source-add' for consistency's sake (closes #3732169)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9577
diff changeset
   486
    name = 'source-add'
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   487
    arguments = '<instance>'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   488
    min_args = max_args = 1
7030
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   489
    options = (
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   490
        ('config-level',
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   491
         {'short': 'l', 'type': 'int', 'default': 1,
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   492
          'help': 'level threshold for questions asked when configuring another source'
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   493
          }),
d48c6b0158b9 allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6939
diff changeset
   494
        )
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   495
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   496
    def run(self, args):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   497
        appid = args[0]
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   498
        config = ServerConfiguration.config_for(appid)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   499
        repo, cnx = repo_cnx(config)
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   500
        with cnx:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   501
            used = set(n for n, in cnx.execute('Any SN WHERE S is CWSource, S name SN'))
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   502
            cubes = repo.get_cubes()
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   503
            while True:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   504
                type = raw_input('source type (%s): '
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   505
                                    % ', '.join(sorted(SOURCE_TYPES)))
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   506
                if type not in SOURCE_TYPES:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   507
                    print '-> unknown source type, use one of the available types.'
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   508
                    continue
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   509
                sourcemodule = SOURCE_TYPES[type].module
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   510
                if not sourcemodule.startswith('cubicweb.'):
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   511
                    # module names look like cubes.mycube.themodule
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   512
                    sourcecube = SOURCE_TYPES[type].module.split('.', 2)[1]
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   513
                    # if the source adapter is coming from an external component,
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   514
                    # ensure it's specified in used cubes
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   515
                    if not sourcecube in cubes:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   516
                        print ('-> this source type require the %s cube which is '
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   517
                               'not used by the instance.')
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   518
                        continue
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   519
                break
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   520
            while True:
10009
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   521
                parser = raw_input('parser type (%s): '
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   522
                                    % ', '.join(sorted(repo.vreg['parsers'])))
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   523
                if parser in repo.vreg['parsers']:
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   524
                    break
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   525
                print '-> unknown parser identifier, use one of the available types.'
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   526
            while True:
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   527
                sourceuri = raw_input('source identifier (a unique name used to '
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   528
                                      'tell sources apart): ').strip()
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   529
                if not sourceuri:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   530
                    print '-> mandatory.'
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
   531
                else:
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   532
                    sourceuri = unicode(sourceuri, sys.stdin.encoding)
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   533
                    if sourceuri in used:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   534
                        print '-> uri already used, choose another one.'
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   535
                    else:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   536
                        break
10010
3dd66bdbfe8e [serverctl] Ask for URL when adding a new source (closes #3824868)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 10009
diff changeset
   537
            url = raw_input('source URL (leave empty for none): ').strip()
3dd66bdbfe8e [serverctl] Ask for URL when adding a new source (closes #3824868)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 10009
diff changeset
   538
            url = unicode(url) if url else None
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   539
            # XXX configurable inputlevel
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   540
            sconfig = ask_source_config(config, type, inputlevel=self.config.config_level)
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   541
            cfgstr = unicode(generate_source_config(sconfig), sys.stdin.encoding)
10009
560747cd7774 [serverctl] Ask for parser type when adding a new source (closes #3484231)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 9984
diff changeset
   542
            cnx.create_entity('CWSource', name=sourceuri, type=unicode(type),
10010
3dd66bdbfe8e [serverctl] Ask for URL when adding a new source (closes #3824868)
Paul Tonelli <paul.tonelli@logilab.fr>
parents: 10009
diff changeset
   543
                              config=cfgstr, parser=unicode(parser), url=unicode(url))
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
   544
            cnx.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   547
class GrantUserOnInstanceCommand(Command):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
    """Grant a database user on a repository system database.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   549
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   550
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   551
      the identifier of the instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
    <user>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   553
      the database's user requiring grant access
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
    name = 'db-grant-user'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   556
    arguments = '<instance> <user>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   557
    min_args = max_args = 2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   559
        ('set-owner',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   560
         {'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
          'help': 'Set the user as tables owner if yes (no by default).'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   563
         ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   564
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
        """run the command with its specific arguments"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
        from cubicweb.server.sqlutils import sqlexec, sqlgrants
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   568
        appid, user = args
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   569
        config = ServerConfiguration.config_for(appid)
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9457
diff changeset
   570
        source = config.system_source_config
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   571
        set_owner = self.config.set_owner
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
        cnx = system_source_cnx(source, special_privs='GRANT')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
        cursor = cnx.cursor()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
        schema = config.load_schema()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   575
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
            sqlexec(sqlgrants(schema, source['db-driver'], user,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   577
                              set_owner=set_owner), cursor)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   578
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
            cnx.rollback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
            import traceback
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
            traceback.print_exc()
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6072
diff changeset
   582
            print '-> an error occurred:', ex
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
            cnx.commit()
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   585
            print '-> rights granted to %s on instance %s.' % (appid, user)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
3954
56a8b14d68aa properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3904
diff changeset
   587
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   588
class ResetAdminPasswordCommand(Command):
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   589
    """Reset the administrator password.
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   590
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   591
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   592
      the identifier of the instance
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   593
    """
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   594
    name = 'reset-admin-pwd'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   595
    arguments = '<instance>'
7669
b96c41c6de4d [cwctl] set min/max args for reset admin password command. Closes #1832986
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7595
diff changeset
   596
    min_args = max_args = 1
7594
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   597
    options = (
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   598
        ('password',
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   599
         {'short': 'p', 'type' : 'string', 'metavar' : '<new-password>',
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   600
          'default' : None,
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   601
          'help': 'Use this password instead of prompt for one.\n'
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   602
                  '/!\ THIS IS AN INSECURE PRACTICE /!\ \n'
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   603
                  'the password will appear in shell history'}
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   604
         ),
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   605
        )
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   606
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   607
    def run(self, args):
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   608
        """run the command with its specific arguments"""
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   609
        from cubicweb.server.utils import crypt_password, manager_userpasswd
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   610
        appid = args[0]
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   611
        config = ServerConfiguration.config_for(appid)
2105
92ea410806fe refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1980
diff changeset
   612
        sourcescfg = config.read_sources_file()
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   613
        try:
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   614
            adminlogin = sourcescfg['admin']['login']
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   615
        except KeyError:
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2107
diff changeset
   616
            print '-> Error: could not get cubicweb administrator login.'
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   617
            sys.exit(1)
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   618
        cnx = source_cnx(sourcescfg['system'])
6379
3f67f7ea5632 [R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6351
diff changeset
   619
        driver = sourcescfg['system']['db-driver']
3f67f7ea5632 [R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6351
diff changeset
   620
        dbhelper = get_db_helper(driver)
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   621
        cursor = cnx.cursor()
4670
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   622
        # check admin exists
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   623
        cursor.execute("SELECT * FROM cw_CWUser WHERE cw_login=%(l)s",
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   624
                       {'l': adminlogin})
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   625
        if not cursor.fetchall():
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   626
            print ("-> error: admin user %r specified in sources doesn't exist "
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   627
                   "in the database" % adminlogin)
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   628
            print "   fix your sources file before running this command"
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   629
            cnx.close()
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   630
            sys.exit(1)
7594
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   631
        if self.config.password is None:
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   632
            # ask for a new password
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   633
            msg = 'new password for %s' % adminlogin
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   634
            _, pwd = manager_userpasswd(adminlogin, confirm=True, passwdmsg=msg)
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   635
        else:
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   636
            pwd = self.config.password
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   637
        try:
4670
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   638
            cursor.execute("UPDATE cw_CWUser SET cw_upassword=%(p)s WHERE cw_login=%(l)s",
7594
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   639
                           {'p': dbhelper.binary_value(crypt_password(pwd)), 'l': adminlogin})
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   640
            sconfig = Configuration(options=USER_OPTIONS)
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   641
            sconfig['login'] = adminlogin
7594
d177c0755b10 [cwc shell] add ``--password`` option to ``reset-admin-pwd`` (close #1794866)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7564
diff changeset
   642
            sconfig['password'] = pwd
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   643
            sourcescfg['admin'] = sconfig
2105
92ea410806fe refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1980
diff changeset
   644
            config.write_sources_file(sourcescfg)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   645
        except Exception as ex:
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   646
            cnx.rollback()
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   647
            import traceback
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   648
            traceback.print_exc()
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6072
diff changeset
   649
            print '-> an error occurred:', ex
1912
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   650
        else:
2b9432262240 [server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1682
diff changeset
   651
            cnx.commit()
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2107
diff changeset
   652
            print '-> password reset, sources file regenerated.'
4670
48ba6f820fae fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   653
        cnx.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   654
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   655
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   656
class StartRepositoryCommand(Command):
6700
accb7290a31c fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6427
diff changeset
   657
    """Start a CubicWeb RQL server for a given instance.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   658
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   659
    The server will be remotely accessible through pyro or ZMQ
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   660
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   661
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   662
      the identifier of the instance to initialize.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   663
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   664
    name = 'start-repository'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   665
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   666
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   668
        ('debug',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   669
         {'short': 'D', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   670
          'help': 'start server in debug mode.'}),
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   671
        ('loglevel',
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   672
         {'short': 'l', 'type' : 'choice', 'metavar': '<log level>',
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   673
          'default': None, 'choices': ('debug', 'info', 'warning', 'error'),
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   674
          'help': 'debug if -D is set, error otherwise',
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   675
          }),
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   676
        ('address',
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   677
         {'short': 'a', 'type': 'string', 'metavar': '<protocol>://<host>:<port>',
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   678
          'default': '',
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   679
          'help': ('specify a ZMQ URI on which to bind, or use "pyro://"'
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   680
                   'to create a pyro-based repository'),
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   681
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   684
    def create_repo(self, config):
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   685
        address = self['address']
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   686
        if not address:
8376
f42dcb798142 [serverctl] avoid a crash if zmq-address is not defined/empty (config.get is a false friend wrt dict behaviour)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8350
diff changeset
   687
            address = config.get('zmq-repository-address') or 'pyro://'
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   688
        if address.startswith('pyro://'):
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   689
            from cubicweb.server.server import RepositoryServer
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   690
            return RepositoryServer(config), config['host']
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   691
        else:
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   692
            from cubicweb.server.utils import TasksManager
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   693
            from cubicweb.server.cwzmq import ZMQRepositoryServer
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   694
            repo = Repository(config, TasksManager())
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   695
            return ZMQRepositoryServer(repo), address
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   696
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   697
    def run(self, args):
7764
c709f6c457ff Drop privileges properly (closes #1910561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 7669
diff changeset
   698
        from logilab.common.daemon import daemonize, setugid
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   699
        from cubicweb.cwctl import init_cmdline_log_threshold
9890
57e2cffa6329 Add warning messages when enabling remote pickle-based repository access
Julien Cristau <julien.cristau@logilab.fr>
parents: 9797
diff changeset
   700
        print 'WARNING: Standalone repository with pyro or zmq access is deprecated'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   701
        appid = args[0]
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   702
        debug = self['debug']
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   703
        if sys.platform == 'win32' and not debug:
7457
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
   704
            logger = logging.getLogger('cubicweb.ctl')
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   705
            logger.info('Forcing debug mode on win32 platform')
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   706
            debug = True
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   707
        config = ServerConfiguration.config_for(appid, debugmode=debug)
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5754
diff changeset
   708
        init_cmdline_log_threshold(config, self['loglevel'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
        # create the server
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   710
        server, address = self.create_repo(config)
2711
7aee3bd7a704 ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2682
diff changeset
   711
        # ensure the directory where the pid-file should be set exists (for
7aee3bd7a704 ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2682
diff changeset
   712
        # instance /var/run/cubicweb may be deleted on computer restart)
5692
59ceed788bdb [repo server] fix startup bug, lgc.daemon.daemonize return 1 for the parent process, not -1
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5586
diff changeset
   713
        pidfile = config['pid-file']
2711
7aee3bd7a704 ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2682
diff changeset
   714
        piddir = os.path.dirname(pidfile)
5692
59ceed788bdb [repo server] fix startup bug, lgc.daemon.daemonize return 1 for the parent process, not -1
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5586
diff changeset
   715
        # go ! (don't daemonize in debug mode)
2711
7aee3bd7a704 ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2682
diff changeset
   716
        if not os.path.exists(piddir):
7aee3bd7a704 ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2682
diff changeset
   717
            os.makedirs(piddir)
6779
accf5978a440 [daemon] make process umask configurable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6778
diff changeset
   718
        if not debug and daemonize(pidfile, umask=config['umask']):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
        uid = config['uid']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
        if uid is not None:
7764
c709f6c457ff Drop privileges properly (closes #1910561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 7669
diff changeset
   722
            setugid(uid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   723
        server.install_sig_handlers()
8350
e1c05bf6fdeb [zmq] Implement a ZMQ-based Repository (closes #2290125)
David Douard <david.douard@logilab.fr>
parents: 8233
diff changeset
   724
        server.connect(address)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   725
        server.run()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   726
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   728
def _remote_dump(host, appid, output, sudo=False):
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   729
    # XXX generate unique/portable file name
2837
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   730
    from datetime import date
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   731
    filename = '%s-%s.tgz' % (appid, date.today().strftime('%Y-%m-%d'))
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   732
    dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
    if sudo:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
        dmpcmd = 'sudo %s' % (dmpcmd)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
    dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
    print dmpcmd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
    if os.system(dmpcmd):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
        raise ExecutionError('Error while dumping the database')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
    if output is None:
2837
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   740
        output = filename
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   741
    cmd = 'scp %s:/tmp/%s %s' % (host, filename, output)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   742
    print cmd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   743
    if os.system(cmd):
2837
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   744
        raise ExecutionError('Error while retrieving the dump at /tmp/%s' % filename)
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   745
    rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
    print rmcmd
2615
1ea41b7c0836 F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2507
diff changeset
   747
    if os.system(rmcmd) and not ASK.confirm(
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6072
diff changeset
   748
        'An error occurred while deleting remote dump at /tmp/%s. '
2837
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   749
        'Continue anyway?' % filename):
65df5df190a5 B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2711
diff changeset
   750
        raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   752
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   753
def _local_dump(appid, output, format='native'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
    config = ServerConfiguration.config_for(appid)
5043
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5033
diff changeset
   755
    config.quick_start = True
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5033
diff changeset
   756
    mih = config.migration_handler(connect=False, verbosity=1)
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   757
    mih.backup_database(output, askconfirm=False, format=format)
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   758
    mih.shutdown()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   759
9457
d5ed6efd6448 [c-c restore] drop useless restore-all option, and related systemonly argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9303
diff changeset
   760
def _local_restore(appid, backupfile, drop, format='native'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
    config = ServerConfiguration.config_for(appid)
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   762
    config.verbosity = 1 # else we won't be asked for confirmation on problems
5043
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5033
diff changeset
   763
    config.quick_start = True
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5033
diff changeset
   764
    mih = config.migration_handler(connect=False, verbosity=1)
9457
d5ed6efd6448 [c-c restore] drop useless restore-all option, and related systemonly argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9303
diff changeset
   765
    mih.restore_database(backupfile, drop, askconfirm=False, format=format)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
    repo = mih.repo_connect()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   767
    # version of the database
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
    dbversions = repo.get_versions()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
    mih.shutdown()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   770
    if not dbversions:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   771
        print "bad or missing version information in the database, don't upgrade file system"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   773
    # version of installed software
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   774
    eversion = dbversions['cubicweb']
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   775
    status = instance_status(config, eversion, dbversions)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   776
    # * database version > installed software
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   777
    if status == 'needsoftupgrade':
2851
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   778
        print "** The database of %s is more recent than the installed software!" % config.appid
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   779
        print "** Upgrade your software, then migrate the database by running the command"
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   780
        print "** 'cubicweb-ctl upgrade %s'" % config.appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
    # * database version < installed software, an upgrade will be necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   783
    #   anyway, just rewrite vc.conf and warn user he has to upgrade
2851
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   784
    elif status == 'needapplupgrade':
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   785
        print "** The database of %s is older than the installed software." % config.appid
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   786
        print "** Migrate the database by running the command"
d3b80a4faf3f F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2837
diff changeset
   787
        print "** 'cubicweb-ctl upgrade %s'" % config.appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   788
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   789
    # * database version = installed software, database version = instance fs version
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   790
    #   ok!
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   791
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   792
def instance_status(config, cubicwebapplversion, vcconf):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   793
    cubicwebversion = config.cubicweb_version()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   794
    if cubicwebapplversion > cubicwebversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   795
        return 'needsoftupgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   796
    if cubicwebapplversion < cubicwebversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   797
        return 'needapplupgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   798
    for cube in config.cubes():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   799
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   800
            softversion = config.cube_version(cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   801
        except ConfigurationError:
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   802
            print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   803
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   804
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   805
            applversion = vcconf[cube]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   806
        except KeyError:
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   807
            print '-> Error: no cube version information for %s in version configuration.' % cube
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   808
            continue
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   809
        if softversion == applversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   810
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   811
        if softversion > applversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   812
            return 'needsoftupgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   813
        elif softversion < applversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   814
            return 'needapplupgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   815
    return None
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   816
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   817
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   818
class DBDumpCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   819
    """Backup the system database of an instance.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   820
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   821
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   822
      the identifier of the instance to backup
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   823
      format [[user@]host:]appname
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   824
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   825
    name = 'db-dump'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   826
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   827
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   828
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   829
        ('output',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   830
         {'short': 'o', 'type' : 'string', 'metavar' : '<file>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   831
          'default' : None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   832
          'help': 'Specify the backup file where the backup will be stored.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   833
         ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   834
        ('sudo',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   835
         {'short': 's', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   836
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   837
          'help': 'Use sudo on the remote host.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   838
         ),
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   839
        ('format',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   840
         {'short': 'f', 'default': 'native', 'type': 'choice',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   841
          'choices': ('native', 'portable'),
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   842
          'help': '"native" format uses db backend utilities to dump the database. '
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   843
                  '"portable" format uses a database independent format'}
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   844
         ),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   845
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   846
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   847
    def run(self, args):
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   848
        appid = args[0]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   849
        if ':' in appid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   850
            host, appid = appid.split(':')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   851
            _remote_dump(host, appid, self.config.output, self.config.sudo)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   852
        else:
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   853
            _local_dump(appid, self.config.output, format=self.config.format)
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   854
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   855
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   856
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   857
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   858
class DBRestoreCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   859
    """Restore the system database of an instance.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   860
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   861
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   862
      the identifier of the instance to restore
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   863
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   864
    name = 'db-restore'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   865
    arguments = '<instance> <backupfile>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   866
    min_args = max_args = 2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   867
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   868
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   869
        ('no-drop',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   870
         {'short': 'n', 'action' : 'store_true', 'default' : False,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   871
          'help': 'for some reason the database doesn\'t exist and so '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   872
          'should not be dropped.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   873
         ),
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   874
        ('format',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   875
         {'short': 'f', 'default': 'native', 'type': 'choice',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   876
          'choices': ('native', 'portable'),
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   877
          'help': 'the format used when dumping the database'}),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   878
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   879
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   880
    def run(self, args):
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   881
        appid, backupfile = args
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   882
        if self.config.format == 'portable':
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   883
            # we need to ensure a DB exist before restoring from portable format
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   884
            if not self.config.no_drop:
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   885
                try:
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   886
                    CWCTL.run(['db-create', '--automatic', appid])
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   887
                except SystemExit as exc:
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   888
                    # continue if the command exited with status 0 (success)
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   889
                    if exc.code:
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   890
                        raise
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   891
        _local_restore(appid, backupfile,
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   892
                       drop=not self.config.no_drop,
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   893
                       format=self.config.format)
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   894
        if self.config.format == 'portable':
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   895
            try:
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   896
                CWCTL.run(['db-rebuild-fti', appid])
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8669
diff changeset
   897
            except SystemExit as exc:
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   898
                if exc.code:
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   899
                    raise
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   900
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   901
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   902
class DBCopyCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   903
    """Copy the system database of an instance (backup and restore).
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   904
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   905
    <src-instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   906
      the identifier of the instance to backup
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   907
      format [[user@]host:]appname
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   908
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   909
    <dest-instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   910
      the identifier of the instance to restore
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   911
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   912
    name = 'db-copy'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   913
    arguments = '<src-instance> <dest-instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   914
    min_args = max_args = 2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   915
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   916
        ('no-drop',
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   917
         {'short': 'n', 'action' : 'store_true',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   918
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   919
          'help': 'For some reason the database doesn\'t exist and so '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   920
          'should not be dropped.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   921
         ),
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   922
        ('keep-dump',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   923
         {'short': 'k', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   924
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   925
          'help': 'Specify that the dump file should not be automatically removed.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   926
         ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   927
        ('sudo',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   928
         {'short': 's', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   929
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   930
          'help': 'Use sudo on the remote host.'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   931
         ),
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   932
        ('format',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   933
         {'short': 'f', 'default': 'native', 'type': 'choice',
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   934
          'choices': ('native', 'portable'),
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   935
          'help': '"native" format uses db backend utilities to dump the database. '
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   936
                  '"portable" format uses a database independent format'}
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   937
         ),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   938
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   939
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   940
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   941
        import tempfile
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   942
        srcappid, destappid = args
3148
81d373245ce9 close file handler leak
Aurelien Campeas
parents: 3115
diff changeset
   943
        fd, output = tempfile.mkstemp()
81d373245ce9 close file handler leak
Aurelien Campeas
parents: 3115
diff changeset
   944
        os.close(fd)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   945
        if ':' in srcappid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   946
            host, srcappid = srcappid.split(':')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   947
            _remote_dump(host, srcappid, output, self.config.sudo)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   948
        else:
7342
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   949
            _local_dump(srcappid, output, format=self.config.format)
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   950
        _local_restore(destappid, output, not self.config.no_drop,
d1c8b5b3531c adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7155
diff changeset
   951
                       self.config.format)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   952
        if self.config.keep_dump:
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2107
diff changeset
   953
            print '-> you can get the dump file at', output
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   954
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   955
            os.remove(output)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   956
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   957
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   958
class CheckRepositoryCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   959
    """Check integrity of the system database of an instance.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   960
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   961
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   962
      the identifier of the instance to check
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   963
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   964
    name = 'db-check'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   965
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
   966
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   967
    options = (
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   968
        ('checks',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   969
         {'short': 'c', 'type' : 'csv', 'metavar' : '<check list>',
7036
63386b35ec69 [c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7030
diff changeset
   970
          'default' : ('entities', 'relations',
63386b35ec69 [c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7030
diff changeset
   971
                       'mandatory_relations', 'mandatory_attributes',
63386b35ec69 [c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7030
diff changeset
   972
                       'metadata', 'schema', 'text_index'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   973
          'help': 'Comma separated list of check to run. By default run all \
7036
63386b35ec69 [c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7030
diff changeset
   974
checks, i.e. entities, relations, mandatory_relations, mandatory_attributes, \
63386b35ec69 [c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7030
diff changeset
   975
metadata, text_index and schema.'}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   976
         ),
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   977
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   978
        ('autofix',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   979
         {'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   980
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   981
          'help': 'Automatically correct integrity problems if this option \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   982
is set to "y" or "yes", else only display them'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   983
         ),
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   984
        ('reindex',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   985
         {'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   986
          'default' : False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   987
          'help': 're-indexes the database for full text search if this \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   988
option is set to "y" or "yes" (may be long for large database).'}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   989
         ),
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   990
        ('force',
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   991
         {'short': 'f', 'action' : 'store_true',
2473
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2461
diff changeset
   992
          'default' : False,
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2461
diff changeset
   993
          'help': 'don\'t check instance is up to date.'}
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2461
diff changeset
   994
         ),
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   995
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   996
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   997
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   998
    def run(self, args):
6184
da580218a5b3 [c-c db-check] add missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6138
diff changeset
   999
        from cubicweb.server.checkintegrity import check
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1000
        appid = args[0]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1001
        config = ServerConfiguration.config_for(appid)
2473
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2461
diff changeset
  1002
        config.repairing = self.config.force
9797
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
  1003
        repo, _cnx = repo_cnx(config)
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
  1004
        with repo.internal_cnx() as cnx:
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
  1005
            check(repo, cnx,
9797
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
  1006
                  self.config.checks,
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
  1007
                  self.config.reindex,
4e640ab62f51 [tests/checkintegrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9759
diff changeset
  1008
                  self.config.autofix)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1009
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1010
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1011
class RebuildFTICommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
  1012
    """Rebuild the full-text index of the system database of an instance.
1469
ba9759972b52 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1013
8449
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1014
    <instance> [etype(s)]
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
  1015
      the identifier of the instance to rebuild
8449
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1016
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1017
    If no etype is specified, cubicweb will reindex everything, otherwise
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1018
    only specified etypes will be considered.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1019
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1020
    name = 'db-rebuild-fti'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
  1021
    arguments = '<instance>'
8449
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1022
    min_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1023
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1024
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1025
        from cubicweb.server.checkintegrity import reindex_entities
8449
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1026
        appid = args.pop(0)
cc83a3f16c0f [fti] make db-rebuild-fti accept etypes on command line (closes #2410508)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8434
diff changeset
  1027
        etypes = args or None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1028
        config = ServerConfiguration.config_for(appid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1029
        repo, cnx = repo_cnx(config)
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
  1030
        with cnx:
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
  1031
            reindex_entities(repo.schema, cnx._cnx, etypes=etypes)
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
  1032
            cnx.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1033
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1034
7457
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1035
class SynchronizeSourceCommand(Command):
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1036
    """Force a source synchronization.
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1037
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1038
    <instance>
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1039
      the identifier of the instance
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1040
    <source>
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1041
      the name of the source to synchronize.
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1042
    """
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1043
    name = 'source-sync'
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1044
    arguments = '<instance> <source>'
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1045
    min_args = max_args = 2
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1046
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1047
    def run(self, args):
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1048
        config = ServerConfiguration.config_for(args[0])
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1049
        config.global_set_option('log-file', None)
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1050
        config.log_format = '%(levelname)s %(name)s: %(message)s'
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1051
        logger = logging.getLogger('cubicweb.sources')
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1052
        logger.setLevel(logging.INFO)
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1053
        # only retrieve cnx to trigger authentication, close it right away
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1054
        repo, cnx = repo_cnx(config)
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1055
        cnx.close()
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1056
        try:
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1057
            source = repo.sources_by_uri[args[1]]
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1058
        except KeyError:
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1059
            raise ExecutionError('no source named %r' % args[1])
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1060
        session = repo.internal_session()
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1061
        stats = source.pull_data(session, force=True, raise_on_error=True)
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1062
        for key, val in stats.iteritems():
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1063
            if val:
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1064
                print key, ':', val
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1065
9303
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1066
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1067
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1068
def permissionshandler(relation, perms):
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1069
    from yams.schema import RelationDefinitionSchema
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1070
    from yams.buildobjs import DEFAULT_ATTRPERMS
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1071
    from cubicweb.schema import (PUB_SYSTEM_ENTITY_PERMS, PUB_SYSTEM_REL_PERMS,
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1072
                                 PUB_SYSTEM_ATTR_PERMS, RO_REL_PERMS, RO_ATTR_PERMS)
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1073
    defaultrelperms = (DEFAULT_ATTRPERMS, PUB_SYSTEM_REL_PERMS,
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1074
                       PUB_SYSTEM_ATTR_PERMS, RO_REL_PERMS, RO_ATTR_PERMS)
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1075
    defaulteperms = (PUB_SYSTEM_ENTITY_PERMS,)
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1076
    # canonicalize vs str/unicode
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1077
    for p in ('read', 'add', 'update', 'delete'):
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1078
        rule = perms.get(p)
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1079
        if rule:
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1080
            perms[p] = tuple(str(x) if isinstance(x, basestring) else x
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1081
                             for x in rule)
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1082
    return perms, perms in defaultrelperms or perms in defaulteperms
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1083
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1084
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1085
class SchemaDiffCommand(Command):
8984
0f2fad7a92d4 [cw-ctl] fix help message for schemadiff In global help (closes #2888538)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8924
diff changeset
  1086
    """Generate a diff between schema and fsschema description.
0f2fad7a92d4 [cw-ctl] fix help message for schemadiff In global help (closes #2888538)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8924
diff changeset
  1087
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1088
    <instance>
8984
0f2fad7a92d4 [cw-ctl] fix help message for schemadiff In global help (closes #2888538)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8924
diff changeset
  1089
      the identifier of the instance
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1090
    <diff-tool>
8984
0f2fad7a92d4 [cw-ctl] fix help message for schemadiff In global help (closes #2888538)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8924
diff changeset
  1091
      the name of the diff tool to compare the two generated files.
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1092
    """
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1093
    name = 'schema-diff'
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1094
    arguments = '<instance> <diff-tool>'
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1095
    min_args = max_args = 2
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1096
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1097
    def run(self, args):
8924
8666438778c7 [c-c serverctl] import locally to speed up c-c
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8749
diff changeset
  1098
        from yams.diff import schema_diff
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1099
        appid = args.pop(0)
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1100
        diff_tool = args.pop(0)
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1101
        config = ServerConfiguration.config_for(appid)
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1102
        repo, cnx = repo_cnx(config)
9577
c52441e4a3d7 [serverctl] use repoapi for db-check, add-source, rebuild-fti commands
Julien Cristau <julien.cristau@logilab.fr>
parents: 9485
diff changeset
  1103
        cnx.close()
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1104
        fsschema = config.load_schema(expand_cubes=True)
9303
e84414be8506 [serverctl] complete the schema-diff command with options that will fix & enhance it.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9002
diff changeset
  1105
        schema_diff(fsschema, repo.schema, permissionshandler, diff_tool, ignore=('eid',))
8749
9db7580d6427 add a command to compare db schema and file system schema (closes #464991)
Alexandre Richardson <alexandre.richardson@logilab.fr>
parents: 8695
diff changeset
  1106
7457
f7c97a3cd121 [datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
  1107
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1108
for cmdclass in (CreateInstanceDBCommand, InitInstanceCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1109
                 GrantUserOnInstanceCommand, ResetAdminPasswordCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1110
                 StartRepositoryCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1111
                 DBDumpCommand, DBRestoreCommand, DBCopyCommand,
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6379
diff changeset
  1112
                 AddSourceCommand, CheckRepositoryCommand, RebuildFTICommand,
9485
2b47e800cea6 [serverctl] remove broken schema-sync command
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
  1113
                 SynchronizeSourceCommand, SchemaDiffCommand,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1114
                 ):
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6132
diff changeset
  1115
    CWCTL.register(cmdclass)
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1116
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1117
# extend configure command to set options in sources config file ###############
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1118
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1119
db_options = (
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1120
    ('db',
10077
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1121
     {'short': 'd', 'type' : 'named', 'metavar' : '[section1.]key1:value1,[section2.]key2:value2',
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1122
      'default': None,
10077
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1123
      'help': '''set <key> in <section> to <value> in "source" configuration file. If <section> is not specified, it defaults to "system".
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1124
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1125
Beware that changing admin.login or admin.password using this command
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1126
will NOT update the database with new admin credentials.  Use the
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1127
reset-admin-pwd command instead.
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1128
''',
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1129
      }),
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1130
    )
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1131
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1132
ConfigureInstanceCommand.options = merge_options(
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1133
        ConfigureInstanceCommand.options + db_options)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1134
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1135
configure_instance = ConfigureInstanceCommand.configure_instance
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1136
def configure_instance2(self, appid):
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1137
    configure_instance(self, appid)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1138
    if self.config.db is not None:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1139
        appcfg = ServerConfiguration.config_for(appid)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1140
        srccfg = appcfg.read_sources_file()
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1141
        for key, value in self.config.db.iteritems():
10077
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1142
            if '.' in key:
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1143
                section, key = key.split('.', 1)
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1144
            else:
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1145
                section = 'system'
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1146
            try:
10077
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1147
                srccfg[section][key] = value
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1148
            except KeyError:
10077
1a28c0b0a0b4 [c-c configure] make it possible to specify the section for sources configuration (closes #3477678)
David Douard <david.douard@logilab.fr>
parents: 10010
diff changeset
  1149
                raise ConfigurationError('unknown configuration key "%s" in section "%s" for source' % (key, section))
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1150
        admcfg = Configuration(options=USER_OPTIONS)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1151
        admcfg['login'] = srccfg['admin']['login']
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1152
        admcfg['password'] = srccfg['admin']['password']
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1153
        srccfg['admin'] = admcfg
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1154
        appcfg.write_sources_file(srccfg)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8984
diff changeset
  1155
ConfigureInstanceCommand.configure_instance = configure_instance2