Exemple #1
0
def quicksetup(
    ctx, non_interactive, profile, email, first_name, last_name, institution, db_engine, db_backend, db_host, db_port,
    db_name, db_username, db_password, su_db_name, su_db_username, su_db_password, repository
):
    """Setup a new profile in a fully automated fashion."""
    # pylint: disable=too-many-arguments,too-many-locals
    from aiida.manage.external.postgres import Postgres, manual_setup_instructions

    dbinfo_su = {
        'host': db_host,
        'port': db_port,
        'user': su_db_username,
        'password': su_db_password,
    }
    postgres = Postgres(interactive=not non_interactive, quiet=False, dbinfo=dbinfo_su)

    if not postgres.is_connected:
        echo.echo_critical('failed to determine the PostgreSQL setup')

    try:
        create = True
        if not postgres.dbuser_exists(db_username):
            postgres.create_dbuser(db_username, db_password)
        else:
            db_name, create = postgres.check_db_name(db_name)

        if create:
            postgres.create_db(db_username, db_name)
    except Exception as exception:
        echo.echo_error(
            '\n'.join([
                'Oops! quicksetup was unable to create the AiiDA database for you.',
                'For AiiDA to work, please either create the database yourself as follows:',
                manual_setup_instructions(dbuser=su_db_username, dbname=su_db_name), '',
                'Alternatively, give your (operating system) user permission to create postgresql databases' +
                'and run quicksetup again.', ''
            ])
        )
        raise exception

    # The contextual defaults or `verdi setup` are not being called when `invoking`, so we have to explicitly define
    # them here, even though the `verdi setup` command would populate those when called from the command line.
    setup_parameters = {
        'non_interactive': non_interactive,
        'profile': profile,
        'email': email,
        'first_name': first_name,
        'last_name': last_name,
        'institution': institution,
        'db_engine': db_engine,
        'db_backend': db_backend,
        'db_name': db_name,
        # from now on we connect as the AiiDA DB user, which may be forbidden when going via sockets
        'db_host': db_host or 'localhost',
        'db_port': db_port,
        'db_username': db_username,
        'db_password': db_password,
        'repository': repository,
    }
    ctx.invoke(setup, **setup_parameters)
def create_db(profile):
    """Create PostgreSQL database, if missing."""
    dbinfo_su = {
        'host': profile.database_hostname,
        'port': profile.database_port,
        'user': os.getenv("AIIDADB_SUPER_USER"),
        'password': os.getenv("AIIDADB_SUPER_PASS"),
        'database': 'template1',
    }
    postgres = Postgres(interactive=False, quiet=False,
                        dbinfo=dbinfo_su)  #, determine_setup=False)

    if not postgres.is_connected:
        raise ConnectionError("Unable to connect as super user")

    try:
        if not postgres.dbuser_exists(dbuser=profile.database_username):
            postgres.create_dbuser(dbuser=profile.database_username,
                                   dbpass=profile.database_password)

        if not postgres.db_exists(dbname=profile.database_name):
            postgres.create_db(profile.database_username,
                               profile.database_name)

            # Fill DB with vanilla content
            load_profile(profile.name)
            backend = get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access

            try:
                backend.migrate()
            except Exception as exception:  # pylint: disable=broad-except
                print(
                    'database migration failed, probably because connection details are incorrect:\n{}'
                    .format(exception))

            # Create the user if it does not yet exist
            created, user = orm.User.objects.get_or_create(
                email=profile.default_user,
                first_name='AiiDA',
                last_name='EXPLORER',
                institution='WEBSERVICE')
            if created:
                user.store()
            profile.default_user = user.email

    except:
        print(traceback.format_exc())