chrisjsewell / activate_aiida

a package to activate an aiida environment, from a yaml config file
MIT License
0 stars 2 forks source link

aiida_environment.yaml <> config.yaml ? #1

Open ltalirz opened 4 years ago

ltalirz commented 4 years ago

since there are anyhow plans to move the config.json to a config.yaml format in aiida-core https://github.com/aiidateam/aiida-core/issues/2701 , it seems to me we should think a bit about how to distribute information between the two in order to avoid duplication of information.

mentioning @pzarabadip for info

chrisjsewell commented 4 years ago

Yeh, so I moved away from using this script for day-to-day use (once a database has already been setup). Instead, I've created some simpler click commands to print out the relevant command lines for activating/deactiving environments, which I then just copy/paste to the terminal. As we've discussed before, you can't run these lines directly with python's subprocess, because the environmental variables are not passed back to the parent process (i.e. the command line terminal).

Instead of using aiida_environment.yaml I could just use config.yaml and also add an argument for specifying the profile name.

import os
import time
import click
from aiida.cmdline.utils import echo
from aiidapy_fes.cli.cli import root

@root.command('environ')
@click.option(
    "-f", '--filename',
    'fhandle',
    type=click.File('r'),
    help="path to config file",
    default="aiida_environment.yaml"
)
@click.option(
    "-a", '--activate',
    'activate',
    is_flag=True,
    help="include commands to start the database server and daemon",
    default=False
)
@click.option(
    "-b", '--backup',
    'backup',
    is_flag=True,
    help="include commands to backup the sql database",
    default=False
)
def print_env_variables(fhandle, activate, backup):
    """print the environmental variable commands,
    to setup an aiida environment
    """
    from ruamel.yaml import YAML
    import jsonschema

    yaml = YAML(typ='safe')
    config = yaml.load(fhandle)

    jsonschema.validate(config, _CONFIG_SCHEMA)

    db_path = config["db_pgsql"]["path"]
    db_port = config["db_pgsql"]["port"]
    db_user = config["db_pgsql"]["user"]
    db_name = config["db_pgsql"]["name"]
    db_host = "localhost"
    aiida_path = config["aiida_path"]
    aiida_profile = config["db_aiida"]["profile"]

    echo.echo_success("Command-line Settings:")
    echo.echo('eval "$(_VERDI_COMPLETE=source verdi)"')
    echo.echo('export PGDATA={}'.format(db_path))
    echo.echo('export AIIDA_PATH={}'.format(aiida_path))
    echo.echo('verdi profile setdefault {}'.format(aiida_profile))

    if activate:
        echo.echo('rabbitmq-server -detached >/dev/null 2>&1')
        echo.echo(
            'pg_ctl -D {path} start '
            '-o "-F -p {port}" '
            '-l "{path}/postgres_env.log"'.format(path=db_path, port=db_port))
        echo.echo('verdi daemon start')

    if backup:
        bcmnd = 'pg_dump -h {host} -p {port} -U {user} {name} | gzip > {ofile}'
        ofile = os.path.join(
            aiida_path, "{db_name}-backup-{time}.psql.gz".format(
                db_name=db_name, time=time.strftime("%Y%m%d-%H%M")))
        echo.echo(bcmnd.format(host=db_host, port=db_port,
                               user=db_user, name=db_name, ofile=ofile))

    echo.echo('verdi status')
    echo.echo('')

@root.command('environ-deactivate')
def print_deactivate_commands():
    """print the command lines to deactivate aiida and the database"""
    echo.echo('verdi daemon stop')
    echo.echo('pg_ctl stop')
    echo.echo('')
chrisjsewell commented 4 years ago

Hey @ltalirz FYI I have updated and tested this to work with aiida-core v1.2 + Linux, while I was setting up the development environment on my EPFL workstation (I have lsmosrv1.epfl.ch working with VS Code Remote which makes things so easy!). The pgsu package looks to be working great 👍 then the README on this package should be quite self-explanatory on what it is doing extra.

ltalirz commented 4 years ago

Hey @ltalirz FYI I have updated and tested this to work with aiida-core v1.2 + Linux, while I was setting up the development environment on my EPFL workstation

Great. Let's try to find some time (next week?) to "sit together" to discuss plans for how to integrate things with the aiida-core conda package.

I have lsmosrv1.epfl.ch working with VS Code Remote which makes things so easy!

Right, I've been using the same ;-)

The pgsu package looks to be working great 👍

Great!

chrisjsewell commented 4 years ago

Let's try to find some time (next week?)

Yep no problem, mostly anytime next week would be fine (it's not as though I will be going far from my house!)