]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add director time endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 30 May 2023 06:26:34 +0000 (08:26 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 1 Jun 2023 11:20:55 +0000 (13:20 +0200)
gui/baculum/protected/API/Modules/Bconsole.php
gui/baculum/protected/API/Modules/TimeManager.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/DirectorTime.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/config.xml
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json
gui/baculum/protected/Common/Modules/Errors/TimeError.php [new file with mode: 0644]

index b067c59dce514dd93c49cd15f04fc516fdd2048a..acfa03ef4f08b12222ae1de553fe8f23cd9c6e54 100644 (file)
@@ -102,7 +102,8 @@ class Bconsole extends APIModule {
                '.jlist',
                '.search',
                '@putfile',
-               'cloud'
+               'cloud',
+               'time'
        );
 
        private $config;
diff --git a/gui/baculum/protected/API/Modules/TimeManager.php b/gui/baculum/protected/API/Modules/TimeManager.php
new file mode 100644 (file)
index 0000000..50885d2
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+namespace Baculum\API\Modules;
+
+/**
+ * Time manager module.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category Module
+ * @package Baculum API
+ */
+class TimeManager extends APIModule {
+
+       /**
+        * Director time format.
+        * Example: '29-May-2023 09:01:34'.
+        */
+       const DIRECTOR_TIME_FORMAT = 'j-M-Y h:i:s';
+
+       /**
+        * Parse time returned by Director time command.
+        * It is in form: '29-May-2023 09:01:34'
+        * @TODO: Make sure that 'May' is always returned in English
+        *
+        * @param string date/time string from the Director
+        * @return array split date and time single values or empty array if error or warning happens
+        */
+       public function parseDirectorTime($time) {
+               list($dow, $date_time) = explode(' ', $time, 2);
+               $tres = date_parse_from_format(self::DIRECTOR_TIME_FORMAT, $date_time);
+               $ret = [];
+               if ($tres['warning_count'] === 0 && $tres['error_count'] === 0) {
+                       $ret = [
+                               'year' => $tres['year'],
+                               'month' => $tres['month'],
+                               'day' => $tres['day'],
+                               'hour' => $tres['hour'],
+                               'minute' => $tres['minute'],
+                               'second' => $tres['second']
+                       ];
+               }
+               return $ret;
+       }
+}
diff --git a/gui/baculum/protected/API/Pages/API/DirectorTime.php b/gui/baculum/protected/API/Pages/API/DirectorTime.php
new file mode 100644 (file)
index 0000000..5d11935
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+use Baculum\Common\Modules\Errors\BconsoleError;
+use Baculum\Common\Modules\Errors\TimeError;
+
+/**
+ * Director time.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class DirectorTime extends BaculumAPIServer {
+
+       public function get() {
+               $misc = $this->getModule('misc');
+               $director = $this->Request->contains('name') && $misc->isValidName($this->Request['name']) ? $this->Request['name'] : null;
+
+               $dirs = [];
+               $result = $this->getModule('bconsole')->getDirectors();
+               if ($result->exitcode === 0) {
+                       $dirs = $result->output;
+               }
+
+               if (is_null($director) || !in_array($director, $dirs)) {
+                       // Invalid director
+                       $this->output = BconsoleError::MSG_ERROR_INVALID_DIRECTOR;
+                       $this->error = BconsoleError::ERROR_INVALID_DIRECTOR;
+                       return;
+               }
+
+               $result = $this->getModule('bconsole')->bconsoleCommand(
+                       $director,
+                       ['time'],
+                       null,
+                       true
+               );
+               if ($result->exitcode === 0) {
+                       $result->output = array_filter($result->output);
+                       if (count($result->output) == 1) {
+                               $this->output = $this->getModule('time')->parseDirectorTime($result->output[0]);
+                               if (count($this->output) > 0) {
+                                       $this->error = TimeError::ERROR_NO_ERRORS;
+                               } else {
+                                       $this->output = TimeError::MSG_ERROR_INVALID_DATE_TIME . ' Output=>' . $result->output[0];
+                                       $this->error = TimeError::ERROR_INVALID_DATE_TIME;
+                               }
+                       } else {
+                               $this->output = TimeError::MSG_ERROR_INVALID_DATE_TIME . ' Output=>' . implode('', $result->output);
+                               $this->error = TimeError::ERROR_INVALID_DATE_TIME;
+                       }
+               } else {
+                       $this->output = TimeError::MSG_ERROR_WRONG_EXITCODE . ' Exitcode=' . $result->exitcode;
+                       $this->error = TimeError::ERROR_WRONG_EXITCODE;
+               }
+       }
+}
index ffdfe6c3c85c763f5a2b0f317827c5a23f4c52c7..a7865a57ca6c992961ece38aceeb7ef8b755a207 100644 (file)
@@ -55,6 +55,7 @@
                <!-- bconsole command modules -->
                <module id="ls" class="Baculum\API\Modules\Ls" />
                <module id="list" class="Baculum\API\Modules\BList" />
+               <module id="time" class="Baculum\API\Modules\TimeManager" />
                <!-- changer command modules -->
                <module id="changer_command" class="Baculum\API\Modules\ChangerCommand" />
                <!-- plugin modules -->
index 841fecf70510af5d1c8ec66ee4484cc367c04432..e923977d06835fb04197de9acc84f079829cc84b 100644 (file)
        <!-- director endpoints -->
        <url ServiceParameter="DirectorShow" pattern="api/v2/directors/{name}/show/" parameters.name="[a-zA-Z0-9:.\-_ ]+" />
        <url ServiceParameter="DirectorStatus" pattern="api/v2/directors/{name}/status/" parameters.name="[a-zA-Z0-9:.\-_ ]+" />
+       <url ServiceParameter="DirectorTime" pattern="api/v2/directors/{name}/time/" parameters.name="[a-zA-Z0-9:.\-_ ]+" />
        <!-- actions endpoints -->
        <url ServiceParameter="Actions" pattern="api/v2/actions/{component}/{action}/" parameters.component="(director|storage|client)" parameters.action="(start|stop|restart)" />
        <!-- OAuth2 client endpoints -->
index e0143e675245f4c7c5edef6857ff5eea339c65c8..44ece156585a11361cfb66f2d654bc45c442b6e4 100644 (file)
                                ]
                        }
                },
+               "/api/v2/directors/{director_name}/time": {
+                       "get": {
+                               "tags": ["directors"],
+                               "summary": "Get director system time",
+                               "description": "Get director system time",
+                               "responses": {
+                                       "200": {
+                                               "description": "Director system time",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "object",
+                                                                                       "properties": {
+                                                                                               "year": {
+                                                                                                       "description": "Four digit year",
+                                                                                                       "type": "integer"
+                                                                                               },
+                                                                                               "month": {
+                                                                                                       "description": "Month (1-12)",
+                                                                                                       "type": "integer"
+                                                                                               },
+                                                                                               "day": {
+                                                                                                       "description": "Day (1-31)",
+                                                                                                       "type": "integer"
+                                                                                               },
+                                                                                               "hour": {
+                                                                                                       "description": "Hour (0-23)",
+                                                                                                       "type": "integer"
+                                                                                               },
+                                                                                               "minute": {
+                                                                                                       "description": "Minute (0-59)",
+                                                                                                       "type": "integer"
+                                                                                               },
+                                                                                               "second": {
+                                                                                                       "description": "Second (0-59)",
+                                                                                                       "type": "integer"
+                                                                                               }
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 530, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "director_name",
+                                               "in": "path",
+                                               "description": "Director name",
+                                               "required": true,
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       }
+               },
                "/api/v2/oauth2/clients": {
                        "get": {
                                "tags": ["oauth2"],
diff --git a/gui/baculum/protected/Common/Modules/Errors/TimeError.php b/gui/baculum/protected/Common/Modules/Errors/TimeError.php
new file mode 100644 (file)
index 0000000..9dbf706
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+namespace Baculum\Common\Modules\Errors;
+
+/**
+ * Time error class.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category Errors
+ * @package Baculum Common
+ */
+class TimeError extends GenericError {
+       const ERROR_INVALID_DATE_TIME = 530;
+
+       const MSG_ERROR_INVALID_DATE_TIME = 'Invalid date/time value.';
+}