diff --git a/docs/source/reference/command_line.rst b/docs/source/reference/command_line.rst index aa00b218e4..0953d027f7 100644 --- a/docs/source/reference/command_line.rst +++ b/docs/source/reference/command_line.rst @@ -391,13 +391,13 @@ Below is a list with all available subcommands. --help Show this message and exit. Commands: - delete Delete one or more profiles. - list Display a list of all available profiles. - set-default Set a profile as the default profile. - setdefault (Deprecated) Set a profile as the default profile (use `verdi profile set- - default`). - setup Set up a new profile. - show Show details for a profile. + configure-rabbitmq Configure RabbitMQ for a profile. + delete Delete one or more profiles. + list Display a list of all available profiles. + set-default Set a profile as the default profile. + setdefault (Deprecated) Set a profile as the default profile. + setup Set up a new profile. + show Show details for a profile. .. _reference:command-line:verdi-quicksetup: diff --git a/src/aiida/cmdline/commands/cmd_profile.py b/src/aiida/cmdline/commands/cmd_profile.py index 2de2ce173d..0b8065a9a8 100644 --- a/src/aiida/cmdline/commands/cmd_profile.py +++ b/src/aiida/cmdline/commands/cmd_profile.py @@ -128,6 +128,26 @@ def profile_setup(): """Set up a new profile.""" +@verdi_profile.command('configure-rabbitmq') # type: ignore[arg-type] +@arguments.PROFILE(default=defaults.get_default_profile) +@setup.SETUP_BROKER_PROTOCOL() +@setup.SETUP_BROKER_USERNAME() +@setup.SETUP_BROKER_PASSWORD() +@setup.SETUP_BROKER_HOST() +@setup.SETUP_BROKER_PORT() +@setup.SETUP_BROKER_VIRTUAL_HOST() +@options.NON_INTERACTIVE() +@click.pass_context +def profile_configure_rabbitmq(ctx, profile, **kwargs): + """Configure RabbitMQ for a profile. + + Enable RabbitMQ for a profile that was created without a broker, or reconfigure existing connection details. + """ + profile.set_process_controller(name='core.rabbitmq', config=kwargs) + ctx.obj.config.update_profile(profile) + ctx.obj.config.store() + + @verdi_profile.command('list') def profile_list(): """Display a list of all available profiles.""" @@ -179,7 +199,7 @@ def profile_show(profile): @verdi_profile.command('setdefault', deprecated='Please use `verdi profile set-default` instead.') @arguments.PROFILE(required=True, default=None) def profile_setdefault(profile): - """Set a profile as the default profile (use `verdi profile set-default`).""" + """Set a profile as the default profile.""" _profile_set_default(profile) diff --git a/src/aiida/cmdline/params/options/commands/setup.py b/src/aiida/cmdline/params/options/commands/setup.py index bbd980c976..008f51b3a0 100644 --- a/src/aiida/cmdline/params/options/commands/setup.py +++ b/src/aiida/cmdline/params/options/commands/setup.py @@ -50,6 +50,8 @@ def get_profile_attribute_default(attribute_tuple, ctx): try: data = ctx.params['profile'].dictionary for part in parts: + if data is None: + return default data = data[part] return data except KeyError: diff --git a/tests/cmdline/commands/test_profile.py b/tests/cmdline/commands/test_profile.py index 909562245a..51594b6ca7 100644 --- a/tests/cmdline/commands/test_profile.py +++ b/tests/cmdline/commands/test_profile.py @@ -269,3 +269,26 @@ def test_setup_no_use_rabbitmq(run_cli_command, isolated_config): profile = isolated_config.get_profile(profile_name) assert profile.process_control_backend is None assert profile.process_control_config == {} + + +def test_configure_rabbitmq(run_cli_command, isolated_config): + """Test the ``verdi profile configure-rabbitmq`` command.""" + profile_name = 'profile' + + # First setup a profile without a broker configured + options = ['core.sqlite_dos', '-n', '--email', 'a@a', '--profile', profile_name, '--no-use-rabbitmq'] + run_cli_command(cmd_profile.profile_setup, options, use_subprocess=False) + profile = isolated_config.get_profile(profile_name) + assert profile.process_control_backend is None + assert profile.process_control_config == {} + + # Now run the command to configure the broker + options = [profile_name, '-n'] + run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) + assert profile.process_control_backend == 'core.rabbitmq' + + # Call it again to check it works to reconfigure existing broker connection parameters + options = [profile_name, '-n', '--broker-host', 'rabbitmq.broker.com'] + run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) + assert profile.process_control_backend == 'core.rabbitmq' + assert profile.process_control_config['broker_host'] == 'rabbitmq.broker.com'