]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Portlets/JobConfiguration.php
Update ReleaseNotes + ChangeLog
[bacula/bacula] / gui / baculum / protected / Portlets / JobConfiguration.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
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.
11  *
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.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('System.Web.UI.ActiveControls.TActiveHiddenField');
24 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
25 Prado::using('Application.Portlets.Portlets');
26
27 class JobConfiguration extends Portlets {
28
29         const DEFAULT_JOB_PRIORITY = 10;
30
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';
33
34         public $jobToVerify = array('C', 'O', 'd');
35
36         public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
37
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';
46                 } else {
47                         $this->Estimation->CssClass = 'textbox-auto';
48                 }
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");
50
51                 $this->Level->dataSource = $this->Application->getModule('misc')->getJobLevels();
52                 $this->Level->SelectedValue = $jobdata->level;
53                 $this->Level->dataBind();
54
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';
61
62                 $verifyValues = array();
63
64                 foreach($this->verifyOptions as $value => $text) {
65                         $verifyValues[$value] = Prado::localize($text);
66                 }
67
68                 $this->JobToVerifyOptions->dataSource = $verifyValues;
69                 $this->JobToVerifyOptions->dataBind();
70
71                 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output;
72
73                 $jobsAllDirs = array();
74                 foreach($jobTasks as $director => $tasks) {
75                         $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
76                 }
77
78                 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
79                 $this->JobToVerifyJobName->dataBind();
80
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;
85                 }
86                 $this->Client->dataSource = $clientsList;
87                 $this->Client->SelectedValue = $jobdata->clientid;
88                 $this->Client->dataBind();
89
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);
94                 }
95                 $selectedFileset = '';
96                 if($jobdata->filesetid != 0) {
97                         $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid), true)->output->fileset;
98                 }
99                 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
100                 $this->FileSet->SelectedValue = array_key_exists('fileset', $params) ? $params['fileset'] : $selectedFileset;
101                 $this->FileSet->dataBind();
102
103                 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
104                 $poolList = array();
105                 foreach($pools as $pool) {
106                         $poolList[$pool->poolid] = $pool->name;
107                 }
108                 $this->Pool->dataSource = $poolList;
109                 $this->Pool->SelectedValue = array_key_exists('poolid', $params) ? $params['poolid'] : $jobdata->poolid;
110                 $this->Pool->dataBind();
111
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;
120                         }
121                         $storagesList[$storage->storageid] = $storage->name;
122                 }
123                 $this->Storage->dataSource = $storagesList;
124                 if (!is_null($selectedStorageId)) {
125                         $this->Storage->SelectedValue = $selectedStorageId;
126                 }
127                 $this->Storage->dataBind();
128
129                 $runningJobStates = $this->Application->getModule('misc')->getRunningJobStates();
130                 $isJobRunning = in_array($jobdata->jobstatus, $runningJobStates);
131
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;
139         }
140
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;
146                 } else {
147                         $this->RefreshStart->Value = false;
148                         $this->CancelButton->Visible = false;
149                         $this->Estimation->CssClass = 'textbox-auto';
150                 }
151
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");
154         }
155
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';
163         }
164
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';
170         }
171
172         public function run_again($sender, $param) {
173                 if($this->PriorityValidator->IsValid === false) {
174                         return false;
175                 }
176
177                 $params = array();
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;
188                         }
189                         $pool = $this->getResourceName('pool', $jobshow);
190                         if (!is_null($pool)) {
191                                 $params['poolid'] = $this->getPoolByName($pool)->poolid;
192                         }
193                 } else {
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;
201
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;
208                                 }
209                         }
210                 }
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);
216                 } else {
217                         $this->Estimation->Text = implode(PHP_EOL, $result);
218                 }
219         }
220
221         public function estimate($sender, $param) {
222                 $params = array();
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);
230         }
231
232         public function priorityValidator($sender, $param) {
233                 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
234                 $param->setIsValid($isValid);
235         }
236
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;
241                 } else {
242                         $isValid = true;
243                 }
244                 $param->setIsValid($isValid);
245                 return $isValid;
246         }
247
248         private function getVerifyVals() {
249                 $verifyOpt = array_keys($this->verifyOptions);
250                 $verifyVals = array_combine($verifyOpt, $verifyOpt);
251                 return $verifyVals;
252         }
253
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];
260                                 break;
261                         }
262                 }
263                 return $resource_name;
264         }
265
266         public function getJobAttr($jobshow) {
267                 $attr = array();
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'];
273                                 break;
274                         }
275                 }
276                 return $attr;
277         }
278
279         private function getPoolByName($name) {
280                 $pool = null;
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) {
284                                 $pool = $pools[$i];
285                                 break;
286                         }
287                 }
288                 return $pool;
289         }
290
291         private function getStorageByName($name) {
292                 $storage = null;
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];
297                                 break;
298                         }
299                 }
300                 return $storage;
301         }
302 }
303 ?>