From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Cli/Command.php | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 library/Director/Cli/Command.php (limited to 'library/Director/Cli/Command.php') diff --git a/library/Director/Cli/Command.php b/library/Director/Cli/Command.php new file mode 100644 index 0000000..69d61b1 --- /dev/null +++ b/library/Director/Cli/Command.php @@ -0,0 +1,115 @@ +fail('Invalid JSON: %s', $e->getMessage()); + } + } + + /** + * @param string $msg + * @return never-return + */ + public function fail($msg) + { + $args = func_get_args(); + array_shift($args); + if (count($args)) { + $msg = vsprintf($msg, $args); + } + echo $this->screen->colorize("ERROR", 'red') . ": $msg\n"; + exit(1); + } + + /** + * @param null $endpointName + * @return CoreApi|\Icinga\Module\Director\Core\LegacyDeploymentApi + * @throws \Icinga\Exception\NotFoundError + */ + protected function api($endpointName = null) + { + if ($this->api === null) { + if ($endpointName === null) { + $endpoint = $this->db()->getDeploymentEndpoint(); + } else { + $endpoint = IcingaEndpoint::load($endpointName, $this->db()); + } + + $this->api = $endpoint->api(); + } + + return $this->api; + } + + /** + * Raise PHP resource limits + * + * @return self; + */ + protected function raiseLimits() + { + MemoryLimit::raiseTo('1024M'); + + ini_set('max_execution_time', 0); + if (version_compare(PHP_VERSION, '7.0.0') < 0) { + ini_set('zend.enable_gc', 0); + } + + return $this; + } + + /** + * @return Db + */ + protected function db() + { + if ($this->db === null) { + $resourceName = $this->params->get('dbResourceName'); + + if ($resourceName === null) { + // Hint: not using $this->Config() intentionally. This allows + // CLI commands in other modules to use this as a base class. + $resourceName = Config::module('director')->get('db', 'resource'); + } + if ($resourceName) { + $this->db = Db::fromResourceName($resourceName); + } else { + throw new RuntimeException('Director is not configured correctly'); + } + } + + return $this->db; + } +} -- cgit v1.2.3