[KEERO-83] Windows Agent: Ability to reboot machine after execution plan is executed

This commit is contained in:
Stan Lagun 2013-02-21 00:19:46 +04:00
parent 5873d3610b
commit 58d823eca5
4 changed files with 42 additions and 5 deletions

View File

@ -16,5 +16,6 @@ namespace Mirantis.Keero.WindowsAgent
public string[] Scripts { get; set; } public string[] Scripts { get; set; }
public LinkedList<Command> Commands { get; set; } public LinkedList<Command> Commands { get; set; }
public int RebootOnCompletion { get; set; }
} }
} }

View File

@ -24,20 +24,23 @@ namespace Mirantis.Keero.WindowsAgent
this.path = path; this.path = path;
} }
public bool RebootNeeded { get; set; }
public string Execute() public string Execute()
{ {
RebootNeeded = false;
try try
{ {
var plan = JsonConvert.DeserializeObject<ExecutionPlan>(File.ReadAllText(this.path)); var plan = JsonConvert.DeserializeObject<ExecutionPlan>(File.ReadAllText(this.path));
var resultPath = this.path + ".result"; var resultPath = this.path + ".result";
List<object> currentResults = null; List<ExecutionResult> currentResults = null;
try try
{ {
currentResults = JsonConvert.DeserializeObject<List<object>>(File.ReadAllText(resultPath)); currentResults = JsonConvert.DeserializeObject<List<ExecutionResult>>(File.ReadAllText(resultPath));
} }
catch catch
{ {
currentResults = new List<object>(); currentResults = new List<ExecutionResult>();
} }
@ -100,6 +103,19 @@ namespace Mirantis.Keero.WindowsAgent
IsException = false, IsException = false,
Result = currentResults Result = currentResults
}, Formatting.Indented); }, Formatting.Indented);
if (plan.RebootOnCompletion > 0)
{
if (plan.RebootOnCompletion == 1)
{
RebootNeeded = !currentResults.Any(t => t.IsException);
}
else
{
RebootNeeded = true;
}
}
File.Delete(resultPath); File.Delete(resultPath);
return executionResult; return executionResult;
} }

View File

@ -29,6 +29,7 @@ namespace Mirantis.Keero.WindowsAgent
void Loop() void Loop()
{ {
var doReboot = false;
const string filePath = "data.json"; const string filePath = "data.json";
while (!stop) while (!stop)
{ {
@ -40,10 +41,16 @@ namespace Mirantis.Keero.WindowsAgent
File.WriteAllText(filePath, message.Body); File.WriteAllText(filePath, message.Body);
message.Ack(); message.Ack();
} }
var result = new PlanExecutor(filePath).Execute(); var executor = new PlanExecutor(filePath);
var result = executor.Execute();
if(stop) break; if(stop) break;
rabbitMqClient.SendResult(result); rabbitMqClient.SendResult(result);
File.Delete(filePath); File.Delete(filePath);
if (executor.RebootNeeded)
{
doReboot = true;
break;
}
} }
catch (Exception exception) 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);
}
}
} }

View File

@ -32,5 +32,6 @@
"Name": "TestThrow", "Name": "TestThrow",
} }
] ],
"RebootOnCompletion": 0
} }