Skip to content

Commit 46598e0

Browse files
committed
Added VM actions start, stop, restart and creating of ostemplates.
1 parent bfed925 commit 46598e0

File tree

9 files changed

+495
-209
lines changed

9 files changed

+495
-209
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'a
5252
unset($path_parts);
5353
}
5454

55-
$header = "From: $from\nReply-To: $from\n";
55+
$header = "Return-Path: $form\nFrom: $from\nReply-To: $from\n";
5656
$header .= "MIME-Version: 1.0\n";
5757
$header .= "Content-Type: multipart/mixed; boundary=$uid\n";
5858

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
/**
3+
* sites_web_domain_plugin plugin
4+
*
5+
* @author Till Brehm, projektfarm GmbH
6+
*/
7+
8+
class vm_openvz_plugin {
9+
10+
var $plugin_name = 'vm_openvz_plugin';
11+
var $class_name = 'vm_openvz_plugin';
12+
var $id = 0;
13+
var $dataRecord = array();
14+
var $oldDataRecord = array();
15+
16+
17+
/*
18+
This function is called when the plugin is loaded
19+
*/
20+
function onLoad() {
21+
global $app;
22+
23+
//* Register for events
24+
$app->plugin->registerEvent('vm:openvz_vm:on_after_insert','vm_openvz_plugin','openvz_vm_insert');
25+
$app->plugin->registerEvent('vm:openvz_vm:on_after_update','vm_openvz_plugin','openvz_vm_update');
26+
}
27+
28+
/*
29+
Function that gets called after a new vm was inserted
30+
*/
31+
function openvz_vm_insert($event_name, $page_form) {
32+
global $app, $conf;
33+
34+
$this->id = $page_form->id;
35+
$this->dataRecord = $page_form->dataRecord;
36+
$this->oldDataRecord = $page_form->oldDataRecord;
37+
38+
// make sure that the record belongs to the clinet group and not the admin group when admin inserts it
39+
// also make sure that the user can not delete domain created by a admin
40+
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
41+
$client_group_id = intval($this->dataRecord["client_group_id"]);
42+
$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
43+
}
44+
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
45+
$client_group_id = intval($this->dataRecord["client_group_id"]);
46+
$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
47+
}
48+
49+
// Set the VEID
50+
$tmp = $app->db->queryOneRecord('SELECT MAX(veid) + 1 as newveid FROM openvz_vm');
51+
$veid = ($tmp['newveid'] > 100)?$tmp['newveid']:101;
52+
$app->db->query("UPDATE openvz_vm SET veid = ".$veid." WHERE vm_id = ".$this->id);
53+
unset($tmp);
54+
55+
// Apply template values to the advanced tab settings
56+
$this->applyTemplate();
57+
58+
// Set the IP address
59+
$app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
60+
61+
// Create the OpenVZ config file and store it in config field
62+
$this->makeOpenVZConfig();
63+
64+
// Create the DNS record
65+
$this->createDNS();
66+
67+
}
68+
69+
/*
70+
Function that gets called after a vm was updated
71+
*/
72+
function openvz_vm_update($event_name, $page_form) {
73+
global $app, $conf;
74+
75+
$this->id = $page_form->id;
76+
$this->dataRecord = $page_form->dataRecord;
77+
$this->oldDataRecord = $page_form->oldDataRecord;
78+
79+
// make sure that the record belongs to the clinet group and not the admin group when a admin inserts it
80+
// also make sure that the user can not delete domain created by a admin
81+
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
82+
$client_group_id = intval($this->dataRecord["client_group_id"]);
83+
$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
84+
}
85+
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
86+
$client_group_id = intval($this->dataRecord["client_group_id"]);
87+
$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
88+
}
89+
90+
if(isset($this->dataRecord["ostemplate_id"]) && $this->oldDataRecord["ostemplate_id"] != $this->dataRecord["ostemplate_id"]) {
91+
$this->applyTemplate();
92+
}
93+
94+
// Set the IP address
95+
if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
96+
97+
// Create the OpenVZ config file and store it in config field
98+
$this->makeOpenVZConfig();
99+
100+
// Create the DNS record
101+
if((isset($this->dataRecord['hostname']) && $this->dataRecord['hostname'] != $this->oldDataRecord['hostname'])
102+
or (isset($this->dataRecord['create_dns']) && $this->dataRecord['create_dns'] != $this->oldDataRecord['create_dns'])) {
103+
$this->createDNS();
104+
}
105+
106+
}
107+
108+
private function applyTemplate() {
109+
global $app, $conf;
110+
111+
$tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$this->dataRecord["template_id"]);
112+
113+
$sql = "UPDATE openvz_vm SET ";
114+
$sql .= "diskspace = '".$tpl['diskspace']."', ";
115+
$sql .= "ram = '".$tpl['ram']."', ";
116+
$sql .= "ram_burst = '".$tpl['ram_burst']."', ";
117+
$sql .= "cpu_units = '".$tpl['cpu_units']."', ";
118+
$sql .= "cpu_num = '".$tpl['cpu_num']."', ";
119+
$sql .= "cpu_limit = '".$tpl['cpu_limit']."', ";
120+
$sql .= "io_priority = '".$tpl['io_priority']."', ";
121+
$sql .= "nameserver = '".$tpl['nameserver']."', ";
122+
$sql .= "create_dns = '".$tpl['create_dns']."', ";
123+
$sql .= "capability = '".$tpl['capability']."' ";
124+
$sql .= "WHERE vm_id = ".$this->id;
125+
$app->db->query($sql);
126+
127+
}
128+
129+
private function makeOpenVZConfig() {
130+
global $app, $conf;
131+
132+
$vm = $app->tform->getDataRecord($this->id);
133+
$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$vm['template_id']);
134+
$burst_ram = $vm['ram_burst']*256;
135+
$guar_ram = $vm['ram']*256;
136+
137+
$tpl = new tpl();
138+
$tpl->newTemplate('templates/openvz.conf.tpl');
139+
140+
$onboot = ($vm['start_boot'] == 'y')?'yes':'no';
141+
$tpl->setVar('onboot',$onboot);
142+
143+
$tpl->setVar('kmemsize',$vm_template['kmemsize']);
144+
$tpl->setVar('lockedpages',$vm_template['lockedpages']);
145+
$tpl->setVar('privvmpages',$burst_ram.':'.$burst_ram);
146+
$tpl->setVar('shmpages',$guar_ram.':'.$guar_ram);
147+
$tpl->setVar('numproc',$vm_template['numproc']);
148+
$tpl->setVar('physpages',$vm_template['physpages']);
149+
$tpl->setVar('vmguarpages',$guar_ram.':'.$guar_ram);
150+
$tpl->setVar('oomguarpages',$guar_ram.':'.$guar_ram);
151+
$tpl->setVar('numtcpsock',$vm_template['numtcpsock']);
152+
$tpl->setVar('numflock',$vm_template['numflock']);
153+
$tpl->setVar('numpty',$vm_template['numpty']);
154+
$tpl->setVar('numsiginfo',$vm_template['numsiginfo']);
155+
$tpl->setVar('tcpsndbuf',$vm_template['tcpsndbuf']);
156+
$tpl->setVar('tcprcvbuf',$vm_template['tcprcvbuf']);
157+
$tpl->setVar('othersockbuf',$vm_template['othersockbuf']);
158+
$tpl->setVar('dgramrcvbuf',$vm_template['dgramrcvbuf']);
159+
$tpl->setVar('numothersock',$vm_template['numothersock']);
160+
$tpl->setVar('dcachesize',$vm_template['dcachesize']);
161+
$tpl->setVar('numfile',$vm_template['numfile']);
162+
$tpl->setVar('avnumproc',$vm_template['avnumproc']);
163+
$tpl->setVar('numiptent',$vm_template['numiptent']);
164+
165+
$diskspace = $vm['diskspace']*1048576;
166+
$diskinodes = $vm['diskspace']*524288;
167+
168+
$tpl->setVar('diskspace',$diskspace.":".$diskspace);
169+
$tpl->setVar('diskinodes',$diskinodes.":".$diskinodes);
170+
$tpl->setVar('io_priority',$vm['io_priority']);
171+
172+
$tpl->setVar('cpu_num',$vm['cpu_num']);
173+
$tpl->setVar('cpu_units',$vm['cpu_units']);
174+
$tpl->setVar('cpu_limit',$vm['cpu_limit']);
175+
176+
$hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
177+
178+
$tpl->setVar('hostname',$hostname);
179+
$tpl->setVar('ip_address',$vm['ip_address']);
180+
$tpl->setVar('nameserver',$vm['nameserver']);
181+
$tpl->setVar('capability',$vm['capability']);
182+
183+
$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ".$vm['ostemplate_id']);
184+
$tpl->setVar('ostemplate',$tmp['template_file']);
185+
unset($tmp);
186+
187+
$openvz_config = $app->db->quote($tpl->grab());
188+
$app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$this->id);
189+
190+
unset($tpl);
191+
192+
}
193+
194+
private function createDNS() {
195+
global $app, $conf;
196+
197+
$vm = $app->tform->getDataRecord($this->id);
198+
199+
if($vm['create_dns'] != 'y') return;
200+
201+
$full_hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
202+
$hostname_parts = explode('.',$full_hostname);
203+
$hostname = $hostname_parts[0];
204+
unset($hostname_parts[0]);
205+
$zone = implode('.',$hostname_parts);
206+
unset($hostname_parts);
207+
208+
// Find the dns zone
209+
$zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '$zone.'");
210+
$rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$zone_rec['id']."' AND name = '$hostname'");
211+
212+
if($zone_rec['id'] > 0) {
213+
$ip_address = $vm['ip_address'];
214+
$sys_userid = $zone_rec['sys_userid'];
215+
$sys_groupid = $zone_rec['sys_groupid'];
216+
$server_id = $zone_rec['server_id'];
217+
$dns_soa_id = $zone_rec['id'];
218+
219+
if($rr_rec['id'] > 0) {
220+
$app->uses('validate_dns');
221+
$app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $rr_rec['id']);
222+
$serial = $app->validate_dns->increase_serial($zone_rec['serial']);
223+
$app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $zone_rec['id']);
224+
} else {
225+
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
226+
('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$hostname', 'A', '$ip_address', '0', '3600', 'Y')";
227+
$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
228+
}
229+
230+
}
231+
}
232+
233+
}

interface/web/tools/templates/user_settings.htm

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ <h2><tmpl_var name="list_head_txt"></h2>
2424
{tmpl_var name='language'}
2525
</select>
2626
</div>
27-
<div class="ctrlHolder">
28-
<label for="id_rsa">{tmpl_var name='id_rsa_txt'}</label>
29-
<textarea name="id_rsa" id="id_rsa" readonly rows='10' cols='30'>{tmpl_var name='id_rsa'}</textarea>
30-
</div>
31-
<div class="ctrlHolder">
32-
<label for="ssh_rsa">{tmpl_var name='ssh_rsa_txt'}</label>
33-
<input name="ssh_rsa" id="ssh_rsa" value="{tmpl_var name='ssh_rsa'}" size="30" maxlength="600" type="text" class="textInput" />
34-
</div>
3527

3628
<input type="hidden" name="id" value="{tmpl_var name='id'}">
3729

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
$wb['head_txt'] = 'Virtual server actions for VM:';
3+
$wb['start_txt'] = 'Start virtual server';
4+
$wb['stop_txt'] = 'Stop virtual server';
5+
$wb['restart_txt'] = 'Restart virtual server';
6+
$wb['ostemplate_txt'] = 'Create OSTemplate';
7+
$wb['ostemplate_desc_txt'] = '(example: debian-6.0-i386-custom)';
8+
$wb['btn_save_txt'] = 'Execute selected action';
9+
$wb['btn_cancel_txt'] = 'Cancel';
10+
$wb['start_exec_txt'] = 'Start command has been sent to the VM host server. It may take a minute until the VM is started.';
11+
$wb['stop_exec_txt'] = 'Stop command has been sent to the VM host server. It may take a minute until the VM is stopped.';
12+
$wb['restart_exec_txt'] = 'Restart command has been sent to the VM host server. It may take a minute until the VM is restarted.';
13+
$wb['ostemplate_name_error'] = 'The OSTemplate name conatains unallowed characters.';
14+
$wb['ostemplate_name_unique_error'] = 'There is already a OSTemplate with that name.';
15+
$wb['ostemplate_exec_txt'] = 'The command to create a OSTemplate has been sent to the host server. It will take several minutes until the OSTemplate has been created.';
16+
17+
?>

0 commit comments

Comments
 (0)