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