Merge changes I58d823ec,I5873d361

* changes:
  [KEERO-83] Windows Agent: Ability to reboot machine after execution plan is executed
  [KEERO-83] Windows Agent: Typo fixes + sample values in config
This commit is contained in:
Timur Nurlygayanov 2013-02-21 18:49:31 +04:00 committed by Gerrit Code Review
commit ee6fc07b83
9 changed files with 51 additions and 7 deletions

View File

@ -18,4 +18,13 @@
<logger name="*" minlevel="Debug" writeTo="file" /> <logger name="*" minlevel="Debug" writeTo="file" />
</rules> </rules>
</nlog> </nlog>
<appSettings>
<add key="rabbitmq.host" value="localhost"/>
<add key="rabbitmq.user" value="guest"/>
<add key="rabbitmq.password" value="guest"/>
<add key="rabbitmq.vhost" value="/"/>
<add key="rabbitmq.resultExchange" value=""/>
<add key="rabbitmq.resultQueue" value="-execution-results"/>
</appSettings>
</configuration> </configuration>

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

@ -45,9 +45,7 @@ namespace Mirantis.Keero.WindowsAgent
session.QueueDeclare(queueName, true, false, false, null); session.QueueDeclare(queueName, true, false, false, null);
var consumer = new QueueingBasicConsumer(session); var consumer = new QueueingBasicConsumer(session);
var consumeTag = session.BasicConsume(queueName, false, consumer); var consumeTag = session.BasicConsume(queueName, false, consumer);
Console.WriteLine("Deq");
var e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue(); var e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
Console.WriteLine("Message received");
Action ackFunc = delegate { Action ackFunc = delegate {
session.BasicAck(e.DeliveryTag, false); session.BasicAck(e.DeliveryTag, false);
session.BasicCancel(consumeTag); session.BasicCancel(consumeTag);

View File

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