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