Exemple #1
0
        /**
         * Retrieves the cached base image.
         *
         * @return the cached image
         * @throws IOException when an I/O exception occurs
         * @throws CacheCorruptedException if the cache is corrupted
         * @throws LayerPropertyNotFoundException if adding image layers fails
         * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
         *     format
         */
        private Image PullBaseImageOffline()
        {
            IImageReference           baseImage = buildConfiguration.GetBaseImageConfiguration().GetImage();
            Maybe <ManifestAndConfig> metadata  =
                buildConfiguration.GetBaseImageLayersCache().RetrieveMetadata(baseImage);

            if (!metadata.IsPresent())
            {
                throw new IOException(
                          "Cannot run Fib in offline mode; " + baseImage + " not found in local Fib cache");
            }

            IManifestTemplate manifestTemplate = metadata.Get().GetManifest();

            if (manifestTemplate is V21ManifestTemplate v21ManifestTemplate)
            {
                return(JsonToImageTranslator.ToImage(v21ManifestTemplate));
            }

            ContainerConfigurationTemplate configurationTemplate =
                metadata.Get().GetConfig().OrElseThrow(() => new InvalidOperationException());

            return(JsonToImageTranslator.ToImage(
                       (IBuildableManifestTemplate)manifestTemplate, configurationTemplate));
        }
Exemple #2
0
        public void TestToImage_v21()
        {
            // Loads the JSON string.
            SystemPath jsonFile =
                Paths.Get(TestResources.GetResource("core/json/v21manifest.json").ToURI());

            // Deserializes into a manifest JSON object.
            V21ManifestTemplate manifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <V21ManifestTemplate>(jsonFile);

            Image image = JsonToImageTranslator.ToImage(manifestTemplate);

            IList <ILayer> layers = image.GetLayers();

            Assert.AreEqual(2, layers.Count);
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:5bd451067f9ab05e97cda8476c82f86d9b69c2dffb60a8ad2fe3723942544ab3"),
                layers[0].GetBlobDescriptor().GetDigest());
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                layers[1].GetBlobDescriptor().GetDigest());
        }
Exemple #3
0
        /**
         * Pulls the base image.
         *
         * @param registryAuthorization authentication credentials to possibly use
         * @param progressEventDispatcher the {@link ProgressEventDispatcher} for emitting {@link
         *     ProgressEvent}s
         * @return the pulled image
         * @throws IOException when an I/O exception occurs during the pulling
         * @throws RegistryException if communicating with the registry caused a known error
         * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
         *     information
         * @throws LayerPropertyNotFoundException if adding image layers fails
         * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
         *     format
         */
        private async Task <Image> PullBaseImageAsync(
            Authorization registryAuthorization,
            ProgressEventDispatcher progressEventDispatcher)
        {
            RegistryClient registryClient =
                buildConfiguration
                .NewBaseImageRegistryClientFactory()
                .SetAuthorization(registryAuthorization)
                .NewRegistryClient();

            IManifestTemplate manifestTemplate =
                await registryClient.PullManifestAsync(buildConfiguration.GetBaseImageConfiguration().GetImageTag()).ConfigureAwait(false);

            // TODO: Make schema version be enum.
            switch (manifestTemplate.SchemaVersion)
            {
            case 1:
                V21ManifestTemplate v21ManifestTemplate = (V21ManifestTemplate)manifestTemplate;
                await buildConfiguration
                .GetBaseImageLayersCache()
                .WriteMetadataAsync(
                    buildConfiguration.GetBaseImageConfiguration().GetImage(), v21ManifestTemplate).ConfigureAwait(false);

                return(JsonToImageTranslator.ToImage(v21ManifestTemplate));

            case 2:
                IBuildableManifestTemplate buildableManifestTemplate =
                    (IBuildableManifestTemplate)manifestTemplate;
                if (buildableManifestTemplate.GetContainerConfiguration() == null ||
                    buildableManifestTemplate.GetContainerConfiguration().Digest == null)
                {
                    throw new UnknownManifestFormatException(
                              "Invalid container configuration in Docker V2.2/OCI manifest: \n"
                              + JsonTemplateMapper.ToUtf8String(buildableManifestTemplate));
                }

                DescriptorDigest containerConfigurationDigest =
                    buildableManifestTemplate.GetContainerConfiguration().Digest;

                using (ThrottledProgressEventDispatcherWrapper progressEventDispatcherWrapper =
                           new ThrottledProgressEventDispatcherWrapper(
                               progressEventDispatcher.NewChildProducer(),
                               "pull container configuration " + containerConfigurationDigest))
                {
                    string containerConfigurationString =
                        await Blobs.WriteToStringAsync(
                            registryClient.PullBlob(
                                containerConfigurationDigest,
                                progressEventDispatcherWrapper.SetProgressTarget,
                                progressEventDispatcherWrapper.DispatchProgress)).ConfigureAwait(false);

                    ContainerConfigurationTemplate containerConfigurationTemplate =
                        JsonTemplateMapper.ReadJson <ContainerConfigurationTemplate>(
                            containerConfigurationString);
                    await buildConfiguration
                    .GetBaseImageLayersCache()
                    .WriteMetadataAsync(
                        buildConfiguration.GetBaseImageConfiguration().GetImage(),
                        buildableManifestTemplate,
                        containerConfigurationTemplate).ConfigureAwait(false);

                    return(JsonToImageTranslator.ToImage(
                               buildableManifestTemplate, containerConfigurationTemplate));
                }
            }

            throw new InvalidOperationException(Resources.PullBaseImageStepUnknownManifestErrorMessage);
        }
Exemple #4
0
        private void TestToImage_buildable <T>(
            string jsonFilename) where T : IBuildableManifestTemplate
        {
            // Loads the container configuration JSON.
            SystemPath containerConfigurationJsonFile =
                Paths.Get(
                    TestResources.GetResource("core/json/containerconfig.json").ToURI());
            ContainerConfigurationTemplate containerConfigurationTemplate =
                JsonTemplateMapper.ReadJsonFromFile <ContainerConfigurationTemplate>(
                    containerConfigurationJsonFile);

            // Loads the manifest JSON.
            SystemPath manifestJsonFile =
                Paths.Get(TestResources.GetResource(jsonFilename).ToURI());
            T manifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <T>(manifestJsonFile);

            Image image = JsonToImageTranslator.ToImage(manifestTemplate, containerConfigurationTemplate);

            IList <ILayer> layers = image.GetLayers();

            Assert.AreEqual(1, layers.Count);
            Assert.AreEqual(
                new BlobDescriptor(
                    1000000,
                    DescriptorDigest.FromDigest(
                        "sha256:4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236")),
                layers[0].GetBlobDescriptor());
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                layers[0].GetDiffId());
            CollectionAssert.AreEqual(
                ImmutableArray.Create(
                    HistoryEntry.CreateBuilder()
                    .SetCreationTimestamp(Instant.FromUnixTimeSeconds(0))
                    .SetAuthor("Bazel")
                    .SetCreatedBy("bazel build ...")
                    .SetEmptyLayer(true)
                    .Build(),
                    HistoryEntry.CreateBuilder()
                    .SetCreationTimestamp(Instant.FromUnixTimeSeconds(20))
                    .SetAuthor("Fib")
                    .SetCreatedBy("fib")
                    .Build()),
                image.GetHistory());
            Assert.AreEqual(Instant.FromUnixTimeSeconds(20), image.GetCreated());
            Assert.AreEqual(new[] { "some", "entrypoint", "command" }, image.GetEntrypoint());
            Assert.AreEqual(ImmutableDic.Of("VAR1", "VAL1", "VAR2", "VAL2"), image.GetEnvironment());
            Assert.AreEqual("/some/workspace", image.GetWorkingDirectory());
            Assert.AreEqual(
                ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000), Port.Udp(3000)), image.GetExposedPorts());
            Assert.AreEqual(
                ImmutableHashSet.Create(
                    AbsoluteUnixPath.Get("/var/job-result-data"),
                    AbsoluteUnixPath.Get("/var/log/my-app-logs")),
                image.GetVolumes());
            Assert.AreEqual("tomcat", image.GetUser());
            Assert.AreEqual("value1", image.GetLabels()["key1"]);
            Assert.AreEqual("value2", image.GetLabels()["key2"]);
            Assert.AreEqual(2, image.GetLabels().Count);
        }