Search in sources :

Example 1 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class RegistryClient method pushBlob.

// TODO: Add mount with 'from' parameter
/**
 * Pushes the BLOB, or skips if the BLOB already exists on the registry.
 *
 * @param blobDigest the digest of the BLOB, used for existence-check
 * @param blob the BLOB to push
 * @return {@code true} if the BLOB already exists on the registry and pushing was skipped; false
 *     if the BLOB was pushed
 */
public boolean pushBlob(DescriptorDigest blobDigest, Blob blob) throws IOException, RegistryException {
    BlobPusher blobPusher = new BlobPusher(registryEndpointProperties, blobDigest, blob);
    try (Timer t = parentTimer.subTimer("pushBlob")) {
        try (Timer t2 = t.subTimer("pushBlob POST " + blobDigest)) {
            // POST /v2/<name>/blobs/uploads/?mount={blob.digest}
            String locationHeader = callRegistryEndpoint(blobPusher.initializer());
            if (locationHeader == null) {
                // The BLOB exists already.
                return true;
            }
            URL patchLocation = new URL(locationHeader);
            t2.lap("pushBlob PATCH " + blobDigest);
            // PATCH <Location> with BLOB
            URL putLocation = new URL(callRegistryEndpoint(blobPusher.writer(patchLocation)));
            t2.lap("pushBlob PUT " + blobDigest);
            // PUT <Location>?digest={blob.digest}
            callRegistryEndpoint(blobPusher.committer(putLocation));
            return false;
        }
    }
}
Also used : Timer(com.google.cloud.tools.jib.Timer) URL(java.net.URL)

Example 2 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class PullBaseImageStep method call.

/**
 * Depends on {@code pullAuthorizationFuture}.
 */
@Override
public Image call() throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pullAuthorizationFuture), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        ManifestTemplate manifestTemplate = registryClient.pullManifest(buildConfiguration.getBaseImageTag());
        // TODO: Make schema version be enum.
        switch(manifestTemplate.getSchemaVersion()) {
            case 1:
                V21ManifestTemplate v21ManifestTemplate = (V21ManifestTemplate) manifestTemplate;
                return JsonToImageTranslator.toImage(v21ManifestTemplate);
            case 2:
                V22ManifestTemplate v22ManifestTemplate = (V22ManifestTemplate) manifestTemplate;
                if (v22ManifestTemplate.getContainerConfiguration() == null || v22ManifestTemplate.getContainerConfiguration().getDigest() == null) {
                    throw new UnknownManifestFormatException("Invalid container configuration in Docker V2.2 manifest: \n" + Blobs.writeToString(JsonTemplateMapper.toBlob(v22ManifestTemplate)));
                }
                ByteArrayOutputStream containerConfigurationOutputStream = new ByteArrayOutputStream();
                registryClient.pullBlob(v22ManifestTemplate.getContainerConfiguration().getDigest(), containerConfigurationOutputStream);
                String containerConfigurationString = new String(containerConfigurationOutputStream.toByteArray(), StandardCharsets.UTF_8);
                ContainerConfigurationTemplate containerConfigurationTemplate = JsonTemplateMapper.readJson(containerConfigurationString, ContainerConfigurationTemplate.class);
                return JsonToImageTranslator.toImage(v22ManifestTemplate, containerConfigurationTemplate);
        }
        throw new IllegalStateException("Unknown manifest schema version");
    }
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Timer(com.google.cloud.tools.jib.Timer) UnknownManifestFormatException(com.google.cloud.tools.jib.image.json.UnknownManifestFormatException) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate)

Example 3 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class AuthenticatePushStep method call.

/**
 * Depends on {@link RetrieveRegistryCredentialsStep}.
 */
@Override
@Nullable
public Authorization call() throws ExecutionException, InterruptedException, RegistryAuthenticationFailedException, IOException, RegistryException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, buildConfiguration.getTargetRegistry()))) {
        Authorization registryCredentials = NonBlockingFutures.get(registryCredentialsFuture);
        RegistryAuthenticator registryAuthenticator = RegistryAuthenticators.forOther(buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository());
        if (registryAuthenticator == null) {
            return registryCredentials;
        }
        return registryAuthenticator.setAuthorization(NonBlockingFutures.get(registryCredentialsFuture)).authenticatePush();
    }
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Timer(com.google.cloud.tools.jib.Timer) RegistryAuthenticator(com.google.cloud.tools.jib.registry.RegistryAuthenticator) Nullable(javax.annotation.Nullable)

Example 4 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class BuildAndPushContainerConfigurationStep method afterBaseImageLayerFuturesFuture.

/**
 * Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
 * {@code buildApplicationLayerFutures}.
 */
private BlobDescriptor afterBaseImageLayerFuturesFuture() throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException, RegistryException {
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
        // Constructs the image.
        Image image = new Image();
        for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        image.setEnvironment(buildConfiguration.getEnvironment());
        image.setEntrypoint(entrypoint);
        ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
        // Gets the container configuration content descriptor.
        Blob containerConfigurationBlob = imageToJsonTranslator.getContainerConfigurationBlob();
        CountingDigestOutputStream digestOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream());
        containerConfigurationBlob.writeTo(digestOutputStream);
        BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();
        timer.lap("Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());
        // TODO: Use PushBlobStep.
        // Pushes the container configuration.
        registryClient.pushBlob(containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);
        return containerConfigurationBlobDescriptor;
    }
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Timer(com.google.cloud.tools.jib.Timer) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Image(com.google.cloud.tools.jib.image.Image)

Example 5 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class BuildImageSteps method run.

public void run() throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException, CacheDirectoryNotOwnedException {
    List<String> entrypoint = EntrypointBuilder.makeEntrypoint(sourceFilesConfiguration, buildConfiguration.getJvmFlags(), buildConfiguration.getMainClass());
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        try (Timer timer2 = timer.subTimer("Initializing cache")) {
            ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
            try (Caches caches = cachesInitializer.init()) {
                Cache baseLayersCache = caches.getBaseCache();
                Cache applicationLayersCache = caches.getApplicationCache();
                timer2.lap("Setting up credential retrieval");
                ListenableFuture<Authorization> retrieveTargetRegistryCredentialsFuture = listeningExecutorService.submit(new RetrieveRegistryCredentialsStep(buildConfiguration, buildConfiguration.getTargetRegistry()));
                ListenableFuture<Authorization> retrieveBaseImageRegistryCredentialsFuture = listeningExecutorService.submit(new RetrieveRegistryCredentialsStep(buildConfiguration, buildConfiguration.getBaseImageRegistry()));
                timer2.lap("Setting up image push authentication");
                // Authenticates push.
                ListenableFuture<Authorization> authenticatePushFuture = Futures.whenAllSucceed(retrieveTargetRegistryCredentialsFuture).call(new AuthenticatePushStep(buildConfiguration, retrieveTargetRegistryCredentialsFuture), listeningExecutorService);
                timer2.lap("Setting up image pull authentication");
                // Authenticates base image pull.
                ListenableFuture<Authorization> authenticatePullFuture = Futures.whenAllSucceed(retrieveBaseImageRegistryCredentialsFuture).call(new AuthenticatePullStep(buildConfiguration, retrieveBaseImageRegistryCredentialsFuture), listeningExecutorService);
                timer2.lap("Setting up base image pull");
                // Pulls the base image.
                ListenableFuture<Image> pullBaseImageFuture = Futures.whenAllSucceed(authenticatePullFuture).call(new PullBaseImageStep(buildConfiguration, authenticatePullFuture), listeningExecutorService);
                timer2.lap("Setting up base image layer pull");
                // Pulls and caches the base image layers.
                ListenableFuture<List<ListenableFuture<CachedLayer>>> pullBaseImageLayerFuturesFuture = Futures.whenAllSucceed(pullBaseImageFuture).call(new PullAndCacheBaseImageLayersStep(buildConfiguration, baseLayersCache, listeningExecutorService, authenticatePullFuture, pullBaseImageFuture), listeningExecutorService);
                timer2.lap("Setting up base image layer push");
                // Pushes the base image layers.
                ListenableFuture<List<ListenableFuture<Void>>> pushBaseImageLayerFuturesFuture = Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture).call(new PushLayersStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture), listeningExecutorService);
                timer2.lap("Setting up build application layers");
                // Builds the application layers.
                List<ListenableFuture<CachedLayer>> buildAndCacheApplicationLayerFutures = new BuildAndCacheApplicationLayersStep(buildConfiguration, sourceFilesConfiguration, applicationLayersCache, listeningExecutorService).call();
                timer2.lap("Setting up container configuration push");
                // Builds and pushes the container configuration.
                ListenableFuture<ListenableFuture<BlobDescriptor>> buildAndPushContainerConfigurationFutureFuture = Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture).call(new BuildAndPushContainerConfigurationStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture, buildAndCacheApplicationLayerFutures, entrypoint), listeningExecutorService);
                timer2.lap("Setting up application layer push");
                // Pushes the application layers.
                List<ListenableFuture<Void>> pushApplicationLayersFuture = new PushLayersStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, Futures.immediateFuture(buildAndCacheApplicationLayerFutures)).call();
                timer2.lap("Setting up image manifest push");
                // Pushes the new image manifest.
                ListenableFuture<Void> pushImageFuture = Futures.whenAllSucceed(pushBaseImageLayerFuturesFuture, buildAndPushContainerConfigurationFutureFuture).call(new PushImageStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture, buildAndCacheApplicationLayerFutures, pushBaseImageLayerFuturesFuture, pushApplicationLayersFuture, buildAndPushContainerConfigurationFutureFuture), listeningExecutorService);
                timer2.lap("Running push new image");
                pushImageFuture.get();
            }
        }
    }
    buildConfiguration.getBuildLogger().lifecycle("");
    buildConfiguration.getBuildLogger().lifecycle("Container entrypoint set to " + entrypoint);
}
Also used : Caches(com.google.cloud.tools.jib.cache.Caches) Image(com.google.cloud.tools.jib.image.Image) Authorization(com.google.cloud.tools.jib.http.Authorization) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) List(java.util.List) Timer(com.google.cloud.tools.jib.Timer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cache(com.google.cloud.tools.jib.cache.Cache)

Aggregations

Timer (com.google.cloud.tools.jib.Timer)12 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)5 Authorization (com.google.cloud.tools.jib.http.Authorization)4 Image (com.google.cloud.tools.jib.image.Image)3 CacheReader (com.google.cloud.tools.jib.cache.CacheReader)2 CacheWriter (com.google.cloud.tools.jib.cache.CacheWriter)2 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)2 RegistryAuthenticator (com.google.cloud.tools.jib.registry.RegistryAuthenticator)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Nullable (javax.annotation.Nullable)2 Blob (com.google.cloud.tools.jib.blob.Blob)1 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 Cache (com.google.cloud.tools.jib.cache.Cache)1 Caches (com.google.cloud.tools.jib.cache.Caches)1 CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)1 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)1 LayerBuilder (com.google.cloud.tools.jib.image.LayerBuilder)1 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)1 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)1