Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kduret committed Dec 6, 2018
1 parent a90c1fa commit b8bb937
Show file tree
Hide file tree
Showing 39 changed files with 501 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
use Centreon\Domain\Repository\Interfaces\CfgCentreonBrokerInterface;
use Centreon\Infrastructure\Service\Exception\NotFoundException;

/**
* Repository to manage main centreon broker configuration
* @todo move management of cfg_centreonbroker_info in another repository
*/
class CfgCentreonBrokerRepository extends ServiceEntityRepository implements CfgCentreonBrokerInterface
{
/**
Expand Down Expand Up @@ -105,7 +109,10 @@ public function export(array $pollerIds): array
return $result;
}

public function truncate()
/**
* Truncate centreon broker configuration in database (cfg_centreonbroker, cfg_centreonbroker_info)
*/
public function truncate(): void
{
$sql = <<<SQL
TRUNCATE TABLE `cfg_centreonbroker`;
Expand Down
68 changes: 43 additions & 25 deletions src/CentreonRemote/Application/Clapi/CentreonRemoteServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Centreon\Infrastructure\Service\CentreonClapiServiceInterface;
use ReflectionClass;

/**
* Class to manage remote server with clapi (enable, disable, import)
*/
class CentreonRemoteServer implements CentreonClapiServiceInterface
{

Expand All @@ -28,70 +31,85 @@ public static function getName() : string
return (new \ReflectionClass(__CLASS__))->getShortName();
}

public function enableRemote($ip)
/**
* Clapi command to enable remote server
*
* @param string $ip ip address of the central server
*/
public function enableRemote(string $ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
echo "Incorrect IP parameter, please pass `-v IP` of the master server\n"; return 200;
echo "Incorrect IP parameter, please pass `-v IP` of the master server\n";
return null;
}

echo "Starting Centreon Remote enable process: \n";

echo "\n Limiting Menu Access...";
echo "Limiting Menu Access...";
$result = $this->getDi()['centreon.db-manager']->getRepository(TopologyRepository::class)->disableMenus();
echo ($result) ? 'Success' : 'Fail' . "\n";

echo "\n Limiting Actions...";
$result = $this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->toggleRemote('yes');
echo 'Done'. "\n";
echo "Limiting Actions...";
$this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->toggleRemote('yes');
echo "Done\n";

echo "\n Authorizing Master...";
$result = $this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->authorizeMaster($ip);
echo 'Done'. "\n";
$this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->authorizeMaster($ip);
echo "Done\n";

echo "\n Set 'remote' instance type...";
system("sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1remote\\3/' " . _CENTREON_ETC_ . "/conf.pm");
echo 'Done'. "\n";
system(
"sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1remote\\3/' " . _CENTREON_ETC_ . "/conf.pm"
);
echo "Done\n";

echo "\n Notifying Master...";
$result = $this->getDi()['centreon.notifymaster']->pingMaster($ip);
echo (!empty($result['status']) && $result['status'] == 'success') ? 'Success' : 'Fail' . "\n";

echo "\n Centreon Remote enabling finished.\n";
echo "Centreon Remote enabling finished.\n";
}

public function disableRemote()
/**
* Clapi command to disable remote server
*/
public function disableRemote(): void
{
echo "Starting Centreon Remote disable process: \n";

echo "\n Restoring Menu Access...";
echo "Restoring Menu Access...";
$result = $this->getDi()['centreon.db-manager']->getRepository(TopologyRepository::class)->enableMenus();
echo ($result) ? 'Success' : 'Fail' . "\n";

echo "\n Restoring Actions...";
$result = $this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->toggleRemote('no');
echo "Restoring Actions...";
$this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->toggleRemote('no');
echo 'Done'. "\n";

echo "\n Restore 'central' instance type...";
system("sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1central\\3/' " . _CENTREON_ETC_ . "/conf.pm");
echo 'Done'. "\n";
echo "Restore 'central' instance type...";
system(
"sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1central\\3/' " . _CENTREON_ETC_ . "/conf.pm"
);
echo "Done\n";

echo "\n Centreon Remote disabling finished.\n";
echo "Centreon Remote disabling finished.\n";
}

public function import()
/**
* Import files which are stored in import directory
*/
public function import(): void
{
echo "Starting Centreon Remote import process: \n";
echo "\n Importing...";
echo "Importing...";

try {
$this->getDi()['centreon_remote.export']->import();
echo "Success\n";
} catch (\Exception $ex) {
echo "Fail:\n";
echo $ex->__toString()."\n";
} catch (\Exception $e) {
echo "Fail: " . $e->getMessage() . "\n";
}

echo "\n Centreon Remote import finished.\n";
echo "Centreon Remote import finished.\n";
}

public function getDi(): Container
Expand Down
47 changes: 38 additions & 9 deletions src/CentreonRemote/Application/Clapi/CentreonWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Centreon\Domain\Entity\Task;
use Centreon\Domain\Entity\Command;

/**
* Manage worker queue with centcore (import/export tasks...)
*/
class CentreonWorker implements CentreonClapiServiceInterface
{

Expand All @@ -34,17 +37,32 @@ public static function getName() : string
}

/**
* Worker method to process task queue for import/export
* @return int
* Process task queue for import/export
*
* @return void
*/
public function processQueue()
public function processQueue(): void
{
// check export tasks in database and execute these
$this->processExportTasks();

// check import tasks in database and execute these
$this->processImportTasks();
}

/**
* Execute export tasks which are store in task table
*
* @return void
*/
private function processExportTasks(): void
{
$datetime = (new \DateTime())->format("Y-m-d H:i:s");
$tasks = $this->getDi()['centreon.db-manager']->getRepository(TaskRepository::class)->findExportTasks() ?? [];

echo "{$datetime} - Checking for pending export tasks: " . count($tasks) . " task(s) found\n";

foreach ($tasks as $x => $task) {
foreach (array_values($tasks) as $task) {
echo "{$datetime} - Processing task #" . $task->getId() . " ... ";

/*
Expand All @@ -58,7 +76,7 @@ public function processQueue()
try {
$this->getDi()['centreon_remote.export']->export($commitment);
} catch (\Exception $e) {
echo $e->__toString() . "\n";
echo $e->getMessage() . "\n";
}

$this->getDi()['centreon.taskservice']->updateStatus($task->getId(), Task::STATE_COMPLETED);
Expand All @@ -74,8 +92,15 @@ public function processQueue()

echo "finished.\n";
}
}


/**
* Execute import tasks which are store in task table
*
* @return void
*/
private function processImportTasks(): void
{
$datetime = (new \DateTime())->format("Y-m-d H:i:s");
$tasks = $this->getDi()['centreon.db-manager']->getRepository(TaskRepository::class)->findImportTasks() ?? [];

Expand All @@ -92,7 +117,7 @@ public function processQueue()
try {
$this->getDi()['centreon_remote.export']->import();
} catch (\Exception $e) {
echo $e->__toString() . "\n";
echo $e->getMessage() . "\n";
}
$this->getDi()['centreon.taskservice']->updateStatus($task->getId(), Task::STATE_COMPLETED);

Expand All @@ -104,9 +129,13 @@ public function processQueue()

/**
* Worker method to create task for import on remote.
*
* @param int $taskId the task id to create on the remote server
* @return void
*/
public function createRemoteTask(int $taskId)
public function createRemoteTask(int $taskId): void
{
// find task parameters (type, status, params...)
$task = $this->getDi()['centreon.db-manager']->getRepository(TaskRepository::class)->findOneById($taskId);

/**
Expand All @@ -128,7 +157,7 @@ public function createRemoteTask(int $taskId)

} catch (\Exception $e) {
echo "Error while creating parent task on $url\n";
echo $e->getMessage()."\n";
echo $e->getMessage() . "\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function postGetWaitList(): array
* Get list with connected remotes
*
* @return array
* @example ['id' => 'poller id', 'ip' => 'poller ip address', 'name' => 'poller name']
*/
public function postGetRemotesList(): array
{
Expand Down Expand Up @@ -205,6 +206,8 @@ public function postGetRemotesList(): array
* Link centreon remote server
*
* @return array
* @example ['error' => true, 'message' => 'error message']
* @example ['success' => true, 'task_id' => 'task id']
*
* @throws \RestBadRequestException
*/
Expand Down
30 changes: 20 additions & 10 deletions src/CentreonRemote/Application/Webservice/CentreonTaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public static function getName(): string
*
* Get Status of task
*
* @return string
* @return array
* @example ['success' => true, 'status' => 'status of the task']
*
* @throws \RestBadRequestException
*/
public function postGetTaskStatus()
public function postGetTaskStatus(): array
{
if (!isset($this->arguments['task_id'])) {
throw new \RestBadRequestException('Missing argument task_id');
Expand Down Expand Up @@ -104,11 +105,12 @@ public function postGetTaskStatus()
*
* Get Status of task by parent
*
* @return string
* @return array
* @example ['success' => true, 'status' => 'status of the task']
*
* @throws \RestBadRequestException
*/
public function postGetRemoteTaskStatusByParent()
public function postGetRemoteTaskStatusByParent(): array
{
if (!isset($this->arguments['server_ip']) ||
!isset($this->arguments['centreon_folder']) ||
Expand All @@ -127,7 +129,15 @@ public function postGetRemoteTaskStatusByParent()
return ['success' => true, 'status' => $result];
}

public function postGetTaskStatusByParent()
/**
* Find task status by parent id (used on remote server)
*
* @return array
* @example ['success' => true, 'status' => 'status of the task']
*
* @throws \RestBadRequestException
*/
public function postGetTaskStatusByParent(): array
{
if (!isset($this->arguments['parent_id'])) {
throw new \RestBadRequestException('Missing argument parent_id');
Expand Down Expand Up @@ -180,11 +190,12 @@ public function postGetTaskStatusByParent()
*
* Add new import task with parent ID
*
* @return string
* @return array
* @example ['success' => true, 'status' => 'status of the task']
*
* @throws \RestBadRequestException
*/
public function postAddImportTaskWithParent()
public function postAddImportTaskWithParent(): array
{
if (!isset($this->arguments['parent_id'])) {
throw new \RestBadRequestException('Missing parent_id parameter');
Expand Down Expand Up @@ -217,8 +228,7 @@ public function postAddImportTaskWithParent()
}

// add new task
$result = $this->getDi()['centreon.taskservice']
->addTask(Task::TYPE_IMPORT, $params, $parentId);
$result = $this->getDi()['centreon.taskservice']->addTask(Task::TYPE_IMPORT, $params, $parentId);

return ['success' => true, 'status' => $result];
}
Expand All @@ -232,7 +242,7 @@ public function postAddImportTaskWithParent()
*
* @return boolean If the user has access to the action
*/
public function authorize($action, $user, $isInternal = false)
public function authorize($action, $user, $isInternal = false): bool
{
return true;
}
Expand Down
Loading

0 comments on commit b8bb937

Please sign in to comment.