def run(args):
    if args.type == "core":
        create_core(args)
    elif args.type == "data":
        create_data(args)
    else:
        raise utils.D2DockerError("Unknown subcommand for create: {}".format(
            args.type))
def upgrade_to_version(*, version, source_image, dest_image, core_image, port,
                       migrations_dir, keep_running):
    utils.logger.info("Upgrade: {} -> {}".format(source_image, dest_image))
    version_path = os.path.join(migrations_dir,
                                version) if migrations_dir else None

    # Create core image
    dhis_war_path = os.path.join(version_path,
                                 "dhis.war") if version_path else None
    dhis2_home_paths = (glob.glob(os.path.join(version_path, "dhis2-home",
                                               "*")) if version_path else [])
    war_exists = dhis_war_path and os.path.exists(dhis_war_path)
    create_core_kwargs = dict(war=dhis_war_path) if war_exists else dict(
        version=version)
    core_docker_dir = utils.get_docker_directory("core")
    utils.create_core(docker_dir=core_docker_dir,
                      image=core_image,
                      dhis2_home_paths=dhis2_home_paths,
                      **create_core_kwargs)

    # Create data image
    data_docker_dir = utils.get_docker_directory("data")
    utils.copy_image(data_docker_dir, source_image, dest_image)

    # Start
    final_port = port or utils.get_free_port()

    utils.run_docker_compose(["down", "--volumes"],
                             dest_image,
                             core_image=core_image)

    with utils.stop_docker_on_interrupt(dest_image, core_image):
        utils.run_docker_compose(
            ["up", "--force-recreate", "-d"],
            dest_image,
            port=final_port,
            core_image=core_image,
            load_from_data=True,
            post_sql_dir=version_path,
            scripts_dir=version_path,
        )

        if not utils.wait_for_server(final_port):
            raise utils.D2DockerError(
                "Error waiting for DHIS2 instance to be active")

        # Commit
        utils.build_image_from_source(data_docker_dir, dest_image, dest_image)

    # Stop
    if not keep_running:
        utils.run_docker_compose(["stop"], dest_image)
Exemple #3
0
def import_from_file(images_path):
    dhis2_data_image_re = "/{}:".format(utils.DHIS2_DATA_IMAGE)
    result = utils.load_images_file(images_path)
    lines = result.stdout.decode("utf-8").splitlines()
    lines_splitted = [line.split() for line in lines]
    dhis2_data_images = [
        parts[-1] for parts in lines_splitted
        if parts and re.search(dhis2_data_image_re, parts[-1])
    ]
    if dhis2_data_images:
        return dhis2_data_images[0]
    else:
        msg = "Cannot find dhis2 data image (pattern={})".format(
            dhis2_data_image_re)
        raise utils.D2DockerError(msg)
Exemple #4
0
def start(args, image_name):
    utils.logger.info("Start image: {}".format(image_name))
    result = utils.get_image_status(image_name)
    if result["state"] == "running":
        msg = "Container already runnning for image {}".format(
            result["containers"]["db"])
        raise utils.D2DockerError(msg)
    port = args.port or utils.get_free_port()
    utils.logger.info("Port: {}".format(port))
    core_image = args.core_image
    override_containers = not args.keep_containers

    if args.pull:
        utils.run_docker_compose(["pull"], image_name, core_image=core_image)

    if override_containers:
        utils.run_docker_compose(["down", "--volumes"],
                                 image_name,
                                 core_image=core_image)

    up_args = filter(bool, [
        "--force-recreate" if override_containers else None,
        "-d" if args.detach else None
    ])

    deploy_path = "/" + re.sub("^/*", "",
                               args.deploy_path) if args.deploy_path else ""

    with utils.stop_docker_on_interrupt(image_name, core_image):
        utils.run_docker_compose(
            ["up", *up_args],
            image_name,
            port=port,
            core_image=core_image,
            load_from_data=override_containers,
            post_sql_dir=args.run_sql,
            db_port=args.db_port,
            scripts_dir=args.run_scripts,
            deploy_path=deploy_path,
            dhis2_auth=args.auth,
            tomcat_server=args.tomcat_server_xml,
            dhis_conf=args.dhis_conf,
            java_opts=args.java_opts,
            postgis_version=args.postgis_version,
        )

    if args.detach:
        utils.logger.info("Detaching... run d2-docker logs to see logs")
def copy(source, destinations, docker_dir):
    logger = utils.logger
    source_type = utils.get_item_type(source)
    logger.debug("Source {} has type: {}".format(source, source_type))

    for dest in destinations:
        dest_type = utils.get_item_type(dest)
        logger.debug("Destination {} has type: {}".format(dest, dest_type))
        logger.info("Copying: {}:{} -> {}:{}".format(source_type, source,
                                                     dest_type, dest))

        if source_type == "docker-image" and dest_type == "docker-image":
            utils.copy_image(docker_dir, source, dest)
        elif source_type == "docker-image" and dest_type == "folder":
            utils.export_data_from_image(source, dest)
        elif source_type == "folder" and dest_type == "docker-image":
            utils.build_image_from_directory(docker_dir, source, dest)
        elif source_type == "folder" and dest_type == "folder":
            utils.copytree(source, dest)
        else:
            raise utils.D2DockerError("Not implemented")

        logger.info("Done")