Example #1
0
def generate_ddc_services_file() -> str:
    """Generate the global docker-compose config file that will drive
    ddc-services and return its path.
    """
    local_path = DEREX_DIR / "services" / DDC_SERVICES_YML_PATH.name
    # Copy all files
    dir_util.copy_tree(
        str(DDC_SERVICES_YML_PATH.parent),
        str(local_path.parent),
        update=1,  # Do not copy files more than once
        verbose=1,
    )
    # Compile the mailslurper template to include the mysql password
    tmpl = Template(MAILSLURPER_JSON_TEMPLATE.read_text())
    MYSQL_ROOT_PASSWORD = get_secret(DerexSecrets.mysql)
    text = tmpl.render(MYSQL_ROOT_PASSWORD=MYSQL_ROOT_PASSWORD)
    (local_path.parent /
     MAILSLURPER_JSON_TEMPLATE.name.replace(".j2", "")).write_text(text)

    # Compile the docker compose yaml template
    ensure_dir(local_path)
    tmpl = Template(DDC_SERVICES_YML_PATH.read_text())
    text = tmpl.render(
        MINIO_SECRET_KEY=get_secret(DerexSecrets.minio),
        MONGODB_ROOT_USERNAME=MONGODB_ROOT_USER,
        MONGODB_ROOT_PASSWORD=get_secret(DerexSecrets.mongodb),
        MYSQL_ROOT_PASSWORD=MYSQL_ROOT_PASSWORD,
    )
    local_path.write_text(text)
    return str(local_path)
Example #2
0
def test_derived_secret():
    from derex.runner.secrets import get_secret
    from derex.runner.secrets import compute_entropy

    foo_secret = get_secret(FooSecrets.foo)
    # The same name should always yield the same secrets
    assert get_secret(FooSecrets.foo) == foo_secret

    # Two names should have different secrets
    assert foo_secret != get_secret(FooSecrets.bar)

    # Secrets must have enough entropy
    assert compute_entropy(foo_secret) > 256
Example #3
0
def run_minio_shell(command="sh"):
    """Invoke a minio shell
    """
    minio_key = get_secret(DerexSecrets.minio)
    os.system(
        "docker run -ti --rm --network derex --entrypoint /bin/sh minio/mc -c '"
        f'mc config host add local http://minio:80 minio_derex "{minio_key}" --api s3v4 ; set -ex; {command}\''
    )
def run_minio_shell(command: str = "sh", tty: bool = True):
    """Invoke a minio shell
    """
    minio_key = get_secret(DerexSecrets.minio)
    os.system(
        f"docker run {'-ti ' if tty else ''}--rm --network derex --entrypoint /bin/sh minio/mc -c '"
        f'mc config host add local http://minio:80 minio_derex "{minio_key}" --api s3v4 ; set -ex; {command}\''
    )
Example #5
0
def print_secret(secret):
    from derex.runner.secrets import get_secret, DerexSecrets

    derex_secret = getattr(DerexSecrets, secret, None)
    if not derex_secret:
        raise click.exceptions.ClickException(
            f'No secrets found for "{secret}"')
    click.echo(get_secret(derex_secret))
    return 0
Example #6
0
def generate_services_file() -> str:
    """Generate the global docker-compose config file that will drive
    ddc-services and return its path.
    """
    local_path = DEREX_DIR / "services" / SERVICES_YML_PATH.name
    dir_util.copy_tree(
        str(SERVICES_YML_PATH.parent),
        str(local_path.parent),
        update=1,  # Do not copy files more than once
        verbose=1,
    )
    ensure_dir(local_path)
    tmpl = Template(SERVICES_YML_PATH.read_text())
    minio_secret_key = get_secret(DerexSecrets.minio)
    text = tmpl.render(MINIO_SECRET_KEY=minio_secret_key)
    local_path.write_text(text)
    return str(local_path)
Example #7
0
 def secret(self, name: str) -> str:
     return get_secret(DerexSecrets[name])
Example #8
0
from derex.runner.project import Project
from derex.runner.secrets import DerexSecrets
from derex.runner.secrets import get_secret
from derex.runner.utils import abspath_from_egg
from functools import wraps
from typing import cast
from typing import List
from typing import Optional
from typing import Tuple

import logging
import pymysql


logger = logging.getLogger(__name__)
MYSQL_ROOT_PASSWORD = get_secret(DerexSecrets.mysql)


def ensure_mysql(func):
    """Decorator to raise an exception before running a function in case the MySQL
    server is not available.
    """

    @wraps(func)
    def inner(*args, **kwargs):
        wait_for_service("mysql")
        return func(*args, **kwargs)

    return inner

Example #9
0
from derex.runner.ddc import run_ddc_services
from derex.runner.docker_utils import client as docker_client
from derex.runner.docker_utils import wait_for_service
from derex.runner.secrets import DerexSecrets
from derex.runner.secrets import get_secret
from functools import wraps
from pymongo import MongoClient
from typing import cast
from typing import List
from typing import Optional

import logging
import urllib.parse

logger = logging.getLogger(__name__)
MONGODB_ROOT_PASSWORD = get_secret(DerexSecrets.mongodb)

try:
    wait_for_service("mongodb")
    container = docker_client.containers.get("mongodb")
    mongo_address = container.attrs["NetworkSettings"]["Networks"]["derex"][
        "IPAddress"]
    user = urllib.parse.quote_plus(MONGODB_ROOT_USER)
    password = urllib.parse.quote_plus(MONGODB_ROOT_PASSWORD)
    MONGODB_CLIENT = MongoClient(
        f"mongodb://{user}:{password}@{mongo_address}:27017/",
        authSource="admin")
except RuntimeError as e:
    MONGODB_CLIENT = None
    logger.warning(e)