Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
c1418f 1 <?php
M 2
3 /*
4 Copyright (c) 2007 - 2013, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 --UPDATED 08.2009--
31 Full SOAP support for ISPConfig 3.1.4 b
32 Updated by Arkadiusz Roch & Artur Edelman
33 Copyright (c) Tri-Plex technology
34
35 --UPDATED 08.2013--
36 Migrated into new remote classes system
37 by Marius Cramer <m.cramer@pixcept.de>
38
39 */
40
41 class remoting_openvz extends remoting {
42     //* Functions for virtual machine management
b1a6a5 43
c1418f 44     //* Get OpenVZ OStemplate details
M 45     public function openvz_ostemplate_get($session_id, $ostemplate_id)
b1a6a5 46     {
c1418f 47         global $app;
b1a6a5 48
c1418f 49         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 50             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
51             return false;
52         }
53         $app->uses('remoting_lib');
54         $app->remoting_lib->loadFormDef('../vm/form/openvz_ostemplate.tform.php');
55         return $app->remoting_lib->getDataRecord($ostemplate_id);
56     }
b1a6a5 57
c1418f 58     //* Add a openvz ostemplate record
M 59     public function openvz_ostemplate_add($session_id, $client_id, $params)
b1a6a5 60     {
c1418f 61         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 62             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
63             return false;
64         }
b1a6a5 65         return $this->insertQuery('../vm/form/openvz_ostemplate.tform.php', $client_id, $params);
c1418f 66     }
b1a6a5 67
c1418f 68     //* Update openvz ostemplate record
M 69     public function openvz_ostemplate_update($session_id, $client_id, $ostemplate_id, $params)
b1a6a5 70     {
c1418f 71         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 72             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
73             return false;
74         }
b1a6a5 75         $affected_rows = $this->updateQuery('../vm/form/openvz_ostemplate.tform.php', $client_id, $ostemplate_id, $params);
c1418f 76         return $affected_rows;
M 77     }
b1a6a5 78
c1418f 79     //* Delete openvz ostemplate record
M 80     public function openvz_ostemplate_delete($session_id, $ostemplate_id)
b1a6a5 81     {
c1418f 82         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 83             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
84             return false;
85         }
b1a6a5 86         $affected_rows = $this->deleteQuery('../vm/form/openvz_ostemplate.tform.php', $ostemplate_id);
c1418f 87         return $affected_rows;
M 88     }
b1a6a5 89
c1418f 90     //* Get OpenVZ template details
M 91     public function openvz_template_get($session_id, $template_id)
b1a6a5 92     {
c1418f 93         global $app;
b1a6a5 94
c1418f 95         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 96             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
97             return false;
98         }
99         $app->uses('remoting_lib');
100         $app->remoting_lib->loadFormDef('../vm/form/openvz_template.tform.php');
101         return $app->remoting_lib->getDataRecord($template_id);
102     }
b1a6a5 103
c1418f 104     //* Add a openvz template record
M 105     public function openvz_template_add($session_id, $client_id, $params)
b1a6a5 106     {
c1418f 107         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 108             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
109             return false;
110         }
b1a6a5 111         return $this->insertQuery('../vm/form/openvz_template.tform.php', $client_id, $params);
c1418f 112     }
b1a6a5 113
c1418f 114     //* Update openvz template record
M 115     public function openvz_template_update($session_id, $client_id, $template_id, $params)
b1a6a5 116     {
c1418f 117         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 118             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
119             return false;
120         }
b1a6a5 121         $affected_rows = $this->updateQuery('../vm/form/openvz_template.tform.php', $client_id, $template_id, $params);
c1418f 122         return $affected_rows;
M 123     }
b1a6a5 124
c1418f 125     //* Delete openvz template record
M 126     public function openvz_template_delete($session_id, $template_id)
b1a6a5 127     {
c1418f 128         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 129             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
130             return false;
131         }
b1a6a5 132         $affected_rows = $this->deleteQuery('../vm/form/openvz_template.tform.php', $template_id);
c1418f 133         return $affected_rows;
M 134     }
b1a6a5 135
c1418f 136     //* Get OpenVZ ip details
M 137     public function openvz_ip_get($session_id, $ip_id)
b1a6a5 138     {
c1418f 139         global $app;
b1a6a5 140
c1418f 141         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 142             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
143             return false;
144         }
145         $app->uses('remoting_lib');
146         $app->remoting_lib->loadFormDef('../vm/form/openvz_ip.tform.php');
147         return $app->remoting_lib->getDataRecord($ip_id);
148     }
b1a6a5 149
c1418f 150     //* Get OpenVZ a free IP address
M 151     public function openvz_get_free_ip($session_id, $server_id = 0)
b1a6a5 152     {
c1418f 153         global $app;
b1a6a5 154
c1418f 155         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 156             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
157             return false;
158         }
159         $server_id = $app->functions->intval($server_id);
b1a6a5 160
c1418f 161         if($server_id > 0) {
cc7a82 162             $tmp = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 AND server_id = ? LIMIT 0,1", $server_id);
c1418f 163         } else {
M 164             $tmp = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 LIMIT 0,1");
165         }
b1a6a5 166
c1418f 167         if(count($tmp) > 0) {
M 168             return $tmp;
169         } else {
170             throw new SoapFault('no_free_ip', 'There is no free IP available.');
171         }
172     }
b1a6a5 173
c1418f 174     //* Add a openvz ip record
M 175     public function openvz_ip_add($session_id, $client_id, $params)
b1a6a5 176     {
c1418f 177         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 178             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
179             return false;
180         }
b1a6a5 181         return $this->insertQuery('../vm/form/openvz_ip.tform.php', $client_id, $params);
c1418f 182     }
b1a6a5 183
c1418f 184     //* Update openvz ip record
M 185     public function openvz_ip_update($session_id, $client_id, $ip_id, $params)
b1a6a5 186     {
c1418f 187         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 188             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
189             return false;
190         }
b1a6a5 191         $affected_rows = $this->updateQuery('../vm/form/openvz_ip.tform.php', $client_id, $ip_id, $params);
c1418f 192         return $affected_rows;
M 193     }
b1a6a5 194
c1418f 195     //* Delete openvz ip record
M 196     public function openvz_ip_delete($session_id, $ip_id)
b1a6a5 197     {
c1418f 198         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 199             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
200             return false;
201         }
b1a6a5 202         $affected_rows = $this->deleteQuery('../vm/form/openvz_ip.tform.php', $ip_id);
c1418f 203         return $affected_rows;
M 204     }
b1a6a5 205
c1418f 206     //* Get OpenVZ vm details
M 207     public function openvz_vm_get($session_id, $vm_id)
b1a6a5 208     {
c1418f 209         global $app;
b1a6a5 210
c1418f 211         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 212             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
213             return false;
214         }
215         $app->uses('remoting_lib');
216         $app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
217         return $app->remoting_lib->getDataRecord($vm_id);
218     }
b1a6a5 219
c1418f 220     //* Get OpenVZ list
M 221     public function openvz_vm_get_by_client($session_id, $client_id)
b1a6a5 222     {
c1418f 223         global $app;
b1a6a5 224
c1418f 225         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 226             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
227             return false;
228         }
b1a6a5 229
c1418f 230         if (!empty($client_id)) {
b1a6a5 231             $client_id      = $app->functions->intval($client_id);
cc7a82 232             $tmp    = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ?", $client_id);
MC 233             $sql            = "SELECT * FROM openvz_vm WHERE sys_groupid = ?";
234             $result         = $app->db->queryAllRecords($sql, $tmp['groupid']);
b1a6a5 235             return          $result;
MC 236         }
237         return false;
c1418f 238     }
b1a6a5 239
c1418f 240     //* Add a openvz vm record
M 241     public function openvz_vm_add($session_id, $client_id, $params)
b1a6a5 242     {
c1418f 243         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 244             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
245             return false;
246         }
b1a6a5 247         return $this->insertQuery('../vm/form/openvz_vm.tform.php', $client_id, $params);
c1418f 248     }
b1a6a5 249
c1418f 250     //* Add a openvz vm record from template
M 251     public function openvz_vm_add_from_template($session_id, $client_id, $ostemplate_id, $template_id, $override_params = array())
b1a6a5 252     {
c1418f 253         global $app;
b1a6a5 254
c1418f 255         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 256             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
257             return false;
258         }
b1a6a5 259
MC 260
c1418f 261         $template_id = $app->functions->intval($template_id);
M 262         $ostemplate_id = $app->functions->intval($ostemplate_id);
b1a6a5 263
c1418f 264         //* Verify parameters
M 265         if($template_id == 0) {
266             throw new SoapFault('template_id_error', 'Template ID must be > 0.');
267             return false;
268         }
269         if($ostemplate_id == 0) {
270             throw new SoapFault('ostemplate_id_error', 'OSTemplate ID must be > 0.');
271             return false;
272         }
b1a6a5 273
c1418f 274         // Verify if template and ostemplate exist
cc7a82 275         $tmp = $app->db->queryOneRecord("SELECT template_id FROM openvz_template WHERE template_id = ?", $template_id);
c1418f 276         if(!is_array($tmp)) {
M 277             throw new SoapFault('template_id_error', 'Template does not exist.');
278             return false;
279         }
cc7a82 280         $tmp = $app->db->queryOneRecord("SELECT ostemplate_id FROM openvz_ostemplate WHERE ostemplate_id = ?", $ostemplate_id);
c1418f 281         if(!is_array($tmp)) {
M 282             throw new SoapFault('ostemplate_id_error', 'OSTemplate does not exist.');
283             return false;
284         }
b1a6a5 285
c1418f 286         //* Get the template
cc7a82 287         $vtpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ?", $template_id);
b1a6a5 288
c1418f 289         //* Get the IP address and server_id
M 290         if($override_params['server_id'] > 0) {
cc7a82 291             $vmip = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 AND server_id = ? LIMIT 0,1", $override_params['server_id']);
c1418f 292         } else {
M 293             $vmip = $app->db->queryOneRecord("SELECT ip_address_id, server_id, ip_address FROM openvz_ip WHERE reserved = 'n' AND vm_id = 0 LIMIT 0,1");
294         }
295         if(!is_array($vmip)) {
296             throw new SoapFault('vm_ip_error', 'Unable to get a free VM IP.');
297             return false;
298         }
b1a6a5 299
c1418f 300         //* Build the $params array
M 301         $params = array();
302         $params['server_id'] = $vmip['server_id'];
303         $params['ostemplate_id'] = $ostemplate_id;
304         $params['template_id'] = $template_id;
305         $params['ip_address'] = $vmip['ip_address'];
306         $params['hostname'] = (isset($override_params['hostname']))?$override_params['hostname']:$vtpl['hostname'];
307         $params['vm_password'] = (isset($override_params['vm_password']))?$override_params['vm_password']:$app->auth->get_random_password(10);
308         $params['start_boot'] = (isset($override_params['start_boot']))?$override_params['start_boot']:'y';
309         $params['active'] = (isset($override_params['active']))?$override_params['active']:'y';
651642 310         $params['active_until_date'] = (isset($override_params['active_until_date']))?$override_params['active_until_date']:null;
c1418f 311         $params['description'] = (isset($override_params['description']))?$override_params['description']:'';
b1a6a5 312
MC 313         //* The next params get filled with pseudo values, as the get replaced
c1418f 314         //* by the openvz event plugin anyway with values from the template
M 315         $params['veid'] = 1;
316         $params['diskspace'] = 1;
317         $params['ram'] = 1;
318         $params['ram_burst'] = 1;
319         $params['cpu_units'] = 1;
320         $params['cpu_num'] = 1;
321         $params['cpu_limit'] = 1;
322         $params['io_priority'] = 1;
323         $params['nameserver'] = '8.8.8.8 8.8.4.4';
324         $params['create_dns'] = 'n';
325         $params['capability'] = '';
b1a6a5 326
MC 327         return $this->insertQuery('../vm/form/openvz_vm.tform.php', $client_id, $params, 'vm:openvz_vm:on_after_insert');
c1418f 328     }
b1a6a5 329
c1418f 330     //* Update openvz vm record
M 331     public function openvz_vm_update($session_id, $client_id, $vm_id, $params)
b1a6a5 332     {
c1418f 333         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 334             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
335             return false;
336         }
b1a6a5 337         $affected_rows = $this->updateQuery('../vm/form/openvz_vm.tform.php', $client_id, $vm_id, $params, 'vm:openvz_vm:on_after_update');
c1418f 338         return $affected_rows;
M 339     }
b1a6a5 340
c1418f 341     //* Delete openvz vm record
M 342     public function openvz_vm_delete($session_id, $vm_id)
b1a6a5 343     {
c1418f 344         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 345             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
346             return false;
347         }
b1a6a5 348         $affected_rows = $this->deleteQuery('../vm/form/openvz_vm.tform.php', $vm_id, 'vm:openvz_vm:on_after_delete');
c1418f 349         return $affected_rows;
M 350     }
b1a6a5 351
c1418f 352     //* Start VM
M 353     public function openvz_vm_start($session_id, $vm_id)
b1a6a5 354     {
c1418f 355         global $app;
b1a6a5 356
c1418f 357         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 358             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
359             return false;
360         }
b1a6a5 361
c1418f 362         $app->uses('remoting_lib');
M 363         $app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
364         $vm = $app->remoting_lib->getDataRecord($vm_id);
b1a6a5 365
c1418f 366         if(!is_array($vm)) {
M 367             throw new SoapFault('action_pending', 'No VM with this ID available.');
368             return false;
369         }
b1a6a5 370
c1418f 371         if($vm['active'] == 'n') {
M 372             throw new SoapFault('action_pending', 'VM is not in active state.');
373             return false;
374         }
b1a6a5 375
c1418f 376         $action = 'openvz_start_vm';
b1a6a5 377
MC 378         $tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
cc7a82 379                 WHERE server_id = ?
MC 380                 AND action_type = ?
381                 AND action_param = ?
382                 AND action_state = 'pending'", $vm['server_id'], $action, $vm['veid']);
b1a6a5 383
c1418f 384         if($tmp['actions'] > 0) {
M 385             throw new SoapFault('action_pending', 'There is already a action pending for this VM.');
386             return false;
387         } else {
388             $sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
cc7a82 389                 "VALUES (?, ?, ?, ?, 'pending', '')";
MC 390             $app->db->query($sql, (int)$vm['server_id'], time(), $action, $vm['veid']);
c1418f 391         }
M 392     }
b1a6a5 393
c1418f 394     //* Stop VM
M 395     public function openvz_vm_stop($session_id, $vm_id)
b1a6a5 396     {
c1418f 397         global $app;
b1a6a5 398
c1418f 399         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 400             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
401             return false;
402         }
b1a6a5 403
c1418f 404         $app->uses('remoting_lib');
M 405         $app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
406         $vm = $app->remoting_lib->getDataRecord($vm_id);
b1a6a5 407
c1418f 408         if(!is_array($vm)) {
M 409             throw new SoapFault('action_pending', 'No VM with this ID available.');
410             return false;
411         }
b1a6a5 412
c1418f 413         if($vm['active'] == 'n') {
M 414             throw new SoapFault('action_pending', 'VM is not in active state.');
415             return false;
416         }
b1a6a5 417
c1418f 418         $action = 'openvz_stop_vm';
b1a6a5 419
MC 420         $tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
cc7a82 421                 WHERE server_id = ?
MC 422                 AND action_type = ?
423                 AND action_param = ?
424                 AND action_state = 'pending'", $vm['server_id'], $action, $vm['veid']);
b1a6a5 425
c1418f 426         if($tmp['actions'] > 0) {
M 427             throw new SoapFault('action_pending', 'There is already a action pending for this VM.');
428             return false;
429         } else {
430             $sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
cc7a82 431                 "VALUES (?, ?, ?, ?, 'pending', '')";
MC 432             $app->db->query($sql, (int)$vm['server_id'], time(), $action, $vm['veid']);
c1418f 433         }
M 434     }
b1a6a5 435
c1418f 436     //* Restart VM
M 437     public function openvz_vm_restart($session_id, $vm_id)
b1a6a5 438     {
c1418f 439         global $app;
b1a6a5 440
c1418f 441         if(!$this->checkPerm($session_id, 'vm_openvz')) {
M 442             throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
443             return false;
444         }
b1a6a5 445
c1418f 446         $app->uses('remoting_lib');
M 447         $app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
448         $vm = $app->remoting_lib->getDataRecord($vm_id);
b1a6a5 449
c1418f 450         if(!is_array($vm)) {
M 451             throw new SoapFault('action_pending', 'No VM with this ID available.');
452             return false;
453         }
b1a6a5 454
c1418f 455         if($vm['active'] == 'n') {
M 456             throw new SoapFault('action_pending', 'VM is not in active state.');
457             return false;
458         }
b1a6a5 459
c1418f 460         $action = 'openvz_restart_vm';
b1a6a5 461
MC 462         $tmp = $app->db->queryOneRecord("SELECT count(action_id) as actions FROM sys_remoteaction
cc7a82 463                 WHERE server_id = ?
MC 464                 AND action_type = ?
465                 AND action_param = ?
466                 AND action_state = 'pending'", $vm['server_id'], $action, $vm['veid']);
b1a6a5 467
c1418f 468         if($tmp['actions'] > 0) {
M 469             throw new SoapFault('action_pending', 'There is already a action pending for this VM.');
470             return false;
471         } else {
472             $sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
cc7a82 473                 "VALUES (?, ?, ?, ?, 'pending', '')";
MC 474             $app->db->query($sql, (int)$vm['server_id'], time(), $action, $vm['veid']);
c1418f 475         }
M 476     }
b1a6a5 477
c1418f 478 }
M 479
b1a6a5 480 ?>