aiida.manage.external package

User facing APIs to control AiiDA from the verdi cli, scripts or plugins

Submodules

Provides an API for postgres database maintenance tasks.

This API creates and drops postgres users and databases used by the verdi quicksetup commandline tool. It allows convenient access to this functionality from within python without knowing details about how postgres is installed by default on various systems. If the postgres setup is not the default installation, additional information needs to be provided.

class aiida.manage.external.postgres.Postgres(dbinfo=None, **kwargs)[source]

Bases: pgsu.PGSU

Adds convenience functions to pgsu.PGSU.

Provides convenience functions for
  • creating/dropping users

  • creating/dropping databases

etc.

Example:

postgres = Postgres()
postgres.create_dbuser('username', 'password')
if not postgres.db_exists('dbname'):
    postgres.create_db('username', 'dbname')
__init__(dbinfo=None, **kwargs)[source]

See documentation of pgsu.PGSU.__init__().

__module__ = 'aiida.manage.external.postgres'
check_db(dbname)[source]

Looks up if a database with the name exists, prompts for using or creating a differently named one.

Parameters

dbname (str) – Name of the database to be created or reused.

Returns

tuple (dbname, created)

check_dbuser(dbuser)[source]

Looks up if a given user already exists, prompts for using or creating a differently named one.

Parameters

dbuser (str) – Name of the user to be created or reused.

Returns

tuple (dbuser, created)

copy_db(src_db, dest_db, dbuser)[source]
create_db(dbuser, dbname)[source]

Create a database in postgres

Parameters
  • dbuser (str) – Name of the user which should own the db.

  • dbname (str) – Name of the database.

create_dbuser(dbuser, dbpass, privileges='')[source]

Create a database user in postgres

Parameters
  • dbuser (str) – Name of the user to be created.

  • dbpass (str) – Password the user should be given.

Raises

psycopg2.errors.DuplicateObject if user already exists and self.connection_mode == PostgresConnectionMode.PSYCOPG

create_dbuser_db_safe(dbname, dbuser, dbpass)[source]

Create DB and user + grant privileges.

Prompts when reusing existing users / databases.

db_exists(dbname)[source]

Check wether a postgres database with dbname exists

Parameters

dbname (str) – Name of the database to check for

Returns

(bool), True if database exists, False otherwise

property dbinfo

Alias for Postgres.dsn.

dbuser_exists(dbuser)[source]

Find out if postgres user with name dbuser exists

Parameters

dbuser (str) – database user to check for

Returns

(bool) True if user exists, False otherwise

drop_db(dbname)[source]

Drop a database in postgres

Parameters

dbname (str) – Name of the database.

drop_dbuser(dbuser)[source]

Drop a database user in postgres

Parameters

dbuser (str) – Name of the user to be dropped.

classmethod from_profile(profile: Profile, **kwargs)[source]

Create Postgres instance with dbinfo from AiiDA profile data.

Note: This only uses host and port from the profile, since the others are not going to be relevant for the

database superuser.

Parameters
  • profile – AiiDA profile instance

  • kwargs – keyword arguments forwarded to PGSU constructor

Returns

Postgres instance pre-populated with data from AiiDA profile

property host_for_psycopg2

Return correct host for psycopg2 connection (as required by regular AiiDA operation).

property port_for_psycopg2

Return port for psycopg2 connection (as required by regular AiiDA operation).

aiida.manage.external.postgres.manual_setup_instructions(dbuser, dbname)[source]

Create a message with instructions for manually creating a database

Components to communicate tasks to RabbitMQ.

class aiida.manage.external.rmq.ProcessLauncher(loop: Optional[asyncio.events.AbstractEventLoop] = None, persister: Optional[plumpy.persistence.Persister] = None, load_context: Optional[plumpy.persistence.LoadSaveContext] = None, loader: Optional[plumpy.loaders.ObjectLoader] = None)[source]

Bases: plumpy.process_comms.ProcessLauncher

A sub class of plumpy.ProcessLauncher to launch a Process.

It overrides the _continue method to make sure the node corresponding to the task can be loaded and that if it is already marked as terminated, it is not continued but the future is reconstructed and returned

__module__ = 'aiida.manage.external.rmq'
async _continue(communicator, pid, nowait, tag=None)[source]

Continue the task.

Note that the task may already have been completed, as indicated from the corresponding the node, in which case it is not continued, but the corresponding future is reconstructed and returned. This scenario may occur when the Process was already completed by another worker that however failed to send the acknowledgment.

Parameters
  • communicator – the communicator that called this method

  • pid – the pid of the process to continue

  • nowait – if True don’t wait for the process to finish, just return the pid, otherwise wait and return the results

  • tag – the tag of the checkpoint to continue from

static handle_continue_exception(node, exception, message)[source]

Handle exception raised in _continue call.

If the process state of the node has not yet been put to excepted, the exception was raised before the process instance could be reconstructed, for example when the process class could not be loaded, thereby circumventing the exception handling of the state machine. Raising this exception will then acknowledge the process task with RabbitMQ leaving an uncleaned node in the CREATED state for ever. Therefore we have to perform the node cleaning manually.

Parameters
  • exception – the exception object

  • message – string message to use for the log message

aiida.manage.external.rmq._store_inputs(inputs)[source]

Try to store the values in the input dictionary.

For nested dictionaries, the values are stored by recursively.

aiida.manage.external.rmq.get_launch_queue_name(prefix=None)[source]

Return the launch queue name with an optional prefix.

Returns

launch queue name

aiida.manage.external.rmq.get_message_exchange_name(prefix)[source]

Return the message exchange name for a given prefix.

Returns

message exchange name

aiida.manage.external.rmq.get_rmq_url(protocol=None, username=None, password=None, host=None, port=None, virtual_host=None, **kwargs)[source]

Return the URL to connect to RabbitMQ.

Note

The default of the host is set to 127.0.0.1 instead of localhost because on some computers localhost resolves first to IPv6 with address ::1 and if RMQ is not running on IPv6 one gets an annoying warning. For more info see: https://github.com/aiidateam/aiida-core/issues/1142

Parameters
  • protocol – the protocol to use, amqp or amqps.

  • username – the username for authentication.

  • password – the password for authentication.

  • host – the hostname of the RabbitMQ server.

  • port – the port of the RabbitMQ server.

  • virtual_host – the virtual host to connect to.

  • kwargs – remaining keyword arguments that will be encoded as query parameters.

Returns

the connection URL string.

aiida.manage.external.rmq.get_task_exchange_name(prefix)[source]

Return the task exchange name for a given prefix.

Returns

task exchange name