From 545d068d16823b0ee47d8d486f17100af071202f Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Sun, 19 Nov 2017 15:00:33 +0100 Subject: [PATCH] baculum: Add module to check resource dependencies --- .../protected/Web/Class/DataDependencies.php | 156 ++++++++++++++++++ gui/baculum/protected/Web/Data/data_deps.json | 65 ++++++++ 2 files changed, 221 insertions(+) create mode 100644 gui/baculum/protected/Web/Class/DataDependencies.php create mode 100644 gui/baculum/protected/Web/Data/data_deps.json diff --git a/gui/baculum/protected/Web/Class/DataDependencies.php b/gui/baculum/protected/Web/Class/DataDependencies.php new file mode 100644 index 0000000000..4f59f50bb0 --- /dev/null +++ b/gui/baculum/protected/Web/Class/DataDependencies.php @@ -0,0 +1,156 @@ +data_deps = $this->loadDataDependencies(); + } + + /** + * Load data dependencies file. + * + * @return mixed object with data dependencies or null if problem with reading data dependencies file + */ + private function loadDataDependencies() { + $data_deps = null; + $deps_file = Prado::getPathOfNamespace(self::DATA_DEPS_FILE, '.json'); + if (file_exists($deps_file) && is_readable($deps_file)) { + $deps_file = file_get_contents($deps_file); + $data_deps = json_decode($deps_file); + } else { + $emsg = "Data dependencies file '$deps_file' does not exist or is not readable."; + $this->Application->getModule('logging')->log( + __FUNCTION__, + $emsg, + Logging::CATEGORY_APPLICATION, + __FILE__, + __LINE__ + ); + } + return $data_deps; + } + + /** + * Get single resource dependencies. + * + * @param string $component_type component type: 'dir', 'sd, 'fd' or 'bcons'. + * @param string $resource_type resource type: Job, Client, Pool...etc. + * @return mixed resource dependencies object or null if resource dependencies don't exist + */ + private function getDependencies($component_type, $resource_type) { + $deps = null; + if (isset($this->data_deps->{$component_type}->{$resource_type})) { + $deps = (array)$this->data_deps->{$component_type}->{$resource_type}; + } + return $deps; + } + + /** + * Check if resource is used in other resources. + * Method to internal use. + * + * @param object $resource resource to check + * @param string $resource_type resource type to check + * @param string $resource_name resource name to check + * @param string $deps_directive dependent directive name in this resource (from data_deps.json) + * @return bool true if resource is used in other resources, othwerise false + */ + private function isResourceDependent($resource, $resource_type, $resource_name, $deps_directive) { + $dependent = false; + if ($resource_type === 'Schedule' && property_exists($resource, 'Run')) { + for ($i = 0; $i < count($resource->Run); $i++) { + if (property_exists($resource->Run[$i], $deps_directive) && $resource->Run[$i]->{$deps_directive} === $resource_name) { + $dependent = true; + break; + } + } + } else { + if (property_exists($resource, $deps_directive)) { + if (is_array($resource->{$deps_directive})) { + for ($i = 0; $i < count($resource->{$deps_directive}); $i++) { + if ($resource->{$deps_directive}[$i] === $resource_name) { + $dependent = true; + break; + } + + } + } elseif ($resource->{$deps_directive} === $resource_name) { + $dependent = true; + } + } + } + return $dependent; + } + + /** + * Check if resource is dependent on other resources. + * Get detailed list wit all resources where examined resource is used. + * + * @param string $component_type component type: 'dir', 'sd, 'fd' or 'bcons'. + * @param string $resource_type resource type: Job, Client, Pool...etc. + * @param string $resource_name resource name + * @param array $config all component configuration to check + * @return array array with all resources in which current resource is used or empty array if resource doesn't depend from other resources + */ + public function checkDependencies($component_type, $resource_type, $resource_name, $config) { + $result = array(); + $deps = $this->getDependencies($component_type, $resource_type); + if (is_array($deps)) { + foreach ($deps as $rdtype => $directives) { + for ($i = 0; $i < count($config); $i++) { + foreach ($config[$i] as $rctype => $resource) { + if ($rdtype !== $rctype) { + continue; + } + for ($j = 0; $j < count($directives); $j++) { + if (!$this->isResourceDependent($resource, $rdtype, $resource_name, $directives[$j])) { + continue; + } + $result[] = array( + 'component_type' => $component_type, + 'resource_type' => $rctype, + 'resource_name' => $resource->Name, + 'directive_name' => $directives[$j] + ); + } + } + } + } + } + return $result; + } +} +?> diff --git a/gui/baculum/protected/Web/Data/data_deps.json b/gui/baculum/protected/Web/Data/data_deps.json new file mode 100644 index 0000000000..0ffa94255c --- /dev/null +++ b/gui/baculum/protected/Web/Data/data_deps.json @@ -0,0 +1,65 @@ +{ + "dir": { + "Catalog": { + "Client": ["Catalog"], + "Counter": ["Catalog"], + "Messages": ["Catalog"], + "Pool": ["Catalog"] + }, + "Client": { + "Job": ["Client"], + "JobDefs": ["Client"] + }, + "Fileset": { + "Job": ["Fileset"], + "JobDefs": ["Fileset"] + }, + "Job": { + "Job": ["Base", "JobToVerify", "VerifyJob"], + "JobBase": ["Base", "JobToVerify", "VerifyJob"] + }, + "JobDefs": { + "Job": ["JobDefs"] + }, + "Messages": { + "Director": ["Messages"], + "Job": ["Messages"], + "JobDefs": ["Messages"] + }, + "Pool": { + "Job": ["DifferentialBackupPool", "FullBackupPool", "IncrementalBackupPool", "NextPool", "Pool"], + "JobDefs": ["DifferentialBackupPool", "FullBackupPool", "IncrementalBackupPool", "NextPool", "Pool"], + "Pool": ["CopyPool", "NextPool", "RecyclePool", "ScratchPool"], + "Schedule": ["DifferentialPool", "FullPool", "IncrementalPool", "NextPool", "Pool"] + }, + "Storage": { + "Job": ["Storage"], + "JobDefs": ["Storage"], + "Pool": ["Storage"], + "Schedule": ["Storage"], + "Storage": ["Autochanger"] + }, + "Schedule": { + "Job": ["Schedule"], + "JobDefs": ["Schedule"] + } + }, + "sd": { + "Device": { + "Autochanger": ["Device"] + }, + "Messages": { + "Storage": ["Messages"] + } + }, + "fd": { + "Messages": { + "FileDaemon": ["Messages"] + } + }, + "bcons": { + "Console": { + "Director": ["Console"] + } + } +} -- 2.39.5