Skip to content

Commit 077c277

Browse files
committed
Added functions to manage OpenVZ virtual machines to the remote API.
Added a example file for adding a VM with the remote API
1 parent 5d5d88e commit 077c277

File tree

5 files changed

+351
-6
lines changed

5 files changed

+351
-6
lines changed

interface/lib/classes/remoting.inc.php

Lines changed: 302 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
Copyright (c) 2007 - 2009, Till Brehm, projektfarm Gmbh
4+
Copyright (c) 2007 - 2011, Till Brehm, projektfarm Gmbh
55
All rights reserved.
66
77
Redistribution and use in source and binary forms, with or without modification,
@@ -2257,7 +2257,7 @@ private function updateQuery($formdef_file, $client_id, $primary_id, $params, $e
22572257
return $affected_rows;
22582258
}
22592259

2260-
private function deleteQuery($formdef_file, $primary_id)
2260+
private function deleteQuery($formdef_file, $primary_id, $event_identifier = '')
22612261
{
22622262
global $app;
22632263

@@ -2282,6 +2282,9 @@ private function deleteQuery($formdef_file, $primary_id)
22822282
$app->db->query($sql);
22832283

22842284
if($app->db->errorMessage != '') {
2285+
2286+
if($event_identifier != '') $app->plugin->raiseEvent($event_identifier,$this);
2287+
22852288
$this->server->fault('database_error', $app->db->errorMessage . ' '.$sql);
22862289
return false;
22872290
}
@@ -2611,5 +2614,302 @@ public function mail_domain_set_status($session_id, $primary_id, $status) {
26112614
return false;
26122615
}
26132616
}
2617+
2618+
//* Functions for virtual machine management
2619+
2620+
//* Get OpenVZ OStemplate details
2621+
public function openvz_ostemplate_get($session_id, $ostemplate_id)
2622+
{
2623+
global $app;
2624+
2625+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2626+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2627+
return false;
2628+
}
2629+
$app->uses('remoting_lib');
2630+
$app->remoting_lib->loadFormDef('../vm/form/openvz_ostemplate.tform.php');
2631+
return $app->remoting_lib->getDataRecord($ostemplate_id);
2632+
}
2633+
2634+
//* Add a openvz ostemplate record
2635+
public function openvz_ostemplate_add($session_id, $client_id, $params)
2636+
{
2637+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2638+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2639+
return false;
2640+
}
2641+
return $this->insertQuery('../vm/form/openvz_ostemplate.tform.php',$client_id,$params);
2642+
}
2643+
2644+
//* Update openvz ostemplate record
2645+
public function openvz_ostemplate_update($session_id, $client_id, $ostemplate_id, $params)
2646+
{
2647+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2648+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2649+
return false;
2650+
}
2651+
$affected_rows = $this->updateQuery('../vm/form/openvz_ostemplate.tform.php',$client_id,$ostemplate_id,$params);
2652+
return $affected_rows;
2653+
}
2654+
2655+
//* Delete openvz ostemplate record
2656+
public function openvz_ostemplate_delete($session_id, $ostemplate_id)
2657+
{
2658+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2659+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2660+
return false;
2661+
}
2662+
$affected_rows = $this->deleteQuery('../vm/form/openvz_ostemplate.tform.php',$ostemplate_id);
2663+
return $affected_rows;
2664+
}
2665+
2666+
//* Get OpenVZ template details
2667+
public function openvz_template_get($session_id, $template_id)
2668+
{
2669+
global $app;
2670+
2671+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2672+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2673+
return false;
2674+
}
2675+
$app->uses('remoting_lib');
2676+
$app->remoting_lib->loadFormDef('../vm/form/openvz_template.tform.php');
2677+
return $app->remoting_lib->getDataRecord($template_id);
2678+
}
2679+
2680+
//* Add a openvz template record
2681+
public function openvz_template_add($session_id, $client_id, $params)
2682+
{
2683+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2684+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2685+
return false;
2686+
}
2687+
return $this->insertQuery('../vm/form/openvz_template.tform.php',$client_id,$params);
2688+
}
2689+
2690+
//* Update openvz template record
2691+
public function openvz_template_update($session_id, $client_id, $template_id, $params)
2692+
{
2693+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2694+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2695+
return false;
2696+
}
2697+
$affected_rows = $this->updateQuery('../vm/form/openvz_template.tform.php',$client_id,$template_id,$params);
2698+
return $affected_rows;
2699+
}
2700+
2701+
//* Delete openvz template record
2702+
public function openvz_template_delete($session_id, $template_id)
2703+
{
2704+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2705+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2706+
return false;
2707+
}
2708+
$affected_rows = $this->deleteQuery('../vm/form/openvz_template.tform.php',$template_id);
2709+
return $affected_rows;
2710+
}
2711+
2712+
//* Get OpenVZ ip details
2713+
public function openvz_ip_get($session_id, $ip_id)
2714+
{
2715+
global $app;
2716+
2717+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2718+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2719+
return false;
2720+
}
2721+
$app->uses('remoting_lib');
2722+
$app->remoting_lib->loadFormDef('../vm/form/openvz_ip.tform.php');
2723+
return $app->remoting_lib->getDataRecord($ip_id);
2724+
}
2725+
2726+
//* Get OpenVZ a free IP address
2727+
public function openvz_get_free_ip($session_id, $server_id = 0)
2728+
{
2729+
global $app;
2730+
2731+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2732+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2733+
return false;
2734+
}
2735+
$server_id = intval($server_id);
2736+
2737+
if($server_id > 0) {
2738+
$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 = $server_id LIMIT 0,1");
2739+
} else {
2740+
$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");
2741+
}
2742+
2743+
if(count($tmp) > 0) {
2744+
return $tmp;
2745+
} else {
2746+
$this->server->fault('no_free_ip', 'There is no free IP available.');
2747+
}
2748+
}
2749+
2750+
//* Add a openvz ip record
2751+
public function openvz_ip_add($session_id, $client_id, $params)
2752+
{
2753+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2754+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2755+
return false;
2756+
}
2757+
return $this->insertQuery('../vm/form/openvz_ip.tform.php',$client_id,$params);
2758+
}
2759+
2760+
//* Update openvz ip record
2761+
public function openvz_ip_update($session_id, $client_id, $ip_id, $params)
2762+
{
2763+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2764+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2765+
return false;
2766+
}
2767+
$affected_rows = $this->updateQuery('../vm/form/openvz_ip.tform.php',$client_id,$ip_id,$params);
2768+
return $affected_rows;
2769+
}
2770+
2771+
//* Delete openvz ip record
2772+
public function openvz_ip_delete($session_id, $ip_id)
2773+
{
2774+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2775+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2776+
return false;
2777+
}
2778+
$affected_rows = $this->deleteQuery('../vm/form/openvz_ip.tform.php',$ip_id);
2779+
return $affected_rows;
2780+
}
2781+
2782+
//* Get OpenVZ vm details
2783+
public function openvz_vm_get($session_id, $vm_id)
2784+
{
2785+
global $app;
2786+
2787+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2788+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2789+
return false;
2790+
}
2791+
$app->uses('remoting_lib');
2792+
$app->remoting_lib->loadFormDef('../vm/form/openvz_vm.tform.php');
2793+
return $app->remoting_lib->getDataRecord($vm_id);
2794+
}
2795+
2796+
//* Add a openvz vm record
2797+
public function openvz_vm_add($session_id, $client_id, $params)
2798+
{
2799+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2800+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2801+
return false;
2802+
}
2803+
return $this->insertQuery('../vm/form/openvz_vm.tform.php',$client_id,$params);
2804+
}
2805+
2806+
//* Add a openvz vm record from template
2807+
public function openvz_vm_add_from_template($session_id, $client_id, $ostemplate_id, $template_id, $override_params = array())
2808+
{
2809+
global $app;
2810+
2811+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2812+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2813+
return false;
2814+
}
2815+
2816+
2817+
$template_id = intval($template_id);
2818+
$ostemplate_id = intval($ostemplate_id);
2819+
2820+
//* Verify parameters
2821+
if($template_id == 0) {
2822+
$this->server->fault('template_id_error', 'Template ID must be > 0.');
2823+
return false;
2824+
}
2825+
if($ostemplate_id == 0) {
2826+
$this->server->fault('ostemplate_id_error', 'OSTemplate ID must be > 0.');
2827+
return false;
2828+
}
2829+
2830+
// Verify if template and ostemplate exist
2831+
$tmp = $app->db->queryOneRecord("SELECT template_id FROM openvz_template WHERE template_id = $template_id");
2832+
if(!is_array($tmp)) {
2833+
$this->server->fault('template_id_error', 'Template does not exist.');
2834+
return false;
2835+
}
2836+
$tmp = $app->db->queryOneRecord("SELECT ostemplate_id FROM openvz_ostemplate WHERE ostemplate_id = $ostemplate_id");
2837+
if(!is_array($tmp)) {
2838+
$this->server->fault('ostemplate_id_error', 'OSTemplate does not exist.');
2839+
return false;
2840+
}
2841+
2842+
//* Get the template
2843+
$vtpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = $template_id");
2844+
2845+
//* Get the IP address and server_id
2846+
if($override_params['server_id'] > 0) {
2847+
$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 = ".$override_params['server_id']." LIMIT 0,1");
2848+
} else {
2849+
$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");
2850+
}
2851+
if(!is_array($vmip)) {
2852+
$this->server->fault('vm_ip_error', 'Unable to get a free VM IP.');
2853+
return false;
2854+
}
2855+
2856+
//* Build the $params array
2857+
$params = array();
2858+
$params['server_id'] = $vmip['server_id'];
2859+
$params['ostemplate_id'] = $ostemplate_id;
2860+
$params['template_id'] = $template_id;
2861+
$params['ip_address'] = $vmip['ip_address'];
2862+
$params['hostname'] = (isset($override_params['hostname']))?$override_params['hostname']:$vtpl['hostname'];
2863+
$params['vm_password'] = (isset($override_params['vm_password']))?$override_params['vm_password']:$app->auth->get_random_password(10);
2864+
$params['start_boot'] = (isset($override_params['start_boot']))?$override_params['start_boot']:'y';
2865+
$params['active'] = (isset($override_params['active']))?$override_params['active']:'y';
2866+
$params['active_until_date'] = (isset($override_params['active_until_date']))?$override_params['active_until_date']:'0000-00-00';
2867+
$params['description'] = (isset($override_params['description']))?$override_params['description']:'';
2868+
2869+
//* The next params get filled with pseudo values, as the get replaced
2870+
//* by the openvz event plugin anyway with values from the template
2871+
$params['veid'] = 1;
2872+
$params['diskspace'] = 1;
2873+
$params['ram'] = 1;
2874+
$params['ram_burst'] = 1;
2875+
$params['cpu_units'] = 1;
2876+
$params['cpu_num'] = 1;
2877+
$params['cpu_limit'] = 1;
2878+
$params['io_priority'] = 1;
2879+
$params['nameserver'] = '8.8.8.8 8.8.4.4';
2880+
$params['create_dns'] = 'n';
2881+
$params['capability'] = '';
2882+
2883+
return $this->insertQuery('../vm/form/openvz_vm.tform.php',$client_id,$params,'vm:openvz_vm:on_after_insert');
2884+
}
2885+
2886+
//* Update openvz vm record
2887+
public function openvz_vm_update($session_id, $client_id, $vm_id, $params)
2888+
{
2889+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2890+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2891+
return false;
2892+
}
2893+
$affected_rows = $this->updateQuery('../vm/form/openvz_vm.tform.php',$client_id,$vm_id,$params,'vm:openvz_vm:on_after_update');
2894+
return $affected_rows;
2895+
}
2896+
2897+
//* Delete openvz vm record
2898+
public function openvz_vm_delete($session_id, $vm_id)
2899+
{
2900+
if(!$this->checkPerm($session_id, 'vm_openvz')) {
2901+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
2902+
return false;
2903+
}
2904+
$affected_rows = $this->deleteQuery('../vm/form/openvz_vm.tform.php',$vm_id,'vm:openvz_vm:on_after_delete');
2905+
return $affected_rows;
2906+
}
2907+
2908+
2909+
2910+
2911+
2912+
2913+
26142914
}
26152915
?>

interface/lib/plugins/vm_openvz_plugin.inc.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ private function applyTemplate() {
140140
private function makeOpenVZConfig() {
141141
global $app, $conf;
142142

143-
$vm = $app->tform->getDataRecord($this->id);
143+
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$this->id);
144144
$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$vm['template_id']);
145145
$burst_ram = $vm['ram_burst']*256;
146146
$guar_ram = $vm['ram']*256;
147147

148+
$app->load('tpl');
148149
$tpl = new tpl();
149-
$tpl->newTemplate('templates/openvz.conf.tpl');
150+
$tpl->newTemplate('../vm/templates/openvz.conf.tpl');
150151

151152
$onboot = ($vm['start_boot'] == 'y')?'yes':'no';
152153
$tpl->setVar('onboot',$onboot);
@@ -205,7 +206,7 @@ private function makeOpenVZConfig() {
205206
private function createDNS() {
206207
global $app, $conf;
207208

208-
$vm = $app->tform->getDataRecord($this->id);
209+
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$this->id);
209210

210211
if($vm['create_dns'] != 'y') return;
211212

interface/web/admin/form/remote_user.tform.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
$function_list['dns_srv_get,dns_srv_add,dns_srv_update,dns_srv_delete'] = 'DNS srv functions';
7373
$function_list['dns_txt_get,dns_txt_add,dns_txt_update,dns_txt_delete'] = 'DNS txt functions';
7474
$function_list['domains_domain_get,domains_domain_add,domains_domain_delete,domains_get_all_by_user'] = 'Domaintool functions';
75+
$function_list['vm_openvz'] = 'OpenVZ VM functions';
7576

7677
$form["title"] = "Remote user";
7778
$form["description"] = "";

interface/web/vm/templates/openvz.conf.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ORIGIN_SAMPLE="vps.basic"
4343
HOSTNAME="{tmpl_var name='hostname'}"
4444
IP_ADDRESS="{tmpl_var name='ip_address'}"
4545
NAMESERVER="{tmpl_var name='nameserver'}"
46-
MEMINFO="pages:256000"
46+
# alternative meminfo: "pages:256000"
47+
MEMINFO="privvmpages:1"
4748

4849
CAPABILITY="{tmpl_var name='capability'}"

0 commit comments

Comments
 (0)