3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
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.Web.Portlets.Portlets');
27 class JobConfiguration extends Portlets {
29 const USE_CACHE = true;
31 const DEFAULT_JOB_PRIORITY = 10;
33 const RESOURCE_SHOW_PATTERN = '/^\s+--> %resource: name=(.+?(?=\s\S+\=.+)|.+$)/i';
34 const JOB_SHOW_PATTERN = '/^Job:\sname=(?P<jobname>.+)\sJobType=\d+\slevel=(?P<level>\w+)\sPriority=(?P<priority>\d+)/i';
36 public $jobToVerify = array('C', 'O', 'd');
38 public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
40 public function configure($jobid, $params = array()) {
41 $jobdata = $this->Application->getModule('api')->get(array('jobs', $jobid))->output;
42 $this->JobName->Text = $jobdata->job;
43 $this->JobID->Text = $jobdata->jobid;
44 $joblog = $this->Application->getModule('api')->get(array('joblog', $jobdata->jobid))->output;
45 $running_job_states = $this->Application->getModule('misc')->getRunningJobStates();
46 if (in_array($jobdata->jobstatus, $running_job_states)) {
47 $this->Estimation->CssClass = 'textbox-auto wheel-loader';
49 $this->Estimation->CssClass = 'textbox-auto';
51 $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");
53 $this->Level->dataSource = $this->Application->getModule('misc')->getJobLevels();
54 $this->Level->SelectedValue = $jobdata->level;
55 $this->Level->dataBind();
57 $isVerifyOption = in_array($jobdata->level, $this->jobToVerify);
58 $this->JobToVerifyOptionsLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
59 $this->JobToVerifyJobNameLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
60 $this->JobToVerifyJobIdLine->Display = 'None';
61 $this->AccurateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
62 $this->EstimateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
64 $verifyValues = array();
66 foreach($this->verifyOptions as $value => $text) {
67 $verifyValues[$value] = Prado::localize($text);
70 $this->JobToVerifyOptions->dataSource = $verifyValues;
71 $this->JobToVerifyOptions->dataBind();
73 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), null, true, self::USE_CACHE)->output;
74 $jobsAllDirs = array();
75 foreach($jobTasks as $director => $tasks) {
76 $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
79 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
80 $this->JobToVerifyJobName->dataBind();
82 $clients = $this->Application->getModule('api')->get(array('clients'), null, true, self::USE_CACHE)->output;
83 $clientsList = array();
84 foreach($clients as $client) {
85 $clientsList[$client->clientid] = $client->name;
87 $this->Client->dataSource = $clientsList;
88 $this->Client->SelectedValue = $jobdata->clientid;
89 $this->Client->dataBind();
91 $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), null, true, self::USE_CACHE)->output;
92 $filesetsList = array();
93 foreach($filesetsAll as $director => $filesets) {
94 $filesetsList = array_merge($filesets, $filesetsList);
96 $selectedFileset = '';
97 if($jobdata->filesetid != 0) {
98 $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid), null, true, self::USE_CACHE)->output->fileset;
100 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
101 $this->FileSet->SelectedValue = array_key_exists('fileset', $params) ? $params['fileset'] : $selectedFileset;
102 $this->FileSet->dataBind();
104 $pools = $this->Application->getModule('api')->get(array('pools'), null, true, self::USE_CACHE)->output;
106 foreach($pools as $pool) {
107 $poolList[$pool->poolid] = $pool->name;
109 $this->Pool->dataSource = $poolList;
110 $this->Pool->SelectedValue = array_key_exists('poolid', $params) ? $params['poolid'] : $jobdata->poolid;
111 $this->Pool->dataBind();
113 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid), null, true, self::USE_CACHE)->output;
114 $storageShow = $this->getResourceName('storage', $jobshow);
115 $storagesList = array();
116 $selectedStorageId = null;
117 $storages = $this->Application->getModule('api')->get(array('storages'), null, true, self::USE_CACHE)->output;
118 foreach($storages as $storage) {
119 if ($storage->name == $storageShow) {
120 $selectedStorageId = $storage->storageid;
122 $storagesList[$storage->storageid] = $storage->name;
124 $this->Storage->dataSource = $storagesList;
125 if (!is_null($selectedStorageId)) {
126 $this->Storage->SelectedValue = $selectedStorageId;
128 $this->Storage->dataBind();
130 $isJobRunning = in_array($jobdata->jobstatus, $running_job_states);
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 if (key_exists('prev_window', $params)) {
140 $this->GoBack->Value = $params['prev_window'];
141 $this->getPage()->getCallbackClient()->show('job_go_back');
144 $this->getPage()->getCallbackClient()->hide('job_go_back');
148 public function status($sender, $param) {
149 $jobdata = $this->Application->getModule('api')->get(array('jobs', $this->JobID->Text))->output;
150 $running_job_states = $this->Application->getModule('misc')->getRunningJobStates();
151 if (in_array($jobdata->jobstatus, $running_job_states)) {
152 $this->RefreshStart->Value = true;
154 $this->RefreshStart->Value = false;
155 $this->CancelButton->Visible = false;
156 $this->Estimation->CssClass = 'textbox-auto';
159 $joblog = $this->Application->getModule('api')->get(array('joblog', $this->JobID->Text))->output;
160 $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");
163 public function delete($sender, $param) {
164 $this->Application->getModule('api')->remove(array('jobs', $this->JobID->Text));
165 $this->Status->Visible = false;
166 $this->Run->Display = 'None';
167 $this->DeleteButton->Visible = false;
168 $this->EstimateLine->Display = 'None';
169 $this->Estimation->CssClass = 'textbox-auto';
172 public function cancel($sender, $param) {
173 $this->Application->getModule('api')->set(array('jobs', 'cancel', $this->JobID->Text), array('a' => 'b'));
174 $this->CancelButton->Visible = false;
175 $this->status(null, null);
176 $this->Estimation->CssClass = 'textbox-auto';
179 public function run_again($sender, $param) {
180 if($this->PriorityValidator->IsValid === false) {
185 if (is_null($sender) && is_numeric($param)) {
186 $jobdata = $this->Application->getModule('api')->get(array('jobs', $param))->output;
187 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid))->output;
188 $params['id'] = $jobdata->jobid;
189 $params['level'] = $jobdata->level;
190 $params['fileset'] = $this->getResourceName('fileset', $jobshow);
191 $params['clientid'] = $jobdata->clientid;
192 $storage = $this->getResourceName('storage', $jobshow);
193 if (!is_null($storage)) {
194 $params['storageid'] = $this->getStorageByName($storage)->storageid;
196 $autochanger = $this->getResourceName('autochanger', $jobshow);
197 if (!is_null($autochanger)) {
198 $params['storageid'] = $this->getStorageByName($autochanger)->storageid;
201 $pool = $this->getResourceName('pool', $jobshow);
202 if (!is_null($pool)) {
203 $params['poolid'] = $this->getPoolByName($pool)->poolid;
206 $params['id'] = $this->JobID->Text;
207 $params['level'] = $this->Level->SelectedValue;
208 $params['fileset'] = $this->FileSet->SelectedValue;
209 $params['clientid'] = $this->Client->SelectedValue;
210 $params['storageid'] = $this->Storage->SelectedValue;
211 $params['poolid'] = $this->Pool->SelectedValue;
212 $params['priority'] = $this->Priority->Text;
214 if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
215 $verifyVals = $this->getVerifyVals();
216 if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
217 $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
218 } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
219 $params['jobid'] = $this->JobToVerifyJobId->Text;
223 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
224 $startedJobId = $this->Application->getModule('misc')->findJobIdStartedJob($result);
225 if (is_numeric($startedJobId)) {
226 $params['jobid'] = $startedJobId;
227 $this->configure($startedJobId, $params);
229 $this->Estimation->Text = implode(PHP_EOL, $result);
233 public function estimate($sender, $param) {
235 $params['id'] = $this->JobID->Text;
236 $params['level'] = $this->Level->SelectedValue;
237 $params['fileset'] = $this->FileSet->SelectedValue;
238 $params['clientid'] = $this->Client->SelectedValue;
239 $params['accurate'] = (integer)$this->Accurate->Checked;
240 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
241 $this->Estimation->Text = implode(PHP_EOL, $result);
244 public function priorityValidator($sender, $param) {
245 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
246 $param->setIsValid($isValid);
249 public function jobIdToVerifyValidator($sender, $param) {
250 $verifyVals = $this->getVerifyVals();
251 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
252 $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
256 $param->setIsValid($isValid);
260 private function getVerifyVals() {
261 $verifyOpt = array_keys($this->verifyOptions);
262 $verifyVals = array_combine($verifyOpt, $verifyOpt);
266 public function getResourceName($resource, $jobshow) {
267 $resource_name = null;
268 $pattern = str_replace('%resource', $resource, self::RESOURCE_SHOW_PATTERN);
269 for ($i = 0; $i < count($jobshow); $i++) {
270 if (preg_match($pattern, $jobshow[$i], $match) === 1) {
271 $resource_name = $match[1];
275 return $resource_name;
278 public function getJobAttr($jobshow) {
280 for ($i = 0; $i < count($jobshow); $i++) {
281 if (preg_match(self::JOB_SHOW_PATTERN, $jobshow[$i], $match) === 1) {
282 $attr['jobname'] = $match['jobname'];
283 $attr['level'] = $match['level'];
284 $attr['priority'] = $match['priority'];
291 private function getPoolByName($name) {
293 $pools = $this->Application->getModule('api')->get(array('pools'), null, true, self::USE_CACHE)->output;
294 for ($i = 0; $i < count($pools); $i++) {
295 if ($pools[$i]->name == $name) {
303 private function getStorageByName($name) {
305 $storages = $this->Application->getModule('api')->get(array('storages'), null, true, self::USE_CACHE)->output;
306 for ($i = 0; $i < count($storages); $i++) {
307 if ($storages[$i]->name == $name) {
308 $storage = $storages[$i];