aiidateam / aiida-core

The official repository for the AiiDA code
https://aiida-core.readthedocs.io
Other
431 stars 187 forks source link

πŸ› Profile with `sqlite_zip` backend breaks `verdi quicksetup` #6319

Closed GeigerJ2 closed 6 months ago

GeigerJ2 commented 6 months ago

As the title suggests, when having a profile with the core.sqlite_zip backend in the main config, verdi quicksetup:

┬─[geiger_j@mpc3152:~]─[I]─[A:aiida-main]
╰─>$ verdi quicksetup
Report: enter ? for help.
Report: enter ! to ignore the default and set no value.
Profile name [quicksetup]: bug-reproduce
Email Address (for sharing data) [julian.geiger@psi.ch]: 
First name [Julian]: 
Last name [Geiger]: 
Institution [PSI]: 

breaks, leading to:

Traceback (most recent call last):
  File "/home/geiger_j/.pyenv/versions/3.12.1/bin/verdi", line 8, in <module>
    sys.exit(verdi())
             ^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/geiger_j/.pyenv/versions/3.12.1/lib/python3.12/site-packages/click/core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/geiger_j/dev/aiida-dev/aiidateam/aiida-core/src/aiida/cmdline/params/options/commands/setup.py", line 151, in get_quicksetup_password
    if available_profile.storage_config['database_username'] == username:
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
KeyError: 'database_username'

As evident from the error, the problem occurs in the get_quicksetup_password function in cmdline/params/options/commands/setup.py due to the missing database_username key for a core.sqlite_zip (or core.sqlite_dos, for that matter) profile in the for loop:

https://github.com/aiidateam/aiida-core/blob/aa4d2a890699a8a41ba893eb0626dc25b3bc4930/src/aiida/cmdline/params/options/commands/setup.py#L147-L153

Wrapping it in a simple try...except or checking the storage_backend before, e.g.:

    for available_profile in config.profiles:
        if available_profile.storage_backend == 'core.psql_dos':
            storage_config = available_profile.storage_config
            if storage_config['database_username'] == username:
                value = storage_config['database_password']
                break
    else:
        value = get_random_string(16)

solves the issue. Any additional thoughts, or should I go ahead and create a PR?

sphuber commented 6 months ago

verdi quicksetup is specific to core.psql_dos. Historically, that was the only backend available so the code didn't bother to check and assumed all profiles would be the same. So I would opt for explicitly checking that the storage backend is core.psql_dos

GeigerJ2 commented 6 months ago

Thanks for weighing in, @sphuber! I created PR #6326 that should solve it. (it ain't much, but it's honest work :D)