def test_check_services(mocker): from derex.runner.docker import check_services client = mocker.patch("derex.runner.docker.client") client.containers.get.return_value.status = "running" assert check_services() client.containers.get.side_effect = docker.errors.NotFound( "Mysql container not found") assert not check_services()
def reset_mysql(project): """Reset the discovery mysql database""" from derex.runner.docker import check_services from derex.runner.docker import wait_for_mysql if project.runmode is not ProjectRunMode.debug: click.get_current_context().fail( "This command can only be run in `debug` runmode") if not check_services(["mysql"]): click.echo( "Mysql service not found.\nMaybe you forgot to run\nddc-services up -d" ) return wait_for_mysql() restore_dump_path = abspath_from_egg("derex.discovery", "derex/discovery/restore_dump.py") run_compose( [ "run", "--rm", "-v", f"{restore_dump_path}:/openedx/discovery/restore_dump.py", "discovery", "python", "/openedx/discovery/restore_dump.py", ], project=project, ) return 0
def ddc_project(): """Proxy for docker-compose: writes a docker-compose.yml file with the configuration of this project, and then run `docker-compose` on it. You probably want do run `ddc-project up -d` and `ddc-project logs -f`. """ check_docker() setup_logging() try: project = Project() except ValueError: click.echo("You need to run this command in a derex project") sys.exit(1) compose_args, dry_run = ddc_parse_args(sys.argv) # If trying to start up containers, first check that needed services are running is_start_cmd = any(param in compose_args for param in ["up", "start"]) if is_start_cmd and not check_services(["mysql", "mongodb", "rabbitmq"]): click.echo( "Mysql/mongo/rabbitmq services not found.\nMaybe you forgot to run\nddc-services up -d" ) return run_compose(list(compose_args), project=project, dry_run=dry_run, exit_afterwards=True)
def get_mysql_client(user: str = "root", password: str = "secret", database: Optional[str] = "", **kwargs) -> pymysql.cursors.Cursor: """Return a cursor on the mysql server. If the connection object is needed it can be accessed from the cursor object: .. code-block:: python mysql_client = get_mysql_client() mysql_client.connection.autocommit(True) """ if not check_services(["mysql"]): raise RuntimeError( "Mysql service not found.\nMaybe you forgot to run\nddc-services up -d" ) wait_for_mysql() container = docker_client.containers.get("mysql") mysql_host = container.attrs["NetworkSettings"]["Networks"]["derex"][ "IPAddress"] connection = pymysql.connect(host=mysql_host, port=3306, user=user, passwd=password, db=database, **kwargs) return connection.cursor()
def reset_mysql_cmd(project): """Reset mysql database for the project""" from derex.runner.compose_utils import reset_mysql from derex.runner.docker import check_services from derex.runner.docker import execute_mysql_query from derex.runner.docker import wait_for_mysql if not check_services(["mysql"]): click.echo( "Mysql service not found.\nMaybe you forgot to run\nddc-services up -d" ) return wait_for_mysql() execute_mysql_query( f"CREATE DATABASE IF NOT EXISTS {project.mysql_db_name}") reset_mysql(project) return 0
def reset_mailslurper(project): """Reset the mailslurper database. """ from derex.runner.docker import check_services from derex.runner.docker import execute_mysql_query from derex.runner.docker import load_dump from derex.runner.docker import wait_for_mysql if not check_services(["mysql"]): click.echo( "Mysql not found.\nMaybe you forgot to run\nddc-services up -d") return 1 wait_for_mysql() click.echo("Dropping mailslurper database") execute_mysql_query("DROP DATABASE IF EXISTS mailslurper") click.echo("Priming mailslurper database") load_dump("derex/runner/fixtures/mailslurper.sql") return 0
def refresh_course_metadata(project): """Run discovery `refresh_course_metadata` Django command""" from derex.runner.docker import check_services if not check_services(["elasticsearch"]): click.echo( "Elasticsearch service not found.\nMaybe you forgot to run\nddc-services up -d" ) return run_compose( [ "run", "--rm", "discovery", "python", "manage.py", "refresh_course_metadata" ], project=project, ) return 0
def create_index(project): """Run discovery `install_es_indexes` Django command""" from derex.runner.docker import check_services if not check_services(["elasticsearch"]): click.echo( "Elasticsearch service not found.\nMaybe you forgot to run\nddc-services up -d" ) return run_compose( [ "run", "--rm", "discovery", "python", "manage.py", "install_es_indexes" ], project=project, ) return 0
def update_index(project): """Run discovery `update_index` Django command""" from derex.runner.docker import check_services if not check_services(["elasticsearch"]): click.echo( "Elasticsearch service not found.\nMaybe you forgot to run\nddc-services up -d" ) return run_compose( [ "run", "--rm", "discovery", "python", "manage.py", "update_index", "--disable-change-limit", ], project=project, ) return 0
from typing import cast from typing import List import logging logger = logging.getLogger(__name__) def wait_for_mongodb(max_seconds: int = 20): """With a freshly created container mongodb might need a bit of time to prime its files. This functions waits up to max_seconds seconds. """ return wait_for_service("mongodb", "mongo", max_seconds) if not check_services(["mongodb"]): MONGODB_CLIENT = None else: wait_for_mongodb() container = docker_client.containers.get("mongodb") mongo_address = container.attrs["NetworkSettings"]["Networks"]["derex"][ "IPAddress"] MONGODB_CLIENT = MongoClient(f"mongodb://{mongo_address}:27017/") def ensure_mongodb(func): """Decorator to raise an exception before running a function in case the mongodb server is not available. """ @wraps(func) def inner(*args, **kwargs):