|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace StackDeployCheck |
| 7 | +{ |
| 8 | + class DockerClient |
| 9 | + { |
| 10 | + private StackState currentStackState; |
| 11 | + public StackState GetCurrentState(string stackName) |
| 12 | + { |
| 13 | + currentStackState = new StackState() |
| 14 | + { |
| 15 | + StackName = stackName |
| 16 | + }; |
| 17 | + |
| 18 | + GetStackTasks(stackName); |
| 19 | + |
| 20 | + return currentStackState; |
| 21 | + } |
| 22 | + |
| 23 | + public void GetStackTasks(string stackName) |
| 24 | + { |
| 25 | + var formatString = "{{.ID}} {{.Name}} {{.Image}} {{.Node}} {{.DesiredState}} [{{.CurrentState}}] [{{.Error}}]"; |
| 26 | + ProcessStartInfo processStartInfo = new ProcessStartInfo("docker", $"stack ps {stackName} --no-trunc --format \"{formatString}\"") |
| 27 | + { |
| 28 | + RedirectStandardError = true, |
| 29 | + RedirectStandardOutput = true, |
| 30 | + RedirectStandardInput = true, |
| 31 | + UseShellExecute = false, |
| 32 | + CreateNoWindow = true |
| 33 | + }; |
| 34 | + using (Process process = new Process() |
| 35 | + { |
| 36 | + StartInfo = processStartInfo, |
| 37 | + EnableRaisingEvents = true |
| 38 | + }) |
| 39 | + { |
| 40 | + process.OutputDataReceived += Process_OutputDataReceived; |
| 41 | + process.ErrorDataReceived += Process_ErrorDataReceived; |
| 42 | + //process.Exited += Process_Exited; |
| 43 | + process.Start(); |
| 44 | + process.BeginOutputReadLine(); |
| 45 | + process.BeginErrorReadLine(); |
| 46 | + //process.WaitForExit(waitTimeout); |
| 47 | + process.WaitForExit(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) |
| 53 | + { |
| 54 | + if (!String.IsNullOrEmpty(e.Data)) |
| 55 | + { |
| 56 | + Console.Error.WriteLineAsync($"ERROR: {e.Data}"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 61 | + { |
| 62 | + if (e.Data == String.Empty |
| 63 | + || e.Data == null) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + //if (e.Data.StartsWith("ID ")) |
| 68 | + //{ |
| 69 | + // //headerLine = e.Data; |
| 70 | + // return; |
| 71 | + //} |
| 72 | + string currentLine = e.Data; |
| 73 | + |
| 74 | + var currentStart = currentLine.IndexOf("[") + 1; |
| 75 | + var currentEnd = currentLine.IndexOf("]"); |
| 76 | + var errorStart = currentLine.IndexOf("[", currentEnd) + 1; |
| 77 | + var errorEnd = currentLine.LastIndexOf("]"); |
| 78 | + |
| 79 | + var taskElements = e.Data.Split(" "); |
| 80 | + var dockerTask = new DockerTask() |
| 81 | + { |
| 82 | + Id = taskElements[0], |
| 83 | + Name = taskElements[1], |
| 84 | + Image = taskElements[2], |
| 85 | + Node = taskElements[3], |
| 86 | + DesiredState = taskElements[4], |
| 87 | + CurrentState = currentLine.Substring(currentStart, currentEnd - currentStart), |
| 88 | + Error = currentLine.Substring(errorStart, errorEnd - errorStart) |
| 89 | + }; |
| 90 | + |
| 91 | + if (dockerTask.DesiredState == "Ready" |
| 92 | + || dockerTask.DesiredState == "Running") |
| 93 | + { |
| 94 | + dockerTask.IsCurrent = true; |
| 95 | + } |
| 96 | + currentStackState.Tasks.Add(dockerTask); |
| 97 | + |
| 98 | + //Console.Error.WriteLineAsync($"OUT: {e.Data}"); |
| 99 | + //Console.Out.WriteLine(dockerTask.Id); |
| 100 | + //Console.Out.WriteLine($"{dockerTask.IsCurrent} {dockerTask.Name}"); |
| 101 | + //Console.Out.WriteLine(dockerTask.Image); |
| 102 | + //Console.Out.WriteLine(dockerTask.Node); |
| 103 | + //Console.Out.WriteLine(dockerTask.DesiredState); |
| 104 | + //Console.Out.WriteLine(dockerTask.CurrentState); |
| 105 | + //Console.Out.WriteLine(dockerTask.Error); |
| 106 | + //Console.Out.WriteLine(""); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments