Till Brehm
2014-08-13 94b44c61a92adcc52feed671f24a09f1f564cc3a
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"]);
46598e 44             $app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
T 45         }
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"]);
46598e 48             $app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
T 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;
54         $app->db->query("UPDATE openvz_vm SET veid = ".$veid." WHERE vm_id = ".$this->id);
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
94b44c 61         $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$app->db->quote($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"]);
46598e 85             $app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
T 86         }
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"]);
46598e 89             $app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
T 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
94b44c 97         if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$app->db->quote($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
94b44c 114         $tmp = $app->db->queryOneRecord("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ".$app->functions->intval($page_form->id));
deff20 115         $app->db->datalogUpdate('openvz_ip', 'vm_id = 0', 'ip_address_id', $tmp['ip_address_id']);
T 116         unset($tmp);
7fe908 117
deff20 118     }
7fe908 119
46598e 120     private function applyTemplate() {
T 121         global $app, $conf;
7fe908 122
94b44c 123         $tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$app->functions->intval($this->dataRecord["template_id"]));
7fe908 124
46598e 125         $sql = "UPDATE openvz_vm SET ";
94b44c 126         $sql .= "diskspace = '".$app->db->quote($tpl['diskspace'])."', ";
TB 127         $sql .= "ram = '".$app->db->quote($tpl['ram'])."', ";
128         $sql .= "ram_burst = '".$app->db->quote($tpl['ram_burst'])."', ";
129         $sql .= "cpu_units = '".$app->db->quote($tpl['cpu_units'])."', ";
130         $sql .= "cpu_num = '".$app->db->quote($tpl['cpu_num'])."', ";
131         $sql .= "cpu_limit = '".$app->db->quote($tpl['cpu_limit'])."', ";
132         $sql .= "io_priority = '".$app->db->quote($tpl['io_priority'])."', ";
133         $sql .= "nameserver = '".$app->db->quote($tpl['nameserver'])."', ";
134         $sql .= "create_dns = '".$app->db->quote($tpl['create_dns'])."', ";
135         $sql .= "capability = '".$app->db->quote($tpl['capability'])."' ";
136         $sql .= "WHERE vm_id = ".$app->functions->intval($this->id);
46598e 137         $app->db->query($sql);
7fe908 138
46598e 139     }
7fe908 140
46598e 141     private function makeOpenVZConfig() {
T 142         global $app, $conf;
7fe908 143
94b44c 144         $vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$app->functions->intval($this->id));
TB 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
94b44c 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
46598e 201         $openvz_config = $app->db->quote($tpl->grab());
94b44c 202         $app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$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
94b44c 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);
94b44c 217         $hostname = $app->db->quote($hostname_parts[0]);
46598e 218         unset($hostname_parts[0]);
94b44c 219         $zone = $app->db->quote((implode('.', $hostname_parts));
46598e 220         unset($hostname_parts);
7fe908 221
46598e 222         // Find the dns zone
94b44c 223         $zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '".$app->db->quote($zone).".'");
TB 224         $rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$app->functions->intval($zone_rec['id'])."' AND name = '".$app->db->quote($hostname)."'");
7fe908 225
46598e 226         if($zone_rec['id'] > 0) {
94b44c 227             $ip_address = $app->db->quote($vm['ip_address']);
TB 228             $sys_userid = $app->functions->intval($zone_rec['sys_userid']);
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');
94b44c 235                 $app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $app->functions->intval($rr_rec['id']));
46598e 236                 $serial = $app->validate_dns->increase_serial($zone_rec['serial']);
94b44c 237                 $app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $app->functions->intval($zone_rec['id']));
46598e 238             } else {
7fe908 239                 $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
46598e 240                 ('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$hostname', 'A', '$ip_address', '0', '3600', 'Y')";
T 241                 $dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
242             }
7fe908 243
46598e 244         }
T 245     }
246
7fe908 247 }