Source code for aiida.cmdline.commands.cmd_status

# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved.                     #
# This file is part of the AiiDA code.                                    #
#                                                                         #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file        #
# For further information please visit http://www.aiida.net               #
###########################################################################
"""`verdi status` command."""
import sys

import enum
import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.common.log import override_log_level
from ..utils.echo import ExitCode


[docs]class ServiceStatus(enum.IntEnum): """Describe status of services for 'verdi status' command.""" UP = 0 # pylint: disable=invalid-name ERROR = 1 WARNING = 2 DOWN = 3
STATUS_SYMBOLS = { ServiceStatus.UP: { 'color': 'green', 'string': '\u2714', }, ServiceStatus.ERROR: { 'color': 'red', 'string': '\u2718', }, ServiceStatus.WARNING: { 'color': 'yellow', 'string': '\u23FA', }, ServiceStatus.DOWN: { 'color': 'red', 'string': '\u2718', }, } @verdi.command('status') @click.option('--no-rmq', is_flag=True, help='Do not check RabbitMQ status') def verdi_status(no_rmq): """Print status of AiiDA services.""" # pylint: disable=broad-except,too-many-statements from aiida.cmdline.utils.daemon import get_daemon_status, delete_stale_pid_file from aiida.common.utils import Capturing from aiida.manage.external.rmq import get_rmq_url from aiida.manage.manager import get_manager from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER exit_code = ExitCode.SUCCESS print_status(ServiceStatus.UP, 'config dir', AIIDA_CONFIG_FOLDER) manager = get_manager() profile = manager.get_profile() try: profile = manager.get_profile() print_status(ServiceStatus.UP, 'profile', 'On profile {}'.format(profile.name)) except Exception as exc: print_status(ServiceStatus.ERROR, 'profile', 'Unable to read AiiDA profile', exception=exc) sys.exit(ExitCode.CRITICAL) # stop here - without a profile we cannot access anything # Getting the repository repo_folder = 'undefined' try: repo_folder = profile.repository_path except Exception as exc: print_status(ServiceStatus.ERROR, 'repository', 'Error with repo folder', exception=exc) exit_code = ExitCode.CRITICAL else: print_status(ServiceStatus.UP, 'repository', repo_folder) # Getting the postgres status by trying to get a database cursor database_data = [profile.database_username, profile.database_hostname, profile.database_port] try: with override_log_level(): # temporarily suppress noisy logging backend = manager.get_backend() backend.cursor() except Exception: print_status(ServiceStatus.DOWN, 'postgres', 'Unable to connect as {}@{}:{}'.format(*database_data)) exit_code = ExitCode.CRITICAL else: print_status(ServiceStatus.UP, 'postgres', 'Connected as {}@{}:{}'.format(*database_data)) # Getting the rmq status if not no_rmq: try: with Capturing(capture_stderr=True): with override_log_level(): # temporarily suppress noisy logging comm = manager.create_communicator(with_orm=False) comm.stop() except Exception as exc: print_status(ServiceStatus.ERROR, 'rabbitmq', 'Unable to connect to rabbitmq', exception=exc) exit_code = ExitCode.CRITICAL else: print_status(ServiceStatus.UP, 'rabbitmq', 'Connected to {}'.format(get_rmq_url())) # Getting the daemon status try: client = manager.get_daemon_client() delete_stale_pid_file(client) daemon_status = get_daemon_status(client) daemon_status = daemon_status.split('\n')[0] # take only the first line if client.is_daemon_running: print_status(ServiceStatus.UP, 'daemon', daemon_status) else: print_status(ServiceStatus.WARNING, 'daemon', daemon_status) exit_code = ExitCode.SUCCESS # A daemon that is not running is not a failure except Exception as exc: print_status(ServiceStatus.ERROR, 'daemon', 'Error getting daemon status', exception=exc) exit_code = ExitCode.CRITICAL # Note: click does not forward return values to the exit code, see https://github.com/pallets/click/issues/747 sys.exit(exit_code)