cwctl.py
author Rémi Cardona <remi.cardona@free.fr>
Wed, 30 Jul 2014 22:26:18 +0200
changeset 10068 1b0cb3c6c95e
parent 9990 c84ad981fc4a
child 10104 cdc0b0216da2
permissions -rw-r--r--
[view] Remove unused imports
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
     1
# copyright 2003-2013 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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
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: 5288
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
4720
ddf4f19eb07a don't use module doc string as usage string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4718
diff changeset
    18
"""the cubicweb-ctl tool, based on logilab.common.clcommands to
ddf4f19eb07a don't use module doc string as usage string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4718
diff changeset
    19
provide a pluggable commands system.
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
    20
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
4720
ddf4f19eb07a don't use module doc string as usage string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4718
diff changeset
    22
__docformat__ = "restructuredtext en"
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
4718
3dc3ad02d091 avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4684
diff changeset
    24
# *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: 4684
diff changeset
    25
# 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: 4684
diff changeset
    26
# completion). So import locally in command helpers.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
import sys
5025
2826f5406201 deprecate __use__ and __recommend__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5021
diff changeset
    28
from warnings import warn
3115
29262ba01464 minimal steps to have cw running on windows
Aurélien Campéas
parents: 2905
diff changeset
    29
from os import remove, listdir, system, pathsep
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: 8564
diff changeset
    30
from os.path import exists, join, isfile, isdir, dirname, abspath
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
    31
from urlparse import urlparse
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
    32
3115
29262ba01464 minimal steps to have cw running on windows
Aurélien Campéas
parents: 2905
diff changeset
    33
try:
3562
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3560
diff changeset
    34
    from os import kill, getpgid
3115
29262ba01464 minimal steps to have cw running on windows
Aurélien Campéas
parents: 2905
diff changeset
    35
except ImportError:
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4720
diff changeset
    36
    def kill(*args):
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4720
diff changeset
    37
        """win32 kill implementation"""
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4720
diff changeset
    38
    def getpgid():
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4720
diff changeset
    39
        """win32 getpgid implementation"""
3115
29262ba01464 minimal steps to have cw running on windows
Aurélien Campéas
parents: 2905
diff changeset
    40
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
    41
from logilab.common.clcommands import CommandLine
2615
1ea41b7c0836 F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2532
diff changeset
    42
from logilab.common.shellutils import ASK
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
    43
from logilab.common.configuration import merge_options
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
    44
2790
968108e16066 move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2743
diff changeset
    45
from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
6914
5be96d9cbedc [c-c] lgc 0.54 check for duplicated commands, this may occurs with cw where plugins are automaticall (re)loaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6845
diff changeset
    46
from cubicweb.utils import support_args
3638
648d6dbec630 system/user modes + CWDEV instead of installed/dev mixed modes. Fix behaviour when setting CW_MODE explicitly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3619
diff changeset
    47
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
    48
from cubicweb.toolsutils import Command, rm, create_dir, underline_title
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
    49
from cubicweb.__pkginfo__ import version
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
    50
9371
1348202527a6 [cwctl] Don't check for old lgc versions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9340
diff changeset
    51
# don't check duplicated commands, it occurs when reloading site_cubicweb
1348202527a6 [cwctl] Don't check for old lgc versions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9340
diff changeset
    52
CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
1348202527a6 [cwctl] Don't check for old lgc versions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9340
diff changeset
    53
                    version=version, check_duplicated_command=False)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    54
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
def wait_process_end(pid, maxtry=10, waittime=1):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    """wait for a process to actually die"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    import signal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    from time import sleep
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    nbtry = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    while nbtry < maxtry:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            kill(pid, signal.SIGUSR1)
3117
32686ae66c75 mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents: 3115
diff changeset
    63
        except (OSError, AttributeError): # XXX win32
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        nbtry += 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        sleep(waittime)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        raise ExecutionError('can\'t kill process %s' % pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
def list_instances(regdir):
6505
ac4cecb26813 [distribution] Handle missing instance directory when listing and running instance command.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6145
diff changeset
    71
    if isdir(regdir):
ac4cecb26813 [distribution] Handle missing instance directory when listing and running instance command.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6145
diff changeset
    72
        return sorted(idir for idir in listdir(regdir) if isdir(join(regdir, idir)))
ac4cecb26813 [distribution] Handle missing instance directory when listing and running instance command.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6145
diff changeset
    73
    else:
ac4cecb26813 [distribution] Handle missing instance directory when listing and running instance command.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6145
diff changeset
    74
        return []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
def detect_available_modes(templdir):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    modes = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    for fname in ('schema', 'schema.py'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        if exists(join(templdir, fname)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
            modes.append('repository')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    for fname in ('data', 'views', 'views.py'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        if exists(join(templdir, fname)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
            modes.append('web ui')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    return modes
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    87
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    88
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    89
class InstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    90
    """base class for command taking 0 to n instance id as arguments
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    91
    (0 meaning all registered instances)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    """
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    93
    arguments = '[<instance>...]'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        ("force",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
         {'short': 'f', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
          'default': False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
          'help': 'force command without asking confirmation',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
          }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
         ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    actionverb = None
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   103
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    def ordered_instances(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        """return instances in the order in which they should be started,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        considering $REGISTRY_DIR/startorder file if it exists (useful when
3619
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   107
        some instances depends on another as external source).
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   108
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   109
        Instance used by another one should appears first in the file (one
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   110
        instance per line)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
        """
5021
58e89f3dfbae handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   112
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        _allinstances = list_instances(regdir)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        if isfile(join(regdir, 'startorder')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            allinstances = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
            for line in file(join(regdir, 'startorder')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
                line = line.strip()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
                if line and not line.startswith('#'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
                        _allinstances.remove(line)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
                        allinstances.append(line)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
                    except ValueError:
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   123
                        print ('ERROR: startorder file contains unexistant '
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   124
                               'instance %s' % line)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
            allinstances += _allinstances
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
            allinstances = _allinstances
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        return allinstances
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   129
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    def run(self, args):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   131
        """run the <command>_method on each argument (a list of instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        identifiers)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        if not args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
            args = self.ordered_instances()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
                askconfirm = not self.config.force
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
            except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
                # no force option
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
                askconfirm = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            askconfirm = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        self.run_args(args, askconfirm)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   144
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def run_args(self, args, askconfirm):
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   146
        status = 0
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   150
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
                    continue
7593
0c1024b3e7fc [c-c instance commands] keyboard interrupt should stop the command, not jump to the next instance. Closes #1794850
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7538
diff changeset
   152
            try:
0c1024b3e7fc [c-c instance commands] keyboard interrupt should stop the command, not jump to the next instance. Closes #1794850
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7538
diff changeset
   153
                status = max(status, self.run_arg(appid))
0c1024b3e7fc [c-c instance commands] keyboard interrupt should stop the command, not jump to the next instance. Closes #1794850
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7538
diff changeset
   154
            except (KeyboardInterrupt, SystemExit):
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   155
                sys.stderr.write('%s aborted\n' % self.name)
7593
0c1024b3e7fc [c-c instance commands] keyboard interrupt should stop the command, not jump to the next instance. Closes #1794850
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7538
diff changeset
   156
                return 2 # specific error code
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   157
        sys.exit(status)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   158
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
    def run_arg(self, appid):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   160
        cmdmeth = getattr(self, '%s_instance' % self.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        try:
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   162
            status = cmdmeth(appid)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   163
        except (ExecutionError, ConfigurationError) as ex:
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   164
            sys.stderr.write('instance %s not %s: %s\n' % (
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   165
                    appid, self.actionverb, ex))
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   166
            status = 4
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   167
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
            import traceback
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
            traceback.print_exc()
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   170
            sys.stderr.write('instance %s not %s: %s\n' % (
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   171
                    appid, self.actionverb, ex))
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   172
            status = 8
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   173
        return status
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   175
class InstanceCommandFork(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   176
    """Same as `InstanceCommand`, but command is forked in a new environment
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
    for each argument
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    def run_args(self, args, askconfirm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
        if len(args) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
            forkcmd = ' '.join(w for w in sys.argv if not w in args)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
            forkcmd = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   188
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
            if forkcmd:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
                status = system('%s %s' % (forkcmd, appid))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
                if status:
2156
fadb86b040a9 continue to run command on other instances, do not exit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2104
diff changeset
   193
                    print '%s exited with status %s' % (forkcmd, status)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
                self.run_arg(appid)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   196
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   197
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
# base commands ###############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
class ListCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   201
    """List configurations, cubes and instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   203
    List available configurations, installed cubes, and registered instances.
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   204
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   205
    If given, the optional argument allows to restrict listing only a category of items.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
    name = 'list'
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   208
    arguments = '[all|cubes|configurations|instances]'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        ('verbose',
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   211
         {'short': 'v', 'action' : 'store_true',
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   212
          'help': "display more information."}),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   214
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        """run the command with its specific arguments"""
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   217
        if not args:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   218
            mode = 'all'
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   219
        elif len(args) == 1:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   220
            mode = args[0]
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   221
        else:
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5498
diff changeset
   222
            raise BadCommandUsage('Too many arguments')
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   223
5027
d688daf0a62c [config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5026
diff changeset
   224
        from cubicweb.migration import ConfigurationProblem
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   225
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   226
        if mode == 'all':
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   227
            print 'CubicWeb %s (%s mode)' % (cwcfg.cubicweb_version(), cwcfg.mode)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   228
            print
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   229
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   230
        if mode in ('all', 'config', 'configurations'):
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   231
            print 'Available configurations:'
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   232
            for config in CONFIGURATIONS:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   233
                print '*', config.name
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   234
                for line in config.__doc__.splitlines():
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   235
                    line = line.strip()
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   236
                    if not line:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   237
                        continue
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   238
                    print '   ', line
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   239
            print
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   240
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   241
        if mode in ('all', 'cubes'):
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   242
            cfgpb = ConfigurationProblem(cwcfg)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   243
            try:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   244
                cubesdir = pathsep.join(cwcfg.cubes_search_path())
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   245
                namesize = max(len(x) for x in cwcfg.available_cubes())
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   246
            except ConfigurationError as ex:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   247
                print 'No cubes available:', ex
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   248
            except ValueError:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   249
                print 'No cubes available in %s' % cubesdir
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   250
            else:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   251
                print 'Available cubes (%s):' % cubesdir
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   252
                for cube in cwcfg.available_cubes():
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   253
                    try:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   254
                        tinfo = cwcfg.cube_pkginfo(cube)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   255
                        tversion = tinfo.version
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   256
                        cfgpb.add_cube(cube, tversion)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   257
                    except (ConfigurationError, AttributeError) as ex:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   258
                        tinfo = None
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   259
                        tversion = '[missing cube information: %s]' % ex
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   260
                    print '* %s %s' % (cube.ljust(namesize), tversion)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   261
                    if self.config.verbose:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   262
                        if tinfo:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   263
                            descr = getattr(tinfo, 'description', '')
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   264
                            if not descr:
9255
46f41c3e1443 remove 3.8 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9251
diff changeset
   265
                                descr = tinfo.__doc__
5025
2826f5406201 deprecate __use__ and __recommend__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5021
diff changeset
   266
                            if descr:
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   267
                                print '    '+ '    \n'.join(descr.splitlines())
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   268
                        modes = detect_available_modes(cwcfg.cube_dir(cube))
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   269
                        print '    available modes: %s' % ', '.join(modes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
            print
8829
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   271
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   272
        if mode in ('all', 'instances'):
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   273
            try:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   274
                regdir = cwcfg.instances_dir()
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   275
            except ConfigurationError as ex:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   276
                print 'No instance available:', ex
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   277
                print
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   278
                return
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   279
            instances = list_instances(regdir)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   280
            if instances:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   281
                print 'Available instances (%s):' % regdir
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   282
                for appid in instances:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   283
                    modes = cwcfg.possible_configurations(appid)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   284
                    if not modes:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   285
                        print '* %s (BROKEN instance, no configuration found)' % appid
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   286
                        continue
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   287
                    print '* %s (%s)' % (appid, ', '.join(modes))
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   288
                    try:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   289
                        config = cwcfg.config_for(appid, modes[0])
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   290
                    except Exception as exc:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   291
                        print '    (BROKEN instance, %s)' % exc
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   292
                        continue
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   293
            else:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   294
                print 'No instance available in %s' % regdir
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   295
            print
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   296
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   297
        if mode == 'all':
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   298
            # configuration management problem solving
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   299
            cfgpb.solve()
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   300
            if cfgpb.warnings:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   301
                print 'Warnings:\n', '\n'.join('* '+txt for txt in cfgpb.warnings)
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   302
            if cfgpb.errors:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   303
                print 'Errors:'
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   304
                for op, cube, version, src in cfgpb.errors:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   305
                    if op == 'add':
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   306
                        print '* cube', cube,
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   307
                        if version:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   308
                            print ' version', version,
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   309
                        print 'is not installed, but required by %s' % src
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   310
                    else:
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   311
                        print '* cube %s version %s is installed, but version %s is required by %s' % (
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   312
                            cube, cfgpb.cubes[cube], version, src)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   314
def check_options_consistency(config):
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   315
    if config.automatic and config.config_level > 0:
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   316
        raise BadCommandUsage('--automatic and --config-level should not be '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   317
                              'used together')
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   318
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   319
class CreateInstanceCommand(Command):
9704
43c9896d0e9f an -> a (misc)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9700
diff changeset
   320
    """Create an instance from a cube. This is a unified
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
    command which can handle web / server / all-in-one installation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
    according to available parts of the software library and of the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    desired cube.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
    <cube>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
      the name of cube to use (list available cube names using
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
      the "list" command). You can use several cubes by separating
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   328
      them using comma (e.g. 'jpl,email')
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   329
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   330
      an identifier for the instance to create
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
    name = 'create'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   333
    arguments = '<cube> <instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   334
    min_args = max_args = 2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
    options = (
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   336
        ('automatic',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   337
         {'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: 7131
diff changeset
   338
          'default': False,
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   339
          '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: 7131
diff changeset
   340
          '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: 7131
diff changeset
   341
          '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: 7131
diff changeset
   342
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   343
        ('config-level',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
         {'short': 'l', 'type' : 'int', 'metavar': '<level>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
          'default': 0,
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   346
          '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: 7131
diff changeset
   347
          '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: 7131
diff changeset
   348
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   349
        ('config',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
         {'short': 'c', 'type' : 'choice', 'metavar': '<install type>',
9251
e4d753c8b1c4 remove "twisted" configuration (closes #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9013
diff changeset
   351
          'choices': ('all-in-one', 'repository'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
          'default': 'all-in-one',
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   353
          'help': 'installation type, telling which part of an instance '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   354
          'should be installed. You can list available configurations using the'
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   355
          ' "list" command. Default to "all-in-one", e.g. an installation '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   356
          'embedding both the RQL repository and the web server.',
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   357
          }),
9001
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   358
        ('no-db-create',
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   359
         {'short': 'S',
8851
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   360
          'action': 'store_true',
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   361
          'default': False,
9001
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   362
          'help': 'stop after creation and do not continue with db-create',
8851
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   363
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   365
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
        """run the command with its specific arguments"""
2633
bc9386c3b2c9 get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2615
diff changeset
   368
        from logilab.common.textutils import splitstrip
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   369
        check_options_consistency(self.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
        configname = self.config.config
6144
40af8e328e64 [c-c create] argument inversed accidentally
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6138
diff changeset
   371
        cubes, appid = args
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   372
        cubes = splitstrip(cubes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        # get the configuration and helper
7129
455b503fb7ff [config] generalise config.creating, allowing to drop set_language class attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7028
diff changeset
   374
        config = cwcfg.config_for(appid, configname, creating=True)
2104
b4ffcea3275b change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2032
diff changeset
   375
        cubes = config.expand_cubes(cubes)
b4ffcea3275b change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2032
diff changeset
   376
        config.init_cubes(cubes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        helper = self.config_helper(config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        # check the cube exists
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
        try:
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   380
            templdirs = [cwcfg.cube_dir(cube)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
                         for cube in cubes]
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   382
        except ConfigurationError as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
            print ex
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
            print '\navailable cubes:',
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   385
            print ', '.join(cwcfg.available_cubes())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
            return
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   387
        # create the registry directory for this instance
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   388
        print '\n'+underline_title('Creating the instance %s' % appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
        create_dir(config.apphome)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
        # cubicweb-ctl configuration
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   391
        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: 7131
diff changeset
   392
            print '\n'+underline_title('Configuring the instance (%s.conf)'
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   393
                                       % configname)
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   394
            config.input_config('main', self.config.config_level)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        # configuration'specific stuff
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
        print
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   397
        helper.bootstrap(cubes, self.config.automatic, self.config.config_level)
4610
7bf205b9a845 when creating an instance, ask for cubes specific options properly (fix #607349)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4609
diff changeset
   398
        # input for cubes specific options
9923
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   399
        if not self.config.automatic:
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   400
            sections = set(sect.lower() for sect, opt, odict in config.all_options()
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   401
                           if 'type' in odict
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   402
                           and odict.get('level') <= self.config.config_level)
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   403
            for section in sections:
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   404
                if section not in ('main', 'email', 'pyro', 'web'):
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   405
                    print '\n' + underline_title('%s options' % section)
da09975300aa [cwctl] don't prompt for cube options in automatic mode
Julien Cristau <julien.cristau@logilab.fr>
parents: 9704
diff changeset
   406
                    config.input_config(section, self.config.config_level)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
        # write down configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
        config.save()
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   409
        self._handle_win32(config, appid)
7155
4bab50b02927 [cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7140
diff changeset
   410
        print '-> generated config %s' % config.main_config_file()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   411
        # handle i18n files structure
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
        # in the first cube given
4021
280c910c8710 move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3720
diff changeset
   413
        from cubicweb import i18n
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
        langs = [lang for lang, _ in i18n.available_catalogs(join(templdirs[0], 'i18n'))]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
        errors = config.i18ncompile(langs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
            print '\n'.join(errors)
7140
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   418
            if self.config.automatic \
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   419
                   or not ASK.confirm('error while compiling message catalogs, '
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   420
                                      'continue anyway ?'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
                print 'creation not completed'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
                return
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   423
        # create the additional data directory for this instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
        if config.appdatahome != config.apphome: # true in dev mode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
            create_dir(config.appdatahome)
2489
37a747ad6fd4 #344772: instance backups should be done in instance_data_dir
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   426
        create_dir(join(config.appdatahome, 'backup'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
        if config['uid']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
            from logilab.common.shellutils import chown
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
            # this directory should be owned by the uid of the server process
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
            print 'set %s as owner of the data directory' % config['uid']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   431
            chown(config.appdatahome, config['uid'])
7155
4bab50b02927 [cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7140
diff changeset
   432
        print '\n-> creation done for %s\n' % repr(config.apphome)[1:-1]
9001
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   433
        if not self.config.no_db_create:
9013
b4bcabf55e77 3.17 is the new stable
David Douard <david.douard@logilab.fr>
parents: 8994 9002
diff changeset
   434
            helper.postcreate(self.config.automatic, self.config.config_level)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   436
    def _handle_win32(self, config, appid):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   437
        if sys.platform != 'win32':
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   438
            return
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   439
        service_template = """
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   440
import sys
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   441
import win32serviceutil
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   442
sys.path.insert(0, r"%(CWPATH)s")
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   443
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   444
from cubicweb.etwist.service import CWService
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   445
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   446
classdict = {'_svc_name_': 'cubicweb-%(APPID)s',
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   447
             '_svc_display_name_': 'CubicWeb ' + '%(CNAME)s',
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   448
             'instance': '%(APPID)s'}
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   449
%(CNAME)sService = type('%(CNAME)sService', (CWService,), classdict)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   450
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   451
if __name__ == '__main__':
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   452
    win32serviceutil.HandleCommandLine(%(CNAME)sService)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   453
"""
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   454
        open(join(config.apphome, 'win32svc.py'), 'wb').write(
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   455
            service_template % {'APPID': appid,
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   456
                                'CNAME': appid.capitalize(),
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   457
                                'CWPATH': abspath(join(dirname(__file__), '..'))})
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   458
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   459
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   460
class DeleteInstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   461
    """Delete an instance. Will remove instance's files and
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
    unregister it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
    name = 'delete'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   465
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   466
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
    options = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
        """run the command with its specific arguments"""
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   471
        appid = args[0]
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   472
        configs = [cwcfg.config_for(appid, configname)
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   473
                   for configname in cwcfg.possible_configurations(appid)]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
        if not configs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
            raise ExecutionError('unable to guess configuration for %s' % appid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
        for config in configs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
            helper = self.config_helper(config, required=False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
            if helper:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
                helper.cleanup()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
        # remove home
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   481
        rm(config.apphome)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   482
        # remove instance data directory
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
            rm(config.appdatahome)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   485
        except OSError as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
            import errno
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
            if ex.errno != errno.ENOENT:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
                raise
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
        confignames = ', '.join([config.name for config in configs])
2532
f7ca29d75183 [cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2512
diff changeset
   490
        print '-> instance %s (%s) deleted.' % (appid, confignames)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   493
# instance commands ########################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   495
class StartInstanceCommand(InstanceCommandFork):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   496
    """Start the given instances. If no instance is given, start them all.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   497
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   498
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   499
      identifiers of the instances to start. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
      given, start them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
    name = 'start'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
    actionverb = 'started'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
        ("debug",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   506
         {'short': 'D', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   507
          'help': 'start server in debug mode.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
        ("force",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
         {'short': 'f', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
          'default': False,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   511
          'help': 'start the instance even if it seems to be already \
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
running.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
        ('profile',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
         {'short': 'P', 'type' : 'string', 'metavar': '<stat file>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
          'help': 'profile code and use the specified file to store stats',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
          }),
2654
6512522860aa [twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2633
diff changeset
   518
        ('loglevel',
6512522860aa [twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2633
diff changeset
   519
         {'short': 'l', 'type' : 'choice', 'metavar': '<log level>',
6512522860aa [twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2633
diff changeset
   520
          'default': None, 'choices': ('debug', 'info', 'warning', 'error'),
6512522860aa [twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2633
diff changeset
   521
          'help': 'debug if -D is set, error otherwise',
6512522860aa [twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2633
diff changeset
   522
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   524
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   525
    def start_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   526
        """start the instance's server"""
5442
3ed8afbbdf70 [webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5436
diff changeset
   527
        config = cwcfg.config_for(appid, debugmode=self['debug'])
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   528
        init_cmdline_log_threshold(config, self['loglevel'])
3182
76ad941f694e cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3180
diff changeset
   529
        if self['profile']:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   530
            config.global_set_option('profile', self.config.profile)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
        helper = self.config_helper(config, cmdname='start')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   532
        pidf = config['pid-file']
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   533
        if exists(pidf) and not self['force']:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   534
            msg = "%s seems to be running. Remove %s by hand if necessary or use \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   535
the --force option."
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   536
            raise ExecutionError(msg % (appid, pidf))
6778
7dd4835d5198 properly tell we've started an instance (close #1391262)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6675
diff changeset
   537
        if helper.start_server(config) == 1:
7dd4835d5198 properly tell we've started an instance (close #1391262)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6675
diff changeset
   538
            print 'instance %s started' % appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
5814
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   541
def init_cmdline_log_threshold(config, loglevel):
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   542
    if loglevel is not None:
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   543
        config.global_set_option('log-threshold', loglevel.upper())
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   544
        config.init_log(config['log-threshold'], force=True)
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   545
51cc4b61f9ae [repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5811
diff changeset
   546
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   547
class StopInstanceCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   548
    """Stop the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
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
      identifiers of the instances to stop. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
      given, stop them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   553
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
    name = 'stop'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
    actionverb = 'stopped'
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   556
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
    def ordered_instances(self):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   558
        instances = super(StopInstanceCommand, self).ordered_instances()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   559
        instances.reverse()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   560
        return instances
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   561
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   562
    def stop_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   563
        """stop the instance's server"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   564
        config = cwcfg.config_for(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
        helper = self.config_helper(config, cmdname='stop')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
        helper.poststop() # do this anyway
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
        pidf = config['pid-file']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
        if not exists(pidf):
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   569
            sys.stderr.write("%s doesn't exist.\n" % pidf)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   570
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   571
        import signal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
        pid = int(open(pidf).read().strip())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
            kill(pid, signal.SIGTERM)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7595
diff changeset
   575
        except Exception:
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   576
            sys.stderr.write("process %s seems already dead.\n" % pid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   577
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
                wait_process_end(pid)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   580
            except ExecutionError as ex:
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   581
                sys.stderr.write('%s\ntrying SIGKILL\n' % ex)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   582
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
                    kill(pid, signal.SIGKILL)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7595
diff changeset
   584
                except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   585
                    # probably dead now
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
                    pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   587
                wait_process_end(pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   588
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   589
            remove(pidf)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   590
        except OSError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
            # already removed by twistd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
            pass
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   593
        print 'instance %s stopped' % appid
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   594
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   596
class RestartInstanceCommand(StartInstanceCommand):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   597
    """Restart the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   598
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   599
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   600
      identifiers of the instances to restart. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
      given, restart them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   602
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
    name = 'restart'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
    actionverb = 'restarted'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   605
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   606
    def run_args(self, args, askconfirm):
5021
58e89f3dfbae handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   607
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   608
        if not isfile(join(regdir, 'startorder')) or len(args) <= 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   609
            # no specific startorder
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   610
            super(RestartInstanceCommand, self).run_args(args, askconfirm)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   612
        print ('some specific start order is specified, will first stop all '
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   613
               'instances then restart them.')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   614
        # get instances in startorder
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   615
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   616
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   617
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   618
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   619
                    continue
6145
528be1e059aa more cwctl api update and fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6144
diff changeset
   620
            StopInstanceCommand(self.logger).stop_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   621
        forkcmd = [w for w in sys.argv if not w in args]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   622
        forkcmd[1] = 'start'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   623
        forkcmd = ' '.join(forkcmd)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   624
        for appid in reversed(args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   625
            status = system('%s %s' % (forkcmd, appid))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   626
            if status:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   627
                sys.exit(status)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   628
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   629
    def restart_instance(self, appid):
6145
528be1e059aa more cwctl api update and fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6144
diff changeset
   630
        StopInstanceCommand(self.logger).stop_instance(appid)
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   631
        self.start_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   632
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   633
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   634
class ReloadConfigurationCommand(RestartInstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   635
    """Reload the given instances. This command is equivalent to a
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   636
    restart for now.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   637
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   638
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   639
      identifiers of the instances to reload. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   640
      given, reload them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   641
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   642
    name = 'reload'
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   643
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   644
    def reload_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   645
        self.restart_instance(appid)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   646
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   647
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   648
class StatusCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   649
    """Display status information about the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   650
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   651
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   652
      identifiers of the instances to status. If no instance is
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   653
      given, get status information about all registered instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   654
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   655
    name = 'status'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   656
    options = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   657
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   658
    @staticmethod
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   659
    def status_instance(appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   660
        """print running status information for an instance"""
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   661
        status = 0
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   662
        for mode in cwcfg.possible_configurations(appid):
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   663
            config = cwcfg.config_for(appid, mode)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   664
            print '[%s-%s]' % (appid, mode),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   665
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   666
                pidf = config['pid-file']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
            except KeyError:
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   668
                print 'buggy instance, pid file not specified'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   669
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   670
            if not exists(pidf):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   671
                print "doesn't seem to be running"
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   672
                status = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   673
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
            pid = int(open(pidf).read().strip())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
            # trick to guess whether or not the process is running
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   676
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   677
                getpgid(pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
            except OSError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
                print "should be running with pid %s but the process can not be found" % pid
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   680
                status = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   681
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
            print "running with pid %s" % (pid)
5380
a4e7e87d315f [c-c status] return status code 1 when some instances isn't running. Closes #510817
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5323
diff changeset
   683
        return status
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   685
class UpgradeInstanceCommand(InstanceCommandFork):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   686
    """Upgrade an instance after cubicweb and/or component(s) upgrade.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   688
    For repository update, you will be prompted for a login / password to use
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   689
    to connect to the system database.  For some upgrades, the given user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   690
    should have create or alter table permissions.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   691
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   692
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   693
      identifiers of the instances to upgrade. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   694
      given, upgrade them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   695
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   696
    name = 'upgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   697
    actionverb = 'upgraded'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   698
    options = InstanceCommand.options + (
7028
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   699
        ('force-cube-version',
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   700
         {'short': 't', 'type' : 'named', 'metavar': 'cube1:X.Y.Z,cube2:X.Y.Z',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   701
          'default': None,
7028
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   702
          'help': 'force migration from the indicated version for the specified cube(s).'}),
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   703
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
        ('force-cubicweb-version',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
         {'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   706
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   707
          'help': 'force migration from the indicated cubicweb version.'}),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   708
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
        ('fs-only',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   710
         {'short': 's', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   711
          'default': False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   712
          'help': 'only upgrade files on the file system, not the database.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   713
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
        ('nostartstop',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
         {'short': 'n', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   716
          'default': False,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   717
          'help': 'don\'t try to stop instance before migration and to restart it after.'}),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   718
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
        ('verbosity',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
         {'short': 'v', 'type' : 'int', 'metavar': '<0..2>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
          'default': 1,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   722
          'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   723
for everything."}),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   724
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   725
        ('backup-db',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   726
         {'short': 'b', 'type' : 'yn', 'metavar': '<y or n>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
          'default': None,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   728
          'help': "Backup the instance database before upgrade.\n"\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   729
          "If the option is ommitted, confirmation will be ask.",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   730
          }),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   731
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   732
        ('ext-sources',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
         {'short': 'E', 'type' : 'csv', 'metavar': '<sources>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
          'help': "For multisources instances, specify to which sources the \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
repository should connect to for upgrading. When unspecified or 'migration' is \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
given, appropriate sources for migration will be automatically selected \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
(recommended). If 'all' is given, will connect to all defined sources.",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
          }),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   740
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   742
    def upgrade_instance(self, appid):
2512
106b2a05dc88 [cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2489
diff changeset
   743
        print '\n' + underline_title('Upgrading the instance %s' % appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
        from logilab.common.changelog import Version
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   745
        config = cwcfg.config_for(appid)
9315
5298cfb132e6 [cwctl] Only start the instance after upgrade if it was running before
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9013
diff changeset
   746
        instance_running = exists(config['pid-file'])
2473
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2395
diff changeset
   747
        config.repairing = True # notice we're not starting the server
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
        config.verbosity = self.config.verbosity
7577
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7538
diff changeset
   749
        set_sources_mode = getattr(config, 'set_sources_mode', None)
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7538
diff changeset
   750
        if set_sources_mode is not None:
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7538
diff changeset
   751
            set_sources_mode(self.config.ext_sources or ('migration',))
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   752
        # get instance and installed versions for the server and the componants
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   753
        mih = config.migration_handler()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
        repo = mih.repo_connect()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   755
        vcconf = repo.get_versions()
9372
e87a808a4c32 [webctl] Generate static data directory on upgrade (closes #2167873)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9371
diff changeset
   756
        helper = self.config_helper(config, required=False)
7028
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   757
        if self.config.force_cube_version:
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   758
            for cube, version in self.config.force_cube_version.iteritems():
e8855733b125 [c-c upgrade] rename force-componant-version option to force-cube-version and cleanup code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6914
diff changeset
   759
                vcconf[cube] = Version(version)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
        toupgrade = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
        for cube in config.cubes():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
            installedversion = config.cube_version(cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   763
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   764
                applversion = vcconf[cube]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   765
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
                config.error('no version information for %s' % cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   767
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
            if installedversion > applversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
                toupgrade.append( (cube, applversion, installedversion) )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   770
        cubicwebversion = config.cubicweb_version()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   771
        if self.config.force_cubicweb_version:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
            applcubicwebversion = Version(self.config.force_cubicweb_version)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   773
            vcconf['cubicweb'] = applcubicwebversion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   774
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   775
            applcubicwebversion = vcconf.get('cubicweb')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   776
        if cubicwebversion > applcubicwebversion:
2275
bc0bed0616a3 fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2156
diff changeset
   777
            toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion))
1404
971b19de6b85 stop application only once we're sure we've something to do, fixing #342689
sylvain.thenault@logilab.fr
parents: 1219
diff changeset
   778
        # only stop once we're sure we have something to do
9315
5298cfb132e6 [cwctl] Only start the instance after upgrade if it was running before
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9013
diff changeset
   779
        if instance_running and not (CWDEV or self.config.nostartstop):
6145
528be1e059aa more cwctl api update and fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6144
diff changeset
   780
            StopInstanceCommand(self.logger).stop_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
        # run cubicweb/componants migration scripts
7538
849c04098f3f [migration] when there are no data migration needed, we still want to rewrite the configuration and to restart the server. Closes #1760927
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7372
diff changeset
   782
        if self.config.fs_only or toupgrade:
849c04098f3f [migration] when there are no data migration needed, we still want to rewrite the configuration and to restart the server. Closes #1760927
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7372
diff changeset
   783
            for cube, fromversion, toversion in toupgrade:
849c04098f3f [migration] when there are no data migration needed, we still want to rewrite the configuration and to restart the server. Closes #1760927
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7372
diff changeset
   784
                print '-> migration needed from %s to %s for %s' % (fromversion, toversion, cube)
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9372
diff changeset
   785
            with mih.cnx:
9556
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   786
                with mih.cnx.security_enabled(False, False):
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   787
                    mih.migrate(vcconf, reversed(toupgrade), self.config)
7538
849c04098f3f [migration] when there are no data migration needed, we still want to rewrite the configuration and to restart the server. Closes #1760927
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7372
diff changeset
   788
        else:
849c04098f3f [migration] when there are no data migration needed, we still want to rewrite the configuration and to restart the server. Closes #1760927
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7372
diff changeset
   789
            print '-> no data migration needed for instance %s.' % appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   790
        # rewrite main configuration file
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   791
        mih.rewrite_configuration()
5796
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   792
        mih.shutdown()
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   793
        # handle i18n upgrade
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   794
        if not self.i18nupgrade(config):
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   795
            return
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   796
        print
9372
e87a808a4c32 [webctl] Generate static data directory on upgrade (closes #2167873)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9371
diff changeset
   797
        if helper:
e87a808a4c32 [webctl] Generate static data directory on upgrade (closes #2167873)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9371
diff changeset
   798
            helper.postupgrade(repo)
5796
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   799
        print '-> instance migrated.'
9315
5298cfb132e6 [cwctl] Only start the instance after upgrade if it was running before
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9013
diff changeset
   800
        if instance_running and not (CWDEV or self.config.nostartstop):
5796
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   801
            # restart instance through fork to get a proper environment, avoid
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   802
            # uicfg pb (and probably gettext catalogs, to check...)
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   803
            forkcmd = '%s start %s' % (sys.argv[0], appid)
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   804
            status = system(forkcmd)
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   805
            if status:
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   806
                print '%s exited with status %s' % (forkcmd, status)
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   807
        print
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   808
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   809
    def i18nupgrade(self, config):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   810
        # handle i18n upgrade:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   811
        # * install new languages
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   812
        # * recompile catalogs
5092
e126becc1263 [upgrade] after upgrade, fork command to start instance avoid grave uicfg pb (explicit rules all removed...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5043
diff changeset
   813
        # XXX search available language in the first cube given
4021
280c910c8710 move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3720
diff changeset
   814
        from cubicweb import i18n
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   815
        templdir = cwcfg.cube_dir(config.cubes()[0])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   816
        langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   817
        errors = config.i18ncompile(langs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   818
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   819
            print '\n'.join(errors)
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   820
            if not ASK.confirm('Error while compiling message catalogs, '
5796
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   821
                               'continue anyway?'):
2512
106b2a05dc88 [cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2489
diff changeset
   822
                print '-> migration not completed.'
5796
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   823
                return False
351e84e18a61 closes #1059729: cw-ctl upgrade : i18n upgrade not performed if DB does not need upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   824
        return True
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   825
7591
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   826
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   827
class ListVersionsInstanceCommand(InstanceCommand):
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   828
    """List versions used by an instance.
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   829
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   830
    <instance>...
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   831
      identifiers of the instances to list versions for.
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   832
    """
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   833
    name = 'versions'
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   834
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   835
    def versions_instance(self, appid):
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   836
        config = cwcfg.config_for(appid)
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   837
        # should not raise error if db versions don't match fs versions
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   838
        config.repairing = True
9875
d4f6f4c77e9c [cwctl] make cubicweb-ctl versions lighter (closes #4002158)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9770
diff changeset
   839
        # no need to load all appobjects and schema
d4f6f4c77e9c [cwctl] make cubicweb-ctl versions lighter (closes #4002158)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9770
diff changeset
   840
        config.quick_start = True
7591
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   841
        if hasattr(config, 'set_sources_mode'):
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   842
            config.set_sources_mode(('migration',))
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   843
        repo = config.migration_handler().repo_connect()
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   844
        vcconf = repo.get_versions()
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   845
        for key in sorted(vcconf):
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   846
            print key+': %s.%s.%s' % vcconf[key]
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   847
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   848
class ShellCommand(Command):
3700
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   849
    """Run an interactive migration shell on an instance. This is a python shell
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   850
    with enhanced migration commands predefined in the namespace. An additional
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   851
    argument may be given corresponding to a file containing commands to execute
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   852
    in batch mode.
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   853
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   854
    By default it will connect to a local instance using an in memory
9700
da7d341cca76 an URL -> a URL
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9372
diff changeset
   855
    connection, unless a URL to a running instance is specified.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   856
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   857
    Arguments after bare "--" string will not be processed by the shell command
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   858
    You can use it to pass extra arguments to your script and expect for
5436
2455ca3a2a3a [c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5430
diff changeset
   859
    them in '__args__' afterwards.
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
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 connect.
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 = 'shell'
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   865
    arguments = '<instance> [batch command file(s)] [-- <script arguments>]'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   866
    min_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   867
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   868
        ('system-only',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   869
         {'short': 'S', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   870
          'help': 'only connect to the system source when the instance is '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   871
          'using multiple sources. You can\'t use this option and the '
3700
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   872
          '--ext-sources option at the same time.',
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   873
          'group': 'local'
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   874
         }),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   875
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   876
        ('ext-sources',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   877
         {'short': 'E', 'type' : 'csv', 'metavar': '<sources>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   878
          'help': "For multisources instances, specify to which sources the \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   879
repository should connect to for upgrading. When unspecified or 'all' given, \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   880
will connect to all defined sources. If 'migration' is given, appropriate \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   881
sources for migration will be automatically selected.",
3700
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   882
          'group': 'local'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   883
          }),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   884
2905
b23bbb31368c backport change from default to get -f option on cw shell, may be necessary for the forthcoming migration...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2743
diff changeset
   885
        ('force',
b23bbb31368c backport change from default to get -f option on cw shell, may be necessary for the forthcoming migration...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2743
diff changeset
   886
         {'short': 'f', 'action' : 'store_true',
3700
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   887
          'help': 'don\'t check instance is up to date.',
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   888
          'group': 'local'
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   889
          }),
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   890
8352
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   891
        ('repo-uri',
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   892
         {'short': 'H', 'type' : 'string', 'metavar': '<protocol>://<[host][:port]>',
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   893
          'help': 'URI of the CubicWeb repository to connect to. URI can be \
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   894
pyro://[host:port] the Pyro name server host; if the pyro nameserver is not set, \
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   895
it will be detected by using a broadcast query, a ZMQ URL or \
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: 8564
diff changeset
   896
inmemory:// (default) use an in-memory repository. THIS OPTION IS DEPRECATED, \
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: 8564
diff changeset
   897
directly give URI as instance id instead',
3700
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   898
          'group': 'remote'
fd550e4dc515 #481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   899
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   900
        )
2905
b23bbb31368c backport change from default to get -f option on cw shell, may be necessary for the forthcoming migration...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2743
diff changeset
   901
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   902
    def _handle_inmemory(self, appid):
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   903
        """ returns migration context handler & shutdown function """
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   904
        config = cwcfg.config_for(appid)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   905
        if self.config.ext_sources:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   906
            assert not self.config.system_only
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   907
            sources = self.config.ext_sources
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   908
        elif self.config.system_only:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   909
            sources = ('system',)
8352
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   910
        else:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   911
            sources = ('all',)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   912
        config.set_sources_mode(sources)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   913
        config.repairing = self.config.force
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   914
        mih = config.migration_handler()
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   915
        return mih, lambda: mih.shutdown()
8352
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   916
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   917
    def _handle_networked(self, appuri):
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   918
        """ returns migration context handler & shutdown function """
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   919
        from cubicweb import AuthenticationError
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9372
diff changeset
   920
        from cubicweb.repoapi import connect, get_repository
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   921
        from cubicweb.server.utils import manager_userpasswd
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   922
        from cubicweb.server.migractions import ServerMigrationHelper
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   923
        while True:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   924
            try:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   925
                login, pwd = manager_userpasswd(msg=None)
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9372
diff changeset
   926
                repo = get_repository(appuri)
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9372
diff changeset
   927
                cnx = connect(repo, login=login, password=pwd, mulcnx=False)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
   928
            except AuthenticationError as ex:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   929
                print ex
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   930
            except (KeyboardInterrupt, EOFError):
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   931
                print
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   932
                sys.exit(0)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   933
            else:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   934
                break
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   935
        cnx.load_appobjects()
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   936
        repo = cnx._repo
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   937
        mih = ServerMigrationHelper(None, repo=repo, cnx=cnx, verbosity=0,
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   938
                                    # hack so it don't try to load fs schema
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   939
                                    schema=1)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   940
        return mih, lambda: cnx.close()
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   941
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   942
    def run(self, args):
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: 8564
diff changeset
   943
        appuri = args.pop(0)
8352
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
   944
        if self.config.repo_uri:
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: 8564
diff changeset
   945
            warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id',
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: 8564
diff changeset
   946
                 DeprecationWarning)
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: 8564
diff changeset
   947
            if urlparse(self.config.repo_uri).scheme in ('pyro', 'inmemory'):
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: 8564
diff changeset
   948
                appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri)
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   949
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   950
        from cubicweb.utils import parse_repo_uri
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   951
        protocol, hostport, appid = parse_repo_uri(appuri)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   952
        if protocol == 'inmemory':
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   953
            mih, shutdown_callback = self._handle_inmemory(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   954
        else:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   955
            mih, shutdown_callback = self._handle_networked(appuri)
3709
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   956
        try:
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9372
diff changeset
   957
            with mih.cnx:
9556
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   958
                with mih.cnx.security_enabled(False, False):
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   959
                    if args:
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   960
                        # use cmdline parser to access left/right attributes only
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   961
                        # remember that usage requires instance appid as first argument
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   962
                        scripts, args = self.cmdline_parser.largs[1:], self.cmdline_parser.rargs
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   963
                        for script in scripts:
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   964
                                mih.cmd_process_script(script, scriptargs=args)
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   965
                                mih.commit()
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   966
                    else:
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   967
                        mih.interactive_shell()
3709
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   968
        finally:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   969
            shutdown_callback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   970
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   971
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   972
class RecompileInstanceCatalogsCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   973
    """Recompile i18n catalogs for instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   974
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   975
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   976
      identifiers of the instances to consider. If no instance is
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   977
      given, recompile for all registered instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   978
    """
1898
39b37f90a8a4 [cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1477
diff changeset
   979
    name = 'i18ninstance'
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   980
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   981
    @staticmethod
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   982
    def i18ninstance_instance(appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   983
        """recompile instance's messages catalogs"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   984
        config = cwcfg.config_for(appid)
5043
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5042
diff changeset
   985
        config.quick_start = True # notify this is not a regular start
5042
cc062bb09abb fix #726167: i18ninstance use data in bootstrab_cubes instead of the database one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   986
        repo = config.repository()
cc062bb09abb fix #726167: i18ninstance use data in bootstrab_cubes instead of the database one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   987
        if config._cubes is None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   988
            # web only config
5042
cc062bb09abb fix #726167: i18ninstance use data in bootstrab_cubes instead of the database one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   989
            config.init_cubes(repo.get_cubes())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   990
        errors = config.i18ncompile()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   991
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   992
            print '\n'.join(errors)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   993
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   994
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   995
class ListInstancesCommand(Command):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   996
    """list available instances, useful for bash completion."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   997
    name = 'listinstances'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   998
    hidden = True
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   999
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1000
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1001
        """run the command with its specific arguments"""
5021
58e89f3dfbae handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
  1002
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1003
        for appid in sorted(listdir(regdir)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1004
            print appid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1005
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1006
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1007
class ListCubesCommand(Command):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1008
    """list available componants, useful for bash completion."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1009
    name = 'listcubes'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1010
    hidden = True
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
  1011
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1012
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1013
        """run the command with its specific arguments"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
  1014
        for cube in cwcfg.available_cubes():
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1015
            print cube
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1016
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1017
class ConfigureInstanceCommand(InstanceCommand):
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1018
    """Configure instance.
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1019
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1020
    <instance>...
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1021
      identifier of the instance to configure.
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1022
    """
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1023
    name = 'configure'
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1024
    actionverb = 'configured'
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1025
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1026
    options = merge_options(InstanceCommand.options +
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1027
                            (('param',
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1028
                              {'short': 'p', 'type' : 'named', 'metavar' : 'key1:value1,key2:value2',
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1029
                               'default': None,
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1030
                               'help': 'set <key> to <value> in configuration file.',
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1031
                               }),
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1032
                             ))
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1033
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1034
    def configure_instance(self, appid):
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1035
        if self.config.param is not None:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1036
            appcfg = cwcfg.config_for(appid)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1037
            for key, value in self.config.param.iteritems():
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1038
                try:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1039
                    appcfg.global_set_option(key, value)
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1040
                except KeyError:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1041
                    raise ConfigurationError('unknown configuration key "%s" for mode %s' % (key, appcfg.name))
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1042
            appcfg.save()
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1043
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1044
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1045
# WSGI #########
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1046
9288
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1047
def wsgichoices():
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1048
    try:
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1049
        from werkzeug import serving
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1050
    except ImportError:
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1051
        return ('stdlib',)
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1052
    return ('stdlib', 'werkzeug')
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1053
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1054
class WSGIStartHandler(InstanceCommand):
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1055
    """Start an interactive wsgi server """
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1056
    name = 'wsgi'
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1057
    actionverb = 'started'
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1058
    arguments = '<instance>'
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1059
    options = (
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1060
        ("debug",
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1061
         {'short': 'D', 'action': 'store_true',
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1062
          'default': False,
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1063
          'help': 'start server in debug mode.'}),
9288
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1064
        ('method',
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1065
         {'short': 'm',
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1066
          'type': 'choice',
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1067
          'metavar': '<method>',
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1068
          'default': 'stdlib',
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1069
          'choices': wsgichoices(),
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1070
          'help': 'wsgi utility/method'}),
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1071
        ('loglevel',
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1072
         {'short': 'l',
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1073
          'type': 'choice',
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1074
          'metavar': '<log level>',
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1075
          'default': None,
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1076
          'choices': ('debug', 'info', 'warning', 'error'),
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1077
          'help': 'debug if -D is set, error otherwise',
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1078
          }),
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1079
        )
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1080
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1081
    def wsgi_instance(self, appid):
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1082
        config = cwcfg.config_for(appid, debugmode=self['debug'])
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1083
        init_cmdline_log_threshold(config, self['loglevel'])
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1084
        assert config.name == 'all-in-one'
9288
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1085
        meth = self['method']
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1086
        if meth == 'stdlib':
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1087
            from cubicweb.wsgi import server
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1088
        else:
823cf14bcc37 [wsgi] also provide an example using werkzeug (if available)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9287
diff changeset
  1089
            from cubicweb.wsgi import wz as server
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1090
        return server.run(config)
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1091
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1092
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1093
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1094
for cmdcls in (ListCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1095
               CreateInstanceCommand, DeleteInstanceCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1096
               StartInstanceCommand, StopInstanceCommand, RestartInstanceCommand,
9945
cf5b0d0f5731 [wsgi] add the --debug / -D option to the wsgi command
Christophe de Vienne <christophe@unlish.com>
parents: 9770
diff changeset
  1097
               WSGIStartHandler,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1098
               ReloadConfigurationCommand, StatusCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1099
               UpgradeInstanceCommand,
7591
cb6ece9cdb78 [cw-ctl] add command versions to list versions of cubes used by an instance (closes #508052)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
  1100
               ListVersionsInstanceCommand,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1101
               ShellCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1102
               RecompileInstanceCatalogsCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1103
               ListInstancesCommand, ListCubesCommand,
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1104
               ConfigureInstanceCommand,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1105
               ):
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1106
    CWCTL.register(cmdcls)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1107
9287
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1108
e70c8c70e344 [wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents: 9255
diff changeset
  1109
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1110
def run(args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1111
    """command line tool"""
6562
34be5986bca0 make stdout and stderr non buffered for cwctl commands (#1330647)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6505
diff changeset
  1112
    import os
34be5986bca0 make stdout and stderr non buffered for cwctl commands (#1330647)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6505
diff changeset
  1113
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
34be5986bca0 make stdout and stderr non buffered for cwctl commands (#1330647)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6505
diff changeset
  1114
    sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
  1115
    cwcfg.load_cwctl_plugins()
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1116
    try:
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1117
        CWCTL.run(args)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
  1118
    except ConfigurationError as err:
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1119
        print 'ERROR: ', err
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1120
        sys.exit(1)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8682
diff changeset
  1121
    except ExecutionError as err:
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1122
        print err
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1123
        sys.exit(2)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1124
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1125
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1126
    run(sys.argv[1:])