Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Action series #209

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Added an initial class for running a series
  • Loading branch information
turtle0x1 committed May 16, 2020
commit d03f2484dfa56f1829f82e83be44b8f42b3aef86
29 changes: 29 additions & 0 deletions src/classes/Model/ActionSeries/Commands/FetchCommandDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace dhope0000\LXDClient\Model\ActionSeries\Commands;

use dhope0000\LXDClient\Model\Database\Database;

class FetchCommandDetails
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function fetchDetails(int $commandId)
{
$sql = "SELECT
`ASC_Command` as `command`
FROM
`Action_Series_Commands`
WHERE
`ASC_ID` = :commandId
";
$do = $this->database->prepare($sql);
$do->execute([
":commandId"=>$commandId
]);
return $do->fetch(\PDO::FETCH_ASSOC);
}
}
33 changes: 33 additions & 0 deletions src/classes/Model/ActionSeries/Commands/FetchCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace dhope0000\LXDClient\Model\ActionSeries\Commands;

use dhope0000\LXDClient\Model\Database\Database;

class FetchCommands
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function fetchForSeries(int $seriesId)
{
$sql = "SELECT
`ASC_ID` as `id`,
`ASC_Name` as `name`,
`ASC_Command` as `command`,
`ASC_ASC_Parent` as `parentId`,
`ASC_ASC_Parent_Return_Action` as `parentReturnAction`
FROM
`Action_Series_Commands`
WHERE
`ASC_AS_ID` = :seriesId
";
$do = $this->database->prepare($sql);
$do->execute([
":seriesId"=>$seriesId
]);
return $do->fetchAll(\PDO::FETCH_ASSOC);
}
}
29 changes: 29 additions & 0 deletions src/classes/Model/ActionSeries/Run/UpdateRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace dhope0000\LXDClient\Model\ActionSeries\Run;

use dhope0000\LXDClient\Model\Database\Database;

class UpdateRun
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function closeRun(int $runId)
{
$sql = "UPDATE
`Action_Series_Runs`
SET
`ASR_Date_Finished` = CURRENT_TIMESTAMP
WHERE
`ASR_ID` = :runId
";
$do = $this->database->prepare($sql);
$do->execute([
":runId"=>$runId
]);
return $do->rowCount() ? true : false;
}
}
77 changes: 77 additions & 0 deletions src/classes/Tools/ActionSeries/Run/StartRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace dhope0000\LXDClient\Tools\ActionSeries\Run;

use dhope0000\LXDClient\Model\ActionSeries\Run\InsertRun;
use dhope0000\LXDClient\Tools\ActionSeries\Commands\GetCommandTree;
use dhope0000\LXDClient\Tools\ActionSeries\Run\ExecuteCommand;
use dhope0000\LXDClient\Model\ActionSeries\Run\UpdateRun;

class StartRun
{
private $runId = null;

public function __construct(
InsertRun $insertRun,
GetCommandTree $getCommandTree,
ExecuteCommand $executeCommand,
UpdateRun $updateRun
) {
$this->insertRun = $insertRun;
$this->getCommandTree = $getCommandTree;
$this->executeCommand = $executeCommand;
$this->updateRun = $updateRun;
}

public function start(int $userId, int $seriesId, array $instancesByHost)
{
$this->insertRun->insert($userId, $seriesId);

$this->runId = $this->insertRun->getId();

$commandTree = $this->getCommandTree->get($seriesId);
try {
foreach ($instancesByHost as $hostId => $instances) {
foreach ($instances as $instance) {
$this->traverseTree($hostId, $instance, $commandTree);
}
}
} catch (\Exception $e) {
// Some error logging
// $this->updateRun->setFailed($runId);
} finally {
$this->updateRun->closeRun($this->runId);
}
}

private function traverseTree(int $hostId, string $instance, array $tree)
{
foreach ($tree as $command) {
$result = $this->executeCommand->execute($hostId, $instance, $this->runId, $command["id"]);

// When we have reached the end of the tree
if (!isset($command["children"])) {
break;
}

$childBranch = $this->findResultCommandSeries($result, $command["children"]);

if (empty($childBranch)) {
throw new \Exception("Reached an un-expected result for {$command["command"]}", 1);
}

$this->traverseTree($hostId, $instance, $childBranch);
}

return true;
}

private function findResultCommandSeries(int $result, array $commandOptions)
{
foreach ($commandOptions as $branch) {
if ((int) $branch["parentReturnAction"] === $result) {
return [$branch];
}
}
}
}