cwctl.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 13 Jun 2013 12:03:59 +0200
changeset 9005 63cf580accfc
parent 9002 f98d1c46ed9f
child 9013 b4bcabf55e77
permissions -rw-r--r--
notification: properly handle cnx and lifetime of the hijacked session When we create a session for the notification rendering, we need to 1) give it a cnxset 2) commit and close it after we're done 3) restore the original session for subsequent notifications This changes the ordering of actual mail sending, since there are several different commits involved, but I don't see a way to fix that short of restoring hijack_user. Related to #2934523
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8352
0e3b41118631 [dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents: 7896
diff changeset
     1
# copyright 2003-2012 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
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
    51
if support_args(CommandLine, 'check_duplicated_command'):
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
    52
    # don't check duplicated commands, it occurs when reloading site_cubicweb
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
    53
    CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
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
    54
                        version=version, check_duplicated_command=False)
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
    55
else:
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
    56
    CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
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
    57
                        version=version)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    58
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
def wait_process_end(pid, maxtry=10, waittime=1):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    """wait for a process to actually die"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    import signal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
    from time import sleep
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
    nbtry = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    while nbtry < maxtry:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
            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
    67
        except (OSError, AttributeError): # XXX win32
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        nbtry += 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        sleep(waittime)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        raise ExecutionError('can\'t kill process %s' % pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
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
    75
    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
    76
        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
    77
    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
    78
        return []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
def detect_available_modes(templdir):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    modes = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    for fname in ('schema', 'schema.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('repository')
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
    for fname in ('data', 'views', 'views.py'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        if exists(join(templdir, fname)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
            modes.append('web ui')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    return modes
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    91
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
    92
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    93
class InstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    94
    """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
    95
    (0 meaning all registered instances)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    """
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
    97
    arguments = '[<instance>...]'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        ("force",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
         {'short': 'f', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
          'default': False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
          'help': 'force command without asking confirmation',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
          }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
         ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
    actionverb = None
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   107
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    def ordered_instances(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        """return instances in the order in which they should be started,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        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
   111
        some instances depends on another as external source).
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   112
af81a4f7053e enhance docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3562
diff changeset
   113
        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
   114
        instance per line)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        """
5021
58e89f3dfbae handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4798
diff changeset
   116
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        _allinstances = list_instances(regdir)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        if isfile(join(regdir, 'startorder')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
            allinstances = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
            for line in file(join(regdir, 'startorder')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
                line = line.strip()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
                if line and not line.startswith('#'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
                    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
                        _allinstances.remove(line)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
                        allinstances.append(line)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
                    except ValueError:
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   127
                        print ('ERROR: startorder file contains unexistant '
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   128
                               'instance %s' % line)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
            allinstances += _allinstances
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
            allinstances = _allinstances
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        return allinstances
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   133
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
    def run(self, args):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   135
        """run the <command>_method on each argument (a list of instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        identifiers)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        if not args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
            args = self.ordered_instances()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
                askconfirm = not self.config.force
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
                # no force option
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
                askconfirm = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
            askconfirm = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        self.run_args(args, askconfirm)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   148
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
    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
   150
        status = 0
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   154
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
                    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
   156
            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
   157
                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
   158
            except (KeyboardInterrupt, SystemExit):
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   159
                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
   160
                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
   161
        sys.exit(status)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   162
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
    def run_arg(self, appid):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   164
        cmdmeth = getattr(self, '%s_instance' % self.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
        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
   166
            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
   167
        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
   168
            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
   169
                    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
   170
            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
   171
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
            import traceback
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
            traceback.print_exc()
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   174
            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
   175
                    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
   176
            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
   177
        return status
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   179
class InstanceCommandFork(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   180
    """Same as `InstanceCommand`, but command is forked in a new environment
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
    for each argument
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
    def run_args(self, args, askconfirm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        if len(args) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
            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
   187
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
            forkcmd = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   192
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
            if forkcmd:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
                status = system('%s %s' % (forkcmd, appid))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
                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
   197
                    print '%s exited with status %s' % (forkcmd, status)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
                self.run_arg(appid)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   200
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   201
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
# base commands ###############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
class ListCommand(Command):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   205
    """List configurations, cubes and instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
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
   207
    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
   208
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   209
    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
   210
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    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
   212
    arguments = '[all|cubes|configurations|instances]'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
        ('verbose',
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   215
         {'short': 'v', 'action' : 'store_true',
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   216
          'help': "display more information."}),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
        )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   218
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
        """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
   221
        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
   222
            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
   223
        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
   224
            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
   225
        else:
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5498
diff changeset
   226
            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
   227
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
   228
        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
   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 == '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
   231
            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
   232
            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
   233
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
        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
   235
            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
   236
            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
   237
                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
   238
                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
   239
                    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
   240
                    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
   241
                        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
   242
                    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
   243
            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
   244
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
        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
   246
            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
   247
            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
   248
                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
   249
                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
   250
            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
   251
                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
   252
            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
   253
                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
   254
            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
   255
                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
   256
                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
   257
                    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
   258
                        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
   259
                        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
   260
                        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
   261
                    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
   262
                        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
   263
                        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
   264
                    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
   265
                    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
   266
                        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
   267
                            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
   268
                            if not descr:
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
                                descr = getattr(tinfo, 'short_desc', '')
bb14dc9848ec [c-c list] add an optional argument to the list command (closes #2709703)
David Douard <david.douard@logilab.fr>
parents: 8718
diff changeset
   270
                                if descr:
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
                                    warn('[3.8] short_desc is deprecated, update %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
   272
                                         ' pkginfo' % cube, DeprecationWarning)
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
                                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
   274
                                    descr = tinfo.__doc__
5025
2826f5406201 deprecate __use__ and __recommend__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5021
diff changeset
   275
                            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
   276
                                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
   277
                        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
   278
                        print '    available modes: %s' % ', '.join(modes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
            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
   280
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
        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
   282
            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
   283
                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
   284
            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
   285
                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
   286
                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
   287
                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
   288
            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
   289
            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
   290
                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
   291
                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
   292
                    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
   293
                    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
   294
                        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
   295
                        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
   296
                    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
   297
                    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
   298
                        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
   299
                    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
   300
                        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
   301
                        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
   302
            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
   303
                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
   304
            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
   305
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
        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
   307
            # 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
   308
            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
   309
            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
   310
                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
   311
            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
   312
                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
   313
                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
   314
                    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
   315
                        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
   316
                        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
   317
                            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
   318
                        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
   319
                    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
   320
                        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
   321
                            cube, cfgpb.cubes[cube], version, src)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
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
   323
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
   324
    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
   325
        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
   326
                              '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
   327
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   328
class CreateInstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   329
    """Create an instance from a cube. This is an unified
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
    command which can handle web / server / all-in-one installation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
    according to available parts of the software library and of the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
    desired cube.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
    <cube>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
      the name of cube to use (list available cube names using
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
      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
   337
      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
   338
    <instance>
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   339
      an identifier for the instance to create
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
    name = 'create'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   342
    arguments = '<cube> <instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   343
    min_args = max_args = 2
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
    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
   345
        ('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
   346
         {'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
   347
          '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
   348
          '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
   349
          '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
   350
          '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
   351
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   352
        ('config-level',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
         {'short': 'l', 'type' : 'int', 'metavar': '<level>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
          '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
   355
          '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
   356
          '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
   357
          }),
ba51dac1115d [c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7131
diff changeset
   358
        ('config',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
         {'short': 'c', 'type' : 'choice', 'metavar': '<install type>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
          'choices': ('all-in-one', 'repository', 'twisted'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
          '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
   362
          '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
   363
          '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
   364
          ' "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
   365
          '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
   366
          }),
9001
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   367
        ('no-db-create',
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   368
         {'short': 'S',
8851
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   369
          'action': 'store_true',
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   370
          'default': False,
9001
328c24289253 [cwctl] rename option no-post-create to no-db-create
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8930
diff changeset
   371
          '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
   372
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   374
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
        """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
   377
        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
   378
        check_options_consistency(self.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
        configname = self.config.config
6144
40af8e328e64 [c-c create] argument inversed accidentally
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6138
diff changeset
   380
        cubes, appid = args
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   381
        cubes = splitstrip(cubes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
        # 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
   383
        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
   384
        cubes = config.expand_cubes(cubes)
b4ffcea3275b change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2032
diff changeset
   385
        config.init_cubes(cubes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
        helper = self.config_helper(config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        # check the cube exists
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
        try:
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   389
            templdirs = [cwcfg.cube_dir(cube)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
                         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
   391
        except ConfigurationError as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
            print ex
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
            print '\navailable cubes:',
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   394
            print ', '.join(cwcfg.available_cubes())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
            return
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   396
        # 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
   397
        print '\n'+underline_title('Creating the instance %s' % appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
        create_dir(config.apphome)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
        # 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
   400
        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
   401
            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
   402
                                       % 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
   403
            config.input_config('main', self.config.config_level)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
        # configuration'specific stuff
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
        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
   406
        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
   407
        # input for cubes specific options
7131
5893b899c21f [c-c create] we should also skip option with no type, else we may generate title for section without any option to fill
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7129
diff changeset
   408
        sections = set(sect.lower() for sect, opt, odict in config.all_options()
5893b899c21f [c-c create] we should also skip option with no type, else we may generate title for section without any option to fill
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7129
diff changeset
   409
                       if 'type' in odict
5893b899c21f [c-c create] we should also skip option with no type, else we may generate title for section without any option to fill
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7129
diff changeset
   410
                       and odict.get('level') <= self.config.config_level)
5893b899c21f [c-c create] we should also skip option with no type, else we may generate title for section without any option to fill
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7129
diff changeset
   411
        for section in sections:
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
   412
            if section not in ('main', 'email', 'pyro', 'web'):
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
   413
                print '\n' + underline_title('%s options' % section)
7bf205b9a845 when creating an instance, ask for cubes specific options properly (fix #607349)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4609
diff changeset
   414
                config.input_config(section, self.config.config_level)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
        # write down configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
        config.save()
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   417
        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
   418
        print '-> generated config %s' % config.main_config_file()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
        # handle i18n files structure
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
        # 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
   421
        from cubicweb import i18n
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
        langs = [lang for lang, _ in i18n.available_catalogs(join(templdirs[0], 'i18n'))]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
        errors = config.i18ncompile(langs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
            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
   426
            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
   427
                   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
   428
                                      'continue anyway ?'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
                print 'creation not completed'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
                return
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   431
        # create the additional data directory for this instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
        if config.appdatahome != config.apphome: # true in dev mode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
            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
   434
        create_dir(join(config.appdatahome, 'backup'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
        if config['uid']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
            from logilab.common.shellutils import chown
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
            # this directory should be owned by the uid of the server process
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
            print 'set %s as owner of the data directory' % config['uid']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
            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
   440
        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
   441
        if not self.config.no_db_create:
8851
0020aa12af07 [c-c create] make post-create step optional (closes #2712041)
David Douard <david.douard@logilab.fr>
parents: 8829
diff changeset
   442
            helper.postcreate(self.config.automatic)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   444
    def _handle_win32(self, config, appid):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   445
        if sys.platform != 'win32':
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   446
            return
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   447
        service_template = """
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   448
import sys
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   449
import win32serviceutil
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   450
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
   451
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   452
from cubicweb.etwist.service import CWService
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
classdict = {'_svc_name_': 'cubicweb-%(APPID)s',
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   455
             '_svc_display_name_': 'CubicWeb ' + '%(CNAME)s',
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   456
             'instance': '%(APPID)s'}
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   457
%(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
   458
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   459
if __name__ == '__main__':
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   460
    win32serviceutil.HandleCommandLine(%(CNAME)sService)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   461
"""
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   462
        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
   463
            service_template % {'APPID': appid,
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   464
                                'CNAME': appid.capitalize(),
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   465
                                'CWPATH': abspath(join(dirname(__file__), '..'))})
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents: 3715
diff changeset
   466
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   467
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   468
class DeleteInstanceCommand(Command):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   469
    """Delete an instance. Will remove instance's files and
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
    unregister it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   472
    name = 'delete'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   473
    arguments = '<instance>'
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
   474
    min_args = max_args = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
    options = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
        """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
   479
        appid = args[0]
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   480
        configs = [cwcfg.config_for(appid, configname)
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   481
                   for configname in cwcfg.possible_configurations(appid)]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   482
        if not configs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
            raise ExecutionError('unable to guess configuration for %s' % appid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
        for config in configs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
            helper = self.config_helper(config, required=False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
            if helper:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
                helper.cleanup()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
        # remove home
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
        rm(config.apphome)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
        # remove instance data directory
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
            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
   493
        except OSError as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
            import errno
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
            if ex.errno != errno.ENOENT:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
                raise
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
        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
   498
        print '-> instance %s (%s) deleted.' % (appid, confignames)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   501
# instance commands ########################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   503
class StartInstanceCommand(InstanceCommandFork):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   504
    """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
   505
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   506
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   507
      identifiers of the instances to start. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
      given, start them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
    name = 'start'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
    actionverb = 'started'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
    options = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
        ("debug",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
         {'short': 'D', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
          'help': 'start server in debug mode.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
        ("force",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
         {'short': 'f', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
          'default': False,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   519
          'help': 'start the instance even if it seems to be already \
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
running.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   521
        ('profile',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
         {'short': 'P', 'type' : 'string', 'metavar': '<stat file>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   524
          'help': 'profile code and use the specified file to store stats',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   525
          }),
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
   526
        ('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
   527
         {'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
   528
          '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
   529
          '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
   530
          }),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   532
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   533
    def start_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   534
        """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
   535
        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
   536
        init_cmdline_log_threshold(config, self['loglevel'])
3182
76ad941f694e cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3180
diff changeset
   537
        if self['profile']:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   538
            config.global_set_option('profile', self.config.profile)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
        helper = self.config_helper(config, cmdname='start')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
        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
   541
        if exists(pidf) and not self['force']:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   542
            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
   543
the --force option."
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   544
            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
   545
        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
   546
            print 'instance %s started' % appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
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
   549
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
   550
    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
   551
        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
   552
        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
   553
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
   554
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   555
class StopInstanceCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   556
    """Stop the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   557
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   558
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   559
      identifiers of the instances to stop. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   560
      given, stop them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
    name = 'stop'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   563
    actionverb = 'stopped'
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   564
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
    def ordered_instances(self):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   566
        instances = super(StopInstanceCommand, self).ordered_instances()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
        instances.reverse()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
        return instances
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   569
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   570
    def stop_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   571
        """stop the instance's server"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   572
        config = cwcfg.config_for(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
        helper = self.config_helper(config, cmdname='stop')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
        helper.poststop() # do this anyway
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   575
        pidf = config['pid-file']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
        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
   577
            sys.stderr.write("%s doesn't exist.\n" % pidf)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
        import signal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
        pid = int(open(pidf).read().strip())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   582
            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
   583
        except Exception:
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7815
diff changeset
   584
            sys.stderr.write("process %s seems already dead.\n" % pid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   585
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   587
                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
   588
            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
   589
                sys.stderr.write('%s\ntrying SIGKILL\n' % ex)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   590
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
                    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
   592
                except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   593
                    # probably dead now
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   594
                    pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
                wait_process_end(pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
            remove(pidf)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
        except OSError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
            # already removed by twistd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
            pass
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   601
        print 'instance %s stopped' % appid
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   602
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   604
class RestartInstanceCommand(StartInstanceCommand):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   605
    """Restart the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   606
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   607
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   608
      identifiers of the instances to restart. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   609
      given, restart them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   610
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
    name = 'restart'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   612
    actionverb = 'restarted'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   613
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   614
    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
   615
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   616
        if not isfile(join(regdir, 'startorder')) or len(args) <= 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   617
            # no specific startorder
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   618
            super(RestartInstanceCommand, self).run_args(args, askconfirm)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   619
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   620
        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
   621
               'instances then restart them.')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   622
        # get instances in startorder
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   623
        for appid in args:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   624
            if askconfirm:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   625
                print '*'*72
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   626
                if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   627
                    continue
6145
528be1e059aa more cwctl api update and fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6144
diff changeset
   628
            StopInstanceCommand(self.logger).stop_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   629
        forkcmd = [w for w in sys.argv if not w in args]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   630
        forkcmd[1] = 'start'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   631
        forkcmd = ' '.join(forkcmd)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   632
        for appid in reversed(args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   633
            status = system('%s %s' % (forkcmd, appid))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   634
            if status:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   635
                sys.exit(status)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   636
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   637
    def restart_instance(self, appid):
6145
528be1e059aa more cwctl api update and fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6144
diff changeset
   638
        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
   639
        self.start_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   640
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   641
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   642
class ReloadConfigurationCommand(RestartInstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   643
    """Reload the given instances. This command is equivalent to a
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   644
    restart for now.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   645
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   646
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   647
      identifiers of the instances to reload. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   648
      given, reload them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   649
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   650
    name = 'reload'
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   651
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   652
    def reload_instance(self, appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   653
        self.restart_instance(appid)
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   654
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   655
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   656
class StatusCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   657
    """Display status information about the given instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   658
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   659
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   660
      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
   661
      given, get status information about all registered instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   662
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   663
    name = 'status'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   664
    options = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   665
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   666
    @staticmethod
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   667
    def status_instance(appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   668
        """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
   669
        status = 0
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   670
        for mode in cwcfg.possible_configurations(appid):
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   671
            config = cwcfg.config_for(appid, mode)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   672
            print '[%s-%s]' % (appid, mode),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   673
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
                pidf = config['pid-file']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
            except KeyError:
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   676
                print 'buggy instance, pid file not specified'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   677
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
            if not exists(pidf):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
                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
   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
            pid = int(open(pidf).read().strip())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
            # trick to guess whether or not the process is running
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   685
                getpgid(pid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   686
            except OSError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
                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
   688
                status = 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   689
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   690
            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
   691
        return status
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   692
3180
6bab5746ebf5 [c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2905
diff changeset
   693
class UpgradeInstanceCommand(InstanceCommandFork):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   694
    """Upgrade an instance after cubicweb and/or component(s) upgrade.
0
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
    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
   697
    to connect to the system database.  For some upgrades, the given user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   698
    should have create or alter table permissions.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   699
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   700
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   701
      identifiers of the instances to upgrade. If no instance is
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   702
      given, upgrade them all.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   703
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
    name = 'upgrade'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
    actionverb = 'upgraded'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   706
    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
   707
        ('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
   708
         {'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
   709
          '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
   710
          '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
   711
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   712
        ('force-cubicweb-version',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   713
         {'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
          'help': 'force migration from the indicated cubicweb version.'}),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   716
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
        ('fs-only',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   718
         {'short': 's', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
          'default': False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
          'help': 'only upgrade files on the file system, not the database.'}),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   722
        ('nostartstop',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   723
         {'short': 'n', 'action' : 'store_true',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   724
          'default': False,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   725
          '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
   726
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
        ('verbosity',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   728
         {'short': 'v', 'type' : 'int', 'metavar': '<0..2>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   729
          'default': 1,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   730
          'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   731
for everything."}),
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   732
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
        ('backup-db',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
         {'short': 'b', 'type' : 'yn', 'metavar': '<y or n>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
          'default': None,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   736
          'help': "Backup the instance database before upgrade.\n"\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
          "If the option is ommitted, confirmation will be ask.",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
          }),
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
        ('ext-sources',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
         {'short': 'E', 'type' : 'csv', 'metavar': '<sources>',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   742
          'default': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   743
          'help': "For multisources instances, specify to which sources the \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
repository should connect to for upgrading. When unspecified or 'migration' is \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
given, appropriate sources for migration will be automatically selected \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
(recommended). If 'all' is given, will connect to all defined sources.",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
          }),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
        )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   750
    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
   751
        print '\n' + underline_title('Upgrading the instance %s' % appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
        from logilab.common.changelog import Version
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   753
        config = cwcfg.config_for(appid)
2473
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2395
diff changeset
   754
        config.repairing = True # notice we're not starting the server
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   755
        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
   756
        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
   757
        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
   758
            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
   759
        # get instance and installed versions for the server and the componants
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
        mih = config.migration_handler()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
        repo = mih.repo_connect()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
        vcconf = repo.get_versions()
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
   763
        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
   764
            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
   765
                vcconf[cube] = Version(version)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
        toupgrade = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   767
        for cube in config.cubes():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
            installedversion = config.cube_version(cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   770
                applversion = vcconf[cube]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   771
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
                config.error('no version information for %s' % cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   773
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   774
            if installedversion > applversion:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   775
                toupgrade.append( (cube, applversion, installedversion) )
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   776
        cubicwebversion = config.cubicweb_version()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   777
        if self.config.force_cubicweb_version:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   778
            applcubicwebversion = Version(self.config.force_cubicweb_version)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   779
            vcconf['cubicweb'] = applcubicwebversion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   780
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
            applcubicwebversion = vcconf.get('cubicweb')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
        if cubicwebversion > applcubicwebversion:
2275
bc0bed0616a3 fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2156
diff changeset
   783
            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
   784
        # only stop once we're sure we have something to do
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
   785
        if 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
   786
            StopInstanceCommand(self.logger).stop_instance(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   787
        # 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
   788
        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
   789
            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
   790
                print '-> migration needed from %s to %s for %s' % (fromversion, toversion, cube)
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
   791
            mih.migrate(vcconf, reversed(toupgrade), self.config)
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
   792
        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
   793
            print '-> no data migration needed for instance %s.' % appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   794
        # rewrite main configuration file
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   795
        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
   796
        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
   797
        # 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
   798
        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
   799
            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
   800
        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
   801
        print '-> instance migrated.'
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
        if not (CWDEV or self.config.nostartstop):
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
            # 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
   804
            # 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
   805
            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
   806
            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
   807
            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
   808
                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
   809
        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
   810
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
   811
    def i18nupgrade(self, config):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   812
        # handle i18n upgrade:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   813
        # * install new languages
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   814
        # * 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
   815
        # 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
   816
        from cubicweb import i18n
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   817
        templdir = cwcfg.cube_dir(config.cubes()[0])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   818
        langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   819
        errors = config.i18ncompile(langs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   820
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   821
            print '\n'.join(errors)
2743
b0e79a77ad67 [c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2654
diff changeset
   822
            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
   823
                               'continue anyway?'):
2512
106b2a05dc88 [cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2489
diff changeset
   824
                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
   825
                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
   826
        return True
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   827
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
   828
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
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
   830
    """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
   831
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
    <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
   833
      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
   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
    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
   836
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
    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
   838
        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
   839
        # 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
   840
        config.repairing = True
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
8716
dd53d8d63b4b [c-c shell] fix command description (-P option is gone long ago)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8564
diff changeset
   855
    connection, unless an 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
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   920
        from cubicweb.dbapi import connect
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)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   926
                cnx = connect(appuri, 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
   927
            except AuthenticationError as ex:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   928
                print ex
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   929
            except (KeyboardInterrupt, EOFError):
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   930
                print
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   931
                sys.exit(0)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   932
            else:
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   933
                break
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   934
        cnx.load_appobjects()
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   935
        repo = cnx._repo
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   936
        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
   937
                                    # 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
   938
                                    schema=1)
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   939
        return mih, lambda: cnx.close()
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   940
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   941
    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
   942
        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
   943
        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
   944
            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
   945
                 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
   946
            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
   947
                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
   948
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   949
        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
   950
        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
   951
        if protocol == 'inmemory':
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   952
            mih, shutdown_callback = self._handle_inmemory(appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   953
        else:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   954
            mih, shutdown_callback = self._handle_networked(appuri)
3709
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   955
        try:
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   956
            if args:
5430
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   957
                # use cmdline parser to access left/right attributes only
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   958
                # remember that usage requires instance appid as first argument
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   959
                scripts, args = self.cmdline_parser.largs[1:], self.cmdline_parser.rargs
ed8f71e244f8 [shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5426
diff changeset
   960
                for script in scripts:
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
   961
                    mih.cmd_process_script(script, scriptargs=args)
7372
65bd75f16d65 [c-c shell] we should commit the migration handler, cnx is not None unless connected through pyro
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7369
diff changeset
   962
                    mih.commit()
3709
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   963
            else:
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   964
                mih.interactive_shell()
e67ade20bf80 properly close remote connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   965
        finally:
8682
20bd1cdf86ae [cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8669
diff changeset
   966
            shutdown_callback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   967
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   968
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   969
class RecompileInstanceCatalogsCommand(InstanceCommand):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   970
    """Recompile i18n catalogs for instances.
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   971
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   972
    <instance>...
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   973
      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
   974
      given, recompile for all registered instances.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   975
    """
1898
39b37f90a8a4 [cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1477
diff changeset
   976
    name = 'i18ninstance'
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   977
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   978
    @staticmethod
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   979
    def i18ninstance_instance(appid):
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   980
        """recompile instance's messages catalogs"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
   981
        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
   982
        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
   983
        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
   984
        if config._cubes is None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   985
            # 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
   986
            config.init_cubes(repo.get_cubes())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   987
        errors = config.i18ncompile()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   988
        if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   989
            print '\n'.join(errors)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   990
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   991
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   992
class ListInstancesCommand(Command):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   993
    """list available instances, useful for bash completion."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   994
    name = 'listinstances'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   995
    hidden = True
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
   996
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   997
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   998
        """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
   999
        regdir = cwcfg.instances_dir()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1000
        for appid in sorted(listdir(regdir)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1001
            print appid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1002
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1003
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1004
class ListCubesCommand(Command):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1005
    """list available componants, useful for bash completion."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1006
    name = 'listcubes'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1007
    hidden = True
1446
e951a860eeaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1342
diff changeset
  1008
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1009
    def run(self, args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1010
        """run the command with its specific arguments"""
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
  1011
        for cube in cwcfg.available_cubes():
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1012
            print cube
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1013
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1014
class ConfigureInstanceCommand(InstanceCommand):
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1015
    """Configure instance.
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1016
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1017
    <instance>...
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1018
      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
  1019
    """
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1020
    name = 'configure'
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1021
    actionverb = 'configured'
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
    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
  1024
                            (('param',
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1025
                              {'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
  1026
                               'default': None,
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1027
                               '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
  1028
                               }),
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1029
                             ))
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1030
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1031
    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
  1032
        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
  1033
            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
  1034
            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
  1035
                try:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1036
                    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
  1037
                except KeyError:
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1038
                    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
  1039
            appcfg.save()
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1040
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1041
for cmdcls in (ListCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1042
               CreateInstanceCommand, DeleteInstanceCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1043
               StartInstanceCommand, StopInstanceCommand, RestartInstanceCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1044
               ReloadConfigurationCommand, StatusCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1045
               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
  1046
               ListVersionsInstanceCommand,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1047
               ShellCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1048
               RecompileInstanceCatalogsCommand,
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1049
               ListInstancesCommand, ListCubesCommand,
9002
f98d1c46ed9f [cwctl] add configure command to cw-ctl (closes #2709702)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9001
diff changeset
  1050
               ConfigureInstanceCommand,
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1051
               ):
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1052
    CWCTL.register(cmdcls)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1053
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1054
def run(args):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1055
    """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
  1056
    import os
34be5986bca0 make stdout and stderr non buffered for cwctl commands (#1330647)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6505
diff changeset
  1057
    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
  1058
    sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 151
diff changeset
  1059
    cwcfg.load_cwctl_plugins()
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1060
    try:
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1061
        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
  1062
    except ConfigurationError as err:
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1063
        print 'ERROR: ', err
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1064
        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
  1065
    except ExecutionError as err:
6138
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1066
        print err
65f5e488f983 update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5814
diff changeset
  1067
        sys.exit(2)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1068
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1069
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1070
    run(sys.argv[1:])