]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Portlets/JobConfiguration.php
baculum: Switch to started job status just after job start
[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-2015 Marcin Haba
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                         $params['storageid'] = $this->getStorageByName($storage)->storageid;
187                         $pool = $this->getResourceName('pool', $jobshow);
188                         $params['poolid'] = $this->getPoolByName($pool)->poolid;
189                 } else {
190                         $params['id'] = $this->JobID->Text;
191                         $params['level'] = $this->Level->SelectedValue;
192                         $params['fileset'] = $this->FileSet->SelectedValue;
193                         $params['clientid'] = $this->Client->SelectedValue;
194                         $params['storageid'] = $this->Storage->SelectedValue;
195                         $params['poolid'] = $this->Pool->SelectedValue;
196                         $params['priority'] = $this->Priority->Text;
197
198                         if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
199                                 $verifyVals = $this->getVerifyVals();
200                                 if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
201                                         $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
202                                 } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
203                                         $params['jobid'] = $this->JobToVerifyJobId->Text;
204                                 }
205                         }
206                 }
207                 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
208                 $startedJobId = $this->Application->getModule('misc')->findJobIdStartedJob($result);
209                 if (is_numeric($startedJobId)) {
210                         $params['jobid'] = $startedJobId;
211                         $this->configure($startedJobId, $params);
212                 } else {
213                         $this->Estimation->Text = implode(PHP_EOL, $result);
214                 }
215         }
216
217         public function estimate($sender, $param) {
218                 $params = array();
219                 $params['id'] = $this->JobID->Text;
220                 $params['level'] = $this->Level->SelectedValue;
221                 $params['fileset'] = $this->FileSet->SelectedValue;
222                 $params['clientid'] = $this->Client->SelectedValue;
223                 $params['accurate'] = (integer)$this->Accurate->Checked;
224                 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
225                 $this->Estimation->Text = implode(PHP_EOL, $result);
226         }
227
228         public function priorityValidator($sender, $param) {
229                 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
230                 $param->setIsValid($isValid);
231         }
232
233         public function jobIdToVerifyValidator($sender, $param) {
234                 $verifyVals = $this->getVerifyVals();
235                 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
236                         $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
237                 } else {
238                         $isValid = true;
239                 }
240                 $param->setIsValid($isValid);
241                 return $isValid;
242         }
243
244         private function getVerifyVals() {
245                 $verifyOpt = array_keys($this->verifyOptions);
246                 $verifyVals = array_combine($verifyOpt, $verifyOpt);
247                 return $verifyVals;
248         }
249
250         public function getResourceName($resource, $jobshow) {
251                 $resource_name = null;
252                 $pattern = str_replace('%resource', $resource, self::RESOURCE_SHOW_PATTERN);
253                 for ($i = 0; $i < count($jobshow); $i++) {
254                         if (preg_match($pattern, $jobshow[$i], $match) === 1) {
255                                 $resource_name = $match[1];
256                                 break;
257                         }
258                 }
259                 return $resource_name;
260         }
261
262         public function getJobAttr($jobshow) {
263                 $attr = array();
264                 for ($i = 0; $i < count($jobshow); $i++) {
265                         if (preg_match(self::JOB_SHOW_PATTERN, $jobshow[$i], $match) === 1) {
266                                 $attr['jobname'] = $match['jobname'];
267                                 $attr['level'] = $match['level'];
268                                 $attr['priority'] = $match['priority'];
269                                 break;
270                         }
271                 }
272                 return $attr;
273         }
274
275         private function getPoolByName($name) {
276                 $pool = null;
277                 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
278                 for ($i = 0; $i < count($pools); $i++) {
279                         if ($pools[$i]->name == $name) {
280                                 $pool = $pools[$i];
281                                 break;
282                         }
283                 }
284                 return $pool;
285         }
286
287         private function getStorageByName($name) {
288                 $storage = null;
289                 $storages = $this->Application->getModule('api')->get(array('storages'), true)->output;
290                 for ($i = 0; $i < count($storages); $i++) {
291                         if ($storages[$i]->name == $name) {
292                                 $storage = $storages[$i];
293                                 break;
294                         }
295                 }
296                 return $storage;
297         }
298 }
299 ?>