示例#1
0
        private static void Push(IConsole console, string?registry, string?username, string?password, string?repository)
        {
            try
            {
                RegistryOption.EnsureNotNullorMalformed(registry);
                RepositoryOption.EnsureNotNullorMalformed(repository);
            }
            catch (ArgumentException e)
            {
                console.Error.WriteLine($"Push failed due to bad/missing argument:\t{e.ParamName}");
                return;
            }

            try
            {
                UsernameOption.EnsureNotNull(ref username);
                PasswordOption.EnsureNotNull(ref password);
            }
            catch (ArgumentException e)
            {
                if (CredentialHelper.TryGetCredentials(registry !, out var credential))
                {
                    username = credential !.UserName;
                    password = credential !.Password;
                }
示例#2
0
        private static async Task TestCredentialsAsync(IConsole console, string?registry, string?username, string?password)
        {
            try
            {
                RegistryOption.EnsureNotNullorMalformed(registry);
                UsernameOption.EnsureNotNull(ref username);
                PasswordOption.EnsureNotNull(ref password);
            }
            catch (ArgumentException e)
            {
                console.Error.WriteLine($"Push failed due to bad/missing argument:\t{e.ParamName}");
                return;
            }

            var registryUri      = new UriBuilder("https", registry).Uri;
            var registryInstance = new Registry(registryUri, username, password);

            try
            {
                _ = await registryInstance.GetApiVersionAsync();

                console.Out.WriteLine("Credentials are valid");
            }
            catch (RegistryException)
            {
                console.Error.WriteLine("Credentials are invalid");
            }
        }
        private static async Task TestCredentialsAsync(IConsole console, string?registry, string?username, string?password)
        {
            try
            {
                RegistryOption.EnsureNotNullorMalformed(registry);
                UsernameOption.EnsureNotNull(ref username);
                PasswordOption.EnsureNotNull(ref password);
            }
            catch (ArgumentException e)
            {
                console.Error.WriteLine($"Push failed due to bad/missing argument:\t{e.ParamName}");
                return;
            }

            var registryUri      = new UriBuilder("https", registry).Uri;
            var registryInstance = new Registry(registryUri, username, password);

            var digest = await registryInstance.GetDigestFromReference("helloworld", "latest", ManifestType.DockerV2);

            console.Out.WriteLine(digest);
            var appManifest = await registryInstance.GetManifestAsync("helloworld", "sha256:70fa9f0f8ec7967b6a10df4907b36a8e13cb13b952f41636ffa8044a115306be", ManifestType.OciV1);

            var appDiffId = appManifest.Layers[0].Annotations["io.deis.oras.content.digest"];
            var appSize   = appManifest.Layers[0].Size;
            var appDigest = appManifest.Layers[0].Digest;

            var baseManifest = await registryInstance.GetManifestAsync("helloworld", "base", ManifestType.DockerV2);

            var baseConfigDigest = baseManifest.Config.Digest;


            var config = await registryInstance.GetConfigBlobAsync("helloworld", baseConfigDigest);

            // Modify config and POST it back
            config.rootfs.diff_ids.Add(appDiffId);
            (var newconfigSize, var newConfigSHA) = await registryInstance.PostConfigBlobAsync("helloworld", config);

            var layer = new Layer()
            {
                MediaType = "application/vnd.docker.image.rootfs.diff.tar.gzip",
                Size      = appSize,
                Digest    = appDigest
            };

            baseManifest.Layers.Add(layer);
            baseManifest.Config.Digest = newConfigSHA;
            baseManifest.Config.Size   = newconfigSize;
            //console.Out.WriteLine(JsonConvert.SerializeObject(baseManifest));

            await registryInstance.PutManifestAsync("helloworld", "run2", baseManifest);
        }
        private static void Push(IConsole console, string?registry, string?username, string?password, string?repository)
        {
            try
            {
                RegistryOption.EnsureNotNullorMalformed(registry);
                UsernameOption.EnsureNotNull(ref username);
                PasswordOption.EnsureNotNull(ref password);
                RepositoryOption.EnsureNotNullorMalformed(repository);
            }
            catch (ArgumentException e)
            {
                console.Error.WriteLine($"Push failed due to bad/missing argument:\t{e.ParamName}");
                return;
            }


            var finder      = new MsBuildProjectFinder(Environment.CurrentDirectory);
            var projectFile = finder.FindMsBuildProject();
            var targetsFile = FindTargetsFile();

            var args = new[]
            {
                "msbuild",
                projectFile,
                "/nologo",
                "/restore",
                "/t:Publish",
                $"/p:CustomAfterMicrosoftCommonTargets={targetsFile}",
                $"/p:CustomAfterMicrosoftCommonCrossTargetingTargets={targetsFile}",
                $"/p:ImageName={registry}/{repository}",
                $"/p:RegistryUsername={username}",
                $"/p:RegistryPassword={password}"
            };
            var psi = new ProcessStartInfo
            {
                FileName  = DotNetMuxer.MuxerPathOrDefault(),
                Arguments = ArgumentEscaper.EscapeAndConcatenate(args),
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
            };

            var process = Process.Start(psi);

            process.WaitForExit();
            console.Out.WriteLine(process.StandardOutput.ReadToEnd());

            string FindTargetsFile()
            {
                var assemblyDir = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                var searchPaths = new[]
                {
                    Path.Combine(AppContext.BaseDirectory, "assets"),
                    Path.Combine(assemblyDir, "assets"),
                    AppContext.BaseDirectory,
                    assemblyDir,
                };

                var targetPath = searchPaths.Select(p => Path.Combine(p, "Oras.targets")).FirstOrDefault(File.Exists);

                if (targetPath == null)
                {
                    Console.WriteLine("Fatal error: could not find Oras.targets");
                }
                return(targetPath);
            }
        }