]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Pages/API/JobRun.php
baculum: API endpoints code refactor
[bacula/bacula] / gui / baculum / protected / API / Pages / API / JobRun.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 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 class JobRun extends BaculumAPIServer {
24
25         public function create($params) {
26                 $job = null;
27                 if (property_exists($params, 'id')) {
28                         $jobid = intval($params->id);
29                         $job_row = $this->getModule('job')->getJobById($jobid);
30                         $job = is_object($job_row) ? $job_row->name : null;
31                 } elseif (property_exists($params, 'name') && $this->getModule('misc')->isValidName($params->name)) {
32                         $job = $params->name;
33                 }
34                 $level = null;
35                 if (property_exists($params, 'level')) {
36                         $level = $params->level;
37                 }
38                 $fileset = null;
39                 if (property_exists($params, 'fileset') && $this->getModule('misc')->isValidName($params->fileset)) {
40                         $fileset = $params->fileset;
41                 }
42                 $client = null;
43                 if (property_exists($params, 'clientid')) {
44                         $clientid = intval($params->clientid);
45                         $client_row = $this->getModule('client')->getClientById($clientid);
46                         $client = is_object($client_row) ? $client_row->name : null;
47                 } elseif (property_exists($params, 'client') && $this->getModule('misc')->isValidName($params->client)) {
48                         $client = $params->client;
49                 }
50                 $storage = null;
51                 if (property_exists($params, 'storageid')) {
52                         $storageid = intval($params->storageid);
53                         $storage_row = $this->getModule('storage')->getStorageById($storageid);
54                         $storage = is_object($storage_row) ? $storage_row->name : null;
55                 } elseif (property_exists($params, 'storage') && $this->getModule('misc')->isValidName($params->storage)) {
56                         $storage = $params->storage;
57                 }
58                 $pool = null;
59                 if (property_exists($params, 'poolid')) {
60                         $poolid = intval($params->poolid);
61                         $pool_row = $this->getModule('pool')->getPoolById($poolid);
62                         $pool = is_object($pool_row) ? $pool_row->name : null;
63                 } elseif (property_exists($params, 'pool') && $this->getModule('misc')->isValidName($params->pool)) {
64                         $pool = $params->pool;
65                 }
66                 $priority = property_exists($params, 'priority') ? intval($params->priority) : 10; // default priority is set to 10
67
68                 $jobid = property_exists($params, 'jobid') ? 'jobid="' . intval($params->jobid) . '"' : '';
69                 $verifyjob = null;
70                 if (property_exists($params, 'verifyjob') && $this->getModule('misc')->isValidName($params->verifyjob)) {
71                         $verifyjob = 'verifyjob="' . $params->verifyjob . '"';
72                 }
73                 
74                 if(is_null($job)) {
75                         $this->output = JobError::MSG_ERROR_JOB_DOES_NOT_EXISTS;
76                         $this->error = JobError::ERROR_JOB_DOES_NOT_EXISTS;
77                         return;
78                 }
79
80                 $is_valid_level = $this->getModule('misc')->isValidJobLevel($params->level);
81                 if(!$is_valid_level) {
82                         $this->output = JobError::MSG_ERROR_INVALID_JOBLEVEL;
83                         $this->error = JobError::ERROR_INVALID_JOBLEVEL;
84                         return;
85                 }
86
87                 if(is_null($fileset)) {
88                         $this->output = JobError::MSG_ERROR_FILESET_DOES_NOT_EXISTS;
89                         $this->error = JobError::ERROR_FILESET_DOES_NOT_EXISTS;
90                         return;
91                 }
92
93                 if(is_null($client)) {
94                         $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
95                         $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
96                         return;
97                 }
98
99                 if(is_null($storage)) {
100                         $this->output = JobError::MSG_ERROR_STORAGE_DOES_NOT_EXISTS;
101                         $this->error = JobError::ERROR_STORAGE_DOES_NOT_EXISTS;
102                         return;
103                 }
104
105                 if(is_null($pool)) {
106                         $this->output = JobError::MSG_ERROR_POOL_DOES_NOT_EXISTS;
107                         $this->error = JobError::ERROR_POOL_DOES_NOT_EXISTS;
108                         return;
109                 }
110
111                 $joblevels  = $this->getModule('misc')->getJobLevels();
112                 $command = array(
113                         'run',
114                         'job="' . $job . '"',
115                         'level="' . $joblevels[$level] . '"',
116                         'fileset="' . $fileset . '"',
117                         'client="' . $client . '"',
118                         'storage="' . $storage . '"',
119                         'pool="' . $pool . '"' ,
120                         'priority="' . $priority . '"',
121                         $jobid,
122                         $verifyjob,
123                         'yes'
124                 );
125                 $run = $this->getModule('bconsole')->bconsoleCommand($this->director, $command);
126                 $this->output = $run->output;
127                 $this->error = $run->exitcode;
128         }
129 }
130
131 ?>