icingaweb2-module-director/library/Director/Import/Sync.php

756 lines
21 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director\Import;
use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\SyncRule;
2016-02-24 12:24:19 +01:00
use Icinga\Module\Director\Objects\SyncRun;
use Icinga\Module\Director\Util;
2015-10-20 22:21:48 +02:00
use Icinga\Exception\IcingaException;
class Sync
{
2016-02-23 17:35:47 +01:00
/**
* @var SyncRule
*/
protected $rule;
2016-02-23 17:35:47 +01:00
/**
* Related ImportSource objects
*
* @var array
*/
protected $sources;
2016-02-23 21:05:09 +01:00
/**
* Source columns we want to fetch from our sources
*
* @var array
*/
protected $sourceColumns;
2016-02-23 17:35:47 +01:00
/**
* Imported data
*/
protected $imported;
2016-02-23 17:47:18 +01:00
/**
* Objects to work with
*
* @var array
*/
protected $objects;
2016-02-24 12:24:19 +01:00
/**
* Whether we already prepared your sync
*
* @var bool
*/
protected $isPrepared = false;
2015-12-23 15:10:37 +01:00
protected $modify = array();
protected $remove = array();
protected $create = array();
protected $errors = array();
protected $hasCombinedKey;
protected $sourceKeyPattern;
protected $destinationKeyPattern;
protected $syncProperties;
2016-02-24 12:24:19 +01:00
protected $run;
protected $runStartTime;
protected $columnFilters = array();
/**
* Constructor. No direct initialization allowed right now. Please use one
* of the available static factory methods
*/
public function __construct(SyncRule $rule)
{
$this->rule = $rule;
$this->db = $rule->getConnection();
}
/**
* Whether the given sync rule would apply modifications
*
* @return boolean
*/
public function hasModifications()
{
return count($this->getExpectedModifications()) > 0;
}
/**
* Retrieve modifications a given SyncRule would apply
*
* @return array Array of IcingaObject elements
*/
public function getExpectedModifications()
{
$modified = array();
$objects = $this->prepare();
foreach ($objects as $object) {
if ($object->hasBeenModified()) {
$modified[] = $object;
2016-04-22 14:47:49 +02:00
} elseif ($object instanceof IcingaObject && $object->shouldBeRemoved()) {
$modified[] = $object;
}
}
return $modified;
}
/**
* Extract variable names in the form ${var_name} from a given string
*
* @param string $string
*
* @return array List of variable names (without ${})
*/
protected function extractVariableNames($string)
{
if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
return $m[1];
} else {
return array();
}
}
/**
* Transform the given value to an array
*
* @param array|string|null $value
*
* @return array
*/
2015-08-28 23:56:54 +02:00
protected function wantArray($value)
{
if (is_array($value)) {
return $value;
} elseif ($value === null) {
return array();
} else {
return array($value);
}
}
/**
* Recursively extract a value from a nested structure
*
* For a $val looking like
*
* { 'vars' => { 'disk' => { 'sda' => { 'size' => '256G' } } } }
*
* and a key vars.disk.sda given as [ 'vars', 'disk', 'sda' ] this would
* return { size => '255GB' }
*
* @param string $val The value to extract data from
* @param object $keys A list of nested keys pointing to desired data
*
* @return mixed
*/
protected function getDeepValue($val, $keys)
{
$key = array_shift($keys);
if (! property_exists($val, $key)) {
return null;
}
if (empty($keys)) {
return $val->$key;
}
return $this->getDeepValue($val->$key, $keys);
}
/**
* Return a specific value from a given row object
*
* Supports also keys pointing to nested structures like vars.disk.sda
*
* @param object $row stdClass object providing property values
* @param string $string Variable/property name
*
* @return mixed
*/
2015-12-23 15:10:37 +01:00
public function getSpecificValue($row, $var)
{
if (strpos($var, '.') === false) {
if ($row instanceof IcingaObject) {
return $row->$var;
}
2016-02-25 00:12:10 +01:00
if (! property_exists($row, $var)) {
return null;
}
return $row->$var;
} else {
$parts = explode('.', $var);
$main = array_shift($parts);
if (! is_object($row->$main)) {
throw new IcingaException('Data is not nested, cannot access %s: %s', $var, var_export($row, 1));
2015-08-28 23:52:02 +02:00
}
return $this->getDeepValue($row->$main, $parts);
}
}
/**
* Fill variables in the given string pattern
*
* This replaces all occurances of ${var_name} with the corresponding
* property $row->var_name of the given row object. Missing variables are
* replaced by an empty string. This works also fine in case there are
* multiple variables to be found in your string.
*
* @param string $string String with opional variables/placeholders
* @param object $row stdClass object providing property values
*
* @return string
*/
protected function fillVariables($string, $row)
{
if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
return $this->getSpecificValue($row, $m[1]);
2015-08-28 23:52:02 +02:00
}
2015-12-23 15:10:37 +01:00
// PHP 5.3 :(
$self = $this;
$func = function ($match) use ($self, $row) {
return $self->getSpecificValue($row, $match[1]);
};
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
}
/**
* Raise PHP resource limits
*
* TODO: do this in a failsafe way, and only if necessary
*
* @return self;
*/
protected function raiseLimits()
{
ini_set('memory_limit', '768M');
ini_set('max_execution_time', 0);
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);
return $this;
}
/**
* Fetch the configured properties involved in this sync
*
* @return self
*/
protected function fetchSyncProperties()
{
$this->syncProperties = $this->rule->fetchSyncProperties();
foreach ($this->syncProperties as $key => $prop) {
if (! strlen($prop->filter_expression)) {
continue;
}
$this->columnFilters[$key] = Filter::fromQueryString(
$prop->filter_expression
);
}
2016-02-24 12:24:19 +01:00
return $this;
}
protected function rowMatchesPropertyFilter($row, $key)
{
if (!array_key_exists($key, $this->columnFilters)) {
return true;
}
return $this->columnFilters[$key]->matches($row);
}
/**
* Instantiates all related ImportSource objects
*
2016-02-23 17:35:47 +01:00
* @return self
*/
protected function prepareRelatedImportSources()
{
$this->sources = array();
foreach ($this->syncProperties as $p) {
$id = $p->source_id;
2016-02-23 17:47:18 +01:00
if (! array_key_exists($id, $this->sources)) {
$this->sources[$id] = ImportSource::load($id, $this->db);
}
}
2016-02-23 17:35:47 +01:00
return $this;
}
2016-02-23 21:05:09 +01:00
/**
* Prepare the source columns we want to fetch
*
* @return self
*/
protected function prepareSourceColumns()
{
// $fieldMap = array();
2016-02-23 21:05:09 +01:00
$this->sourceColumns = array();
foreach ($this->syncProperties as $p) {
$sourceId = $p->source_id;
2016-02-23 21:05:09 +01:00
if (! array_key_exists($sourceId, $this->sourceColumns)) {
$this->sourceColumns[$sourceId] = array();
}
foreach ($this->extractVariableNames($p->source_expression) as $varname) {
2016-02-23 21:05:09 +01:00
$this->sourceColumns[$sourceId][$varname] = $varname;
// -> ? $fieldMap[
}
}
2016-02-23 21:05:09 +01:00
return $this;
}
2016-02-23 21:05:09 +01:00
/**
* Whether we have a combined key (e.g. services on hosts)
*
* @return bool
*/
protected function hasCombinedKey()
{
if ($this->hasCombinedKey === null) {
$this->hasCombinedKey = false;
if ($this->rule->object_type === 'service') {
$hasHost = false;
$hasObjectName = false;
foreach ($this->syncProperties as $key => $property) {
if ($property->destination_field === 'host') {
$hasHost = $property->source_expression;
}
if ($property->destination_field === 'object_name') {
$hasObjectName = $property->source_expression;
}
}
if ($hasHost !== false && $hasObjectName !== false) {
$this->hasCombinedKey = true;
$this->sourceKeyPattern = sprintf(
'%s!%s',
$hasHost,
$hasObjectName
);
$this->destinationKeyPattern = '${host}!${object_name}';
}
}
}
return $this->hasCombinedKey;
}
2016-02-23 17:35:47 +01:00
/**
* Fetch latest imported data rows from all involved import sources
*
* @return self
*/
protected function fetchImportedData()
{
2016-02-23 17:35:47 +01:00
$this->imported = array();
2016-02-23 17:35:47 +01:00
foreach ($this->sources as $source) {
$sourceId = $source->id;
2016-02-23 21:05:09 +01:00
// Provide an alias column for our key. TODO: double-check this!
$key = $source->key_column;
2016-02-23 21:05:09 +01:00
$this->sourceColumns[$sourceId][$key] = $key;
$run = $source->fetchLastRun(true);
$rows = $run->fetchRows($this->sourceColumns[$sourceId]);
2016-02-23 17:35:47 +01:00
$this->imported[$sourceId] = array();
foreach ($rows as $row) {
if ($this->hasCombinedKey()) {
$key = $this->fillVariables($this->sourceKeyPattern, $row);
2016-02-23 17:35:47 +01:00
if (array_key_exists($key, $this->imported[$sourceId])) {
throw new IcingaException(
'Trying to import row "%s" (%s) twice: %s VS %s',
$key,
$this->sourceKeyPattern,
2016-02-23 17:35:47 +01:00
json_encode($this->imported[$sourceId][$key]),
json_encode($row)
);
}
} else {
if (! property_exists($row, $key)) {
throw new IcingaException(
'There is no key column "%s" in this row from "%s": %s',
$key,
$source->source_name,
json_encode($row)
);
}
}
2016-02-23 21:05:09 +01:00
if (! $this->rule->matches($row)) {
2015-12-04 10:59:25 +01:00
continue;
}
if ($this->hasCombinedKey()) {
2016-02-23 17:35:47 +01:00
$this->imported[$sourceId][$key] = $row;
} else {
2016-02-23 17:35:47 +01:00
$this->imported[$sourceId][$row->$key] = $row;
}
}
unset($rows);
}
2016-02-23 17:35:47 +01:00
return $this;
}
2016-02-19 12:42:02 +01: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;
foreach ($this->syncProperties as $prop) {
2016-02-19 12:42:02 +01:00
if ($prop->destination_field === 'list_id') {
$listId = (int) $prop->source_expression;
}
}
if ($listId === null) {
throw new IcingaException(
'Cannot sync datalist entry without list_ist'
);
}
$no = array();
2016-02-23 17:47:18 +01:00
foreach ($this->objects as $k => $o) {
2016-02-19 12:42:02 +01:00
if ($o->list_id !== $listId) {
$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
}
}
2016-02-23 17:47:18 +01:00
protected function loadExistingObjects()
{
// TODO: Make object_type (template, object...) and object_name mandatory?
if ($this->hasCombinedKey()) {
$this->objects = array();
foreach (IcingaObject::loadAllByType(
$this->rule->object_type,
$this->db
) as $object) {
if ($object instanceof IcingaService) {
2016-02-26 11:58:37 +01:00
if (! $object->host_id) {
continue;
}
}
$key = $this->fillVariables(
$this->destinationKeyPattern,
$object
);
if (array_key_exists($key, $this->objects)) {
throw new IcingaException(
'Combined destination key "%s" is not unique, got "%s" twice',
$this->destinationKeyPattern,
$key
);
}
$this->objects[$key] = $object;
}
} else {
$this->objects = IcingaObject::loadAllByType(
$this->rule->object_type,
$this->db
);
}
2016-02-23 17:47:18 +01:00
// TODO: should be obsoleted by a better "loadFiltered" method
if ($this->rule->object_type === 'datalistEntry') {
$this->removeForeignListEntries($this->objects);
}
return $this;
}
2016-02-23 17:35:47 +01:00
protected function prepareNewObjects()
{
$newObjects = array();
2016-02-23 17:35:47 +01:00
foreach ($this->sources as $source) {
$sourceId = $source->id;
2016-02-23 17:35:47 +01:00
foreach ($this->imported[$sourceId] as $key => $row) {
$newProps = array();
$newVars = array();
$imports = array();
foreach ($this->syncProperties as $propertyKey => $p) {
2016-02-26 11:58:37 +01:00
if ($p->source_id !== $sourceId) {
continue;
}
if (! $this->rowMatchesPropertyFilter($row, $propertyKey)) {
continue;
}
$prop = $p->destination_field;
$val = $this->fillVariables($p->source_expression, $row);
if (substr($prop, 0, 5) === 'vars.') {
2015-08-28 23:56:54 +02:00
$varName = substr($prop, 5);
if (substr($varName, -2) === '[]') {
$varName = substr($varName, 0, -2);
$val = $this->wantArray($val);
}
$newVars[$varName] = $val;
} else {
if ($prop === 'import') {
$imports[] = $val;
} else {
$newProps[$prop] = $val;
}
}
}
if (! array_key_exists($key, $newObjects)) {
$newObjects[$key] = IcingaObject::createByType(
$this->rule->object_type,
array(),
$this->db
2015-12-10 12:57:11 +01:00
);
}
$object = $newObjects[$key];
2015-12-10 12:57:11 +01:00
// Safe default values for object_type and object_name
if ($this->rule->object_type !== 'datalistEntry') {
if (! array_key_exists('object_type', $newProps)
|| $newProps['object_type'] === null
) {
$newProps['object_type'] = 'object';
}
if (! array_key_exists('object_name', $newProps)
|| $newProps['object_name'] === null
) {
$newProps['object_name'] = $key;
}
}
foreach ($newProps as $prop => $value) {
// TODO: data type?
$object->set($prop, $value);
}
foreach ($newVars as $prop => $var) {
$object->vars()->$prop = $var;
}
if (! empty($imports)) {
// TODO: merge imports!!!
$object->imports()->set($imports);
}
}
}
return $newObjects;
}
/**
* Evaluates a SyncRule and returns a list of modified objects
*
* TODO: This needs to be splitted into smaller methods
*
* @return array List of modified IcingaObjects
*/
protected function prepare()
{
2016-02-24 12:24:19 +01:00
if ($this->isPrepared) {
return $this->objects;
}
$this->raiseLimits()
2016-02-24 12:24:19 +01:00
->startMeasurements()
->fetchSyncProperties()
->prepareRelatedImportSources()
2016-02-23 21:05:09 +01:00
->prepareSourceColumns()
->loadExistingObjects()
->fetchImportedData();
// TODO: directly work on existing objects, remember imported keys, then purge
2016-02-23 17:35:47 +01:00
$newObjects = $this->prepareNewObjects();
foreach ($newObjects as $key => $object) {
2016-02-23 17:47:18 +01:00
if (array_key_exists($key, $this->objects)) {
switch ($this->rule->update_policy) {
case 'override':
2016-02-23 17:47:18 +01:00
$this->objects[$key]->replaceWith($object);
2015-10-20 22:22:58 +02:00
break;
case 'merge':
2016-02-23 09:08:14 +01:00
// TODO: re-evaluate merge settings. vars.x instead of
// just "vars" might suffice.
$this->objects[$key]->merge($object);
break;
2015-07-24 15:29:17 +02:00
default:
// policy 'ignore', no action
}
} else {
2016-02-23 17:47:18 +01:00
$this->objects[$key] = $object;
}
}
2016-02-22 11:01:37 +01:00
$noAction = array();
foreach ($this->rule->purgeStrategy()->listObjectsToPurge() as $key) {
2016-02-22 11:01:37 +01:00
if (array_key_exists($key, $newObjects)) {
// Object has been touched, do not delete
continue;
}
if (array_key_exists($key, $this->objects)) {
$object = $this->objects[$key];
if (! $object->hasBeenModified()) {
$object->markForRemoval();
}
}
}
2016-02-22 11:01:37 +01:00
foreach ($this->objects as $key => $object) {
if (! $object->hasBeenModified() && ! $object->shouldBeRemoved()) {
2016-02-22 11:01:37 +01:00
$noAction[] = $key;
}
}
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;
2016-02-23 17:47:18 +01:00
return $this->objects;
}
/**
* Runs a SyncRule and applies all resulting changes
*
* @return int
*/
public function apply()
{
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();
$dba->beginTransaction();
try {
$formerActivityChecksum = Util::hex2binary(
$db->getLastActivityChecksum()
);
$created = 0;
$modified = 0;
$deleted = 0;
foreach ($objects as $object) {
if ($object instanceof IcingaObject && $object->isTemplate()) {
// TODO: allow to sync templates
if ($object->hasBeenModified()) {
throw new IcingaException(
'Sync is not allowed to modify template "%s"',
$object->$objectKey
);
}
continue;
}
2016-02-09 19:21:36 +01:00
if ($object instanceof IcingaObject && $object->shouldBeRemoved()) {
$object->delete($db);
$deleted++;
continue;
}
2015-12-04 10:24:54 +01:00
if ($object->hasBeenModified()) {
if ($object->hasBeenLoadedFromDb()) {
$modified++;
} else {
$created++;
}
$object->store($db);
2016-02-24 12:24:19 +01:00
}
2015-11-02 09:29:03 +01:00
}
2015-10-20 22:22:58 +02:00
$runProperties = array(
'objects_created' => $created,
'objects_deleted' => $deleted,
'objects_modified' => $modified,
);
2016-02-24 12:24:19 +01:00
if ($created + $deleted + $modified > 0) {
// TODO: What if this has been the very first activity?
$runProperties['last_former_activity'] = $db->quoteBinary($formerActivityChecksum);
$runProperties['last_related_activity'] = $db->quoteBinary(Util::hex2binary(
$db->getLastActivityChecksum()
));
}
$this->run->setProperties($runProperties)->store();
2016-02-24 12:24:19 +01:00
$dba->commit();
2016-02-24 12:24:19 +01:00
// Store duration after commit, as the commit might take some time
$this->run->set('duration_ms', (int) round(
(microtime(true) - $this->runStartTime) * 1000
))->store();
2016-02-24 12:24:19 +01:00
} catch (Exception $e) {
$dba->rollBack();
throw $e;
}
2016-02-24 12:24:19 +01:00
return $this->run->id;
}
}