Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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
a4657c 63         // Set additional IPs
FS 64         if (isset($this->dataRecord['additional_ip'])) {
65             $app->db->query("UPDATE openvz_ip SET vm_id = 0, additional = 'n' WHERE vm_id = ? AND additional='y'", $this->id);
66             foreach ($this->dataRecord['additional_ip'] as $idx => $rec) {
67                 $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'y' WHERE ip_address = ?", $this->id, $rec);
68             }
69         }
70
46598e 71         // Create the OpenVZ config file and store it in config field
T 72         $this->makeOpenVZConfig();
7fe908 73
46598e 74         // Create the DNS record
T 75         $this->createDNS();
7fe908 76
46598e 77     }
7fe908 78
46598e 79     /*
7fe908 80         Function that gets called after a vm was updated
46598e 81     */
7fe908 82     function openvz_vm_update($event_name, $page_form) {
MC 83         global $app, $conf;
84
94b44c 85         $this->id = $app->functions->intval($page_form->id);
46598e 86         $this->dataRecord = $page_form->dataRecord;
7fe908 87         $this->oldDataRecord = $page_form->oldDataRecord;
MC 88
46598e 89         // make sure that the record belongs to the clinet group and not the admin group when a admin inserts it
T 90         // also make sure that the user can not delete domain created by a admin
91         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 92             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
cc7a82 93             $app->db->query("UPDATE openvz_vm SET sys_groupid = ? WHERE vm_id = ?", $client_group_id, $this->id);
46598e 94         }
T 95         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 96             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
cc7a82 97             $app->db->query("UPDATE openvz_vm SET sys_groupid = ? WHERE vm_id = ?", $client_group_id, $this->id);
46598e 98         }
7fe908 99
46598e 100         if(isset($this->dataRecord["ostemplate_id"]) && $this->oldDataRecord["ostemplate_id"] != $this->dataRecord["ostemplate_id"]) {
T 101             $this->applyTemplate();
102         }
7fe908 103
46598e 104         // Set the IP address
168c51 105         if(isset($this->dataRecord['ip_address'])) {
a4657c 106             $app->db->query("UPDATE openvz_ip SET vm_id = 0 WHERE vm_id = ? AND additional='n'", $this->id);
FS 107             $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'n' WHERE ip_address = ?", $this->id, $this->dataRecord['ip_address']);
108         }
109
110         // Set additional IPs
111         if (isset($this->dataRecord['additional_ip'])) {
112             $app->db->query("UPDATE openvz_ip SET vm_id = 0, additional = 'n' WHERE (vm_id = ? AND additional='y')", $this->id);
113             foreach ($this->dataRecord['additional_ip'] as $idx => $rec) {
114                 $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'y' WHERE ip_address = ?", $this->id, $rec);
115             }
168c51 116         }
7fe908 117
46598e 118         // Create the OpenVZ config file and store it in config field
T 119         $this->makeOpenVZConfig();
7fe908 120
46598e 121         // Create the DNS record
7fe908 122         if((isset($this->dataRecord['hostname']) && $this->dataRecord['hostname'] != $this->oldDataRecord['hostname'])
MC 123             or (isset($this->dataRecord['create_dns']) && $this->dataRecord['create_dns'] != $this->oldDataRecord['create_dns'])) {
46598e 124             $this->createDNS();
T 125         }
7fe908 126
46598e 127     }
7fe908 128
deff20 129     function openvz_vm_delete($event_name, $page_form) {
7fe908 130         global $app, $conf;
MC 131
deff20 132         //* Free the IP address
61e08e 133         $tmp_rec = $app->db->queryAllRecords("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ?", $page_form->id);
FS 134         foreach ($tmp_rec as $tmp) {
135             $app->db->datalogUpdate('openvz_ip', array('vm_id' => 0), 'ip_address_id', $tmp['ip_address_id']);
136         }
deff20 137     }
7fe908 138
46598e 139     private function applyTemplate() {
T 140         global $app, $conf;
7fe908 141
cc7a82 142         $tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ?", $this->dataRecord["template_id"]);
7fe908 143
46598e 144         $sql = "UPDATE openvz_vm SET ";
cc7a82 145         $sql .= "diskspace = ?, ";
MC 146         $sql .= "ram = ?, ";
147         $sql .= "ram_burst = ?, ";
148         $sql .= "cpu_units = ?, ";
149         $sql .= "cpu_num = ?, ";
150         $sql .= "cpu_limit = ?, ";
151         $sql .= "io_priority = ?, ";
152         $sql .= "nameserver = ?, ";
153         $sql .= "create_dns = ?, ";
503bbb 154         $sql .= "capability = ?, ";
a3609a 155         $sql .= "features = ?, ";
FS 156         $sql .= "iptables = ? ";
ca0698 157         $sql .= "custom = ? ";
cc7a82 158         $sql .= "WHERE vm_id = ?";
ca0698 159         $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'], $tpl['features'], $tpl['iptables'], $tpl['custom'], $this->id);
7fe908 160
46598e 161     }
7fe908 162
46598e 163     private function makeOpenVZConfig() {
T 164         global $app, $conf;
7fe908 165
cc7a82 166         $vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ?",$app->functions->intval($this->id));
MC 167         $vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ?",$app->functions->intval($vm['template_id']));
46598e 168         $burst_ram = $vm['ram_burst']*256;
T 169         $guar_ram = $vm['ram']*256;
7fe908 170
077c27 171         $app->load('tpl');
46598e 172         $tpl = new tpl();
077c27 173         $tpl->newTemplate('../vm/templates/openvz.conf.tpl');
7fe908 174
46598e 175         $onboot = ($vm['start_boot'] == 'y')?'yes':'no';
7fe908 176         $tpl->setVar('onboot', $onboot);
MC 177
33ad8f 178         $tpl->setVar('bootorder', $vm['bootorder']);
7fe908 179         $tpl->setVar('kmemsize', $vm_template['kmemsize']);
MC 180         $tpl->setVar('lockedpages', $vm_template['lockedpages']);
411a6f 181         $tpl->setVar('privvmpages', $burst_ram.':'.$burst_ram*1.0625);
7fe908 182         $tpl->setVar('shmpages', $guar_ram.':'.$guar_ram);
MC 183         $tpl->setVar('numproc', $vm_template['numproc']);
184         $tpl->setVar('physpages', $vm_template['physpages']);
411a6f 185         $tpl->setVar('vmguarpages', $guar_ram.':unlimited');
7fe908 186         $tpl->setVar('oomguarpages', $guar_ram.':'.$guar_ram);
MC 187         $tpl->setVar('numtcpsock', $vm_template['numtcpsock']);
188         $tpl->setVar('numflock', $vm_template['numflock']);
189         $tpl->setVar('numpty', $vm_template['numpty']);
190         $tpl->setVar('numsiginfo', $vm_template['numsiginfo']);
191         $tpl->setVar('tcpsndbuf', $vm_template['tcpsndbuf']);
192         $tpl->setVar('tcprcvbuf', $vm_template['tcprcvbuf']);
193         $tpl->setVar('othersockbuf', $vm_template['othersockbuf']);
194         $tpl->setVar('dgramrcvbuf', $vm_template['dgramrcvbuf']);
195         $tpl->setVar('numothersock', $vm_template['numothersock']);
196         $tpl->setVar('dcachesize', $vm_template['dcachesize']);
197         $tpl->setVar('numfile', $vm_template['numfile']);
198         $tpl->setVar('avnumproc', $vm_template['avnumproc']);
199         $tpl->setVar('numiptent', $vm_template['numiptent']);
200         $tpl->setVar('swappages', $vm_template['swappages']);
201
46598e 202         $diskspace = $vm['diskspace']*1048576;
T 203         $diskinodes = $vm['diskspace']*524288;
7fe908 204
MC 205         $tpl->setVar('diskspace', $diskspace.":".$diskspace);
206         $tpl->setVar('diskinodes', $diskinodes.":".$diskinodes);
207         $tpl->setVar('io_priority', $vm['io_priority']);
208
209         $tpl->setVar('cpu_num', $vm['cpu_num']);
210         $tpl->setVar('cpu_units', $vm['cpu_units']);
211         $tpl->setVar('cpu_limit', $vm['cpu_limit']);
212
213         $hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']);
214
215         $tpl->setVar('hostname', $hostname);
a4657c 216
FS 217         $additional_ips = $app->db->queryAllRecords("SELECT * FROM openvz_ip WHERE vm_id = ?",$this->id);
218         if (isset($additional_ips)) {
219             $vm['ip_address']='';
220             foreach ($additional_ips as $ip) {
221                 $vm['ip_address'] .= " ".$ip['ip_address'];
222             }
223             $vm['ip_address'] = substr($vm['ip_address'],1);
224         }
225         $tpl->setVar('ip_address', $vm['ip_address']);
226
7fe908 227         $tpl->setVar('ip_address', $vm['ip_address']);
MC 228         $tpl->setVar('nameserver', $vm['nameserver']);
229         $tpl->setVar('capability', $vm['capability']);
503bbb 230         $tpl->setVar('features', $vm['features']);
a3609a 231         $tpl->setVar('iptables', $vm['iptables']);
7fe908 232
ca0698 233         $tpl->setVar('custom', $vm['custom']);
FS 234
cc7a82 235         $tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ?", $app->functions->intval($vm['ostemplate_id']));
7fe908 236         $tpl->setVar('ostemplate', $tmp['template_file']);
46598e 237         unset($tmp);
7fe908 238
cc7a82 239         $openvz_config = $tpl->grab();
MC 240         $app->db->query("UPDATE openvz_vm SET config = ? WHERE vm_id = ?", $openvz_config, $app->functions->intval($this->id));
7fe908 241
46598e 242         unset($tpl);
7fe908 243
46598e 244     }
7fe908 245
46598e 246     private function createDNS() {
T 247         global $app, $conf;
7fe908 248
cc7a82 249         $vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ?", $app->functions->intval($this->id));
7fe908 250
46598e 251         if($vm['create_dns'] != 'y') return;
7fe908 252
MC 253         $full_hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']);
254         $hostname_parts = explode('.', $full_hostname);
2af58c 255         $hostname = $hostname_parts[0];
46598e 256         unset($hostname_parts[0]);
2af58c 257         $zone = implode('.', $hostname_parts);
46598e 258         unset($hostname_parts);
7fe908 259
46598e 260         // Find the dns zone
cc7a82 261         $zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = ?", $zone);
MC 262         $rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = ? AND name = ?", $zone_rec['id'], $hostname);
7fe908 263
46598e 264         if($zone_rec['id'] > 0) {
2af58c 265             $ip_address = $vm['ip_address'];
94b44c 266             $sys_userid = $app->functions->intval($zone_rec['sys_userid']);
TB 267             $sys_groupid = $app->functions->intval($zone_rec['sys_groupid']);
268             $server_id = $app->functions->intval($zone_rec['server_id']);
269             $dns_soa_id = $app->functions->intval($zone_rec['id']);
7fe908 270
46598e 271             if($rr_rec['id'] > 0) {
T 272                 $app->uses('validate_dns');
3a11d2 273                 $app->db->datalogUpdate('dns_rr', array("data" => $ip_address), 'id', $app->functions->intval($rr_rec['id']));
46598e 274                 $serial = $app->validate_dns->increase_serial($zone_rec['serial']);
3a11d2 275                 $app->db->datalogUpdate('dns_soa', array("serial" => $serial), 'id', $app->functions->intval($zone_rec['id']));
46598e 276             } else {
3a11d2 277                 $insert_data = array(
MC 278                     "sys_userid" => $sys_userid,
279                     "sys_groupid" => $sys_groupid,
280                     "sys_perm_user" => 'riud',
281                     "sys_perm_group" => 'riud',
282                     "sys_perm_other" => '',
283                     "server_id" => $server_id,
284                     "zone" => $dns_soa_id,
285                     "name" => $hostname,
349272 286                     "type" => @(preg_match("/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $ip_address))?'A':'AAAA',
3a11d2 287                     "data" => $ip_address,
MC 288                     "aux" => '0',
289                     "ttl" => '3600',
290                     "active" => 'Y'
291                 );
46598e 292                 $dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
T 293             }
7fe908 294
46598e 295         }
T 296     }
297
7fe908 298 }