[973924]: / scripts / qiita-env

Download this file

123 lines (96 with data), 4.2 kB

#!/usr/bin/env python

# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

import click

import qiita_db as qdb
from qiita_core.environment_manager import test as _test, TEST_RUNNERS
from qiita_core.configuration_manager import ConfigurationManager

_CONFIG = ConfigurationManager()


@click.group()
def env():
    pass


@env.command()
@click.option('--load-ontologies/--no-load-ontologies',
              default=True, help='If True, ontologies will be loaded. '
              'Cannot be True if this is a test environment.')
@click.option('--download-reference/--no-download-reference',
              default=False, help='If True, greengenes reference files will '
                                  'be downloaded')
@click.option('--add-demo-user/--no-add-demo-user',
              default=False, help='If True, then demo@microbio.me will be '
                                  'added to the database with password '
                                  '"password"')
def make(load_ontologies, download_reference, add_demo_user):
    """Creates the database specified in config"""
    try:
        qdb.environment_manager.make_environment(
            load_ontologies, download_reference, add_demo_user)
    except Exception as e:
        if "Database qiita_test already present on the system." not in str(e):
            # this will clean our environment so we can try again without
            # having to have an other window open to remove the current
            # environment. This is fine as we are actually creating a new
            # environment.
            qdb.environment_manager.drop_environment(False)
        raise e


@env.command()
@click.option('--ask-for-confirmation/--no-ask-for-confirmation',
              default=True, help='If True, will ask for confirmation before '
              'dropping the production environment.')
def drop(ask_for_confirmation):
    """Drops the database specified in config"""
    try:
        qdb.environment_manager.drop_environment(ask_for_confirmation)
    except RuntimeError as e:
        raise click.ClickException(str(e))


@env.command()
def clean_test():
    """Cleans the test database environment.

    In case that the test database is dirty (i.e. the 'qiita' schema is
    present), this cleans it up by dropping the 'qiita' schema and rebuilding
    the test database.
    """
    qdb.environment_manager.clean_test_environment()


@env.command()
def patch():
    """Patches the database schema based on the SETTINGS table

    Pulls the current patch from the settings table and applies all subsequent
    patches found in the patches directory.
    """
    qdb.environment_manager.patch()


@env.command()
@click.option('--runner', required=False, type=click.Choice(TEST_RUNNERS),
              default='all', help='Test runner to use')
def test(runner):
    """Test the environment

    Check to make sure that basic services are up and working. These include
    connectivity to postgres, and redis.

    Tests are performed both on localhost and ipengines.
    """
    _test(runner)


@env.command(name="create-portal")
@click.argument('portal', required=True, type=str)
@click.argument('description', required=True, type=str)
def add_portal(portal, description):
    """Creates a new portal on the database"""
    try:
        qdb.portal.Portal.create(portal, description)
    except qdb.exceptions.QiitaDBDuplicateError:
        raise click.BadParameter("Portal name already exists!")


@env.command(name="remove-portal")
@click.argument('portal', required=True, type=str)
def rem_portal(portal):
    """Removes a portal from the database"""
    try:
        qdb.portal.Portal.delete(portal)
    except qdb.exceptions.QiitaDBError as e:
        raise click.BadParameter(str(e))
    except qdb.exceptions.QiitaDBLookupError:
        raise click.BadParameter("Portal name does not exist!")


if __name__ == '__main__':
    env()