]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add output parameter to run job endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 27 Feb 2023 15:22:02 +0000 (16:22 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sun, 5 Mar 2023 06:06:30 +0000 (07:06 +0100)
Other changes:
 - provide new queued jobid in separate property for output=json
 - return appropriate error code if new queued jobid is absent

gui/baculum/protected/API/Pages/API/JobRun.php
gui/baculum/protected/API/openapi_baculum.json
gui/baculum/protected/Common/Modules/Miscellaneous.php

index e1a414e7a42abb860d022692b7cd24ecee28e1a2..77a8f5389a38cba39836e7e2566b1b60dca41bdb 100644 (file)
@@ -20,7 +20,7 @@
  * Bacula(R) is a registered trademark of Kern Sibbald.
  */
 
-use Baculum\API\Modules\BaculumAPIServer;
+use Baculum\API\Modules\ConsoleOutputPage;
 use Baculum\Common\Modules\Errors\JobError;
 
 /**
@@ -30,11 +30,15 @@ use Baculum\Common\Modules\Errors\JobError;
  * @category API
  * @package Baculum API
  */
-class JobRun extends BaculumAPIServer {
+class JobRun extends ConsoleOutputPage {
 
        public function create($params) {
                $misc = $this->getModule('misc');
                $bconsole = $this->getModule('bconsole');
+               $out_format = parent::OUTPUT_FORMAT_RAW; // default output format
+               if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) {
+                       $out_format = $this->Request['output'];
+               }
                $job = null;
                if (property_exists($params, 'id')) {
                        $jobid = intval($params->id);
@@ -157,7 +161,7 @@ class JobRun extends BaculumAPIServer {
                }
 
                $joblevels  = $misc->getJobLevels();
-               $command = array(
+               $command = [
                        'run',
                        'job="' . $job . '"',
                        'level="' . $joblevels[$level] . '"',
@@ -167,7 +171,7 @@ class JobRun extends BaculumAPIServer {
                        'pool="' . $pool . '"' ,
                        'priority="' . $priority . '"',
                        'accurate="' . $accurate . '"'
-               );
+               ];
                if (is_int($jobid)) {
                        $command[] = 'jobid="' . $jobid . '"';
                }
@@ -179,8 +183,63 @@ class JobRun extends BaculumAPIServer {
                }
                $command[] = 'yes';
                $run = $bconsole->bconsoleCommand($this->director, $command);
-               $this->output = $run->output;
-               $this->error = $run->exitcode;
+
+               if ($run->exitcode == 0) {
+                       // exit code OK, check output
+                       $queued_jobid = $this->getModule('misc')->findJobIdStartedJob($run->output);
+                       if (is_null($queued_jobid)) {
+                               // new jobid is not detected, error
+                               $this->error = JobError::ERROR_INVALID_PROPERTY;
+                               $this->output = JobError::MSG_ERROR_INVALID_PROPERTY;
+                       } else {
+                               // new jobid is detected, check output format
+                               if ($out_format == parent::OUTPUT_FORMAT_JSON) {
+                                       // JSON format, return output and new jobid
+                                       $this->output = $this->getJSONOutput([
+                                               'output' => $run->output,
+                                               'queued_jobid' => $queued_jobid
+                                       ]);
+                               } elseif ($out_format == parent::OUTPUT_FORMAT_RAW) {
+                                       // RAW format, return output
+                                       $this->output = $this->getRawOutput([
+                                               'output' => $run->output
+                                       ]);
+                               }
+                               $this->error = JobError::ERROR_NO_ERRORS;
+                       }
+               } else {
+                       // exit code WRONG
+                       $this->output = $run->output;
+                       $this->error = JobError::ERROR_WRONG_EXITCODE . ' Exitcode => ' . $run->exitcode;
+               }
+       }
+
+       /**
+        * Get raw output from run job command.
+        * This method will not be called without param.
+        *
+        * @param array output parameter
+        * @return array run job command output
+        */
+       protected function getRawOutput($params = []) {
+               // Not too much to do here. Required by abstract method.
+               return $params['output'];
+       }
+
+       /**
+        * Get parsed JSON output from run job command.
+        * This method will not be called without params.
+        *
+        * @param array output parameter and queued jobid
+        * @return array output and jobid queued job
+        */
+       protected function getJSONOutput($params = []) {
+               $output = implode(PHP_EOL, $params['output']);
+               $jobid = (int) $params['queued_jobid'];
+               return [
+                       'output' => $output,
+                       'jobid' => $jobid
+               ];
        }
 }
 
index b4159e06fac3d567b71def2329dcd1a295316e33..2fe931dc8905c2eefcb270c3a049e58e65ca43dc 100644 (file)
                                        }
                                },
                                "parameters": [
+                                       {
+                                               "$ref": "#/components/parameters/Output"
+                                       },
                                        {
                                                "name": "id",
                                                "in": "body",
index c4403c8d345dc0f4af9c351f8d9628dc1a8ac5d5..a2698e6a86f3429fd2d90d9997b0d2128096af67 100644 (file)
@@ -328,7 +328,7 @@ class Miscellaneous extends TModule {
                $jobid = null;
                $output = array_reverse($output); // jobid is ussually at the end of output
                for ($i = 0; $i < count($output); $i++) {
-                       if (preg_match('/^Job queued\.\sJobId=(?P<jobid>\d+)$/', $output[$i], $match) === 1) {
+                       if (preg_match('/^Job queued\.\sJobId=(?P<jobid>\d+)$/i', $output[$i], $match) === 1) {
                                $jobid = $match['jobid'];
                                break;
                        }