Skip to content

Commit

Permalink
Merge ae114a6 into aa5638b
Browse files Browse the repository at this point in the history
  • Loading branch information
kovrus committed Jul 14, 2016
2 parents aa5638b + ae114a6 commit fda797f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
40 changes: 4 additions & 36 deletions src/crate/crash/command.py
Expand Up @@ -45,13 +45,9 @@

from appdirs import user_data_dir, user_config_dir

from distutils.version import StrictVersion

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

CHECK_MIN_VERSION = StrictVersion("0.52.0")


try:
from logging import NullHandler
Expand Down Expand Up @@ -191,7 +187,6 @@ def __init__(self,
'c': self._connect,
'connect': self._connect,
'dt': self._show_tables,
'check': self._check,
'sysinfo': self.sys_info_cmd.execute,
}
self.commands.update(built_in_commands)
Expand Down Expand Up @@ -242,36 +237,6 @@ def _show_tables(self, *args):
from information_schema.tables
where schema_name not in ('sys','information_schema')""")

def check(self, *args):
success = self._execute("""select description as "Failed checks"
from sys.checks
where passed=false """)
self.exit_code = self.exit_code or int(not success)
if not success:
return False
cur = self.cursor
print_vars = {
's': 'S'[cur.rowcount == 1:],
'rowcount': cur.rowcount
}
checks = cur.fetchall()
if len(checks):
self.pprint(checks, [c[0] for c in cur.description])
tmpl = '{rowcount} CLUSTER CHECK{s} FAILED'
self.logger.critical(tmpl.format(**print_vars))
else:
self.logger.info('CLUSTER CHECK OK')
return True

@noargs_command
def _check(self, *args):
""" print failed cluster checks """
if self.connection.lowest_server_version >= CHECK_MIN_VERSION:
self.check()
else:
tmpl = '\nCrate {version} does not support the cluster "check" command'
self.logger.warn(tmpl.format(version=self.connection.lowest_server_version))

@noargs_command
def _quit(self, *args):
""" quit crash """
Expand Down Expand Up @@ -306,7 +271,10 @@ def _connect(self, server):
self.logger.critical('CONNECT ERROR')
else:
self.logger.info('CONNECT OK')
self._check()

# check for failing checks in a cluster
check = built_in_commands.get("check")
check.cluster_check(self)

def _try_exec_cmd(self, line):
words = line.split(' ', 1)
Expand Down
39 changes: 39 additions & 0 deletions src/crate/crash/commands.py
Expand Up @@ -20,6 +20,8 @@
import functools
import glob

from distutils.version import StrictVersion


class Command(object):
def complete(self, cmd, text):
Expand Down Expand Up @@ -89,10 +91,47 @@ def __call__(self, cmd, *args, **kwargs):
cmd._autocomplete and 'ON' or 'OFF'
)

class ClusterCheckCommand(Command):
""" print failed cluster checks """

CLUSTER_CHECK_STMT = """
SELECT description AS "Failed checks"
FROM sys.checks
WHERE passed = false
ORDER BY id ASC"""

@noargs_command
def __call__(self, cmd, *args, **kwargs):
if cmd.connection.lowest_server_version >= StrictVersion("0.52.0"):
self.cluster_check(cmd)
else:
tmpl = '\nCrate {version} does not support the cluster "check" command'
cmd.logger.warn(tmpl.format(version=cmd.connection.lowest_server_version))

def cluster_check(self, cmd):
success = cmd._execute(ClusterCheckCommand.CLUSTER_CHECK_STMT)
cmd.exit_code = cmd.exit_code or int(not success)
if not success:
return False
cur = cmd.cursor
print_vars = {
's': 'S'[cur.rowcount == 1:],
'rowcount': cur.rowcount
}
checks = cur.fetchall()
if len(checks):
cmd.pprint(checks, [c[0] for c in cur.description])
tmpl = '{rowcount} CLUSTER CHECK{s} FAILED'
cmd.logger.critical(tmpl.format(**print_vars))
else:
cmd.logger.info('CLUSTER CHECK OK')
return True


built_in_commands = {
'?': HelpCommand(),
'r': ReadFileCommand(),
'format': SwitchFormatCommand(),
'autocomplete': ToggleAutocompleteCommand(),
'check': ClusterCheckCommand(),
}

0 comments on commit fda797f

Please sign in to comment.