From 58d823eca55bc7566c45e2219766e106b9e59982 Mon Sep 17 00:00:00 2001 From: Stan Lagun Date: Thu, 21 Feb 2013 00:19:46 +0400 Subject: [PATCH] [KEERO-83] Windows Agent: Ability to reboot machine after execution plan is executed --- WindowsAgent/WindowsAgent/ExecutionPlan.cs | 1 + WindowsAgent/WindowsAgent/PlanExecutor.cs | 22 ++++++++++++++++--- WindowsAgent/WindowsAgent/Program.cs | 21 +++++++++++++++++- .../WindowsAgent/SampleExecutionPlan.json | 3 ++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/WindowsAgent/WindowsAgent/ExecutionPlan.cs b/WindowsAgent/WindowsAgent/ExecutionPlan.cs index 9c0be86bf..899c5cca5 100644 --- a/WindowsAgent/WindowsAgent/ExecutionPlan.cs +++ b/WindowsAgent/WindowsAgent/ExecutionPlan.cs @@ -16,5 +16,6 @@ namespace Mirantis.Keero.WindowsAgent public string[] Scripts { get; set; } public LinkedList Commands { get; set; } + public int RebootOnCompletion { get; set; } } } diff --git a/WindowsAgent/WindowsAgent/PlanExecutor.cs b/WindowsAgent/WindowsAgent/PlanExecutor.cs index 7baab0bd4..ad4910c36 100644 --- a/WindowsAgent/WindowsAgent/PlanExecutor.cs +++ b/WindowsAgent/WindowsAgent/PlanExecutor.cs @@ -24,20 +24,23 @@ namespace Mirantis.Keero.WindowsAgent this.path = path; } + public bool RebootNeeded { get; set; } + public string Execute() { + RebootNeeded = false; try { var plan = JsonConvert.DeserializeObject(File.ReadAllText(this.path)); var resultPath = this.path + ".result"; - List currentResults = null; + List currentResults = null; try { - currentResults = JsonConvert.DeserializeObject>(File.ReadAllText(resultPath)); + currentResults = JsonConvert.DeserializeObject>(File.ReadAllText(resultPath)); } catch { - currentResults = new List(); + currentResults = new List(); } @@ -100,6 +103,19 @@ namespace Mirantis.Keero.WindowsAgent IsException = false, Result = currentResults }, Formatting.Indented); + + if (plan.RebootOnCompletion > 0) + { + if (plan.RebootOnCompletion == 1) + { + RebootNeeded = !currentResults.Any(t => t.IsException); + } + else + { + RebootNeeded = true; + } + } + File.Delete(resultPath); return executionResult; } diff --git a/WindowsAgent/WindowsAgent/Program.cs b/WindowsAgent/WindowsAgent/Program.cs index 8bb49423b..d228f921f 100644 --- a/WindowsAgent/WindowsAgent/Program.cs +++ b/WindowsAgent/WindowsAgent/Program.cs @@ -29,6 +29,7 @@ namespace Mirantis.Keero.WindowsAgent void Loop() { + var doReboot = false; const string filePath = "data.json"; while (!stop) { @@ -40,10 +41,16 @@ namespace Mirantis.Keero.WindowsAgent File.WriteAllText(filePath, message.Body); message.Ack(); } - var result = new PlanExecutor(filePath).Execute(); + var executor = new PlanExecutor(filePath); + var result = executor.Execute(); if(stop) break; rabbitMqClient.SendResult(result); File.Delete(filePath); + if (executor.RebootNeeded) + { + doReboot = true; + break; + } } catch (Exception exception) { @@ -51,6 +58,18 @@ namespace Mirantis.Keero.WindowsAgent } } + if (doReboot) + { + Console.WriteLine("Rebooting..."); + try + { + System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0"); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + } } diff --git a/WindowsAgent/WindowsAgent/SampleExecutionPlan.json b/WindowsAgent/WindowsAgent/SampleExecutionPlan.json index 333ec85ec..9522b70a7 100644 --- a/WindowsAgent/WindowsAgent/SampleExecutionPlan.json +++ b/WindowsAgent/WindowsAgent/SampleExecutionPlan.json @@ -32,5 +32,6 @@ "Name": "TestThrow", } - ] + ], + "RebootOnCompletion": 0 } \ No newline at end of file