Ejemplo n.º 1
0
def build(tgt: DockerImageTarget, options: DockerOptions):
    fs = DockerFieldSet.create(tgt)
    return (BuiltPackage(
        EMPTY_DIGEST,
        (BuiltDockerImage.create(
            fs.image_refs(
                options.default_repository,
                options.registries(),
                DockerVersionContext(),
            ), ), ),
    ), )
Ejemplo n.º 2
0
async def push_docker_images(request: PublishDockerImageRequest,
                             docker: DockerBinary,
                             options: DockerOptions) -> PublishProcesses:
    tags = tuple(
        chain.from_iterable(
            cast(BuiltDockerImage, image).tags for pkg in request.packages
            for image in pkg.artifacts))

    if request.field_set.skip_push.value:
        return PublishProcesses([
            PublishPackages(
                names=tags,
                description=
                f"(by `{request.field_set.skip_push.alias}` on {request.field_set.address})",
            ),
        ])

    env = await Get(Environment, EnvironmentRequest(options.env_vars))
    skip_push = defaultdict(set)
    jobs: list[PublishPackages] = []
    refs: list[str] = []
    processes: list[Get] = []

    for tag in tags:
        for registry in options.registries().registries.values():
            if tag.startswith(registry.address) and registry.skip_push:
                skip_push[registry.alias].add(tag)
                break
        else:
            refs.append(tag)
            processes.append(
                Get(InteractiveProcess,
                    InteractiveProcessRequest(docker.push_image(tag, env))))

    interactive_processes = await MultiGet(processes)
    for ref, process in zip(refs, interactive_processes):
        jobs.append(PublishPackages(
            names=(ref, ),
            process=process,
        ))

    if skip_push:
        for name, skip_tags in skip_push.items():
            jobs.append(
                PublishPackages(
                    names=tuple(skip_tags),
                    description=f"(by `skip_push` on registry @{name})",
                ), )

    return PublishProcesses(jobs)
Ejemplo n.º 3
0
async def build_docker_image(
    field_set: DockerFieldSet,
    options: DockerOptions,
    docker: DockerBinary,
) -> BuiltPackage:
    context = await Get(
        DockerBuildContext,
        DockerBuildContextRequest(
            address=field_set.address,
            build_upstream_images=True,
        ),
    )

    tags = field_set.image_refs(
        default_repository=options.default_repository,
        registries=options.registries(),
        version_context=context.version_context,
    )

    result = await Get(
        ProcessResult,
        Process,
        docker.build_image(
            build_args=context.build_args,
            digest=context.digest,
            dockerfile=context.dockerfile,
            env=context.env,
            tags=tags,
        ),
    )

    logger.debug(f"Docker build output for {tags[0]}:\n"
                 f"{result.stdout.decode()}\n"
                 f"{result.stderr.decode()}")

    return BuiltPackage(
        result.output_digest,
        (BuiltDockerImage.create(tags), ),
    )
Ejemplo n.º 4
0
async def find_docker(docker_request: DockerBinaryRequest,
                      docker_options: DockerOptions) -> DockerBinary:
    env = await Get(Environment, EnvironmentRequest(["PATH"]))
    search_path = docker_options.executable_search_path(env)
    request = BinaryPathRequest(
        binary_name="docker",
        search_path=search_path,
        test=BinaryPathTest(args=["-v"]),
    )
    paths = await Get(BinaryPaths, BinaryPathRequest, request)
    first_path = paths.first_path_or_raise(
        request, rationale="interact with the docker daemon")

    if not docker_options.tools:
        return DockerBinary(first_path.path, first_path.fingerprint)

    tools = await Get(
        BinaryShims,
        BinaryShimsRequest,
        BinaryShimsRequest.for_binaries(
            *docker_options.tools,
            rationale="use docker",
            output_directory="bin",
            search_path=search_path,
        ),
    )
    tools_path = ".shims"
    extra_env = {
        "PATH": os.path.join("{chroot}", tools_path, tools.bin_directory)
    }
    extra_input_digests = {tools_path: tools.digest}

    return DockerBinary(
        first_path.path,
        first_path.fingerprint,
        extra_env=extra_env,
        extra_input_digests=extra_input_digests,
    )
Ejemplo n.º 5
0
async def build_docker_image(
    field_set: DockerFieldSet,
    options: DockerOptions,
    global_options: GlobalOptions,
    docker: DockerBinary,
    process_cleanup: ProcessCleanupOption,
) -> BuiltPackage:
    context, wrapped_target = await MultiGet(
        Get(
            DockerBuildContext,
            DockerBuildContextRequest(
                address=field_set.address,
                build_upstream_images=True,
            ),
        ),
        Get(WrappedTarget, Address, field_set.address),
    )

    tags = field_set.image_refs(
        default_repository=options.default_repository,
        registries=options.registries(),
        interpolation_context=context.interpolation_context,
    )

    context_root = field_set.get_context_root(options.default_context_root)
    process = docker.build_image(
        build_args=context.build_args,
        digest=context.digest,
        dockerfile=context.dockerfile,
        context_root=context_root,
        env=context.build_env.environment,
        tags=tags,
        extra_args=tuple(
            get_build_options(
                context=context,
                field_set=field_set,
                global_target_stage_option=options.build_target_stage,
                target=wrapped_target.target,
            )),
    )
    result = await Get(FallibleProcessResult, Process, process)

    if result.exit_code != 0:
        maybe_msg = format_docker_build_context_help_message(
            address=field_set.address,
            context_root=context_root,
            context=context,
            colors=global_options.colors,
        )
        if maybe_msg:
            logger.warning(maybe_msg)

        raise ProcessExecutionFailure(
            result.exit_code,
            result.stdout,
            result.stderr,
            process.description,
            process_cleanup=process_cleanup.val,
        )

    image_id = parse_image_id_from_docker_build_output(result.stdout,
                                                       result.stderr)
    docker_build_output_msg = "\n".join((
        f"Docker build output for {tags[0]}:",
        "stdout:",
        result.stdout.decode(),
        "stderr:",
        result.stderr.decode(),
    ))

    if options.build_verbose:
        logger.info(docker_build_output_msg)
    else:
        logger.debug(docker_build_output_msg)

    return BuiltPackage(
        result.output_digest,
        (BuiltDockerImage.create(image_id, tags), ),
    )