def create_empty_config_instance(tmp_path) -> Config: """Create a temporary configuration instance. This creates a temporary directory with a clean `.aiida` folder and basic configuration file. The currently loaded configuration and profile are stored in memory and are automatically restored at the end of this context manager. :return: a new empty config instance. """ from aiida.common.utils import Capturing from aiida.manage import configuration from aiida.manage.configuration import settings, load_profile, reset_profile # Store the current configuration instance and config directory path current_config = configuration.CONFIG current_config_path = current_config.dirpath current_profile_name = configuration.PROFILE.name reset_profile() configuration.CONFIG = None # Create a temporary folder, set it as the current config directory path and reset the loaded configuration settings.AIIDA_CONFIG_FOLDER = str(tmp_path) # Create the instance base directory structure, the config file and a dummy profile settings.create_instance_directories() # The constructor of `Config` called by `load_config` will print warning messages about migrating it with Capturing(): configuration.CONFIG = configuration.load_config(create=True) yield get_config() # Reset the config folder path and the config instance. Note this will always be executed after the yield no # matter what happened in the test that used this fixture. reset_profile() settings.AIIDA_CONFIG_FOLDER = current_config_path configuration.CONFIG = current_config load_profile(current_profile_name)
def stop(no_wait, all_profiles): """Stop the daemon.""" from aiida.engine.daemon.client import get_daemon_client config = get_config() if all_profiles is True: profiles = [profile for profile in config.profiles if not profile.is_test_profile] else: profiles = [config.current_profile] for profile in profiles: client = get_daemon_client(profile.name) click.secho('Profile: ', fg='red', bold=True, nl=False) click.secho('{}'.format(profile.name), bold=True) if not client.is_daemon_running: echo.echo('Daemon was not running') continue delete_stale_pid_file(client) wait = not no_wait if wait: echo.echo('Waiting for the daemon to shut down... ', nl=False) else: echo.echo('Shutting the daemon down') response = client.stop_daemon(wait) if wait: if response['status'] == client.DAEMON_ERROR_NOT_RUNNING: click.echo('The daemon was not running.') else: print_client_response_status(response)
def convert(self, value, param, ctx): """Attempt to match the given value to a valid profile.""" from aiida.common.exceptions import MissingConfigurationError, ProfileConfigurationError from aiida.manage.configuration import get_config, load_profile, Profile try: config = get_config(create=True) profile = config.get_profile(value) except (MissingConfigurationError, ProfileConfigurationError) as exception: if not self._cannot_exist: self.fail(str(exception)) # Create a new empty profile profile = Profile(value, {}) else: if self._cannot_exist: self.fail(str('the profile `{}` already exists'.format(value))) if self._load_profile: load_profile(profile.name) return profile
def check_worker_load(active_slots): """ Check if the percentage usage of the daemon worker slots exceeds a threshold. If it does, print a warning. The purpose of this check is to warn the user if they are close to running out of worker slots which could lead to their processes becoming stuck indefinitely. :param active_slots: the number of currently active worker slots """ from aiida.cmdline.utils import echo from aiida.common.exceptions import CircusCallError from aiida.manage.configuration import get_config warning_threshold = 0.9 # 90% config = get_config() slots_per_worker = config.get_option('daemon.worker_process_slots', config.current_profile.name) try: active_workers = get_num_workers() except CircusCallError: echo.echo_critical( 'Could not contact Circus to get the number of active workers') if active_workers is not None: available_slots = active_workers * slots_per_worker percent_load = 1.0 if not available_slots else (active_slots / available_slots) if percent_load > warning_threshold: echo.echo('') # New line echo.echo_warning( f'{percent_load * 100:.0f}% of the available daemon worker slots have been used!' ) echo.echo_warning( "Increase the number of workers with 'verdi daemon incr'.\n")
def get_default_user(**kwargs): """Creates and stores the default user in the database. Default user email is taken from current profile. No-op if user already exists. The same is done in `verdi setup`. :param kwargs: Additional information to use for new user, i.e. 'first_name', 'last_name' or 'institution'. :returns: the :py:class:`~aiida.orm.User` """ from aiida.manage.configuration import get_config email = get_config().current_profile.default_user if kwargs.pop('email', None): raise ValueError( 'Do not specify the user email (must coincide with default user email of profile).' ) # Create the AiiDA user if it does not yet exist created, user = orm.User.objects.get_or_create(email=email, **kwargs) if created: user.store() return user
def get_quicksetup_database_name(ctx, param, value): # pylint: disable=unused-argument """Determine the database name to be used as default for the Postgres connection in `verdi quicksetup` If a value is explicitly passed, that value is returned unchanged. If no value is passed, the name will be <profile_name>_<os_user>_<hash>, where <os_user> is the name of the current operating system user and <hash> is a hash of the path of the configuration directory. Note: This ensures that profiles named ``test_...`` will have databases named ``test_...`` . :param ctx: click context which should contain the contextual parameters :return: the database name """ if value is not None: return value config = get_config() profile = ctx.params['profile'].name config_hash = hashlib.md5(config.dirpath.encode('utf-8')).hexdigest() database_name = '{profile}_{user}_{hash}'.format(profile=profile, user=getpass.getuser(), hash=config_hash) return database_name
# Enable rtd mode via `export READTHEDOCS=True` on_rtd = os.environ.get('READTHEDOCS', None) == 'True' if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme html_theme = 'sphinx_rtd_theme' html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] except ImportError: # No sphinx_rtd_theme installed pass # Load the database environment by first loading the profile and then loading the backend through the manager from aiida.manage.configuration import get_config, load_profile from aiida.manage.manager import get_manager config = get_config() load_profile(config.default_profile_name) get_manager().get_backend() else: # Back-end settings for readthedocs online documentation. from aiida.manage import configuration configuration.IN_RT_DOC_MODE = True configuration.BACKEND = "django" # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. needs_sphinx = '1.5' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
def setUp(self): super().setUp() self.config = get_config() self.profile = self.config.current_profile self.daemon_client = DaemonClient(self.profile) self.cli_runner = VerdiRunner(self.config, self.profile)
def setup(non_interactive, profile, email, first_name, last_name, institution, db_engine, db_backend, db_host, db_port, db_name, db_username, db_password, broker_protocol, broker_username, broker_password, broker_host, broker_port, broker_virtual_host, repository): """Setup a new profile.""" # pylint: disable=too-many-arguments,too-many-locals,unused-argument from aiida import orm from aiida.manage.configuration import get_config profile.database_engine = db_engine profile.database_backend = db_backend profile.database_name = db_name profile.database_port = db_port profile.database_hostname = db_host profile.database_username = db_username profile.database_password = db_password profile.broker_protocol = broker_protocol profile.broker_username = broker_username profile.broker_password = broker_password profile.broker_host = broker_host profile.broker_port = broker_port profile.broker_virtual_host = broker_virtual_host profile.repository_uri = 'file://' + repository config = get_config() # Creating the profile config.add_profile(profile) config.set_default_profile(profile.name) # Load the profile load_profile(profile.name) echo.echo_success('created new profile `{}`.'.format(profile.name)) # Migrate the database echo.echo_info('migrating the database.') backend = get_manager()._load_backend(schema_check=False) # pylint: disable=protected-access try: backend.migrate() except Exception as exception: # pylint: disable=broad-except echo.echo_critical( 'database migration failed, probably because connection details are incorrect:\n{}' .format(exception)) else: echo.echo_success('database migration completed.') # Optionally setting configuration default user settings config.set_option('user.email', email, override=False) config.set_option('user.first_name', first_name, override=False) config.set_option('user.last_name', last_name, override=False) config.set_option('user.institution', institution, override=False) # Create the user if it does not yet exist created, user = orm.User.objects.get_or_create(email=email, first_name=first_name, last_name=last_name, institution=institution) if created: user.store() profile.default_user = user.email config.update_profile(profile) config.store()
# add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # Enable rtd mode via `export READTHEDOCS=True` on_rtd = os.environ.get('READTHEDOCS', None) == 'True' if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme html_theme = 'sphinx_rtd_theme' html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] except ImportError: # No sphinx_rtd_theme installed pass # Load the database environment by first loading the profile and then loading the backend through the manager config = configuration.get_config() configuration.load_profile(config.default_profile_name) get_manager().get_backend() else: # Back-end settings for readthedocs online documentation. configuration.IN_RT_DOC_MODE = True configuration.BACKEND = "django" configuration.reset_config() # empty config was created when importing aiida configuration.load_profile() # load dummy config for RTD # load DB backend (no schema check since no DB) get_manager()._load_backend(schema_check=False) # pylint: disable=protected-access # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here.
def setUp(self): super().setUp() self.daemon_client = DaemonClient(get_config().current_profile) self.cli_runner = CliRunner()
def test_profile_config(aiida_profile): # pylint: disable=unused-argument """Check that the config file created with the test profile passes validation.""" Config.from_file(get_config().filepath)