]>
git.proxmox.com Git - pve-manager.git/blob - www/manager6/Parser.js
1 // Some configuration values are complex strings -
2 // so we need parsers/generators for them.
4 Ext
.define('PVE.Parser', { statics
: {
6 // this class only contains static functions
8 printACME: function(value
) {
9 if (Ext
.isArray(value
.domains
)) {
10 value
.domains
= value
.domains
.join(';');
12 return PVE
.Parser
.printPropertyString(value
);
15 parseACME: function(value
) {
23 Ext
.Array
.each(value
.split(','), function(p
) {
24 var kv
= p
.split('=', 2);
25 if (Ext
.isDefined(kv
[1])) {
28 error
= 'Failed to parse key-value pair: '+p
;
33 if (error
!== undefined) {
38 if (res
.domains
!== undefined) {
39 res
.domains
= res
.domains
.split(/;/);
45 parseBoolean: function(value
, default_value
) {
46 if (!Ext
.isDefined(value
)) {
49 value
= value
.toLowerCase();
50 return value
=== '1' ||
56 parsePropertyString: function(value
, defaultKey
) {
60 if (typeof value
!== 'string' || value
=== '') {
64 Ext
.Array
.each(value
.split(','), function(p
) {
65 var kv
= p
.split('=', 2);
66 if (Ext
.isDefined(kv
[1])) {
68 } else if (Ext
.isDefined(defaultKey
)) {
69 if (Ext
.isDefined(res
[defaultKey
])) {
70 error
= 'defaultKey may be only defined once in propertyString';
71 return false; // break
73 res
[defaultKey
] = kv
[0];
75 error
= 'invalid propertyString, not a key=value pair and no defaultKey defined';
76 return false; // break
80 if (error
!== undefined) {
88 printPropertyString: function(data
, defaultKey
) {
90 gotDefaultKeyVal
= false,
93 Ext
.Object
.each(data
, function(key
, value
) {
94 if (defaultKey
!== undefined && key
=== defaultKey
) {
95 gotDefaultKeyVal
= true;
96 defaultKeyVal
= value
;
97 } else if (value
!== '') {
98 stringparts
.push(key
+ '=' + value
);
102 stringparts
= stringparts
.sort();
103 if (gotDefaultKeyVal
) {
104 stringparts
.unshift(defaultKeyVal
);
107 return stringparts
.join(',');
110 parseQemuNetwork: function(key
, value
) {
111 if (!(key
&& value
)) {
118 Ext
.Array
.each(value
.split(','), function(p
) {
119 if (!p
|| p
.match(/^\s*$/)) {
125 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) {
126 res
.model
= match_res
[1].toLowerCase();
128 res
.macaddr
= match_res
[3];
130 } else if ((match_res
= p
.match(/^bridge=(\S+)$/)) !== null) {
131 res
.bridge
= match_res
[1];
132 } else if ((match_res
= p
.match(/^rate=(\d+(\.\d+)?)$/)) !== null) {
133 res
.rate
= match_res
[1];
134 } else if ((match_res
= p
.match(/^tag=(\d+(\.\d+)?)$/)) !== null) {
135 res
.tag
= match_res
[1];
136 } else if ((match_res
= p
.match(/^firewall=(\d+)$/)) !== null) {
137 res
.firewall
= match_res
[1];
138 } else if ((match_res
= p
.match(/^link_down=(\d+)$/)) !== null) {
139 res
.disconnect
= match_res
[1];
140 } else if ((match_res
= p
.match(/^queues=(\d+)$/)) !== null) {
141 res
.queues
= match_res
[1];
142 } else if ((match_res
= p
.match(/^trunks=(\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*)$/)) !== null) {
143 res
.trunks
= match_res
[1];
146 return false; // break
150 if (errors
|| !res
.model
) {
157 printQemuNetwork: function(net
) {
159 var netstr
= net
.model
;
161 netstr
+= "=" + net
.macaddr
;
164 netstr
+= ",bridge=" + net
.bridge
;
166 netstr
+= ",tag=" + net
.tag
;
169 netstr
+= ",firewall=" + net
.firewall
;
173 netstr
+= ",rate=" + net
.rate
;
176 netstr
+= ",queues=" + net
.queues
;
178 if (net
.disconnect
) {
179 netstr
+= ",link_down=" + net
.disconnect
;
182 netstr
+= ",trunks=" + net
.trunks
;
187 parseQemuDrive: function(key
, value
) {
188 if (!(key
&& value
)) {
194 var match_res
= key
.match(/^([a-z]+)(\d+)$/);
198 res
['interface'] = match_res
[1];
199 res
.index
= match_res
[2];
202 Ext
.Array
.each(value
.split(','), function(p
) {
203 if (!p
|| p
.match(/^\s*$/)) {
206 var match_res
= p
.match(/^([a-z_]+)=(\S+)$/);
208 if (!p
.match(/\=/)) {
213 return false; // break
215 var k
= match_res
[1];
216 if (k
=== 'volume') {
220 if (Ext
.isDefined(res
[k
])) {
222 return false; // break
225 var v
= match_res
[2];
227 if (k
=== 'cache' && v
=== 'off') {
234 if (errors
|| !res
.file
) {
241 printQemuDrive: function(drive
) {
243 var drivestr
= drive
.file
;
245 Ext
.Object
.each(drive
, function(key
, value
) {
246 if (!Ext
.isDefined(value
) || key
=== 'file' ||
247 key
=== 'index' || key
=== 'interface') {
250 drivestr
+= ',' + key
+ '=' + value
;
256 parseIPConfig: function(key
, value
) {
257 if (!(key
&& value
)) {
264 Ext
.Array
.each(value
.split(','), function(p
) {
265 if (!p
|| p
.match(/^\s*$/)) {
270 if ((match_res
= p
.match(/^ip=(\S+)$/)) !== null) {
271 res
.ip
= match_res
[1];
272 } else if ((match_res
= p
.match(/^gw=(\S+)$/)) !== null) {
273 res
.gw
= match_res
[1];
274 } else if ((match_res
= p
.match(/^ip6=(\S+)$/)) !== null) {
275 res
.ip6
= match_res
[1];
276 } else if ((match_res
= p
.match(/^gw6=(\S+)$/)) !== null) {
277 res
.gw6
= match_res
[1];
280 return false; // break
291 printIPConfig: function(cfg
) {
295 str
+= "ip=" + cfg
.ip
;
299 str
+= c
+ "gw=" + cfg
.gw
;
303 str
+= c
+ "ip6=" + cfg
.ip6
;
307 str
+= c
+ "gw6=" + cfg
.gw6
;
313 parseOpenVZNetIf: function(value
) {
321 Ext
.Array
.each(value
.split(';'), function(item
) {
322 if (!item
|| item
.match(/^\s*$/)) {
327 Ext
.Array
.each(item
.split(','), function(p
) {
328 if (!p
|| p
.match(/^\s*$/)) {
331 var match_res
= p
.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/);
334 return false; // break
336 if (match_res
[1] === 'bridge'){
337 var bridgevlanf
= match_res
[2];
338 var bridge_res
= bridgevlanf
.match(/^(vmbr(\d+))(v(\d+))?(f)?$/);
341 return false; // break
343 data
.bridge
= bridge_res
[1];
344 data
.tag
= bridge_res
[4];
345 data
.firewall
= bridge_res
[5] ? 1 : 0;
347 data
[match_res
[1]] = match_res
[2];
351 if (errors
|| !data
.ifname
) {
353 return false; // break
358 res
[data
.ifname
] = data
;
361 return errors
? undefined: res
;
364 printOpenVZNetIf: function(netif
) {
367 Ext
.Object
.each(netif
, function(iface
, data
) {
369 Ext
.Array
.each(['ifname', 'mac', 'bridge', 'host_ifname', 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key
) {
370 var value
= data
[key
];
371 if (key
=== 'bridge'){
373 value
= value
+ 'v' + data
.tag
;
380 tmparray
.push(key
+ '=' + value
);
384 netarray
.push(tmparray
.join(','));
387 return netarray
.join(';');
390 parseLxcNetwork: function(value
) {
396 Ext
.Array
.each(value
.split(','), function(p
) {
397 if (!p
|| p
.match(/^\s*$/)) {
400 var match_res
= p
.match(/^(bridge|hwaddr|mtu|name|ip|ip6|gw|gw6|tag|rate)=(\S+)$/);
402 data
[match_res
[1]] = match_res
[2];
403 } else if ((match_res
= p
.match(/^firewall=(\d+)$/)) !== null) {
404 data
.firewall
= PVE
.Parser
.parseBoolean(match_res
[1]);
406 // todo: simply ignore errors ?
414 printLxcNetwork: function(data
) {
416 Ext
.Array
.each(['bridge', 'hwaddr', 'mtu', 'name', 'ip',
417 'gw', 'ip6', 'gw6', 'firewall', 'tag'], function(key
) {
418 var value
= data
[key
];
420 tmparray
.push(key
+ '=' + value
);
425 tmparray
.push('rate=' + data
.rate
);
427 return tmparray
.join(',');
430 parseLxcMountPoint: function(value
) {
438 Ext
.Array
.each(value
.split(','), function(p
) {
439 if (!p
|| p
.match(/^\s*$/)) {
442 var match_res
= p
.match(/^([a-z_]+)=(.+)$/);
444 if (!p
.match(/\=/)) {
449 return false; // break
451 var k
= match_res
[1];
452 if (k
=== 'volume') {
456 if (Ext
.isDefined(res
[k
])) {
458 return false; // break
461 var v
= match_res
[2];
466 if (errors
|| !res
.file
) {
470 var m
= res
.file
.match(/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):/i);
474 } else if (res
.file
.match(/^\/dev\//)) {
483 printLxcMountPoint: function(mp
) {
484 var drivestr
= mp
.file
;
486 Ext
.Object
.each(mp
, function(key
, value
) {
487 if (!Ext
.isDefined(value
) || key
=== 'file' ||
488 key
=== 'type' || key
=== 'storage') {
491 drivestr
+= ',' + key
+ '=' + value
;
497 parseStartup: function(value
) {
498 if (value
=== undefined) {
505 Ext
.Array
.each(value
.split(','), function(p
) {
506 if (!p
|| p
.match(/^\s*$/)) {
512 if ((match_res
= p
.match(/^(order)?=(\d+)$/)) !== null) {
513 res
.order
= match_res
[2];
514 } else if ((match_res
= p
.match(/^up=(\d+)$/)) !== null) {
515 res
.up
= match_res
[1];
516 } else if ((match_res
= p
.match(/^down=(\d+)$/)) !== null) {
517 res
.down
= match_res
[1];
520 return false; // break
531 printStartup: function(startup
) {
533 if (startup
.order
!== undefined && startup
.order
!== '') {
534 arr
.push('order=' + startup
.order
);
536 if (startup
.up
!== undefined && startup
.up
!== '') {
537 arr
.push('up=' + startup
.up
);
539 if (startup
.down
!== undefined && startup
.down
!== '') {
540 arr
.push('down=' + startup
.down
);
543 return arr
.join(',');
546 parseQemuSmbios1: function(value
) {
547 var res
= value
.split(',').reduce(function (accumulator
, currentValue
) {
548 var splitted
= currentValue
.split(new RegExp("=(.+)"));
549 accumulator
[splitted
[0]] = splitted
[1];
553 if (PVE
.Parser
.parseBoolean(res
.base64
, false)) {
554 Ext
.Object
.each(res
, function(key
, value
) {
555 if (key
=== 'uuid') { return; }
556 res
[key
] = Ext
.util
.Base64
.decode(value
);
563 printQemuSmbios1: function(data
) {
567 Ext
.Object
.each(data
, function(key
, value
) {
568 if (value
=== '') { return; }
569 if (key
=== 'uuid') {
570 datastr
+= (datastr
!== '' ? ',' : '') + key
+ '=' + value
;
572 // values should be base64 encoded from now on, mark config strings correspondingly
575 datastr
+= (datastr
!== '' ? ',' : '') + 'base64=1';
577 datastr
+= (datastr
!== '' ? ',' : '') + key
+ '=' + Ext
.util
.Base64
.encode(value
);
584 parseTfaConfig: function(value
) {
587 Ext
.Array
.each(value
.split(','), function(p
) {
588 var kva
= p
.split('=', 2);
589 res
[kva
[0]] = kva
[1];
595 parseTfaType: function(value
) {
597 if (!value
|| !value
.length
) {
599 } else if (value
=== 'x!oath') {
601 } else if (!!(match
= value
.match(/^x!(.+)$/))) {
608 parseQemuCpu: function(value
) {
616 Ext
.Array
.each(value
.split(','), function(p
) {
617 if (!p
|| p
.match(/^\s*$/)) {
621 if (!p
.match(/\=/)) {
622 if (Ext
.isDefined(res
.cpu
)) {
624 return false; // break
630 var match_res
= p
.match(/^([a-z_]+)=(\S+)$/);
633 return false; // break
636 var k
= match_res
[1];
637 if (Ext
.isDefined(res
[k
])) {
639 return false; // break
642 res
[k
] = match_res
[2];
645 if (errors
|| !res
.cputype
) {
652 printQemuCpu: function(cpu
) {
653 var cpustr
= cpu
.cputype
;
656 Ext
.Object
.each(cpu
, function(key
, value
) {
657 if (!Ext
.isDefined(value
) || key
=== 'cputype') {
660 optstr
+= ',' + key
+ '=' + value
;
665 return 'kvm64' + optstr
;
670 return cpustr
+ optstr
;
673 parseSSHKey: function(key
) {
674 // |--- options can have quotes--| type key comment
675 var keyre
= /^(?:((?:[^\s"]|\"(?:\\.|[^"\\])*")+)\s+)?(\S+)\s+(\S+)(?:\s+(.*))?$/;
676 var typere
= /^(?:ssh-(?:dss|rsa|ed25519)|ecdsa-sha2-nistp\d+)$/;
678 var m
= key
.match(keyre
);
682 if (m
.length
< 3 || !m
[2]) { // [2] is always either type or key
685 if (m
[1] && m
[1].match(typere
)) {
692 if (m
[2].match(typere
)) {
703 parseACMEPluginData: function(data
) {
706 data
.split('\n').forEach((line
) => {
707 // capture everything after the first = as value
708 let [key
, value
] = line
.split(/=(.+)/);
709 if (value
!== undefined) {
712 extradata
.push(line
);
715 return [res
, extradata
];