]>
Commit | Line | Data |
---|---|---|
fcb64fe4 DM |
1 | // Some configuration values are complex strings - |
2 | // so we need parsers/generators for them. | |
3 | ||
4 | Ext.define('PVE.Parser', { statics: { | |
5 | ||
6 | // this class only contains static functions | |
7 | ||
4c1c0d5d EK |
8 | parseBoolean: function(value, default_value) { |
9 | if (!Ext.isDefined(value)) | |
10 | return default_value; | |
11 | value = value.toLowerCase(); | |
12 | return value === 1 || value === '1' || | |
13 | value === 'on' || | |
14 | value === 'yes' || | |
15 | value === 'true'; | |
16 | }, | |
17 | ||
fcb64fe4 DM |
18 | parseQemuNetwork: function(key, value) { |
19 | if (!(key && value)) { | |
20 | return; | |
21 | } | |
22 | ||
23 | var res = {}; | |
24 | ||
25 | var errors = false; | |
26 | Ext.Array.each(value.split(','), function(p) { | |
27 | if (!p || p.match(/^\s*$/)) { | |
28 | return; // continue | |
29 | } | |
30 | ||
31 | var match_res; | |
32 | ||
33 | if ((match_res = p.match(/^(ne2k_pci|e1000|e1000-82540em|e1000-82544gc|e1000-82545em|vmxnet3|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i)) !== null) { | |
34 | res.model = match_res[1].toLowerCase(); | |
35 | if (match_res[3]) { | |
36 | res.macaddr = match_res[3]; | |
37 | } | |
38 | } else if ((match_res = p.match(/^bridge=(\S+)$/)) !== null) { | |
39 | res.bridge = match_res[1]; | |
40 | } else if ((match_res = p.match(/^rate=(\d+(\.\d+)?)$/)) !== null) { | |
41 | res.rate = match_res[1]; | |
42 | } else if ((match_res = p.match(/^tag=(\d+(\.\d+)?)$/)) !== null) { | |
43 | res.tag = match_res[1]; | |
44 | } else if ((match_res = p.match(/^firewall=(\d+)$/)) !== null) { | |
45 | res.firewall = match_res[1]; | |
46 | } else if ((match_res = p.match(/^link_down=(\d+)$/)) !== null) { | |
47 | res.disconnect = match_res[1]; | |
48 | } else if ((match_res = p.match(/^queues=(\d+)$/)) !== null) { | |
49 | res.queues = match_res[1]; | |
50 | } else { | |
51 | errors = true; | |
52 | return false; // break | |
53 | } | |
54 | }); | |
55 | ||
56 | if (errors || !res.model) { | |
57 | return; | |
58 | } | |
59 | ||
60 | return res; | |
61 | }, | |
62 | ||
63 | printQemuNetwork: function(net) { | |
64 | ||
65 | var netstr = net.model; | |
66 | if (net.macaddr) { | |
67 | netstr += "=" + net.macaddr; | |
68 | } | |
69 | if (net.bridge) { | |
70 | netstr += ",bridge=" + net.bridge; | |
71 | if (net.tag) { | |
72 | netstr += ",tag=" + net.tag; | |
73 | } | |
74 | if (net.firewall) { | |
75 | netstr += ",firewall=" + net.firewall; | |
76 | } | |
77 | } | |
78 | if (net.rate) { | |
79 | netstr += ",rate=" + net.rate; | |
80 | } | |
81 | if (net.queues) { | |
82 | netstr += ",queues=" + net.queues; | |
83 | } | |
84 | if (net.disconnect) { | |
85 | netstr += ",link_down=" + net.disconnect; | |
86 | } | |
87 | return netstr; | |
88 | }, | |
89 | ||
90 | parseQemuDrive: function(key, value) { | |
91 | if (!(key && value)) { | |
92 | return; | |
93 | } | |
94 | ||
95 | var res = {}; | |
96 | ||
97 | var match_res = key.match(/^([a-z]+)(\d+)$/); | |
98 | if (!match_res) { | |
99 | return; | |
100 | } | |
101 | res['interface'] = match_res[1]; | |
102 | res.index = match_res[2]; | |
103 | ||
104 | var errors = false; | |
105 | Ext.Array.each(value.split(','), function(p) { | |
106 | if (!p || p.match(/^\s*$/)) { | |
107 | return; // continue | |
108 | } | |
109 | var match_res = p.match(/^([a-z_]+)=(\S+)$/); | |
110 | if (!match_res) { | |
111 | if (!p.match(/\=/)) { | |
112 | res.file = p; | |
113 | return; // continue | |
114 | } | |
115 | errors = true; | |
116 | return false; // break | |
117 | } | |
118 | var k = match_res[1]; | |
119 | if (k === 'volume') { | |
120 | k = 'file'; | |
121 | } | |
122 | ||
123 | if (Ext.isDefined(res[k])) { | |
124 | errors = true; | |
125 | return false; // break | |
126 | } | |
127 | ||
128 | var v = match_res[2]; | |
129 | ||
130 | if (k === 'cache' && v === 'off') { | |
131 | v = 'none'; | |
132 | } | |
133 | ||
134 | res[k] = v; | |
135 | }); | |
136 | ||
137 | if (errors || !res.file) { | |
138 | return; | |
139 | } | |
140 | ||
141 | return res; | |
142 | }, | |
143 | ||
144 | printQemuDrive: function(drive) { | |
145 | ||
146 | var drivestr = drive.file; | |
147 | ||
148 | Ext.Object.each(drive, function(key, value) { | |
149 | if (!Ext.isDefined(value) || key === 'file' || | |
150 | key === 'index' || key === 'interface') { | |
151 | return; // continue | |
152 | } | |
153 | drivestr += ',' + key + '=' + value; | |
154 | }); | |
155 | ||
156 | return drivestr; | |
157 | }, | |
158 | ||
159 | parseOpenVZNetIf: function(value) { | |
160 | if (!value) { | |
161 | return; | |
162 | } | |
163 | ||
164 | var res = {}; | |
165 | ||
166 | var errors = false; | |
167 | Ext.Array.each(value.split(';'), function(item) { | |
168 | if (!item || item.match(/^\s*$/)) { | |
169 | return; // continue | |
170 | } | |
171 | ||
172 | var data = {}; | |
173 | Ext.Array.each(item.split(','), function(p) { | |
174 | if (!p || p.match(/^\s*$/)) { | |
175 | return; // continue | |
176 | } | |
177 | var match_res = p.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/); | |
178 | if (!match_res) { | |
179 | errors = true; | |
180 | return false; // break | |
181 | } | |
182 | if (match_res[1] === 'bridge'){ | |
183 | var bridgevlanf = match_res[2]; | |
184 | var bridge_res = bridgevlanf.match(/^(vmbr(\d+))(v(\d+))?(f)?$/); | |
185 | if (!bridge_res) { | |
186 | errors = true; | |
187 | return false; // break | |
188 | } | |
189 | data['bridge'] = bridge_res[1]; | |
190 | data['tag'] = bridge_res[4]; | |
191 | data['firewall'] = bridge_res[5] ? 1 : 0; | |
192 | } else { | |
193 | data[match_res[1]] = match_res[2]; | |
194 | } | |
195 | }); | |
196 | ||
197 | if (errors || !data.ifname) { | |
198 | errors = true; | |
199 | return false; // break | |
200 | } | |
201 | ||
202 | data.raw = item; | |
203 | ||
204 | res[data.ifname] = data; | |
205 | }); | |
206 | ||
207 | return errors ? undefined: res; | |
208 | }, | |
209 | ||
210 | printOpenVZNetIf: function(netif) { | |
211 | var netarray = []; | |
212 | ||
213 | Ext.Object.each(netif, function(iface, data) { | |
214 | var tmparray = []; | |
215 | Ext.Array.each(['ifname', 'mac', 'bridge', 'host_ifname' , 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key) { | |
216 | var value = data[key]; | |
217 | if (key === 'bridge'){ | |
218 | if(data['tag']){ | |
219 | value = value + 'v' + data['tag']; | |
220 | } | |
221 | if (data['firewall']){ | |
222 | value = value + 'f'; | |
223 | } | |
224 | } | |
225 | if (value) { | |
226 | tmparray.push(key + '=' + value); | |
227 | } | |
228 | ||
229 | }); | |
230 | netarray.push(tmparray.join(',')); | |
231 | }); | |
232 | ||
233 | return netarray.join(';'); | |
234 | }, | |
235 | ||
236 | parseLxcNetwork: function(value) { | |
237 | if (!value) { | |
238 | return; | |
239 | } | |
240 | ||
241 | var data = {}; | |
242 | Ext.Array.each(value.split(','), function(p) { | |
243 | if (!p || p.match(/^\s*$/)) { | |
244 | return; // continue | |
245 | } | |
246 | var match_res = p.match(/^(bridge|hwaddr|mtu|name|ip|ip6|gw|gw6|firewall|tag)=(\S+)$/); | |
247 | if (!match_res) { | |
248 | // todo: simply ignore errors ? | |
249 | return; // continue | |
250 | } | |
251 | data[match_res[1]] = match_res[2]; | |
252 | }); | |
253 | ||
254 | return data; | |
255 | }, | |
256 | ||
257 | printLxcNetwork: function(data) { | |
258 | var tmparray = []; | |
259 | Ext.Array.each(['bridge', 'hwaddr', 'mtu', 'name', 'ip', | |
260 | 'gw', 'ip6', 'gw6', 'firewall', 'tag'], function(key) { | |
261 | var value = data[key]; | |
262 | if (value) { | |
263 | tmparray.push(key + '=' + value); | |
264 | } | |
265 | }); | |
266 | ||
267 | return tmparray.join(','); | |
268 | }, | |
269 | ||
4c1c0d5d EK |
270 | parseLxcMountPoint: function(value) { |
271 | if (!value) { | |
272 | return; | |
273 | } | |
274 | ||
275 | var res = {}; | |
276 | ||
277 | var errors = false; | |
278 | Ext.Array.each(value.split(','), function(p) { | |
279 | if (!p || p.match(/^\s*$/)) { | |
280 | return; // continue | |
281 | } | |
282 | var match_res = p.match(/^([a-z_]+)=(\S+)$/); | |
283 | if (!match_res) { | |
284 | if (!p.match(/\=/)) { | |
285 | res.file = p; | |
286 | return; // continue | |
287 | } | |
288 | errors = true; | |
289 | return false; // break | |
290 | } | |
291 | var k = match_res[1]; | |
292 | if (k === 'volume') { | |
293 | k = 'file'; | |
294 | } | |
295 | ||
296 | if (Ext.isDefined(res[k])) { | |
297 | errors = true; | |
298 | return false; // break | |
299 | } | |
300 | ||
301 | var v = match_res[2]; | |
302 | ||
303 | res[k] = v; | |
304 | }); | |
305 | ||
306 | if (errors || !res.file) { | |
307 | return; | |
308 | } | |
309 | ||
310 | var m = res.file.match(/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):/); | |
311 | if (m) { | |
312 | res.storage = m[1]; | |
313 | res.type = 'volume'; | |
314 | } else if (res.file.match(/^\/dev\//)) { | |
315 | res.type = 'device'; | |
316 | } else { | |
317 | res.type = 'bind'; | |
318 | } | |
319 | ||
320 | return res; | |
321 | }, | |
322 | ||
323 | printLxcMountPoint: function(mp) { | |
324 | var drivestr = mp.file; | |
325 | ||
326 | Ext.Object.each(mp, function(key, value) { | |
327 | if (!Ext.isDefined(value) || key === 'file' || | |
328 | key === 'type' || key === 'storage') { | |
329 | return; // continue | |
330 | } | |
331 | drivestr += ',' + key + '=' + value; | |
332 | }); | |
333 | ||
334 | return drivestr; | |
335 | }, | |
336 | ||
fcb64fe4 DM |
337 | parseStartup: function(value) { |
338 | if (value === undefined) { | |
339 | return; | |
340 | } | |
341 | ||
342 | var res = {}; | |
343 | ||
344 | var errors = false; | |
345 | Ext.Array.each(value.split(','), function(p) { | |
346 | if (!p || p.match(/^\s*$/)) { | |
347 | return; // continue | |
348 | } | |
349 | ||
350 | var match_res; | |
351 | ||
352 | if ((match_res = p.match(/^(order)?=(\d+)$/)) !== null) { | |
353 | res.order = match_res[2]; | |
354 | } else if ((match_res = p.match(/^up=(\d+)$/)) !== null) { | |
355 | res.up = match_res[1]; | |
356 | } else if ((match_res = p.match(/^down=(\d+)$/)) !== null) { | |
357 | res.down = match_res[1]; | |
358 | } else { | |
359 | errors = true; | |
360 | return false; // break | |
361 | } | |
362 | }); | |
363 | ||
364 | if (errors) { | |
365 | return; | |
366 | } | |
367 | ||
368 | return res; | |
369 | }, | |
370 | ||
371 | printStartup: function(startup) { | |
372 | var arr = []; | |
373 | if (startup.order !== undefined && startup.order !== '') { | |
374 | arr.push('order=' + startup.order); | |
375 | } | |
376 | if (startup.up !== undefined && startup.up !== '') { | |
377 | arr.push('up=' + startup.up); | |
378 | } | |
379 | if (startup.down !== undefined && startup.down !== '') { | |
380 | arr.push('down=' + startup.down); | |
381 | } | |
382 | ||
383 | return arr.join(','); | |
384 | }, | |
385 | ||
386 | parseQemuSmbios1: function(value) { | |
387 | var res = {}; | |
388 | ||
389 | Ext.Array.each(value.split(','), function(p) { | |
390 | var kva = p.split(/=/, 2); | |
391 | res[kva[0]] = kva[1]; | |
392 | }); | |
393 | ||
394 | return res; | |
395 | }, | |
396 | ||
397 | printQemuSmbios1: function(data) { | |
398 | ||
399 | var datastr = ''; | |
400 | ||
401 | Ext.Object.each(data, function(key, value) { | |
402 | if (value === '') return; | |
403 | datastr += (datastr !== '' ? ',' : '') + key + '=' + value; | |
404 | }); | |
405 | ||
406 | return datastr; | |
407 | }, | |
408 | ||
409 | parseTfaConfig: function(value) { | |
410 | var res = {}; | |
411 | ||
412 | Ext.Array.each(value.split(','), function(p) { | |
413 | var kva = p.split(/=/, 2); | |
414 | res[kva[0]] = kva[1]; | |
415 | }); | |
416 | ||
417 | return res; | |
4c1c0d5d EK |
418 | }, |
419 | ||
420 | parseQemuCpu: function(value) { | |
421 | if (!value) { | |
422 | return {}; | |
423 | } | |
424 | ||
425 | var res = {}; | |
426 | ||
427 | var errors = false; | |
428 | Ext.Array.each(value.split(','), function(p) { | |
429 | if (!p || p.match(/^\s*$/)) { | |
430 | return; // continue | |
431 | } | |
fcb64fe4 | 432 | |
4c1c0d5d EK |
433 | if (!p.match(/=/)) { |
434 | if (Ext.isDefined(res['cpu'])) { | |
435 | errors = true; | |
436 | return false; // break | |
437 | } | |
438 | res.cputype = p; | |
439 | return; // continue | |
440 | } | |
441 | ||
442 | var match_res = p.match(/^([a-z_]+)=(\S+)$/); | |
443 | if (!match_res) { | |
444 | errors = true; | |
445 | return false; // break | |
446 | } | |
447 | ||
448 | var k = match_res[1]; | |
449 | if (Ext.isDefined(res[k])) { | |
450 | errors = true; | |
451 | return false; // break | |
452 | } | |
453 | ||
454 | res[k] = match_res[2]; | |
455 | }); | |
456 | ||
457 | if (errors || !res.cputype) { | |
458 | return; | |
459 | } | |
460 | ||
461 | return res; | |
462 | }, | |
463 | ||
464 | printQemuCpu: function(cpu) { | |
465 | var cpustr = cpu.cputype; | |
466 | var optstr = ''; | |
467 | ||
468 | Ext.Object.each(cpu, function(key, value) { | |
469 | if (!Ext.isDefined(value) || key === 'cputype') { | |
470 | return; // continue | |
471 | } | |
472 | optstr += ',' + key + '=' + value; | |
473 | }); | |
474 | ||
475 | if (!cpustr) { | |
476 | if (optstr) | |
477 | return 'kvm64' + optstr; | |
478 | return; | |
479 | } | |
480 | ||
481 | return cpustr + optstr; | |
482 | }, | |
fcb64fe4 | 483 | }}); |