Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ REFACTOR: Profile storage backend configuration #5320

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions aiida/backends/djsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
if PROFILE is None:
raise exceptions.ProfileConfigurationError('no profile has been loaded')

if PROFILE.database_backend != 'django':
if PROFILE.storage_backend != 'django':
raise exceptions.ProfileConfigurationError(
f'incommensurate database backend `{PROFILE.database_backend}` for profile `{PROFILE.name}`'
f'incommensurate database backend `{PROFILE.storage_backend}` for profile `{PROFILE.name}`'
)

PROFILE_CONF = PROFILE.dictionary

DATABASES = {
'default': {
'ENGINE': f'django.db.backends.{PROFILE.database_engine}',
'NAME': PROFILE.database_name,
'PORT': PROFILE.database_port,
'HOST': PROFILE.database_hostname,
'USER': PROFILE.database_username,
'PASSWORD': PROFILE.database_password,
'ENGINE': f"django.db.backends.{PROFILE.storage_config['database_engine']}",
'NAME': PROFILE.storage_config['database_name'],
'PORT': PROFILE.storage_config['database_port'],
'HOST': PROFILE.storage_config['database_hostname'],
'USER': PROFILE.storage_config['database_username'],
'PASSWORD': PROFILE.storage_config['database_password'],
}
}

Expand Down
6 changes: 3 additions & 3 deletions aiida/backends/testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def get_backend_class(cls):

# Freeze the __impl_class after the first run
if not hasattr(cls, '__impl_class'):
if PROFILE.database_backend == BACKEND_SQLA:
if PROFILE.storage_backend == BACKEND_SQLA:
from aiida.backends.sqlalchemy.testbase import SqlAlchemyTests
cls.__impl_class = SqlAlchemyTests
elif PROFILE.database_backend == BACKEND_DJANGO:
elif PROFILE.storage_backend == BACKEND_DJANGO:
from aiida.backends.djsite.db.testbase import DjangoTests
cls.__impl_class = DjangoTests
else:
Expand Down Expand Up @@ -248,7 +248,7 @@ def get_default_user(**kwargs):
:returns: the :py:class:`~aiida.orm.User`
"""
from aiida.manage.configuration import get_config
email = get_config().current_profile.default_user
email = get_config().current_profile.default_user_email

if kwargs.pop('email', None):
raise ValueError('Do not specify the user email (must coincide with default user email of profile).')
Expand Down
19 changes: 12 additions & 7 deletions aiida/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Backend-agnostic utility functions"""
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from aiida.manage.configuration.profile import Profile

AIIDA_ATTRIBUTE_SEP = '.'


def create_sqlalchemy_engine(profile, **kwargs):
def create_sqlalchemy_engine(profile: 'Profile', **kwargs):
"""Create SQLAlchemy engine (to be used for QueryBuilder queries)

:param kwargs: keyword arguments that will be passed on to `sqlalchemy.create_engine`.
Expand All @@ -24,16 +29,16 @@ def create_sqlalchemy_engine(profile, **kwargs):

# The hostname may be `None`, which is a valid value in the case of peer authentication for example. In this case
# it should be converted to an empty string, because otherwise the `None` will be converted to string literal "None"
hostname = profile.database_hostname or ''
separator = ':' if profile.database_port else ''
hostname = profile.storage_config['database_hostname'] or ''
separator = ':' if profile.storage_config['database_port'] else ''

engine_url = 'postgresql://{user}:{password}@{hostname}{separator}{port}/{name}'.format(
separator=separator,
user=profile.database_username,
password=profile.database_password,
user=profile.storage_config['database_username'],
password=profile.storage_config['database_password'],
hostname=hostname,
port=profile.database_port,
name=profile.database_name
port=profile.storage_config['database_port'],
name=profile.storage_config['database_name']
)
return create_engine(
engine_url, json_serializer=json.dumps, json_deserializer=json.loads, future=True, encoding='utf-8', **kwargs
Expand Down
37 changes: 22 additions & 15 deletions aiida/cmdline/commands/cmd_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,27 @@ def setup(
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 = f'file://{repository}'
profile.set_storage(
db_backend, {
'database_engine': db_engine,
'database_hostname': db_host,
'database_port': db_port,
'database_name': db_name,
'database_username': db_username,
'database_password': db_password,
'repository_uri': f'file://{repository}',
}
)
profile.set_process_controller(
'rabbitmq', {
'broker_protocol': broker_protocol,
'broker_username': broker_username,
'broker_password': broker_password,
'broker_host': broker_host,
'broker_port': broker_port,
'broker_virtual_host': broker_virtual_host,
}
)

config = get_config()

Expand Down Expand Up @@ -115,7 +122,7 @@ def setup(
)
if created:
user.store()
profile.default_user = user.email
profile.default_user_email = user.email
config.update_profile(profile)
config.store()

Expand Down
7 changes: 6 additions & 1 deletion aiida/cmdline/commands/cmd_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ def verdi_status(print_traceback, no_rmq):
dbgen = backend_manager.get_schema_generation_database()
dbver = backend_manager.get_schema_version_backend()
database_data = [
profile.database_name, dbgen, dbver, profile.database_username, profile.database_hostname, profile.database_port
profile.storage_config['database_name'],
dbgen,
dbver,
profile.storage_config['database_username'],
profile.storage_config['database_hostname'],
profile.storage_config['database_port'],
]
try:
with override_log_level(): # temporarily suppress noisy logging
Expand Down
2 changes: 1 addition & 1 deletion aiida/cmdline/commands/cmd_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def set_default_user(profile, user):
"""
from aiida.manage.configuration import get_config
config = get_config()
profile.default_user = user.email
profile.default_user_email = user.email
config.update_profile(profile)
config.store()

Expand Down
52 changes: 36 additions & 16 deletions aiida/cmdline/params/options/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ def get_profile_attribute_default(attribute_tuple, ctx):
:return: profile attribute default value if set, or None
"""
attribute, default = attribute_tuple
parts = attribute.split('.')

try:
validate_profile_parameter(ctx)
except click.BadParameter:
return default
else:
try:
return getattr(ctx.params['profile'], attribute)
data = ctx.params['profile'].dictionary
for part in parts:
data = data[part]
return data
except KeyError:
return default

Expand Down Expand Up @@ -140,8 +144,8 @@ def get_quicksetup_password(ctx, param, value): # pylint: disable=unused-argume
config = get_config()

for available_profile in config.profiles:
if available_profile.database_username == username:
value = available_profile.database_password
if available_profile.storage_config['database_username'] == username:
value = available_profile.storage_config['database_password']
break
else:
value = get_random_string(16)
Expand Down Expand Up @@ -248,89 +252,105 @@ def get_quicksetup_password(ctx, param, value): # pylint: disable=unused-argume

SETUP_DATABASE_ENGINE = QUICKSETUP_DATABASE_ENGINE.clone(
prompt='Database engine',
contextual_default=functools.partial(get_profile_attribute_default, ('database_engine', 'postgresql_psycopg2')),
contextual_default=functools.partial(
get_profile_attribute_default, ('storage.config.database_engine', 'postgresql_psycopg2')
),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_BACKEND = QUICKSETUP_DATABASE_BACKEND.clone(
prompt='Database backend',
contextual_default=functools.partial(get_profile_attribute_default, ('database_backend', BACKEND_DJANGO)),
contextual_default=functools.partial(get_profile_attribute_default, ('storage_backend', BACKEND_DJANGO)),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_HOSTNAME = QUICKSETUP_DATABASE_HOSTNAME.clone(
prompt='Database host',
contextual_default=functools.partial(get_profile_attribute_default, ('database_hostname', 'localhost')),
contextual_default=functools.partial(
get_profile_attribute_default, ('storage.config.database_hostname', 'localhost')
),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_PORT = QUICKSETUP_DATABASE_PORT.clone(
prompt='Database port',
contextual_default=functools.partial(get_profile_attribute_default, ('database_port', DEFAULT_DBINFO['port'])),
contextual_default=functools.partial(
get_profile_attribute_default, ('storage.config.database_port', DEFAULT_DBINFO['port'])
),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_NAME = QUICKSETUP_DATABASE_NAME.clone(
prompt='Database name',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('database_name', None)),
contextual_default=functools.partial(get_profile_attribute_default, ('storage.config.database_name', None)),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_USERNAME = QUICKSETUP_DATABASE_USERNAME.clone(
prompt='Database username',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('database_username', None)),
contextual_default=functools.partial(get_profile_attribute_default, ('storage.config.database_username', None)),
cls=options.interactive.InteractiveOption
)

SETUP_DATABASE_PASSWORD = QUICKSETUP_DATABASE_PASSWORD.clone(
prompt='Database password',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('database_password', None)),
contextual_default=functools.partial(get_profile_attribute_default, ('storage.config.database_password', None)),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_PROTOCOL = QUICKSETUP_BROKER_PROTOCOL.clone(
prompt='Broker protocol',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('broker_protocol', BROKER_DEFAULTS.protocol)),
contextual_default=functools.partial(
get_profile_attribute_default, ('process_control.config.broker_protocol', BROKER_DEFAULTS.protocol)
),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_USERNAME = QUICKSETUP_BROKER_USERNAME.clone(
prompt='Broker username',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('broker_username', BROKER_DEFAULTS.username)),
contextual_default=functools.partial(
get_profile_attribute_default, ('process_control.config.broker_username', BROKER_DEFAULTS.username)
),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_PASSWORD = QUICKSETUP_BROKER_PASSWORD.clone(
prompt='Broker password',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('broker_password', BROKER_DEFAULTS.password)),
contextual_default=functools.partial(
get_profile_attribute_default, ('process_control.config.broker_password', BROKER_DEFAULTS.password)
),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_HOST = QUICKSETUP_BROKER_HOST.clone(
prompt='Broker host',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('broker_host', BROKER_DEFAULTS.host)),
contextual_default=functools.partial(
get_profile_attribute_default, ('process_control.config.broker_host', BROKER_DEFAULTS.host)
),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_PORT = QUICKSETUP_BROKER_PORT.clone(
prompt='Broker port',
required=True,
contextual_default=functools.partial(get_profile_attribute_default, ('broker_port', BROKER_DEFAULTS.port)),
contextual_default=functools.partial(
get_profile_attribute_default, ('process_control.config.broker_port', BROKER_DEFAULTS.port)
),
cls=options.interactive.InteractiveOption
)

SETUP_BROKER_VIRTUAL_HOST = QUICKSETUP_BROKER_VIRTUAL_HOST.clone(
prompt='Broker virtual host name',
required=True,
contextual_default=functools.partial(
get_profile_attribute_default, ('broker_virtual_host', BROKER_DEFAULTS.virtual_host)
get_profile_attribute_default, ('process_control.config.broker_virtual_host', BROKER_DEFAULTS.virtual_host)
),
cls=options.interactive.InteractiveOption
)
Expand Down
2 changes: 1 addition & 1 deletion aiida/cmdline/params/types/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def convert(self, value, param, ctx):
self.fail(str(exception))

# Create a new empty profile
profile = Profile(value, {})
profile = Profile(value, {}, validate=False)
else:
if self._cannot_exist:
self.fail(str(f'the profile `{value}` already exists'))
Expand Down
Loading