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" />
</rules>
</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>

View File

@ -16,5 +16,6 @@ namespace Mirantis.Keero.WindowsAgent
public string[] Scripts { 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;
}
public bool RebootNeeded { get; set; }
public string Execute()
{
RebootNeeded = false;
try
{
var plan = JsonConvert.DeserializeObject<ExecutionPlan>(File.ReadAllText(this.path));
var resultPath = this.path + ".result";
List<object> currentResults = null;
List<ExecutionResult> currentResults = null;
try
{
currentResults = JsonConvert.DeserializeObject<List<object>>(File.ReadAllText(resultPath));
currentResults = JsonConvert.DeserializeObject<List<ExecutionResult>>(File.ReadAllText(resultPath));
}
catch
{
currentResults = new List<object>();
currentResults = new List<ExecutionResult>();
}
@ -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;
}

View File

@ -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);
}
}
}

View File

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

View File

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