Search in sources :

Example 36 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class ContainerBuildersTest method testCreate_tarBase.

@Test
public void testCreate_tarBase() throws IOException, InvalidImageReferenceException, CacheDirectoryCreationException {
    JibContainerBuilder containerBuilder = ContainerBuilders.create("tar:///path/to.tar", Collections.emptySet(), mockCommonCliOptions, mockLogger);
    BuildContext buildContext = JibContainerBuilderTestHelper.toBuildContext(containerBuilder, Containerizer.to(RegistryImage.named("ignored")));
    ImageConfiguration imageConfiguration = buildContext.getBaseImageConfiguration();
    assertThat(imageConfiguration.getTarPath()).isEqualTo(Optional.of(Paths.get("/path/to.tar")));
    assertThat(imageConfiguration.getDockerClient().isPresent()).isFalse();
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Test(org.junit.Test)

Example 37 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class ContainerBuildersTest method testCreate_registry.

@Test
public void testCreate_registry() throws IOException, InvalidImageReferenceException, CacheDirectoryCreationException {
    JibContainerBuilder containerBuilder = ContainerBuilders.create("registry://registry-image-ref", Collections.emptySet(), mockCommonCliOptions, mockLogger);
    BuildContext buildContext = JibContainerBuilderTestHelper.toBuildContext(containerBuilder, Containerizer.to(RegistryImage.named("ignored")));
    ImageConfiguration imageConfiguration = buildContext.getBaseImageConfiguration();
    assertThat(imageConfiguration.getImage().toString()).isEqualTo("registry-image-ref");
    assertThat(imageConfiguration.getDockerClient().isPresent()).isFalse();
    assertThat(imageConfiguration.getTarPath().isPresent()).isFalse();
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Test(org.junit.Test)

Example 38 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class Containerizer method to.

/**
 * Gets a new {@link Containerizer} that containerizes to a tarball archive.
 *
 * @param tarImage the {@link TarImage} that defines target output file
 * @return a new {@link Containerizer}
 */
public static Containerizer to(TarImage tarImage) {
    Optional<ImageReference> imageReference = tarImage.getImageReference();
    if (!imageReference.isPresent()) {
        throw new IllegalArgumentException("Image name must be set when building a TarImage; use TarImage#named(...) to set the name" + " of the target image");
    }
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(imageReference.get()).build();
    Function<BuildContext, StepsRunner> stepsRunnerFactory = buildContext -> StepsRunner.begin(buildContext).tarBuildSteps(tarImage.getPath());
    return new Containerizer(DESCRIPTION_FOR_TARBALL, imageConfiguration, stepsRunnerFactory, false);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ListMultimap(com.google.common.collect.ListMultimap) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Function(java.util.function.Function) HashSet(java.util.HashSet) DockerClient(com.google.cloud.tools.jib.docker.DockerClient) XdgDirectories(com.google.cloud.tools.jib.filesystem.XdgDirectories) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) Files(java.nio.file.Files) Set(java.util.Set) IOException(java.io.IOException) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner) BuildResult(com.google.cloud.tools.jib.builder.steps.BuildResult) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Paths(java.nio.file.Paths) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner)

Example 39 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class Containerizer method to.

/**
 * Gets a new {@link Containerizer} that containerizes to a container registry.
 *
 * @param registryImage the {@link RegistryImage} that defines target container registry and
 *     credentials
 * @return a new {@link Containerizer}
 */
public static Containerizer to(RegistryImage registryImage) {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(registryImage.getImageReference()).setCredentialRetrievers(registryImage.getCredentialRetrievers()).build();
    Function<BuildContext, StepsRunner> stepsRunnerFactory = buildContext -> StepsRunner.begin(buildContext).registryPushSteps();
    return new Containerizer(DESCRIPTION_FOR_DOCKER_REGISTRY, imageConfiguration, stepsRunnerFactory, true);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ListMultimap(com.google.common.collect.ListMultimap) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Function(java.util.function.Function) HashSet(java.util.HashSet) DockerClient(com.google.cloud.tools.jib.docker.DockerClient) XdgDirectories(com.google.cloud.tools.jib.filesystem.XdgDirectories) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) Files(java.nio.file.Files) Set(java.util.Set) IOException(java.io.IOException) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner) BuildResult(com.google.cloud.tools.jib.builder.steps.BuildResult) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Paths(java.nio.file.Paths) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner)

Example 40 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JibContainerBuilder method containerize.

/**
 * Builds the container.
 *
 * @param containerizer the {@link Containerizer} that configures how to containerize
 * @return the built container
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if a directory to be used for the cache could not be
 *     created
 * @throws HttpHostConnectException if jib failed to connect to a registry
 * @throws RegistryUnauthorizedException if a registry request is unauthorized and needs
 *     authentication
 * @throws RegistryAuthenticationFailedException if registry authentication failed
 * @throws UnknownHostException if the registry does not exist
 * @throws InsecureRegistryException if a server could not be verified due to an insecure
 *     connection
 * @throws RegistryException if some other error occurred while interacting with a registry
 * @throws ExecutionException if some other exception occurred during execution
 * @throws InterruptedException if the execution was interrupted
 */
public JibContainer containerize(Containerizer containerizer) throws InterruptedException, RegistryException, IOException, CacheDirectoryCreationException, ExecutionException {
    try (BuildContext buildContext = toBuildContext(containerizer);
        TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), containerizer.getDescription())) {
        logSources(buildContext.getEventHandlers());
        BuildResult buildResult = containerizer.run(buildContext);
        return JibContainer.from(buildContext, buildResult);
    } catch (ExecutionException ex) {
        // If an ExecutionException occurs, re-throw the cause to be more easily handled by the user
        if (ex.getCause() instanceof RegistryException) {
            throw (RegistryException) ex.getCause();
        }
        throw ex;
    }
}
Also used : BuildResult(com.google.cloud.tools.jib.builder.steps.BuildResult) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)45 Test (org.junit.Test)31 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)23 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)12 IOException (java.io.IOException)12 Set (java.util.Set)12 BuildResult (com.google.cloud.tools.jib.builder.steps.BuildResult)9 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)8 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)8 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)8 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)8 Preconditions (com.google.common.base.Preconditions)8 List (java.util.List)8 Optional (java.util.Optional)8 ExecutionException (java.util.concurrent.ExecutionException)8 Nullable (javax.annotation.Nullable)8 LogEvent (com.google.cloud.tools.jib.api.LogEvent)6 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 StepsRunner (com.google.cloud.tools.jib.builder.steps.StepsRunner)6 DockerClient (com.google.cloud.tools.jib.docker.DockerClient)6