3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2015 Marcin Haba
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('System.Web.UI.ActiveControls.TActiveHiddenField');
24 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
25 Prado::using('Application.Portlets.Portlets');
27 class JobConfiguration extends Portlets {
29 const DEFAULT_JOB_PRIORITY = 10;
31 const RESOURCE_SHOW_PATTERN = '/^\s+--> %resource: name=(.+?(?=\s\S+\=.+)|.+$)/i';
32 const JOB_SHOW_PATTERN = '/^Job:\sname=(?P<jobname>.+)\sJobType=\d+\slevel=(?P<level>\w+)\sPriority=(?P<priority>\d+)/i';
34 public $jobToVerify = array('C', 'O', 'd');
36 public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
38 public function configure($jobId, $params = array()) {
39 $jobdata = $this->Application->getModule('api')->get(array('jobs', $jobId))->output;
40 $this->JobName->Text = $jobdata->job;
41 $this->JobID->Text = $jobdata->jobid;
42 $joblog = $this->Application->getModule('api')->get(array('joblog', $jobdata->jobid))->output;
43 $runningJobStates = $this->Application->getModule('misc')->getRunningJobStates();
44 if (in_array($jobdata->jobstatus, $runningJobStates)) {
45 $this->Estimation->CssClass = 'textbox-auto wheel-loader';
47 $this->Estimation->CssClass = 'textbox-auto';
49 $this->Estimation->Text = is_array($joblog) ? implode(PHP_EOL, $joblog) : Prado::localize("Output for selected job is not available yet or you do not have enabled logging job logs to catalog database. For watching job log there is need to add to the job Messages resource next directive: console = all, !skipped, !saved");
51 $this->Level->dataSource = $this->Application->getModule('misc')->getJobLevels();
52 $this->Level->SelectedValue = $jobdata->level;
53 $this->Level->dataBind();
55 $isVerifyOption = in_array($jobdata->level, $this->jobToVerify);
56 $this->JobToVerifyOptionsLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
57 $this->JobToVerifyJobNameLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
58 $this->JobToVerifyJobIdLine->Display = 'None';
59 $this->AccurateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
60 $this->EstimateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
62 $verifyValues = array();
64 foreach($this->verifyOptions as $value => $text) {
65 $verifyValues[$value] = Prado::localize($text);
68 $this->JobToVerifyOptions->dataSource = $verifyValues;
69 $this->JobToVerifyOptions->dataBind();
71 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output;
73 $jobsAllDirs = array();
74 foreach($jobTasks as $director => $tasks) {
75 $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
78 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
79 $this->JobToVerifyJobName->dataBind();
81 $clients = $this->Application->getModule('api')->get(array('clients'), true)->output;
82 $clientsList = array();
83 foreach($clients as $client) {
84 $clientsList[$client->clientid] = $client->name;
86 $this->Client->dataSource = $clientsList;
87 $this->Client->SelectedValue = $jobdata->clientid;
88 $this->Client->dataBind();
90 $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), true)->output;
91 $filesetsList = array();
92 foreach($filesetsAll as $director => $filesets) {
93 $filesetsList = array_merge($filesets, $filesetsList);
95 $selectedFileset = '';
96 if($jobdata->filesetid != 0) {
97 $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid), true)->output->fileset;
99 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
100 $this->FileSet->SelectedValue = array_key_exists('fileset', $params) ? $params['fileset'] : $selectedFileset;
101 $this->FileSet->dataBind();
103 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
105 foreach($pools as $pool) {
106 $poolList[$pool->poolid] = $pool->name;
108 $this->Pool->dataSource = $poolList;
109 $this->Pool->SelectedValue = array_key_exists('poolid', $params) ? $params['poolid'] : $jobdata->poolid;
110 $this->Pool->dataBind();
112 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid), true)->output;
113 $storageShow = $this->getResourceName('storage', $jobshow);
114 $storagesList = array();
115 $selectedStorageId = null;
116 $storages = $this->Application->getModule('api')->get(array('storages'), true)->output;
117 foreach($storages as $storage) {
118 if ($storage->name == $storageShow) {
119 $selectedStorageId = $storage->storageid;
121 $storagesList[$storage->storageid] = $storage->name;
123 $this->Storage->dataSource = $storagesList;
124 if (!is_null($selectedStorageId)) {
125 $this->Storage->SelectedValue = $selectedStorageId;
127 $this->Storage->dataBind();
129 $runningJobStates = $this->Application->getModule('misc')->getRunningJobStates();
130 $isJobRunning = in_array($jobdata->jobstatus, $runningJobStates);
132 $this->Priority->Text = ($jobdata->priorjobid == 0) ? self::DEFAULT_JOB_PRIORITY : $jobdata->priorjobid;
133 $this->DeleteButton->Visible = true;
134 $this->CancelButton->Visible = $isJobRunning;
135 $this->RefreshStart->Value = $isJobRunning;
136 $this->Run->Display = 'Dynamic';
137 $this->EstimateLine->Display = 'Dynamic';
138 $this->Status->Visible = true;
141 public function status($sender, $param) {
142 $jobdata = $this->Application->getModule('api')->get(array('jobs', $this->JobID->Text))->output;
143 $runningJobStates = $this->Application->getModule('misc')->getRunningJobStates();
144 if (in_array($jobdata->jobstatus, $runningJobStates)) {
145 $this->RefreshStart->Value = true;
147 $this->RefreshStart->Value = false;
148 $this->CancelButton->Visible = false;
149 $this->Estimation->CssClass = 'textbox-auto';
152 $joblog = $this->Application->getModule('api')->get(array('joblog', $this->JobID->Text))->output;
153 $this->Estimation->Text = is_array($joblog) ? implode(PHP_EOL, $joblog) : Prado::localize("Output for selected job is not available yet or you do not have enabled logging job logs to catalog database. For watching job log there is need to add to the job Messages resource next directive: console = all, !skipped, !saved");
156 public function delete($sender, $param) {
157 $this->Application->getModule('api')->remove(array('jobs', $this->JobID->Text));
158 $this->Status->Visible = false;
159 $this->Run->Display = 'None';
160 $this->DeleteButton->Visible = false;
161 $this->EstimateLine->Display = 'None';
162 $this->Estimation->CssClass = 'textbox-auto';
165 public function cancel($sender, $param) {
166 $this->Application->getModule('api')->set(array('jobs', 'cancel', $this->JobID->Text), array('a' => 'b'));
167 $this->CancelButton->Visible = false;
168 $this->status(null, null);
169 $this->Estimation->CssClass = 'textbox-auto';
172 public function run_again($sender, $param) {
173 if($this->PriorityValidator->IsValid === false) {
178 if (is_null($sender) && is_numeric($param)) {
179 $jobdata = $this->Application->getModule('api')->get(array('jobs', $param))->output;
180 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid))->output;
181 $params['id'] = $jobdata->jobid;
182 $params['level'] = $jobdata->level;
183 $params['fileset'] = $this->getResourceName('fileset', $jobshow);
184 $params['clientid'] = $jobdata->clientid;
185 $storage = $this->getResourceName('storage', $jobshow);
186 if (!is_null($storage)) {
187 $params['storageid'] = $this->getStorageByName($storage)->storageid;
189 $pool = $this->getResourceName('pool', $jobshow);
190 if (!is_null($pool)) {
191 $params['poolid'] = $this->getPoolByName($pool)->poolid;
194 $params['id'] = $this->JobID->Text;
195 $params['level'] = $this->Level->SelectedValue;
196 $params['fileset'] = $this->FileSet->SelectedValue;
197 $params['clientid'] = $this->Client->SelectedValue;
198 $params['storageid'] = $this->Storage->SelectedValue;
199 $params['poolid'] = $this->Pool->SelectedValue;
200 $params['priority'] = $this->Priority->Text;
202 if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
203 $verifyVals = $this->getVerifyVals();
204 if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
205 $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
206 } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
207 $params['jobid'] = $this->JobToVerifyJobId->Text;
211 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
212 $startedJobId = $this->Application->getModule('misc')->findJobIdStartedJob($result);
213 if (is_numeric($startedJobId)) {
214 $params['jobid'] = $startedJobId;
215 $this->configure($startedJobId, $params);
217 $this->Estimation->Text = implode(PHP_EOL, $result);
221 public function estimate($sender, $param) {
223 $params['id'] = $this->JobID->Text;
224 $params['level'] = $this->Level->SelectedValue;
225 $params['fileset'] = $this->FileSet->SelectedValue;
226 $params['clientid'] = $this->Client->SelectedValue;
227 $params['accurate'] = (integer)$this->Accurate->Checked;
228 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
229 $this->Estimation->Text = implode(PHP_EOL, $result);
232 public function priorityValidator($sender, $param) {
233 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
234 $param->setIsValid($isValid);
237 public function jobIdToVerifyValidator($sender, $param) {
238 $verifyVals = $this->getVerifyVals();
239 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
240 $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
244 $param->setIsValid($isValid);
248 private function getVerifyVals() {
249 $verifyOpt = array_keys($this->verifyOptions);
250 $verifyVals = array_combine($verifyOpt, $verifyOpt);
254 public function getResourceName($resource, $jobshow) {
255 $resource_name = null;
256 $pattern = str_replace('%resource', $resource, self::RESOURCE_SHOW_PATTERN);
257 for ($i = 0; $i < count($jobshow); $i++) {
258 if (preg_match($pattern, $jobshow[$i], $match) === 1) {
259 $resource_name = $match[1];
263 return $resource_name;
266 public function getJobAttr($jobshow) {
268 for ($i = 0; $i < count($jobshow); $i++) {
269 if (preg_match(self::JOB_SHOW_PATTERN, $jobshow[$i], $match) === 1) {
270 $attr['jobname'] = $match['jobname'];
271 $attr['level'] = $match['level'];
272 $attr['priority'] = $match['priority'];
279 private function getPoolByName($name) {
281 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
282 for ($i = 0; $i < count($pools); $i++) {
283 if ($pools[$i]->name == $name) {
291 private function getStorageByName($name) {
293 $storages = $this->Application->getModule('api')->get(array('storages'), true)->output;
294 for ($i = 0; $i < count($storages); $i++) {
295 if ($storages[$i]->name == $name) {
296 $storage = $storages[$i];