Ejemplo n.º 1
0
def remove_image(image):
    utils.logger.info("Delete image/containers: {}".format(image))
    utils.run_docker_compose(["stop"], image)
    result = utils.run_docker_compose(["ps", "-q"],
                                      data_image=image,
                                      capture_output=True)
    container_ids = result.stdout.decode("utf-8").splitlines()
    utils.logger.debug("Container IDs: {}".format(container_ids))
    if container_ids:
        utils.run_docker(["container", "rm", *container_ids])
    utils.run_docker(["image", "rm", image])
    utils.logger.info("Removed: {}".format(image))
Ejemplo n.º 2
0
def run(args):
    image_name = args.image or utils.get_running_image_name()
    core = args.core_image
    result = utils.run_docker_compose(["config"], image_name, core_image=core, capture_output=True)
    yaml_contents = result.stdout.decode("utf-8")

    # Use regexp instead of using a third-party YAML parser to ease deployment on Windows.
    image_names = re.findall(r"image: (\S+)\r?$", yaml_contents, re.MULTILINE)
    utils.logger.info("Export images: {}".format(", ".join(image_names)))
    tar_file = args.output_file + ".tar"
    utils.save_images(image_names, tar_file)
    compress_tar(tar_file, args.output_file)
Ejemplo n.º 3
0
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)
Ejemplo n.º 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")
Ejemplo n.º 5
0
def run(args):
    image_name = args.image or utils.get_running_image_name()
    utils.logger.info("Show logs: {}".format(image_name))
    args = ["logs", "-t", "-f" if args.follow else None, args.service]
    utils.run_docker_compose(filter(bool, args), image_name)
Ejemplo n.º 6
0
def run(args):
    image_name = args.image or utils.get_running_image_name()
    utils.logger.info("Stop container for image: {}".format(image_name))
    utils.run_docker_compose(["stop"], image_name)