Exemplo n.º 1
0
        public static void Login(string username, string password, string server, bool isDryRun)
        {
            Version clientVersion = GetClientVersion();

            if (clientVersion >= new Version(17, 7))
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(
                    "docker", $"login -u {username} --password-stdin {server}");
                startInfo.RedirectStandardInput = true;
                ExecuteHelper.Execute(
                    startInfo,
                    info =>
                {
                    Process process = Process.Start(info);
                    process.StandardInput.WriteLine(password);
                    process.StandardInput.Close();
                    process.WaitForExit();
                    return(process);
                },
                    isDryRun);
            }
            else
            {
                ExecuteHelper.Execute(
                    "docker",
                    $"login -u {username} -p {password} {server}",
                    isDryRun,
                    executeMessageOverride: $"login -u {username} -p ******** {server}");
            }
        }
Exemplo n.º 2
0
        public static string GetCommitSha(string filePath, bool useFullHash = false)
        {
            // Don't make the assumption that the current working directory is a Git repository
            // Find the Git repo that contains the file being checked.
            DirectoryInfo directory = new FileInfo(filePath).Directory;

            while (!directory.GetDirectories(".git").Any())
            {
                directory = directory.Parent;

                if (directory is null)
                {
                    throw new InvalidOperationException($"File '{filePath}' is not contained within a Git repository.");
                }
            }

            filePath = Path.GetRelativePath(directory.FullName, filePath);

            string format = useFullHash ? "H" : "h";

            return(ExecuteHelper.Execute(
                       new ProcessStartInfo("git", $"log -1 --format=format:%{format} {filePath}")
            {
                WorkingDirectory = directory.FullName
            },
                       false,
                       $"Unable to retrieve the latest commit SHA for {filePath}"));
        }
Exemplo n.º 3
0
 public static void PullBaseImages(ManifestInfo manifest, Options options)
 {
     Utilities.WriteHeading("PULLING LATEST BASE IMAGES");
     foreach (string fromImage in manifest.GetExternalFromImages())
     {
         ExecuteHelper.ExecuteWithRetry("docker", $"pull {fromImage}", options.IsDryRun);
     }
 }
Exemplo n.º 4
0
        public static int Main(string[] args)
        {
            int result = 0;

            try
            {
                ICommand[] commands =
                {
                    new BuildCommand(),
                    new CopyImagesCommand(),
                    new CopyAcrImagesCommand(),
                    new GenerateBuildMatrixCommand(),
                    new GenerateTagsReadmeCommand(),
                    new PublishManifestCommand(),
                    new PublishMcrReadmesCommand(),
                    new UpdateReadmeCommand(),
                    new UpdateVersionsCommand(),
                    new ValidateImageSizeCommand(),
                };

                ArgumentSyntax argSyntax = ArgumentSyntax.Parse(args, syntax =>
                {
                    foreach (ICommand command in commands)
                    {
                        command.Options.ParseCommandLine(syntax);
                    }
                });

                // Workaround for https://github.com/dotnet/corefxlab/issues/1689
                foreach (Argument arg in argSyntax.GetActiveArguments())
                {
                    if (arg.IsParameter && !arg.IsSpecified)
                    {
                        Logger.WriteError($"error: `{arg.Name}` must be specified.");
                        Environment.Exit(1);
                    }
                }

                if (argSyntax.ActiveCommand != null)
                {
                    // Capture the Docker version and info in the output.
                    ExecuteHelper.Execute(fileName: "docker", args: "version", isDryRun: false);
                    ExecuteHelper.Execute(fileName: "docker", args: "info", isDryRun: false);

                    ICommand command = commands.Single(c => c.Options == argSyntax.ActiveCommand.Value);
                    command.LoadManifest();
                    command.ExecuteAsync().Wait();
                }
            }
            catch (Exception e)
            {
                Logger.WriteError(e.ToString());

                result = 1;
            }

            return(result);
        }
Exemplo n.º 5
0
        public static string GetCommitSha(string filePath, bool useFullHash = false)
        {
            string format = useFullHash ? "H" : "h";

            return(ExecuteHelper.Execute(
                       "git",
                       $"log -1 --format=format:%{format} {filePath}",
                       false,
                       $"Unable to retrieve the latest commit SHA for {filePath}"));
        }
Exemplo n.º 6
0
        public void ExecuteAzCommand(string command, bool isDryRun, string commandMessageOverride = null)
        {
            commandMessageOverride = commandMessageOverride ?? command;

            ExecuteHelper.Execute(
                "docker",
                $"run --rm -v {_sessionId}:/root {AzureCliImage} az {command}",
                isDryRun,
                executeMessageOverride: $"docker run -it --rm -v {_sessionId}:/root {AzureCliImage} az {commandMessageOverride}");
        }
Exemplo n.º 7
0
        private static string ExecuteCommand(
            string command, string errorMessage, string additionalArgs = null, bool isDryRun = false)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("docker", $"{command} {additionalArgs}");

            startInfo.RedirectStandardOutput = true;
            Process process = ExecuteHelper.Execute(startInfo, isDryRun, errorMessage);

            return(isDryRun ? "" : process.StandardOutput.ReadToEnd().Trim());
        }
Exemplo n.º 8
0
        public JArray Inspect(string image, bool isDryRun)
        {
            string output = ExecuteHelper.ExecuteWithRetry("manifest-tool", $"inspect {image} --raw", isDryRun);

            if (isDryRun)
            {
                return(null);
            }

            return(JsonConvert.DeserializeObject <JArray>(output));
        }
Exemplo n.º 9
0
        public static string GetCommitSha(string filePath, bool useFullHash = false)
        {
            string           format    = useFullHash ? "H" : "h";
            ProcessStartInfo startInfo = new ProcessStartInfo("git", $"log -1 --format=format:%{format} {filePath}");

            startInfo.RedirectStandardOutput = true;
            Process gitLogProcess = ExecuteHelper.Execute(
                startInfo, false, $"Unable to retrieve the latest commit SHA for {filePath}");

            return(gitLogProcess.StandardOutput.ReadToEnd().Trim());
        }
Exemplo n.º 10
0
        public static int Main(string[] args)
        {
            int result = 0;

            try
            {
                ICommand[] commands = Container.GetExportedValues <ICommand>().ToArray();

                ArgumentSyntax argSyntax = ArgumentSyntax.Parse(args, syntax =>
                {
                    foreach (ICommand command in commands)
                    {
                        command.Options.ParseCommandLine(syntax);
                    }
                });

                // Workaround for https://github.com/dotnet/corefxlab/issues/1689
                foreach (Argument arg in argSyntax.GetActiveArguments())
                {
                    if (arg.IsParameter && !arg.IsSpecified)
                    {
                        Logger.WriteError($"error: `{arg.Name}` must be specified.");
                        Environment.Exit(1);
                    }
                }

                if (argSyntax.ActiveCommand != null)
                {
                    // Capture the Docker version and info in the output.
                    ExecuteHelper.Execute(fileName: "docker", args: "version", isDryRun: false);
                    ExecuteHelper.Execute(fileName: "docker", args: "info", isDryRun: false);

                    ICommand command = commands.Single(c => c.Options == argSyntax.ActiveCommand.Value);
                    if (command is IManifestCommand manifestCommand)
                    {
                        manifestCommand.LoadManifest();
                    }

                    command.ExecuteAsync().Wait();
                }
            }
            catch (Exception e)
            {
                Logger.WriteError(e.ToString());

                result = 1;
            }

            return(result);
        }
Exemplo n.º 11
0
        public static int Main(string[] args)
        {
            int result = 0;

            try
            {
                ICommand[] commands = Container.GetExportedValues <ICommand>().ToArray();

                RootCommand rootCliCommand = new RootCommand();

                foreach (ICommand command in commands)
                {
                    rootCliCommand.AddCommand(command.GetCliCommand());
                }

                Parser parser = new CommandLineBuilder(rootCliCommand)
                                .UseDefaults()
                                .UseMiddleware(context =>
                {
                    if (context.ParseResult.CommandResult.Command != rootCliCommand)
                    {
                        // Capture the Docker version and info in the output.
                        ExecuteHelper.Execute(fileName: "docker", args: "version", isDryRun: false);
                        ExecuteHelper.Execute(fileName: "docker", args: "info", isDryRun: false);
                    }
                })
                                .UseMiddleware(context =>
                {
                    context.BindingContext.AddModelBinder(new ModelBinder <AzdoOptions>());
                    context.BindingContext.AddModelBinder(new ModelBinder <GitOptions>());
                    context.BindingContext.AddModelBinder(new ModelBinder <ManifestFilterOptions>());
                    context.BindingContext.AddModelBinder(new ModelBinder <RegistryCredentialsOptions>());
                    context.BindingContext.AddModelBinder(new ModelBinder <ServicePrincipalOptions>());
                    context.BindingContext.AddModelBinder(new ModelBinder <SubscriptionOptions>());
                })
                                .Build();
                return(parser.Invoke(args));
            }
            catch (Exception e)
            {
                Logger.WriteError(e.ToString());

                result = 1;
            }

            return(result);
        }
Exemplo n.º 12
0
        public static void PullBaseImages(ManifestInfo manifest, Options options)
        {
            Logger.WriteHeading("PULLING LATEST BASE IMAGES");
            IEnumerable <string> baseImages = manifest.GetExternalFromImages().ToArray();

            if (baseImages.Any())
            {
                foreach (string fromImage in baseImages)
                {
                    ExecuteHelper.ExecuteWithRetry("docker", $"pull {fromImage}", options.IsDryRun);
                }
            }
            else
            {
                Logger.WriteMessage("No external base images to pull");
            }
        }
Exemplo n.º 13
0
        public void BuildImage(string dockerfilePath, string buildContextPath, IEnumerable <string> tags, IDictionary <string, string> buildArgs, bool isRetryEnabled, bool isDryRun)
        {
            string tagArgs = $"-t {string.Join(" -t ", tags)}";

            IEnumerable <string> buildArgList = buildArgs
                                                .Select(buildArg => $" --build-arg {buildArg.Key}={buildArg.Value}");
            string buildArgsString = String.Join(string.Empty, buildArgList);

            string dockerArgs = $"build {tagArgs} -f {dockerfilePath}{buildArgsString} {buildContextPath}";

            if (isRetryEnabled)
            {
                ExecuteHelper.ExecuteWithRetry("docker", dockerArgs, isDryRun);
            }
            else
            {
                ExecuteHelper.Execute("docker", dockerArgs, isDryRun);
            }
        }
Exemplo n.º 14
0
 public static void Logout(string server, bool isDryRun)
 {
     ExecuteHelper.Execute("docker", $"logout {server}", isDryRun);
 }
Exemplo n.º 15
0
 private static void Logout(string server, bool isDryRun)
 {
     ExecuteHelper.ExecuteWithRetry("docker", $"logout {server}", isDryRun);
 }
Exemplo n.º 16
0
 public static void PullImage(string image, bool isDryRun)
 {
     ExecuteHelper.ExecuteWithRetry("docker", $"pull {image}", isDryRun);
 }
Exemplo n.º 17
0
 public void Dispose()
 {
     ExecuteHelper.Execute("docker", $"volume rm -f {_sessionId}", false);
 }
Exemplo n.º 18
0
 public string?Execute(
     string fileName, string args, bool isDryRun, string?errorMessage = null, string?executeMessageOverride = null) =>
 ExecuteHelper.Execute(fileName, args, isDryRun, errorMessage, executeMessageOverride);
Exemplo n.º 19
0
 public void PushImage(string tag, bool isDryRun) => ExecuteHelper.ExecuteWithRetry("docker", $"push {tag}", isDryRun);
Exemplo n.º 20
0
 public string?Execute(
     ProcessStartInfo info, bool isDryRun, string?errorMessage = null, string?executeMessageOverride = null) =>
 ExecuteHelper.Execute(info, isDryRun, errorMessage, executeMessageOverride);
Exemplo n.º 21
0
 public void PushFromSpec(string manifestFile, bool isDryRun)
 {
     // ExecuteWithRetry because the manifest-tool fails periodically while communicating
     // with the Docker Registry.
     ExecuteHelper.ExecuteWithRetry("manifest-tool", $"push from-spec {manifestFile}", isDryRun);
 }