From: Marcin Haba Date: Mon, 27 Feb 2023 15:22:02 +0000 (+0100) Subject: baculum: Add output parameter to run job endpoint X-Git-Tag: Release-13.0.3~127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=948a96336d1472fc1ed96a8b359ad9dc815a1576;p=thirdparty%2Fbacula.git baculum: Add output parameter to run job endpoint Other changes: - provide new queued jobid in separate property for output=json - return appropriate error code if new queued jobid is absent --- diff --git a/gui/baculum/protected/API/Pages/API/JobRun.php b/gui/baculum/protected/API/Pages/API/JobRun.php index e1a414e7a..77a8f5389 100644 --- a/gui/baculum/protected/API/Pages/API/JobRun.php +++ b/gui/baculum/protected/API/Pages/API/JobRun.php @@ -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 + ]; } } diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index b4159e06f..2fe931dc8 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -2155,6 +2155,9 @@ } }, "parameters": [ + { + "$ref": "#/components/parameters/Output" + }, { "name": "id", "in": "body", diff --git a/gui/baculum/protected/Common/Modules/Miscellaneous.php b/gui/baculum/protected/Common/Modules/Miscellaneous.php index c4403c8d3..a2698e6a8 100644 --- a/gui/baculum/protected/Common/Modules/Miscellaneous.php +++ b/gui/baculum/protected/Common/Modules/Miscellaneous.php @@ -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\d+)$/', $output[$i], $match) === 1) { + if (preg_match('/^Job queued\.\sJobId=(?P\d+)$/i', $output[$i], $match) === 1) { $jobid = $match['jobid']; break; }