]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Parser.js
add render functions for Certificates
[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 8 parseBoolean: function(value, default_value) {
84de645d 9 if (!Ext.isDefined(value)) {
4c1c0d5d 10 return default_value;
84de645d 11 }
4c1c0d5d 12 value = value.toLowerCase();
ec0bd652 13 return value === '1' ||
4c1c0d5d
EK
14 value === 'on' ||
15 value === 'yes' ||
16 value === 'true';
17 },
18
fcb64fe4
DM
19 parseQemuNetwork: function(key, value) {
20 if (!(key && value)) {
21 return;
22 }
23
24 var res = {};
25
26 var errors = false;
27 Ext.Array.each(value.split(','), function(p) {
28 if (!p || p.match(/^\s*$/)) {
29 return; // continue
30 }
31
32 var match_res;
33
34 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) {
35 res.model = match_res[1].toLowerCase();
36 if (match_res[3]) {
37 res.macaddr = match_res[3];
38 }
39 } else if ((match_res = p.match(/^bridge=(\S+)$/)) !== null) {
40 res.bridge = match_res[1];
41 } else if ((match_res = p.match(/^rate=(\d+(\.\d+)?)$/)) !== null) {
42 res.rate = match_res[1];
43 } else if ((match_res = p.match(/^tag=(\d+(\.\d+)?)$/)) !== null) {
63e62a6f 44 res.tag = match_res[1];
fcb64fe4 45 } else if ((match_res = p.match(/^firewall=(\d+)$/)) !== null) {
63e62a6f 46 res.firewall = match_res[1];
fcb64fe4 47 } else if ((match_res = p.match(/^link_down=(\d+)$/)) !== null) {
63e62a6f 48 res.disconnect = match_res[1];
fcb64fe4 49 } else if ((match_res = p.match(/^queues=(\d+)$/)) !== null) {
63e62a6f 50 res.queues = match_res[1];
a2ed0697
WB
51 } else if ((match_res = p.match(/^trunks=(\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*)$/)) !== null) {
52 res.trunks = match_res[1];
fcb64fe4
DM
53 } else {
54 errors = true;
55 return false; // break
56 }
57 });
58
59 if (errors || !res.model) {
60 return;
61 }
62
63 return res;
64 },
65
66 printQemuNetwork: function(net) {
67
68 var netstr = net.model;
69 if (net.macaddr) {
70 netstr += "=" + net.macaddr;
71 }
72 if (net.bridge) {
73 netstr += ",bridge=" + net.bridge;
74 if (net.tag) {
75 netstr += ",tag=" + net.tag;
76 }
77 if (net.firewall) {
78 netstr += ",firewall=" + net.firewall;
79 }
80 }
81 if (net.rate) {
82 netstr += ",rate=" + net.rate;
83 }
84 if (net.queues) {
85 netstr += ",queues=" + net.queues;
86 }
87 if (net.disconnect) {
88 netstr += ",link_down=" + net.disconnect;
89 }
a2ed0697
WB
90 if (net.trunks) {
91 netstr += ",trunks=" + net.trunks;
92 }
fcb64fe4
DM
93 return netstr;
94 },
95
96 parseQemuDrive: function(key, value) {
97 if (!(key && value)) {
98 return;
99 }
100
101 var res = {};
102
103 var match_res = key.match(/^([a-z]+)(\d+)$/);
104 if (!match_res) {
105 return;
106 }
107 res['interface'] = match_res[1];
108 res.index = match_res[2];
109
110 var errors = false;
111 Ext.Array.each(value.split(','), function(p) {
112 if (!p || p.match(/^\s*$/)) {
113 return; // continue
114 }
115 var match_res = p.match(/^([a-z_]+)=(\S+)$/);
116 if (!match_res) {
117 if (!p.match(/\=/)) {
118 res.file = p;
119 return; // continue
120 }
121 errors = true;
122 return false; // break
123 }
124 var k = match_res[1];
125 if (k === 'volume') {
126 k = 'file';
127 }
128
129 if (Ext.isDefined(res[k])) {
130 errors = true;
131 return false; // break
132 }
133
134 var v = match_res[2];
135
136 if (k === 'cache' && v === 'off') {
137 v = 'none';
138 }
139
140 res[k] = v;
141 });
142
143 if (errors || !res.file) {
144 return;
145 }
146
147 return res;
148 },
149
150 printQemuDrive: function(drive) {
151
152 var drivestr = drive.file;
153
154 Ext.Object.each(drive, function(key, value) {
155 if (!Ext.isDefined(value) || key === 'file' ||
156 key === 'index' || key === 'interface') {
157 return; // continue
158 }
159 drivestr += ',' + key + '=' + value;
160 });
161
162 return drivestr;
163 },
164
01e02121
DC
165 parseIPConfig: function(key, value) {
166 if (!(key && value)) {
167 return;
168 }
169
170 var res = {};
171
172 var errors = false;
173 Ext.Array.each(value.split(','), function(p) {
174 if (!p || p.match(/^\s*$/)) {
175 return; // continue
176 }
177
178 var match_res;
179 if ((match_res = p.match(/^ip=(\S+)$/)) !== null) {
180 res.ip = match_res[1];
181 } else if ((match_res = p.match(/^gw=(\S+)$/)) !== null) {
182 res.gw = match_res[1];
183 } else if ((match_res = p.match(/^ip6=(\S+)$/)) !== null) {
184 res.ip6 = match_res[1];
185 } else if ((match_res = p.match(/^gw6=(\S+)$/)) !== null) {
186 res.gw6 = match_res[1];
187 } else {
188 errors = true;
189 return false; // break
190 }
191 });
192
193 if (errors) {
194 return;
195 }
196
197 return res;
198 },
199
200 printIPConfig: function(cfg) {
201 var c = "";
202 var str = "";
203 if (cfg.ip) {
204 str += "ip=" + cfg.ip;
205 c = ",";
206 }
207 if (cfg.gw) {
208 str += c + "gw=" + cfg.gw;
209 c = ",";
210 }
211 if (cfg.ip6) {
212 str += c + "ip6=" + cfg.ip6;
213 c = ",";
214 }
215 if (cfg.gw6) {
216 str += c + "gw6=" + cfg.gw6;
217 c = ",";
218 }
219 return str;
220 },
221
fcb64fe4
DM
222 parseOpenVZNetIf: function(value) {
223 if (!value) {
224 return;
225 }
226
227 var res = {};
228
229 var errors = false;
230 Ext.Array.each(value.split(';'), function(item) {
231 if (!item || item.match(/^\s*$/)) {
232 return; // continue
233 }
234
235 var data = {};
236 Ext.Array.each(item.split(','), function(p) {
237 if (!p || p.match(/^\s*$/)) {
238 return; // continue
239 }
240 var match_res = p.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/);
241 if (!match_res) {
242 errors = true;
243 return false; // break
244 }
245 if (match_res[1] === 'bridge'){
246 var bridgevlanf = match_res[2];
247 var bridge_res = bridgevlanf.match(/^(vmbr(\d+))(v(\d+))?(f)?$/);
248 if (!bridge_res) {
249 errors = true;
250 return false; // break
251 }
ec0bd652
DC
252 data.bridge = bridge_res[1];
253 data.tag = bridge_res[4];
254 /*jslint confusion: true*/
255 data.firewall = bridge_res[5] ? 1 : 0;
256 /*jslint confusion: false*/
fcb64fe4
DM
257 } else {
258 data[match_res[1]] = match_res[2];
259 }
260 });
261
262 if (errors || !data.ifname) {
263 errors = true;
264 return false; // break
265 }
266
267 data.raw = item;
268
269 res[data.ifname] = data;
270 });
271
272 return errors ? undefined: res;
273 },
274
275 printOpenVZNetIf: function(netif) {
276 var netarray = [];
277
278 Ext.Object.each(netif, function(iface, data) {
279 var tmparray = [];
280 Ext.Array.each(['ifname', 'mac', 'bridge', 'host_ifname' , 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key) {
281 var value = data[key];
282 if (key === 'bridge'){
ec0bd652
DC
283 if(data.tag){
284 value = value + 'v' + data.tag;
fcb64fe4 285 }
ec0bd652 286 if (data.firewall){
fcb64fe4
DM
287 value = value + 'f';
288 }
289 }
290 if (value) {
291 tmparray.push(key + '=' + value);
292 }
293
294 });
295 netarray.push(tmparray.join(','));
296 });
297
298 return netarray.join(';');
299 },
300
301 parseLxcNetwork: function(value) {
302 if (!value) {
303 return;
304 }
305
306 var data = {};
307 Ext.Array.each(value.split(','), function(p) {
308 if (!p || p.match(/^\s*$/)) {
309 return; // continue
310 }
519ca7fa 311 var match_res = p.match(/^(bridge|hwaddr|mtu|name|ip|ip6|gw|gw6|firewall|tag|rate)=(\S+)$/);
fcb64fe4
DM
312 if (!match_res) {
313 // todo: simply ignore errors ?
314 return; // continue
315 }
316 data[match_res[1]] = match_res[2];
317 });
318
319 return data;
320 },
321
322 printLxcNetwork: function(data) {
323 var tmparray = [];
324 Ext.Array.each(['bridge', 'hwaddr', 'mtu', 'name', 'ip',
325 'gw', 'ip6', 'gw6', 'firewall', 'tag'], function(key) {
326 var value = data[key];
327 if (value) {
328 tmparray.push(key + '=' + value);
329 }
330 });
519ca7fa 331
ec0bd652 332 /*jslint confusion: true*/
6efbc4cb 333 if (data.rate > 0) {
519ca7fa
DM
334 tmparray.push('rate=' + data.rate);
335 }
ec0bd652 336 /*jslint confusion: false*/
fcb64fe4
DM
337 return tmparray.join(',');
338 },
339
4c1c0d5d
EK
340 parseLxcMountPoint: function(value) {
341 if (!value) {
342 return;
343 }
344
345 var res = {};
346
347 var errors = false;
348 Ext.Array.each(value.split(','), function(p) {
349 if (!p || p.match(/^\s*$/)) {
350 return; // continue
351 }
352 var match_res = p.match(/^([a-z_]+)=(\S+)$/);
353 if (!match_res) {
354 if (!p.match(/\=/)) {
355 res.file = p;
356 return; // continue
357 }
358 errors = true;
359 return false; // break
360 }
361 var k = match_res[1];
362 if (k === 'volume') {
363 k = 'file';
364 }
365
366 if (Ext.isDefined(res[k])) {
367 errors = true;
368 return false; // break
369 }
370
371 var v = match_res[2];
372
373 res[k] = v;
374 });
375
376 if (errors || !res.file) {
377 return;
378 }
379
283b450e 380 var m = res.file.match(/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):/i);
4c1c0d5d
EK
381 if (m) {
382 res.storage = m[1];
383 res.type = 'volume';
384 } else if (res.file.match(/^\/dev\//)) {
385 res.type = 'device';
386 } else {
387 res.type = 'bind';
388 }
389
390 return res;
391 },
392
393 printLxcMountPoint: function(mp) {
394 var drivestr = mp.file;
395
396 Ext.Object.each(mp, function(key, value) {
397 if (!Ext.isDefined(value) || key === 'file' ||
398 key === 'type' || key === 'storage') {
399 return; // continue
400 }
401 drivestr += ',' + key + '=' + value;
402 });
403
404 return drivestr;
405 },
406
fcb64fe4
DM
407 parseStartup: function(value) {
408 if (value === undefined) {
409 return;
410 }
411
412 var res = {};
413
414 var errors = false;
415 Ext.Array.each(value.split(','), function(p) {
416 if (!p || p.match(/^\s*$/)) {
417 return; // continue
418 }
419
420 var match_res;
421
422 if ((match_res = p.match(/^(order)?=(\d+)$/)) !== null) {
423 res.order = match_res[2];
424 } else if ((match_res = p.match(/^up=(\d+)$/)) !== null) {
425 res.up = match_res[1];
426 } else if ((match_res = p.match(/^down=(\d+)$/)) !== null) {
427 res.down = match_res[1];
428 } else {
429 errors = true;
430 return false; // break
431 }
432 });
433
434 if (errors) {
435 return;
436 }
437
438 return res;
439 },
440
441 printStartup: function(startup) {
442 var arr = [];
443 if (startup.order !== undefined && startup.order !== '') {
444 arr.push('order=' + startup.order);
445 }
446 if (startup.up !== undefined && startup.up !== '') {
447 arr.push('up=' + startup.up);
448 }
449 if (startup.down !== undefined && startup.down !== '') {
450 arr.push('down=' + startup.down);
451 }
452
453 return arr.join(',');
454 },
455
456 parseQemuSmbios1: function(value) {
457 var res = {};
458
459 Ext.Array.each(value.split(','), function(p) {
ec0bd652 460 var kva = p.split('=', 2);
fcb64fe4
DM
461 res[kva[0]] = kva[1];
462 });
463
464 return res;
465 },
466
467 printQemuSmbios1: function(data) {
468
469 var datastr = '';
470
471 Ext.Object.each(data, function(key, value) {
84de645d 472 if (value === '') { return; }
fcb64fe4
DM
473 datastr += (datastr !== '' ? ',' : '') + key + '=' + value;
474 });
475
476 return datastr;
477 },
478
479 parseTfaConfig: function(value) {
480 var res = {};
481
482 Ext.Array.each(value.split(','), function(p) {
ec0bd652 483 var kva = p.split('=', 2);
fcb64fe4
DM
484 res[kva[0]] = kva[1];
485 });
486
487 return res;
4c1c0d5d
EK
488 },
489
490 parseQemuCpu: function(value) {
491 if (!value) {
492 return {};
493 }
494
495 var res = {};
496
497 var errors = false;
498 Ext.Array.each(value.split(','), function(p) {
499 if (!p || p.match(/^\s*$/)) {
500 return; // continue
501 }
fcb64fe4 502
ec0bd652
DC
503 if (!p.match(/\=/)) {
504 if (Ext.isDefined(res.cpu)) {
4c1c0d5d
EK
505 errors = true;
506 return false; // break
507 }
508 res.cputype = p;
509 return; // continue
510 }
511
512 var match_res = p.match(/^([a-z_]+)=(\S+)$/);
513 if (!match_res) {
514 errors = true;
515 return false; // break
516 }
517
518 var k = match_res[1];
519 if (Ext.isDefined(res[k])) {
520 errors = true;
521 return false; // break
522 }
523
524 res[k] = match_res[2];
525 });
526
527 if (errors || !res.cputype) {
528 return;
529 }
530
531 return res;
532 },
533
534 printQemuCpu: function(cpu) {
535 var cpustr = cpu.cputype;
536 var optstr = '';
537
538 Ext.Object.each(cpu, function(key, value) {
539 if (!Ext.isDefined(value) || key === 'cputype') {
540 return; // continue
541 }
542 optstr += ',' + key + '=' + value;
543 });
544
545 if (!cpustr) {
84de645d 546 if (optstr) {
4c1c0d5d 547 return 'kvm64' + optstr;
84de645d 548 }
4c1c0d5d
EK
549 return;
550 }
551
552 return cpustr + optstr;
b1339314
WB
553 },
554
555 parseSSHKey: function(key) {
556 // |--- options can have quotes--| type key comment
557 var keyre = /^(?:((?:[^\s"]|\"(?:\\.|[^"\\])*")+)\s+)?(\S+)\s+(\S+)(?:\s+(.*))?$/;
558 var typere = /^(?:ssh-(?:dss|rsa|ed25519)|ecdsa-sha2-nistp\d+)$/;
559
560 var m = key.match(keyre);
561 if (!m) {
562 return null;
563 }
564 if (m.length < 3 || !m[2]) { // [2] is always either type or key
565 return null;
566 }
567 if (m[1] && m[1].match(typere)) {
568 return {
569 type: m[1],
570 key: m[2],
571 comment: m[3]
572 };
573 }
574 if (m[2].match(typere)) {
575 return {
576 options: m[1],
577 type: m[2],
578 key: m[3],
579 comment: m[4]
580 };
581 }
582 return null;
22f2f9d6 583 }
fcb64fe4 584}});