2015-07-24 10:51:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Import;
|
|
|
|
|
2016-06-28 12:56:53 +02:00
|
|
|
use Exception;
|
2018-03-09 13:29:58 +01:00
|
|
|
use Icinga\Application\Benchmark;
|
2016-03-15 17:28:49 +01:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2017-10-10 14:35:32 +02:00
|
|
|
use Icinga\Module\Director\Application\MemoryLimit;
|
2017-08-21 21:53:19 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
2022-07-01 08:36:01 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectStore;
|
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
2016-10-27 15:43:26 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2022-10-06 13:08:48 +02:00
|
|
|
use Icinga\Module\Director\Db\Branch\BranchSupport;
|
2016-10-31 17:20:17 +01:00
|
|
|
use Icinga\Module\Director\Db\Cache\PrefetchCache;
|
2017-03-14 14:21:15 +01:00
|
|
|
use Icinga\Module\Director\Objects\HostGroupMembershipResolver;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaHostGroup;
|
2015-08-04 19:52:02 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2023-10-11 10:01:14 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObjectGroup;
|
2015-07-24 10:51:55 +02:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
2016-02-24 15:33:08 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaService;
|
2017-10-10 16:32:57 +02:00
|
|
|
use Icinga\Module\Director\Objects\SyncProperty;
|
2015-07-24 10:51:55 +02:00
|
|
|
use Icinga\Module\Director\Objects\SyncRule;
|
2016-02-24 12:24:19 +01:00
|
|
|
use Icinga\Module\Director\Objects\SyncRun;
|
2015-10-20 22:21:48 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2019-04-17 10:47:43 +02:00
|
|
|
use Icinga\Module\Director\Repository\IcingaTemplateRepository;
|
2018-11-24 10:17:18 +01:00
|
|
|
use InvalidArgumentException;
|
2021-03-11 23:00:52 +01:00
|
|
|
use RuntimeException;
|
2015-07-24 10:51:55 +02:00
|
|
|
|
|
|
|
class Sync
|
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var SyncRule */
|
2016-02-23 11:10:37 +01:00
|
|
|
protected $rule;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var Db */
|
2016-10-27 15:43:26 +02:00
|
|
|
protected $db;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var array Related ImportSource objects */
|
2016-02-23 17:03:52 +01:00
|
|
|
protected $sources;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var array Source columns we want to fetch from our sources */
|
2016-02-23 21:05:09 +01:00
|
|
|
protected $sourceColumns;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var array Imported data */
|
2016-02-23 17:35:47 +01:00
|
|
|
protected $imported;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var IcingaObject[] Objects to work with */
|
2016-02-23 17:47:18 +01:00
|
|
|
protected $objects;
|
|
|
|
|
2022-10-07 10:05:01 +02:00
|
|
|
/** @var array<mixed, array<int, string>> key => [property, property]*/
|
|
|
|
protected $setNull = [];
|
|
|
|
|
2022-12-05 19:19:41 +01:00
|
|
|
/** @var array<mixed, array<string, mixed>> key => [propertyName, newValue]*/
|
|
|
|
protected $newProperties = [];
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/** @var bool Whether we already prepared your sync */
|
2016-02-24 12:24:19 +01:00
|
|
|
protected $isPrepared = false;
|
|
|
|
|
2022-08-31 16:36:31 +02:00
|
|
|
/** @var bool Whether we applied strtolower() to existing object keys */
|
|
|
|
protected $usedLowerCasedKeys = false;
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
protected $modify = [];
|
2015-12-23 15:10:37 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
protected $remove = [];
|
2015-12-23 15:10:37 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
protected $create = [];
|
2015-12-23 15:10:37 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
protected $errors = [];
|
2015-12-23 15:10:37 +01:00
|
|
|
|
2017-10-10 16:32:57 +02:00
|
|
|
/** @var SyncProperty[] */
|
2016-02-23 13:03:47 +01:00
|
|
|
protected $syncProperties;
|
|
|
|
|
2017-08-03 10:51:13 +02:00
|
|
|
protected $replaceVars = false;
|
|
|
|
|
2018-05-04 20:52:50 +02:00
|
|
|
protected $hasPropertyDisabled = false;
|
|
|
|
|
|
|
|
protected $serviceOverrideKeyName;
|
|
|
|
|
2016-10-27 15:43:26 +02:00
|
|
|
/**
|
|
|
|
* @var SyncRun
|
|
|
|
*/
|
2016-02-24 12:24:19 +01:00
|
|
|
protected $run;
|
|
|
|
|
|
|
|
protected $runStartTime;
|
|
|
|
|
2017-03-28 18:03:53 +02:00
|
|
|
/** @var Filter[] */
|
2018-05-25 00:03:25 +02:00
|
|
|
protected $columnFilters = [];
|
2016-03-15 17:28:49 +01:00
|
|
|
|
2017-03-14 14:21:15 +01:00
|
|
|
/** @var HostGroupMembershipResolver|bool */
|
2017-03-14 16:08:37 +01:00
|
|
|
protected $hostGroupMembershipResolver;
|
2017-03-14 14:21:15 +01:00
|
|
|
|
2022-07-01 08:36:01 +02:00
|
|
|
/** @var ?DbObjectStore */
|
|
|
|
protected $store;
|
|
|
|
|
2023-10-11 10:01:14 +02:00
|
|
|
/** @var IcingaObjectGroup[] */
|
|
|
|
protected $modifiedGroups = [];
|
|
|
|
|
|
|
|
/** @var IcingaObject[] */
|
|
|
|
protected $modifiedGroupObjects = [];
|
|
|
|
|
2015-12-08 11:51:05 +01:00
|
|
|
/**
|
2016-10-27 15:43:26 +02:00
|
|
|
* @param SyncRule $rule
|
2022-07-01 08:36:01 +02:00
|
|
|
* @param ?DbObjectStore $store
|
2015-12-08 11:51:05 +01:00
|
|
|
*/
|
2022-07-01 08:36:01 +02:00
|
|
|
public function __construct(SyncRule $rule, DbObjectStore $store = null)
|
2015-07-24 10:51:55 +02:00
|
|
|
{
|
2016-02-23 11:10:37 +01:00
|
|
|
$this->rule = $rule;
|
2016-02-23 11:22:25 +01:00
|
|
|
$this->db = $rule->getConnection();
|
2022-07-01 08:36:01 +02:00
|
|
|
$this->store = $store;
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-12-08 11:51:05 +01:00
|
|
|
/**
|
|
|
|
* Whether the given sync rule would apply modifications
|
|
|
|
*
|
|
|
|
* @return boolean
|
2018-05-25 00:03:25 +02:00
|
|
|
* @throws Exception
|
2015-12-08 11:51:05 +01:00
|
|
|
*/
|
2016-02-24 10:50:57 +01:00
|
|
|
public function hasModifications()
|
2015-10-30 23:34:27 +01:00
|
|
|
{
|
2016-02-24 10:50:57 +01:00
|
|
|
return count($this->getExpectedModifications()) > 0;
|
2015-10-30 23:34:27 +01:00
|
|
|
}
|
|
|
|
|
2015-12-08 11:51:05 +01:00
|
|
|
/**
|
|
|
|
* Retrieve modifications a given SyncRule would apply
|
|
|
|
*
|
|
|
|
* @return array Array of IcingaObject elements
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2015-12-08 11:51:05 +01:00
|
|
|
*/
|
2016-02-24 10:50:57 +01:00
|
|
|
public function getExpectedModifications()
|
2015-10-30 23:34:27 +01:00
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
$modified = [];
|
2016-02-24 10:50:57 +01:00
|
|
|
$objects = $this->prepare();
|
2020-01-16 16:31:50 +01:00
|
|
|
$updateOnly = $this->rule->get('update_policy') === 'update-only';
|
|
|
|
$allowCreate = ! $updateOnly;
|
2015-10-30 23:34:27 +01:00
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object->hasBeenModified()) {
|
2020-01-16 16:31:50 +01:00
|
|
|
if ($allowCreate || $object->hasBeenLoadedFromDb()) {
|
|
|
|
$modified[] = $object;
|
|
|
|
}
|
|
|
|
} elseif (! $updateOnly && $object->shouldBeRemoved()) {
|
2016-04-22 14:47:49 +02:00
|
|
|
$modified[] = $object;
|
2015-10-30 23:34:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modified;
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:50:57 +01:00
|
|
|
/**
|
|
|
|
* Raise PHP resource limits
|
|
|
|
*
|
|
|
|
* @return self;
|
|
|
|
*/
|
|
|
|
protected function raiseLimits()
|
|
|
|
{
|
2017-10-10 14:35:32 +02:00
|
|
|
MemoryLimit::raiseTo('1024M');
|
2024-01-18 12:25:54 +01:00
|
|
|
ini_set('max_execution_time', '0');
|
2016-02-24 10:50:57 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:24:19 +01:00
|
|
|
/**
|
|
|
|
* Initialize run summary measurements
|
|
|
|
*
|
|
|
|
* @return self;
|
|
|
|
*/
|
|
|
|
protected function startMeasurements()
|
|
|
|
{
|
|
|
|
$this->run = SyncRun::start($this->rule);
|
|
|
|
$this->runStartTime = microtime(true);
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Starting sync');
|
2016-02-24 12:24:19 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the configured properties involved in this sync
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
protected function fetchSyncProperties()
|
|
|
|
{
|
2017-08-22 00:05:08 +02:00
|
|
|
$this->syncProperties = $this->rule->getSyncProperties();
|
2016-03-15 17:28:49 +01:00
|
|
|
foreach ($this->syncProperties as $key => $prop) {
|
2018-11-24 10:17:18 +01:00
|
|
|
$destinationField = $prop->get('destination_field');
|
|
|
|
if ($destinationField === 'vars' && $prop->get('merge_policy') === 'override') {
|
2017-08-03 10:51:13 +02:00
|
|
|
$this->replaceVars = true;
|
|
|
|
}
|
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($destinationField === 'disabled') {
|
2018-05-04 20:52:50 +02:00
|
|
|
$this->hasPropertyDisabled = true;
|
|
|
|
}
|
|
|
|
|
2021-11-28 11:13:08 +01:00
|
|
|
if ($prop->get('filter_expression') === null || strlen($prop->get('filter_expression')) === 0) {
|
2016-03-15 17:28:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->columnFilters[$key] = Filter::fromQueryString(
|
2018-11-24 10:17:18 +01:00
|
|
|
$prop->get('filter_expression')
|
2016-03-15 17:28:49 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:24:19 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-15 17:28:49 +01:00
|
|
|
protected function rowMatchesPropertyFilter($row, $key)
|
|
|
|
{
|
|
|
|
if (!array_key_exists($key, $this->columnFilters)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->columnFilters[$key]->matches($row);
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:03:52 +01:00
|
|
|
/**
|
|
|
|
* Instantiates all related ImportSource objects
|
|
|
|
*
|
2016-02-23 17:35:47 +01:00
|
|
|
* @return self
|
2018-05-25 00:03:25 +02:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2016-02-23 17:03:52 +01:00
|
|
|
*/
|
|
|
|
protected function prepareRelatedImportSources()
|
2015-07-24 10:51:55 +02:00
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
$this->sources = [];
|
2016-02-23 13:03:47 +01:00
|
|
|
foreach ($this->syncProperties as $p) {
|
2018-11-24 10:17:18 +01:00
|
|
|
$id = $p->get('source_id');
|
2016-02-23 17:47:18 +01:00
|
|
|
if (! array_key_exists($id, $this->sources)) {
|
2018-10-08 06:57:02 +02:00
|
|
|
$this->sources[$id] = ImportSource::loadWithAutoIncId(
|
|
|
|
(int) $id,
|
|
|
|
$this->db
|
|
|
|
);
|
2015-12-08 16:40:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
return $this;
|
2015-12-08 16:40:47 +01:00
|
|
|
}
|
2015-12-08 11:51:05 +01:00
|
|
|
|
2016-02-23 21:05:09 +01:00
|
|
|
/**
|
|
|
|
* Prepare the source columns we want to fetch
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2016-02-23 13:03:47 +01:00
|
|
|
protected function prepareSourceColumns()
|
2015-12-08 16:40:47 +01:00
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
// $fieldMap = [];
|
|
|
|
$this->sourceColumns = [];
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2016-02-23 13:03:47 +01:00
|
|
|
foreach ($this->syncProperties as $p) {
|
2018-11-24 10:17:18 +01:00
|
|
|
$sourceId = $p->get('source_id');
|
2016-02-23 21:05:09 +01:00
|
|
|
if (! array_key_exists($sourceId, $this->sourceColumns)) {
|
2018-05-25 00:03:25 +02:00
|
|
|
$this->sourceColumns[$sourceId] = [];
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
foreach (SyncUtils::extractVariableNames($p->get('source_expression')) as $varname) {
|
2016-02-23 21:05:09 +01:00
|
|
|
$this->sourceColumns[$sourceId][$varname] = $varname;
|
2015-07-24 10:51:55 +02:00
|
|
|
// -> ? $fieldMap[
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 21:05:09 +01:00
|
|
|
return $this;
|
2015-12-08 16:40:47 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
/**
|
|
|
|
* Fetch latest imported data rows from all involved import sources
|
2017-08-03 10:51:13 +02:00
|
|
|
* @return Sync
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2016-02-23 17:35:47 +01:00
|
|
|
*/
|
|
|
|
protected function fetchImportedData()
|
2016-02-23 11:10:37 +01:00
|
|
|
{
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Begin loading imported data');
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($this->rule->get('object_type') === 'host') {
|
2018-05-04 20:52:50 +02:00
|
|
|
$this->serviceOverrideKeyName = $this->db->settings()->override_services_varname;
|
|
|
|
}
|
2018-03-09 13:29:58 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$this->imported = [];
|
2016-02-23 11:10:37 +01:00
|
|
|
|
2016-07-13 19:59:04 +02:00
|
|
|
$sourceKeyPattern = $this->rule->getSourceKeyPattern();
|
|
|
|
$combinedKey = $this->rule->hasCombinedKey();
|
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
foreach ($this->sources as $source) {
|
2016-10-27 15:43:26 +02:00
|
|
|
/** @var ImportSource $source */
|
2018-11-24 10:17:18 +01:00
|
|
|
$sourceId = $source->get('id');
|
2016-02-23 21:05:09 +01:00
|
|
|
|
|
|
|
// Provide an alias column for our key. TODO: double-check this!
|
2015-07-24 10:51:55 +02:00
|
|
|
$key = $source->key_column;
|
2016-02-23 21:05:09 +01:00
|
|
|
$this->sourceColumns[$sourceId][$key] = $key;
|
2016-07-13 10:19:03 +02:00
|
|
|
$run = $source->fetchLastRun(true);
|
2017-03-28 18:03:53 +02:00
|
|
|
|
|
|
|
$usedColumns = SyncUtils::getRootVariables($this->sourceColumns[$sourceId]);
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$filterColumns = [];
|
2017-03-28 18:03:53 +02:00
|
|
|
foreach ($this->columnFilters as $filter) {
|
|
|
|
foreach ($filter->listFilteredColumns() as $column) {
|
|
|
|
$filterColumns[$column] = $column;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 11:58:39 +02:00
|
|
|
if (($ruleFilter = $this->rule->filter()) !== null) {
|
|
|
|
foreach ($ruleFilter->listFilteredColumns() as $column) {
|
|
|
|
$filterColumns[$column] = $column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:31:24 +02:00
|
|
|
if (! empty($filterColumns)) {
|
|
|
|
foreach (SyncUtils::getRootVariables($filterColumns) as $column) {
|
|
|
|
$usedColumns[$column] = $column;
|
|
|
|
}
|
2017-03-28 18:03:53 +02:00
|
|
|
}
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure(sprintf('Done pre-processing columns for source %s', $source->source_name));
|
2017-03-28 18:03:53 +02:00
|
|
|
|
|
|
|
$rows = $run->fetchRows($usedColumns);
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure(sprintf('Fetched source %s', $source->source_name));
|
2015-07-26 15:46:25 +02:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$this->imported[$sourceId] = [];
|
2015-07-24 10:51:55 +02:00
|
|
|
foreach ($rows as $row) {
|
2016-07-13 19:59:04 +02:00
|
|
|
if ($combinedKey) {
|
|
|
|
$key = SyncUtils::fillVariables($sourceKeyPattern, $row);
|
2022-08-31 16:36:31 +02:00
|
|
|
if ($this->usedLowerCasedKeys) {
|
|
|
|
$key = strtolower($key);
|
|
|
|
}
|
2016-07-13 13:52:15 +02:00
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
if (array_key_exists($key, $this->imported[$sourceId])) {
|
2018-11-24 10:17:18 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2016-02-23 08:58:22 +01:00
|
|
|
'Trying to import row "%s" (%s) twice: %s VS %s',
|
|
|
|
$key,
|
2016-07-13 19:59:04 +02:00
|
|
|
$sourceKeyPattern,
|
2016-02-23 17:35:47 +01:00
|
|
|
json_encode($this->imported[$sourceId][$key]),
|
2016-02-23 08:58:22 +01:00
|
|
|
json_encode($row)
|
2018-11-24 10:17:18 +01:00
|
|
|
));
|
2016-02-23 08:58:22 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (! property_exists($row, $key)) {
|
2018-11-24 10:17:18 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2016-02-23 08:58:22 +01:00
|
|
|
'There is no key column "%s" in this row from "%s": %s',
|
|
|
|
$key,
|
|
|
|
$source->source_name,
|
|
|
|
json_encode($row)
|
2018-11-24 10:17:18 +01:00
|
|
|
));
|
2016-02-23 08:58:22 +01:00
|
|
|
}
|
2015-07-24 15:27:37 +02:00
|
|
|
}
|
2016-02-23 08:58:22 +01:00
|
|
|
|
2016-02-23 21:05:09 +01:00
|
|
|
if (! $this->rule->matches($row)) {
|
2015-12-04 10:59:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-23 08:58:22 +01:00
|
|
|
|
2016-07-13 19:59:04 +02:00
|
|
|
if ($combinedKey) {
|
2016-02-23 17:35:47 +01:00
|
|
|
$this->imported[$sourceId][$key] = $row;
|
2016-02-23 08:58:22 +01:00
|
|
|
} else {
|
2022-08-31 16:36:31 +02:00
|
|
|
if ($this->usedLowerCasedKeys) {
|
|
|
|
$this->imported[$sourceId][strtolower($row->$key)] = $row;
|
|
|
|
} else {
|
|
|
|
$this->imported[$sourceId][$row->$key] = $row;
|
|
|
|
}
|
2016-02-23 08:58:22 +01:00
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
2016-07-13 10:19:03 +02:00
|
|
|
|
|
|
|
unset($rows);
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Done loading imported data');
|
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
return $this;
|
2015-12-08 16:40:47 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* TODO: This is rubbish, we need to filter at fetch time
|
|
|
|
*/
|
2016-02-23 17:47:18 +01:00
|
|
|
protected function removeForeignListEntries()
|
2016-02-19 12:42:02 +01:00
|
|
|
{
|
|
|
|
$listId = null;
|
2016-02-23 13:03:47 +01:00
|
|
|
foreach ($this->syncProperties as $prop) {
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($prop->get('destination_field') === 'list_id') {
|
|
|
|
$listId = (int) $prop->get('source_expression');
|
2016-02-19 12:42:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($listId === null) {
|
2018-11-24 10:17:18 +01:00
|
|
|
throw new InvalidArgumentException(
|
2022-08-29 08:53:49 +02:00
|
|
|
'Cannot sync datalist entry without list_id'
|
2016-02-19 12:42:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$no = [];
|
2016-02-23 17:47:18 +01:00
|
|
|
foreach ($this->objects as $k => $o) {
|
2022-08-29 08:53:49 +02:00
|
|
|
if ((int) $o->get('list_id') !== $listId) {
|
2016-02-19 12:42:02 +01:00
|
|
|
$no[] = $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($no as $k) {
|
2016-02-23 17:47:18 +01:00
|
|
|
unset($this->objects[$k]);
|
2016-02-19 12:42:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-02-23 17:47:18 +01:00
|
|
|
protected function loadExistingObjects()
|
|
|
|
{
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Begin loading existing objects');
|
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
$ruleObjectType = $this->rule->get('object_type');
|
2022-08-31 16:36:31 +02:00
|
|
|
$useLowerCaseKeys = $ruleObjectType !== 'datalistEntry';
|
2016-02-23 17:47:18 +01:00
|
|
|
// TODO: Make object_type (template, object...) and object_name mandatory?
|
2016-07-13 19:59:04 +02:00
|
|
|
if ($this->rule->hasCombinedKey()) {
|
2018-05-25 00:03:25 +02:00
|
|
|
$this->objects = [];
|
2016-07-13 19:59:04 +02:00
|
|
|
$destinationKeyPattern = $this->rule->getDestinationKeyPattern();
|
2022-10-06 13:08:48 +02:00
|
|
|
$table = DbObjectTypeRegistry::tableNameByType($ruleObjectType);
|
|
|
|
if ($this->store && BranchSupport::existsForTableName($table)) {
|
|
|
|
$objects = $this->store->loadAll($table);
|
2022-07-01 08:36:01 +02:00
|
|
|
} else {
|
|
|
|
$objects = IcingaObject::loadAllByType($ruleObjectType, $this->db);
|
|
|
|
}
|
2016-02-25 00:10:40 +01:00
|
|
|
|
2022-07-01 08:36:01 +02:00
|
|
|
foreach ($objects as $object) {
|
2016-02-24 15:33:08 +01:00
|
|
|
if ($object instanceof IcingaService) {
|
2024-10-22 14:31:14 +02:00
|
|
|
if (
|
|
|
|
strstr($destinationKeyPattern, '${host}')
|
2018-11-24 10:17:18 +01:00
|
|
|
&& $object->get('host_id') === null
|
|
|
|
) {
|
2016-11-10 14:05:18 +01:00
|
|
|
continue;
|
2024-10-22 14:31:14 +02:00
|
|
|
} elseif (
|
|
|
|
strstr($destinationKeyPattern, '${service_set}')
|
2018-11-24 10:17:18 +01:00
|
|
|
&& $object->get('service_set_id') === null
|
|
|
|
) {
|
2016-02-26 11:58:37 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-24 15:33:08 +01:00
|
|
|
}
|
|
|
|
|
2016-07-13 13:52:15 +02:00
|
|
|
$key = SyncUtils::fillVariables(
|
2016-07-13 19:59:04 +02:00
|
|
|
$destinationKeyPattern,
|
2016-02-24 15:33:08 +01:00
|
|
|
$object
|
|
|
|
);
|
2022-08-31 16:36:31 +02:00
|
|
|
if ($useLowerCaseKeys) {
|
|
|
|
$key = strtolower($key);
|
|
|
|
}
|
2016-02-24 15:33:08 +01:00
|
|
|
|
|
|
|
if (array_key_exists($key, $this->objects)) {
|
2018-11-24 10:17:18 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2016-02-24 15:33:08 +01:00
|
|
|
'Combined destination key "%s" is not unique, got "%s" twice',
|
2016-07-13 19:59:04 +02:00
|
|
|
$destinationKeyPattern,
|
2016-02-24 15:33:08 +01:00
|
|
|
$key
|
2018-11-24 10:17:18 +01:00
|
|
|
));
|
2016-02-24 15:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->objects[$key] = $object;
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($this->store) {
|
2022-08-31 16:36:31 +02:00
|
|
|
$objects = $this->store->loadAll(DbObjectTypeRegistry::tableNameByType($ruleObjectType), 'object_name');
|
|
|
|
} else {
|
2023-05-11 14:57:55 +02:00
|
|
|
$keyColumn = null;
|
|
|
|
$query = null;
|
|
|
|
// We enforce named index for combined-key templates (Services and Sets) and applied Sets
|
|
|
|
if ($ruleObjectType === 'service' || $ruleObjectType === 'serviceSet') {
|
|
|
|
foreach ($this->syncProperties as $prop) {
|
|
|
|
$configuredObjectType = $prop->get('source_expression');
|
2024-10-22 14:31:14 +02:00
|
|
|
if (
|
|
|
|
$prop->get('destination_field') === 'object_type'
|
2023-05-11 14:57:55 +02:00
|
|
|
&& (
|
|
|
|
$configuredObjectType === 'template'
|
|
|
|
|| ($configuredObjectType === 'apply' && $ruleObjectType === 'serviceSet')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$keyColumn = 'object_name';
|
|
|
|
$table = $ruleObjectType === 'service'
|
|
|
|
? BranchSupport::TABLE_ICINGA_SERVICE
|
|
|
|
: BranchSupport::TABLE_ICINGA_SERVICE_SET;
|
|
|
|
$query = $this->db->getDbAdapter()->select()
|
|
|
|
->from($table)->where('object_type = ?', $configuredObjectType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$objects = IcingaObject::loadAllByType($ruleObjectType, $this->db, $query, $keyColumn);
|
2022-08-31 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($useLowerCaseKeys) {
|
|
|
|
$this->objects = [];
|
|
|
|
foreach ($objects as $key => $object) {
|
|
|
|
$this->objects[strtolower($key)] = $object;
|
|
|
|
}
|
2022-07-01 08:36:01 +02:00
|
|
|
} else {
|
2022-08-31 16:36:31 +02:00
|
|
|
$this->objects = $objects;
|
2022-07-01 08:36:01 +02:00
|
|
|
}
|
2016-02-24 15:33:08 +01:00
|
|
|
}
|
2016-02-23 17:47:18 +01:00
|
|
|
|
2022-08-31 16:36:31 +02:00
|
|
|
$this->usedLowerCasedKeys = $useLowerCaseKeys;
|
2016-02-23 17:47:18 +01:00
|
|
|
// TODO: should be obsoleted by a better "loadFiltered" method
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($ruleObjectType === 'datalistEntry') {
|
2017-08-03 10:51:13 +02:00
|
|
|
$this->removeForeignListEntries();
|
2016-02-23 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Done loading existing objects');
|
|
|
|
|
2016-02-23 17:47:18 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2018-05-25 00:03:25 +02:00
|
|
|
*/
|
2016-02-23 17:35:47 +01:00
|
|
|
protected function prepareNewObjects()
|
2015-12-08 16:40:47 +01:00
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
$objects = [];
|
2018-11-24 10:17:18 +01:00
|
|
|
$ruleObjectType = $this->rule->get('object_type');
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
foreach ($this->sources as $source) {
|
2015-07-26 15:46:25 +02:00
|
|
|
$sourceId = $source->id;
|
2022-09-22 17:36:42 +02:00
|
|
|
$keyColumn = $source->get('key_column');
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2016-02-23 17:35:47 +01:00
|
|
|
foreach ($this->imported[$sourceId] as $key => $row) {
|
2019-05-22 17:06:24 +02:00
|
|
|
// Workaround: $a["10"] = "val"; -> array_keys($a) = [(int) 10]
|
|
|
|
$key = (string) $key;
|
2022-09-22 17:36:42 +02:00
|
|
|
$originalKey = $row->$keyColumn;
|
2022-08-31 16:36:31 +02:00
|
|
|
if ($this->usedLowerCasedKeys) {
|
|
|
|
$key = strtolower($key);
|
|
|
|
}
|
2018-05-25 00:03:25 +02:00
|
|
|
if (! array_key_exists($key, $objects)) {
|
|
|
|
// Safe default values for object_type and object_name
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($ruleObjectType === 'datalistEntry') {
|
2018-05-25 00:03:25 +02:00
|
|
|
$props = [];
|
2015-07-26 15:46:25 +02:00
|
|
|
} else {
|
2018-05-25 00:03:25 +02:00
|
|
|
$props = [
|
|
|
|
'object_type' => 'object',
|
2022-09-22 17:36:42 +02:00
|
|
|
'object_name' => $originalKey,
|
2018-05-25 00:03:25 +02:00
|
|
|
];
|
2015-07-26 15:46:25 +02:00
|
|
|
}
|
2018-05-25 00:03:25 +02:00
|
|
|
|
|
|
|
$objects[$key] = IcingaObject::createByType(
|
2018-11-24 10:17:18 +01:00
|
|
|
$ruleObjectType,
|
2018-05-25 00:03:25 +02:00
|
|
|
$props,
|
2016-02-23 11:55:16 +01:00
|
|
|
$this->db
|
2015-12-10 12:57:11 +01:00
|
|
|
);
|
2016-02-19 15:23:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$object = $objects[$key];
|
2022-10-07 10:05:01 +02:00
|
|
|
$this->prepareNewObject($row, $object, $key, $sourceId);
|
2018-05-25 00:03:25 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 12:57:11 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
return $objects;
|
|
|
|
}
|
2015-08-03 15:12:46 +02:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @param DbObject $object
|
|
|
|
* @param $sourceId
|
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2018-05-25 00:03:25 +02:00
|
|
|
*/
|
2022-10-07 10:05:01 +02:00
|
|
|
protected function prepareNewObject($row, DbObject $object, $objectKey, $sourceId)
|
2018-05-25 00:03:25 +02:00
|
|
|
{
|
2022-12-05 19:19:41 +01:00
|
|
|
if (!isset($this->newProperties[$objectKey])) {
|
|
|
|
$this->newProperties[$objectKey] = [];
|
|
|
|
}
|
|
|
|
// TODO: some more improvements are possible here. First, no need to instantiate
|
|
|
|
// all new objects, we could stick with the newProperties array. Next, we
|
|
|
|
// should be more correct when respecting sync property order. Right now,
|
|
|
|
// a property from another Import Source might win, even if property order
|
|
|
|
// tells something different. This is a very rare case, but still incorrect.
|
|
|
|
$properties = &$this->newProperties[$objectKey];
|
2018-05-25 00:03:25 +02:00
|
|
|
foreach ($this->syncProperties as $propertyKey => $p) {
|
2018-11-24 10:17:18 +01:00
|
|
|
if ($p->get('source_id') !== $sourceId) {
|
2018-05-25 00:03:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-19 15:23:44 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
if (! $this->rowMatchesPropertyFilter($row, $propertyKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-19 15:23:44 +01:00
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
$prop = $p->get('destination_field');
|
|
|
|
$val = SyncUtils::fillVariables($p->get('source_expression'), $row);
|
2016-02-19 15:23:44 +01:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
if ($object instanceof IcingaObject) {
|
|
|
|
if ($prop === 'import') {
|
|
|
|
if ($val !== null) {
|
|
|
|
$object->imports()->add($val);
|
|
|
|
}
|
|
|
|
} elseif ($prop === 'groups') {
|
|
|
|
if ($val !== null) {
|
|
|
|
$object->groups()->add($val);
|
|
|
|
}
|
|
|
|
} elseif (substr($prop, 0, 5) === 'vars.') {
|
|
|
|
$varName = substr($prop, 5);
|
|
|
|
if (substr($varName, -2) === '[]') {
|
|
|
|
$varName = substr($varName, 0, -2);
|
|
|
|
$object->vars()->$varName = array_merge(
|
2023-08-22 10:32:19 +02:00
|
|
|
(array) ($object->vars()->$varName),
|
|
|
|
(array) $val
|
2018-05-25 00:03:25 +02:00
|
|
|
);
|
|
|
|
} else {
|
2022-12-05 19:19:41 +01:00
|
|
|
$this->setPropertyWithNullLogic($object, $objectKey, $prop, $val, $properties);
|
2018-05-25 00:03:25 +02:00
|
|
|
}
|
2018-05-25 11:45:57 +02:00
|
|
|
} else {
|
2022-12-05 19:19:41 +01:00
|
|
|
$this->setPropertyWithNullLogic($object, $objectKey, $prop, $val, $properties);
|
2018-05-25 00:03:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-05 19:19:41 +01:00
|
|
|
$this->setPropertyWithNullLogic($object, $objectKey, $prop, $val, $properties);
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
2015-07-26 15:46:25 +02:00
|
|
|
}
|
2016-02-19 15:23:44 +01:00
|
|
|
}
|
2015-07-24 15:30:09 +02:00
|
|
|
|
2022-12-05 19:19:41 +01:00
|
|
|
protected function setPropertyWithNullLogic(DbObject $object, $objectKey, $property, $value, &$allProps)
|
|
|
|
{
|
|
|
|
if ($value === null) {
|
|
|
|
if (! array_key_exists($property, $allProps) || $allProps[$property] === null) {
|
|
|
|
$this->setNull[$objectKey][$property] = $property;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($this->setNull[$objectKey][$property]);
|
|
|
|
$object->set($property, $value);
|
|
|
|
}
|
|
|
|
$allProps[$property] = $value;
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-03-14 14:21:15 +01:00
|
|
|
protected function deferResolvers()
|
|
|
|
{
|
2018-05-25 00:03:25 +02:00
|
|
|
if (in_array($this->rule->get('object_type'), ['host', 'hostgroup'])) {
|
2017-08-22 00:05:08 +02:00
|
|
|
$resolver = $this->getHostGroupMembershipResolver();
|
2017-03-14 14:21:15 +01:00
|
|
|
$resolver->defer()->setUseTransactions(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-03 10:51:13 +02:00
|
|
|
/**
|
2017-08-21 21:53:19 +02:00
|
|
|
* @param DbObject $object
|
2017-08-03 10:51:13 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
2017-03-14 16:08:37 +01:00
|
|
|
protected function setResolver($object)
|
2017-03-14 14:21:15 +01:00
|
|
|
{
|
2017-08-03 10:51:13 +02:00
|
|
|
if (! ($object instanceof IcingaHost || $object instanceof IcingaHostGroup)) {
|
2017-03-14 16:08:37 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2017-08-22 00:05:08 +02:00
|
|
|
if ($resolver = $this->getHostGroupMembershipResolver()) {
|
2017-03-14 14:21:15 +01:00
|
|
|
$object->setHostGroupMembershipResolver($resolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
2018-11-24 10:17:18 +01:00
|
|
|
* @throws \Zend_Db_Adapter_Exception
|
2018-05-25 00:03:25 +02:00
|
|
|
*/
|
2017-03-14 14:21:15 +01:00
|
|
|
protected function notifyResolvers()
|
|
|
|
{
|
2017-08-22 00:05:08 +02:00
|
|
|
if ($resolver = $this->getHostGroupMembershipResolver()) {
|
2023-10-11 10:01:14 +02:00
|
|
|
if ($this->rule->get('object_type') === 'hostgroup') {
|
|
|
|
$resolver->setGroups($this->modifiedGroups);
|
|
|
|
}
|
2017-08-22 00:05:08 +02:00
|
|
|
$resolver->refreshDb(true);
|
2017-03-14 14:21:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-03 10:51:13 +02:00
|
|
|
/**
|
|
|
|
* @return bool|HostGroupMembershipResolver
|
|
|
|
*/
|
2017-08-22 00:05:08 +02:00
|
|
|
protected function getHostGroupMembershipResolver()
|
2017-03-14 14:21:15 +01:00
|
|
|
{
|
2017-03-14 16:08:37 +01:00
|
|
|
if ($this->hostGroupMembershipResolver === null) {
|
2024-10-22 14:31:14 +02:00
|
|
|
if (
|
|
|
|
in_array(
|
|
|
|
$this->rule->get('object_type'),
|
|
|
|
['host', 'hostgroup']
|
|
|
|
)
|
|
|
|
) {
|
2017-03-14 16:08:37 +01:00
|
|
|
$this->hostGroupMembershipResolver = new HostGroupMembershipResolver(
|
2017-03-14 14:21:15 +01:00
|
|
|
$this->db
|
|
|
|
);
|
|
|
|
} else {
|
2017-03-14 16:08:37 +01:00
|
|
|
$this->hostGroupMembershipResolver = false;
|
2017-03-14 14:21:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:08:37 +01:00
|
|
|
return $this->hostGroupMembershipResolver;
|
2017-03-14 14:21:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-19 15:23:44 +01:00
|
|
|
/**
|
|
|
|
* Evaluates a SyncRule and returns a list of modified objects
|
|
|
|
*
|
2018-05-04 20:52:50 +02:00
|
|
|
* TODO: Split this into smaller methods
|
2016-02-19 15:23:44 +01:00
|
|
|
*
|
2018-05-04 20:52:50 +02:00
|
|
|
* @return DbObject|IcingaObject[] List of modified IcingaObjects
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
|
|
|
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
2016-02-19 15:23:44 +01:00
|
|
|
*/
|
2016-02-23 11:22:25 +01:00
|
|
|
protected function prepare()
|
2016-02-19 15:23:44 +01:00
|
|
|
{
|
2016-02-24 12:24:19 +01:00
|
|
|
if ($this->isPrepared) {
|
|
|
|
return $this->objects;
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:50:57 +01:00
|
|
|
$this->raiseLimits()
|
2016-02-24 12:24:19 +01:00
|
|
|
->startMeasurements()
|
2018-05-04 20:52:50 +02:00
|
|
|
->prepareCache()
|
2016-02-24 12:24:19 +01:00
|
|
|
->fetchSyncProperties()
|
2016-02-24 10:50:57 +01:00
|
|
|
->prepareRelatedImportSources()
|
2016-02-23 21:05:09 +01:00
|
|
|
->prepareSourceColumns()
|
2016-02-24 15:33:08 +01:00
|
|
|
->loadExistingObjects()
|
2017-03-14 14:21:15 +01:00
|
|
|
->fetchImportedData()
|
|
|
|
->deferResolvers();
|
2016-02-19 15:23:44 +01:00
|
|
|
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Begin preparing updated objects');
|
2018-05-04 20:52:50 +02:00
|
|
|
$newObjects = $this->prepareNewObjects();
|
2018-03-09 13:29:58 +01:00
|
|
|
|
2018-05-04 20:52:50 +02:00
|
|
|
Benchmark::measure('Ready to process objects');
|
2017-10-13 11:41:59 +02:00
|
|
|
/** @var DbObject|IcingaObject $object */
|
2016-02-19 15:23:44 +01:00
|
|
|
foreach ($newObjects as $key => $object) {
|
2018-05-04 20:52:50 +02:00
|
|
|
$this->processObject($key, $object);
|
2015-10-20 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:52:50 +02:00
|
|
|
Benchmark::measure('Modified objects are ready, applying purge strategy');
|
2018-05-25 00:03:25 +02:00
|
|
|
$noAction = [];
|
2021-03-11 23:00:52 +01:00
|
|
|
$purgeAction = $this->rule->get('purge_action');
|
2016-06-26 14:00:37 +02:00
|
|
|
foreach ($this->rule->purgeStrategy()->listObjectsToPurge() as $key) {
|
2022-11-02 21:12:40 +01:00
|
|
|
$key = strtolower($key);
|
2016-02-22 11:01:37 +01:00
|
|
|
if (array_key_exists($key, $newObjects)) {
|
2016-06-26 14:00:37 +02:00
|
|
|
// Object has been touched, do not delete
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-19 15:23:44 +01:00
|
|
|
|
2016-06-26 14:00:37 +02:00
|
|
|
if (array_key_exists($key, $this->objects)) {
|
|
|
|
$object = $this->objects[$key];
|
|
|
|
if (! $object->hasBeenModified()) {
|
2021-03-11 23:00:52 +01:00
|
|
|
switch ($purgeAction) {
|
|
|
|
case 'delete':
|
|
|
|
$object->markForRemoval();
|
|
|
|
break;
|
|
|
|
case 'disable':
|
|
|
|
$object->set('disabled', 'y');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Unsupported purge action: '$purgeAction'"
|
|
|
|
);
|
|
|
|
}
|
2016-06-26 14:00:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 11:01:37 +01:00
|
|
|
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Done marking objects for purge');
|
|
|
|
|
2016-06-26 14:00:37 +02:00
|
|
|
foreach ($this->objects as $key => $object) {
|
|
|
|
if (! $object->hasBeenModified() && ! $object->shouldBeRemoved()) {
|
2016-02-22 11:01:37 +01:00
|
|
|
$noAction[] = $key;
|
2016-02-19 15:23:44 +01:00
|
|
|
}
|
2015-10-20 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 11:01:37 +01:00
|
|
|
foreach ($noAction as $key) {
|
2016-02-23 17:47:18 +01:00
|
|
|
unset($this->objects[$key]);
|
2016-02-22 11:01:37 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 12:24:19 +01:00
|
|
|
$this->isPrepared = true;
|
|
|
|
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Done preparing objects');
|
|
|
|
|
2016-02-23 17:47:18 +01:00
|
|
|
return $this->objects;
|
2015-10-20 23:29:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:52:50 +02:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param DbObject|IcingaObject $object
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2018-05-04 20:52:50 +02:00
|
|
|
*/
|
|
|
|
protected function processObject($key, $object)
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->objects)) {
|
|
|
|
$this->refreshObject($key, $object);
|
|
|
|
} else {
|
|
|
|
$this->addNewObject($key, $object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:01:14 +02:00
|
|
|
protected function optionallyTellResolverAboutModifiedGroup(IcingaObjectGroup $group)
|
|
|
|
{
|
|
|
|
if (in_array('assign_filter', $group->getModifiedProperties())) {
|
|
|
|
$this->modifiedGroups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:52:50 +02:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param DbObject|IcingaObject $object
|
2019-01-10 12:10:53 +01:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
2018-05-04 20:52:50 +02:00
|
|
|
*/
|
|
|
|
protected function refreshObject($key, $object)
|
|
|
|
{
|
|
|
|
$policy = $this->rule->get('update_policy');
|
|
|
|
|
|
|
|
switch ($policy) {
|
|
|
|
case 'override':
|
2024-10-22 14:31:14 +02:00
|
|
|
if (
|
|
|
|
$object instanceof IcingaHost
|
2022-08-19 10:56:26 +02:00
|
|
|
&& !in_array('api_key', $this->rule->getSyncProperties())
|
|
|
|
) {
|
|
|
|
$this->objects[$key]->replaceWith($object, ['api_key']);
|
|
|
|
} else {
|
|
|
|
$this->objects[$key]->replaceWith($object);
|
|
|
|
}
|
2018-05-04 20:52:50 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'merge':
|
2020-01-16 16:31:50 +01:00
|
|
|
case 'update-only':
|
2018-05-04 20:52:50 +02:00
|
|
|
// TODO: re-evaluate merge settings. vars.x instead of
|
|
|
|
// just "vars" might suffice.
|
|
|
|
$this->objects[$key]->merge($object, $this->replaceVars);
|
|
|
|
if (! $this->hasPropertyDisabled && $object->hasProperty('disabled')) {
|
|
|
|
$this->objects[$key]->resetProperty('disabled');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// policy 'ignore', no action
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($policy === 'override' || $policy === 'merge') {
|
|
|
|
if ($object instanceof IcingaHost) {
|
|
|
|
$keyName = $this->serviceOverrideKeyName;
|
|
|
|
if (! $object->hasInitializedVars() || ! isset($object->vars()->$key)) {
|
|
|
|
$this->objects[$key]->vars()->restoreStoredVar($keyName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 10:05:01 +02:00
|
|
|
|
2022-12-05 19:19:41 +01:00
|
|
|
// Hint: in theory, NULL should be set on new objects, but this has no effect
|
|
|
|
// anyway, and we also do not store vars.something = null, this would
|
|
|
|
// instead delete the variable. So here we do not need to check for new
|
|
|
|
// objects, and skip all null values with update policy = 'ignore'
|
|
|
|
if ($policy !== 'ignore' && isset($this->setNull[$key])) {
|
2022-10-07 10:05:01 +02:00
|
|
|
foreach ($this->setNull[$key] as $property) {
|
|
|
|
$this->objects[$key]->set($property, null);
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 20:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param DbObject|IcingaObject $object
|
|
|
|
*/
|
|
|
|
protected function addNewObject($key, $object)
|
|
|
|
{
|
|
|
|
$this->objects[$key] = $object;
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:51:05 +01:00
|
|
|
/**
|
|
|
|
* Runs a SyncRule and applies all resulting changes
|
|
|
|
* @return int
|
2017-08-03 10:51:13 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @throws IcingaException
|
2015-12-08 11:51:05 +01:00
|
|
|
*/
|
2016-02-24 10:50:57 +01:00
|
|
|
public function apply()
|
2015-10-20 23:29:29 +02:00
|
|
|
{
|
2018-03-09 13:29:58 +01:00
|
|
|
Benchmark::measure('Begin applying objects');
|
|
|
|
|
2016-02-23 13:41:19 +01:00
|
|
|
$objects = $this->prepare();
|
2016-02-24 12:24:19 +01:00
|
|
|
$db = $this->db;
|
|
|
|
$dba = $db->getDbAdapter();
|
2022-07-01 08:36:01 +02:00
|
|
|
if (! $this->store) { // store has it's own transaction
|
|
|
|
$dba->beginTransaction();
|
|
|
|
}
|
2016-06-28 12:56:53 +02:00
|
|
|
|
2016-10-27 15:43:26 +02:00
|
|
|
$object = null;
|
2020-01-16 16:31:50 +01:00
|
|
|
$updateOnly = $this->rule->get('update_policy') === 'update-only';
|
|
|
|
$allowCreate = ! $updateOnly;
|
2016-10-27 15:43:26 +02:00
|
|
|
|
2016-06-28 12:56:53 +02:00
|
|
|
try {
|
2018-10-30 14:52:29 +01:00
|
|
|
$formerActivityChecksum = hex2bin(
|
2016-06-28 12:56:53 +02:00
|
|
|
$db->getLastActivityChecksum()
|
|
|
|
);
|
|
|
|
$created = 0;
|
|
|
|
$modified = 0;
|
|
|
|
$deleted = 0;
|
2018-11-24 10:17:18 +01:00
|
|
|
// TODO: Count also failed ones, once we allow such
|
|
|
|
// $failed = 0;
|
2016-06-28 12:56:53 +02:00
|
|
|
foreach ($objects as $object) {
|
2017-03-14 14:21:15 +01:00
|
|
|
$this->setResolver($object);
|
2020-01-16 16:31:50 +01:00
|
|
|
if (! $updateOnly && $object->shouldBeRemoved()) {
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($this->store) {
|
|
|
|
$this->store->delete($object);
|
|
|
|
} else {
|
|
|
|
$object->delete();
|
|
|
|
}
|
2016-06-28 12:56:53 +02:00
|
|
|
$deleted++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-04 10:24:54 +01:00
|
|
|
|
2016-06-28 12:56:53 +02:00
|
|
|
if ($object->hasBeenModified()) {
|
2023-10-11 10:01:14 +02:00
|
|
|
if ($object instanceof IcingaObjectGroup) {
|
|
|
|
$this->optionallyTellResolverAboutModifiedGroup($object);
|
|
|
|
}
|
2018-05-04 20:52:50 +02:00
|
|
|
$existing = $object->hasBeenLoadedFromDb();
|
|
|
|
if ($existing) {
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($this->store) {
|
|
|
|
$this->store->store($object);
|
|
|
|
} else {
|
|
|
|
$object->store($db);
|
|
|
|
}
|
2016-06-28 12:56:53 +02:00
|
|
|
$modified++;
|
2020-01-16 16:31:50 +01:00
|
|
|
} elseif ($allowCreate) {
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($this->store) {
|
|
|
|
$this->store->store($object);
|
|
|
|
} else {
|
|
|
|
$object->store($db);
|
|
|
|
}
|
2016-06-28 12:56:53 +02:00
|
|
|
$created++;
|
|
|
|
}
|
2016-02-24 12:24:19 +01:00
|
|
|
}
|
2015-11-02 09:29:03 +01:00
|
|
|
}
|
2015-10-20 22:22:58 +02:00
|
|
|
|
2018-05-25 00:03:25 +02:00
|
|
|
$runProperties = [
|
2016-06-28 12:56:53 +02:00
|
|
|
'objects_created' => $created,
|
|
|
|
'objects_deleted' => $deleted,
|
|
|
|
'objects_modified' => $modified,
|
2018-05-25 00:03:25 +02:00
|
|
|
];
|
2016-02-24 12:24:19 +01:00
|
|
|
|
2016-06-28 12:56:53 +02:00
|
|
|
if ($created + $deleted + $modified > 0) {
|
|
|
|
// TODO: What if this has been the very first activity?
|
|
|
|
$runProperties['last_former_activity'] = $db->quoteBinary($formerActivityChecksum);
|
2018-10-30 14:52:29 +01:00
|
|
|
$runProperties['last_related_activity'] = $db->quoteBinary(hex2bin(
|
2016-06-28 12:56:53 +02:00
|
|
|
$db->getLastActivityChecksum()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-10-07 12:22:19 +02:00
|
|
|
$this->run->setProperties($runProperties);
|
2022-11-03 10:22:38 +01:00
|
|
|
if (!$this->store || !$this->store->getBranch()->isBranch()) {
|
2022-10-07 12:22:19 +02:00
|
|
|
$this->run->store();
|
|
|
|
}
|
2017-08-22 00:05:08 +02:00
|
|
|
$this->notifyResolvers();
|
2022-07-01 08:36:01 +02:00
|
|
|
if (! $this->store) {
|
|
|
|
$dba->commit();
|
|
|
|
}
|
2016-02-24 12:24:19 +01:00
|
|
|
|
2016-06-28 12:56:53 +02:00
|
|
|
// Store duration after commit, as the commit might take some time
|
|
|
|
$this->run->set('duration_ms', (int) round(
|
|
|
|
(microtime(true) - $this->runStartTime) * 1000
|
2022-10-07 12:22:19 +02:00
|
|
|
));
|
2022-11-03 10:22:38 +01:00
|
|
|
if (!$this->store || !$this->store->getBranch()->isBranch()) {
|
2022-10-07 12:22:19 +02:00
|
|
|
$this->run->store();
|
|
|
|
}
|
2018-03-09 13:29:58 +01:00
|
|
|
|
|
|
|
Benchmark::measure('Done applying objects');
|
2016-06-28 12:56:53 +02:00
|
|
|
} catch (Exception $e) {
|
2022-07-01 08:36:01 +02:00
|
|
|
if (! $this->store) {
|
|
|
|
$dba->rollBack();
|
|
|
|
}
|
2017-01-13 20:48:50 +01:00
|
|
|
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($object instanceof IcingaObject) {
|
2016-10-27 15:43:26 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'Exception while syncing %s %s: %s',
|
2017-01-13 19:47:54 +01:00
|
|
|
get_class($object),
|
2022-07-01 08:36:01 +02:00
|
|
|
$object->getObjectName(),
|
2017-01-13 19:47:54 +01:00
|
|
|
$e->getMessage(),
|
|
|
|
$e
|
2016-10-27 15:43:26 +02:00
|
|
|
);
|
2017-01-13 19:47:54 +01:00
|
|
|
} else {
|
2016-10-27 15:43:26 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-06-28 12:56:53 +02:00
|
|
|
}
|
2016-02-24 12:24:19 +01:00
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
return $this->run->get('id');
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
2018-03-11 13:08:16 +01:00
|
|
|
|
|
|
|
protected function prepareCache()
|
|
|
|
{
|
2022-07-01 08:36:01 +02:00
|
|
|
if ($this->store) {
|
|
|
|
return $this;
|
|
|
|
}
|
2018-03-11 13:08:16 +01:00
|
|
|
PrefetchCache::initialize($this->db);
|
2019-04-17 10:47:43 +02:00
|
|
|
IcingaTemplateRepository::clear();
|
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
$ruleObjectType = $this->rule->get('object_type');
|
2018-03-11 13:08:16 +01:00
|
|
|
|
2018-11-24 10:17:18 +01:00
|
|
|
$dummy = IcingaObject::createByType($ruleObjectType);
|
2018-05-22 10:50:57 +02:00
|
|
|
if ($dummy instanceof IcingaObject) {
|
2018-11-24 10:17:18 +01:00
|
|
|
IcingaObject::prefetchAllRelationsByType($ruleObjectType, $this->db);
|
2018-05-22 10:50:57 +02:00
|
|
|
}
|
2018-03-11 13:08:16 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|