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