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