Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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 = ?, ";
503bbb 135         $sql .= "capability = ?, ";
a3609a 136         $sql .= "features = ?, ";
FS 137         $sql .= "iptables = ? ";
cc7a82 138         $sql .= "WHERE vm_id = ?";
a3609a 139         $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'], $this->id);
7fe908 140
46598e 141     }
7fe908 142
46598e 143     private function makeOpenVZConfig() {
T 144         global $app, $conf;
7fe908 145
cc7a82 146         $vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ?",$app->functions->intval($this->id));
MC 147         $vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ?",$app->functions->intval($vm['template_id']));
46598e 148         $burst_ram = $vm['ram_burst']*256;
T 149         $guar_ram = $vm['ram']*256;
7fe908 150
077c27 151         $app->load('tpl');
46598e 152         $tpl = new tpl();
077c27 153         $tpl->newTemplate('../vm/templates/openvz.conf.tpl');
7fe908 154
46598e 155         $onboot = ($vm['start_boot'] == 'y')?'yes':'no';
7fe908 156         $tpl->setVar('onboot', $onboot);
MC 157
158         $tpl->setVar('kmemsize', $vm_template['kmemsize']);
159         $tpl->setVar('lockedpages', $vm_template['lockedpages']);
160         $tpl->setVar('privvmpages', $burst_ram.':'.$burst_ram);
161         $tpl->setVar('shmpages', $guar_ram.':'.$guar_ram);
162         $tpl->setVar('numproc', $vm_template['numproc']);
163         $tpl->setVar('physpages', $vm_template['physpages']);
164         $tpl->setVar('vmguarpages', $guar_ram.':'.$guar_ram);
165         $tpl->setVar('oomguarpages', $guar_ram.':'.$guar_ram);
166         $tpl->setVar('numtcpsock', $vm_template['numtcpsock']);
167         $tpl->setVar('numflock', $vm_template['numflock']);
168         $tpl->setVar('numpty', $vm_template['numpty']);
169         $tpl->setVar('numsiginfo', $vm_template['numsiginfo']);
170         $tpl->setVar('tcpsndbuf', $vm_template['tcpsndbuf']);
171         $tpl->setVar('tcprcvbuf', $vm_template['tcprcvbuf']);
172         $tpl->setVar('othersockbuf', $vm_template['othersockbuf']);
173         $tpl->setVar('dgramrcvbuf', $vm_template['dgramrcvbuf']);
174         $tpl->setVar('numothersock', $vm_template['numothersock']);
175         $tpl->setVar('dcachesize', $vm_template['dcachesize']);
176         $tpl->setVar('numfile', $vm_template['numfile']);
177         $tpl->setVar('avnumproc', $vm_template['avnumproc']);
178         $tpl->setVar('numiptent', $vm_template['numiptent']);
179         $tpl->setVar('swappages', $vm_template['swappages']);
180
46598e 181         $diskspace = $vm['diskspace']*1048576;
T 182         $diskinodes = $vm['diskspace']*524288;
7fe908 183
MC 184         $tpl->setVar('diskspace', $diskspace.":".$diskspace);
185         $tpl->setVar('diskinodes', $diskinodes.":".$diskinodes);
186         $tpl->setVar('io_priority', $vm['io_priority']);
187
188         $tpl->setVar('cpu_num', $vm['cpu_num']);
189         $tpl->setVar('cpu_units', $vm['cpu_units']);
190         $tpl->setVar('cpu_limit', $vm['cpu_limit']);
191
192         $hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']);
193
194         $tpl->setVar('hostname', $hostname);
195         $tpl->setVar('ip_address', $vm['ip_address']);
196         $tpl->setVar('nameserver', $vm['nameserver']);
197         $tpl->setVar('capability', $vm['capability']);
503bbb 198         $tpl->setVar('features', $vm['features']);
a3609a 199         $tpl->setVar('iptables', $vm['iptables']);
7fe908 200
cc7a82 201         $tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ?", $app->functions->intval($vm['ostemplate_id']));
7fe908 202         $tpl->setVar('ostemplate', $tmp['template_file']);
46598e 203         unset($tmp);
7fe908 204
cc7a82 205         $openvz_config = $tpl->grab();
MC 206         $app->db->query("UPDATE openvz_vm SET config = ? WHERE vm_id = ?", $openvz_config, $app->functions->intval($this->id));
7fe908 207
46598e 208         unset($tpl);
7fe908 209
46598e 210     }
7fe908 211
46598e 212     private function createDNS() {
T 213         global $app, $conf;
7fe908 214
cc7a82 215         $vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ?", $app->functions->intval($this->id));
7fe908 216
46598e 217         if($vm['create_dns'] != 'y') return;
7fe908 218
MC 219         $full_hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']);
220         $hostname_parts = explode('.', $full_hostname);
2af58c 221         $hostname = $hostname_parts[0];
46598e 222         unset($hostname_parts[0]);
2af58c 223         $zone = implode('.', $hostname_parts);
46598e 224         unset($hostname_parts);
7fe908 225
46598e 226         // Find the dns zone
cc7a82 227         $zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = ?", $zone);
MC 228         $rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = ? AND name = ?", $zone_rec['id'], $hostname);
7fe908 229
46598e 230         if($zone_rec['id'] > 0) {
2af58c 231             $ip_address = $vm['ip_address'];
94b44c 232             $sys_userid = $app->functions->intval($zone_rec['sys_userid']);
TB 233             $sys_groupid = $app->functions->intval($zone_rec['sys_groupid']);
234             $server_id = $app->functions->intval($zone_rec['server_id']);
235             $dns_soa_id = $app->functions->intval($zone_rec['id']);
7fe908 236
46598e 237             if($rr_rec['id'] > 0) {
T 238                 $app->uses('validate_dns');
3a11d2 239                 $app->db->datalogUpdate('dns_rr', array("data" => $ip_address), 'id', $app->functions->intval($rr_rec['id']));
46598e 240                 $serial = $app->validate_dns->increase_serial($zone_rec['serial']);
3a11d2 241                 $app->db->datalogUpdate('dns_soa', array("serial" => $serial), 'id', $app->functions->intval($zone_rec['id']));
46598e 242             } else {
3a11d2 243                 $insert_data = array(
MC 244                     "sys_userid" => $sys_userid,
245                     "sys_groupid" => $sys_groupid,
246                     "sys_perm_user" => 'riud',
247                     "sys_perm_group" => 'riud',
248                     "sys_perm_other" => '',
249                     "server_id" => $server_id,
250                     "zone" => $dns_soa_id,
251                     "name" => $hostname,
252                     "type" => 'A',
253                     "data" => $ip_address,
254                     "aux" => '0',
255                     "ttl" => '3600',
256                     "active" => 'Y'
257                 );
46598e 258                 $dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
T 259             }
7fe908 260
46598e 261         }
T 262     }
263
7fe908 264 }