]>
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', {
7 // this class only contains static functions
9 printACME: function(value
) {
10 if (Ext
.isArray(value
.domains
)) {
11 value
.domains
= value
.domains
.join(';');
13 return PVE
.Parser
.printPropertyString(value
);
16 parseACME: function(value
) {
24 Ext
.Array
.each(value
.split(','), function(p
) {
25 var kv
= p
.split('=', 2);
26 if (Ext
.isDefined(kv
[1])) {
29 error
= 'Failed to parse key-value pair: '+p
;
34 if (error
!== undefined) {
39 if (res
.domains
!== undefined) {
40 res
.domains
= res
.domains
.split(/;/);
46 parseBoolean: function(value
, default_value
) {
47 if (!Ext
.isDefined(value
)) {
50 value
= value
.toLowerCase();
51 return value
=== '1' ||
57 parsePropertyString: function(value
, defaultKey
) {
61 if (typeof value
!== 'string' || value
=== '') {
65 Ext
.Array
.each(value
.split(','), function(p
) {
66 var kv
= p
.split('=', 2);
67 if (Ext
.isDefined(kv
[1])) {
69 } else if (Ext
.isDefined(defaultKey
)) {
70 if (Ext
.isDefined(res
[defaultKey
])) {
71 error
= 'defaultKey may be only defined once in propertyString';
72 return false; // break
74 res
[defaultKey
] = kv
[0];
76 error
= 'invalid propertyString, not a key=value pair and no defaultKey defined';
77 return false; // break
81 if (error
!== undefined) {
89 printPropertyString: function(data
, defaultKey
) {
91 gotDefaultKeyVal
= false,
94 Ext
.Object
.each(data
, function(key
, value
) {
95 if (defaultKey
!== undefined && key
=== defaultKey
) {
96 gotDefaultKeyVal
= true;
97 defaultKeyVal
= value
;
98 } else if (value
!== '') {
99 stringparts
.push(key
+ '=' + value
);
103 stringparts
= stringparts
.sort();
104 if (gotDefaultKeyVal
) {
105 stringparts
.unshift(defaultKeyVal
);
108 return stringparts
.join(',');
111 parseQemuNetwork: function(key
, value
) {
112 if (!(key
&& value
)) {
119 Ext
.Array
.each(value
.split(','), function(p
) {
120 if (!p
|| p
.match(/^\s*$/)) {
126 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) {
127 res
.model
= match_res
[1].toLowerCase();
129 res
.macaddr
= match_res
[3];
131 } else if ((match_res
= p
.match(/^bridge=(\S+)$/)) !== null) {
132 res
.bridge
= match_res
[1];
133 } else if ((match_res
= p
.match(/^rate=(\d+(\.\d+)?)$/)) !== null) {
134 res
.rate
= match_res
[1];
135 } else if ((match_res
= p
.match(/^tag=(\d+(\.\d+)?)$/)) !== null) {
136 res
.tag
= match_res
[1];
137 } else if ((match_res
= p
.match(/^firewall=(\d+)$/)) !== null) {
138 res
.firewall
= match_res
[1];
139 } else if ((match_res
= p
.match(/^link_down=(\d+)$/)) !== null) {
140 res
.disconnect
= match_res
[1];
141 } else if ((match_res
= p
.match(/^queues=(\d+)$/)) !== null) {
142 res
.queues
= match_res
[1];
143 } else if ((match_res
= p
.match(/^trunks=(\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*)$/)) !== null) {
144 res
.trunks
= match_res
[1];
147 return false; // break
151 if (errors
|| !res
.model
) {
158 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
) {
242 var drivestr
= drive
.file
;
244 Ext
.Object
.each(drive
, function(key
, value
) {
245 if (!Ext
.isDefined(value
) || key
=== 'file' ||
246 key
=== 'index' || key
=== 'interface') {
249 drivestr
+= ',' + key
+ '=' + value
;
255 parseIPConfig: function(key
, value
) {
256 if (!(key
&& value
)) {
263 Ext
.Array
.each(value
.split(','), function(p
) {
264 if (!p
|| p
.match(/^\s*$/)) {
269 if ((match_res
= p
.match(/^ip=(\S+)$/)) !== null) {
270 res
.ip
= match_res
[1];
271 } else if ((match_res
= p
.match(/^gw=(\S+)$/)) !== null) {
272 res
.gw
= match_res
[1];
273 } else if ((match_res
= p
.match(/^ip6=(\S+)$/)) !== null) {
274 res
.ip6
= match_res
[1];
275 } else if ((match_res
= p
.match(/^gw6=(\S+)$/)) !== null) {
276 res
.gw6
= match_res
[1];
279 return false; // break
290 printIPConfig: function(cfg
) {
294 str
+= "ip=" + cfg
.ip
;
298 str
+= c
+ "gw=" + cfg
.gw
;
302 str
+= c
+ "ip6=" + cfg
.ip6
;
306 str
+= c
+ "gw6=" + cfg
.gw6
;
312 parseOpenVZNetIf: function(value
) {
320 Ext
.Array
.each(value
.split(';'), function(item
) {
321 if (!item
|| item
.match(/^\s*$/)) {
326 Ext
.Array
.each(item
.split(','), function(p
) {
327 if (!p
|| p
.match(/^\s*$/)) {
330 var match_res
= p
.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/);
333 return false; // break
335 if (match_res
[1] === 'bridge') {
336 var bridgevlanf
= match_res
[2];
337 var bridge_res
= bridgevlanf
.match(/^(vmbr(\d+))(v(\d+))?(f)?$/);
340 return false; // break
342 data
.bridge
= bridge_res
[1];
343 data
.tag
= bridge_res
[4];
344 data
.firewall
= bridge_res
[5] ? 1 : 0;
346 data
[match_res
[1]] = match_res
[2];
350 if (errors
|| !data
.ifname
) {
352 return false; // break
357 res
[data
.ifname
] = data
;
360 return errors
? undefined: res
;
363 printOpenVZNetIf: function(netif
) {
366 Ext
.Object
.each(netif
, function(iface
, data
) {
368 Ext
.Array
.each(['ifname', 'mac', 'bridge', 'host_ifname', 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key
) {
369 var value
= data
[key
];
370 if (key
=== 'bridge') {
372 value
= value
+ 'v' + data
.tag
;
379 tmparray
.push(key
+ '=' + value
);
382 netarray
.push(tmparray
.join(','));
385 return netarray
.join(';');
388 parseLxcNetwork: function(value
) {
394 Ext
.Array
.each(value
.split(','), function(p
) {
395 if (!p
|| p
.match(/^\s*$/)) {
398 var match_res
= p
.match(/^(bridge|hwaddr|mtu|name|ip|ip6|gw|gw6|tag|rate)=(\S+)$/);
400 data
[match_res
[1]] = match_res
[2];
401 } else if ((match_res
= p
.match(/^firewall=(\d+)$/)) !== null) {
402 data
.firewall
= PVE
.Parser
.parseBoolean(match_res
[1]);
404 // todo: simply ignore errors ?
412 printLxcNetwork: function(data
) {
414 Ext
.Array
.each(['bridge', 'hwaddr', 'mtu', 'name', 'ip',
415 'gw', 'ip6', 'gw6', 'firewall', 'tag'], function(key
) {
416 var value
= data
[key
];
418 tmparray
.push(key
+ '=' + value
);
423 tmparray
.push('rate=' + data
.rate
);
425 return tmparray
.join(',');
428 parseLxcMountPoint: function(value
) {
436 Ext
.Array
.each(value
.split(','), function(p
) {
437 if (!p
|| p
.match(/^\s*$/)) {
440 var match_res
= p
.match(/^([a-z_]+)=(.+)$/);
442 if (!p
.match(/\=/)) {
447 return false; // break
449 var k
= match_res
[1];
450 if (k
=== 'volume') {
454 if (Ext
.isDefined(res
[k
])) {
456 return false; // break
459 var v
= match_res
[2];
464 if (errors
|| !res
.file
) {
468 var m
= res
.file
.match(/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):/i);
472 } else if (res
.file
.match(/^\/dev\//)) {
481 printLxcMountPoint: function(mp
) {
482 var drivestr
= mp
.file
;
484 Ext
.Object
.each(mp
, function(key
, value
) {
485 if (!Ext
.isDefined(value
) || key
=== 'file' ||
486 key
=== 'type' || key
=== 'storage') {
489 drivestr
+= ',' + key
+ '=' + value
;
495 parseStartup: function(value
) {
496 if (value
=== undefined) {
503 Ext
.Array
.each(value
.split(','), function(p
) {
504 if (!p
|| p
.match(/^\s*$/)) {
510 if ((match_res
= p
.match(/^(order)?=(\d+)$/)) !== null) {
511 res
.order
= match_res
[2];
512 } else if ((match_res
= p
.match(/^up=(\d+)$/)) !== null) {
513 res
.up
= match_res
[1];
514 } else if ((match_res
= p
.match(/^down=(\d+)$/)) !== null) {
515 res
.down
= match_res
[1];
518 return false; // break
529 printStartup: function(startup
) {
531 if (startup
.order
!== undefined && startup
.order
!== '') {
532 arr
.push('order=' + startup
.order
);
534 if (startup
.up
!== undefined && startup
.up
!== '') {
535 arr
.push('up=' + startup
.up
);
537 if (startup
.down
!== undefined && startup
.down
!== '') {
538 arr
.push('down=' + startup
.down
);
541 return arr
.join(',');
544 parseQemuSmbios1: function(value
) {
545 var res
= value
.split(',').reduce(function(accumulator
, currentValue
) {
546 var splitted
= currentValue
.split(new RegExp("=(.+)"));
547 accumulator
[splitted
[0]] = splitted
[1];
551 if (PVE
.Parser
.parseBoolean(res
.base64
, false)) {
552 Ext
.Object
.each(res
, function(key
, value
) {
553 if (key
=== 'uuid') { return; }
554 res
[key
] = Ext
.util
.Base64
.decode(value
);
561 printQemuSmbios1: function(data
) {
564 Ext
.Object
.each(data
, function(key
, value
) {
565 if (value
=== '') { return; }
566 if (key
=== 'uuid') {
567 datastr
+= (datastr
!== '' ? ',' : '') + key
+ '=' + value
;
569 // values should be base64 encoded from now on, mark config strings correspondingly
572 datastr
+= (datastr
!== '' ? ',' : '') + 'base64=1';
574 datastr
+= (datastr
!== '' ? ',' : '') + key
+ '=' + Ext
.util
.Base64
.encode(value
);
581 parseTfaConfig: function(value
) {
584 Ext
.Array
.each(value
.split(','), function(p
) {
585 var kva
= p
.split('=', 2);
586 res
[kva
[0]] = kva
[1];
592 parseTfaType: function(value
) {
594 if (!value
|| !value
.length
) {
596 } else if (value
=== 'x!oath') {
598 } else if (match
= value
.match(/^x!(.+)$/)) {
605 parseQemuCpu: function(value
) {
613 Ext
.Array
.each(value
.split(','), function(p
) {
614 if (!p
|| p
.match(/^\s*$/)) {
618 if (!p
.match(/\=/)) {
619 if (Ext
.isDefined(res
.cpu
)) {
621 return false; // break
627 var match_res
= p
.match(/^([a-z_]+)=(\S+)$/);
630 return false; // break
633 var k
= match_res
[1];
634 if (Ext
.isDefined(res
[k
])) {
636 return false; // break
639 res
[k
] = match_res
[2];
642 if (errors
|| !res
.cputype
) {
649 printQemuCpu: function(cpu
) {
650 var cpustr
= cpu
.cputype
;
653 Ext
.Object
.each(cpu
, function(key
, value
) {
654 if (!Ext
.isDefined(value
) || key
=== 'cputype') {
657 optstr
+= ',' + key
+ '=' + value
;
662 return 'kvm64' + optstr
;
667 return cpustr
+ optstr
;
670 parseSSHKey: function(key
) {
671 // |--- options can have quotes--| type key comment
672 var keyre
= /^(?:((?:[^\s"]|\"(?:\\.|[^"\\])*")+)\s+)?(\S+)\s+(\S+)(?:\s+(.*))?$/;
673 var typere
= /^(?:ssh-(?:dss|rsa|ed25519)|ecdsa-sha2-nistp\d+)$/;
675 var m
= key
.match(keyre
);
679 if (m
.length
< 3 || !m
[2]) { // [2] is always either type or key
682 if (m
[1] && m
[1].match(typere
)) {
689 if (m
[2].match(typere
)) {
700 parseACMEPluginData: function(data
) {
703 data
.split('\n').forEach((line
) => {
704 // capture everything after the first = as value
705 let [key
, value
] = line
.split(/=(.+)/);
706 if (value
!== undefined) {
709 extradata
.push(line
);
712 return [res
, extradata
];