]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Parser.js
jslint: fix curly braces for if
[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
EK
12 value = value.toLowerCase();
13 return value === 1 || value === '1' ||
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
165 parseOpenVZNetIf: function(value) {
166 if (!value) {
167 return;
168 }
169
170 var res = {};
171
172 var errors = false;
173 Ext.Array.each(value.split(';'), function(item) {
174 if (!item || item.match(/^\s*$/)) {
175 return; // continue
176 }
177
178 var data = {};
179 Ext.Array.each(item.split(','), function(p) {
180 if (!p || p.match(/^\s*$/)) {
181 return; // continue
182 }
183 var match_res = p.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/);
184 if (!match_res) {
185 errors = true;
186 return false; // break
187 }
188 if (match_res[1] === 'bridge'){
189 var bridgevlanf = match_res[2];
190 var bridge_res = bridgevlanf.match(/^(vmbr(\d+))(v(\d+))?(f)?$/);
191 if (!bridge_res) {
192 errors = true;
193 return false; // break
194 }
195 data['bridge'] = bridge_res[1];
196 data['tag'] = bridge_res[4];
197 data['firewall'] = bridge_res[5] ? 1 : 0;
198 } else {
199 data[match_res[1]] = match_res[2];
200 }
201 });
202
203 if (errors || !data.ifname) {
204 errors = true;
205 return false; // break
206 }
207
208 data.raw = item;
209
210 res[data.ifname] = data;
211 });
212
213 return errors ? undefined: res;
214 },
215
216 printOpenVZNetIf: function(netif) {
217 var netarray = [];
218
219 Ext.Object.each(netif, function(iface, data) {
220 var tmparray = [];
221 Ext.Array.each(['ifname', 'mac', 'bridge', 'host_ifname' , 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key) {
222 var value = data[key];
223 if (key === 'bridge'){
224 if(data['tag']){
225 value = value + 'v' + data['tag'];
226 }
227 if (data['firewall']){
228 value = value + 'f';
229 }
230 }
231 if (value) {
232 tmparray.push(key + '=' + value);
233 }
234
235 });
236 netarray.push(tmparray.join(','));
237 });
238
239 return netarray.join(';');
240 },
241
242 parseLxcNetwork: function(value) {
243 if (!value) {
244 return;
245 }
246
247 var data = {};
248 Ext.Array.each(value.split(','), function(p) {
249 if (!p || p.match(/^\s*$/)) {
250 return; // continue
251 }
519ca7fa 252 var match_res = p.match(/^(bridge|hwaddr|mtu|name|ip|ip6|gw|gw6|firewall|tag|rate)=(\S+)$/);
fcb64fe4
DM
253 if (!match_res) {
254 // todo: simply ignore errors ?
255 return; // continue
256 }
257 data[match_res[1]] = match_res[2];
258 });
259
260 return data;
261 },
262
263 printLxcNetwork: function(data) {
264 var tmparray = [];
265 Ext.Array.each(['bridge', 'hwaddr', 'mtu', 'name', 'ip',
266 'gw', 'ip6', 'gw6', 'firewall', 'tag'], function(key) {
267 var value = data[key];
268 if (value) {
269 tmparray.push(key + '=' + value);
270 }
271 });
519ca7fa 272
6efbc4cb 273 if (data.rate > 0) {
519ca7fa
DM
274 tmparray.push('rate=' + data.rate);
275 }
fcb64fe4
DM
276 return tmparray.join(',');
277 },
278
4c1c0d5d
EK
279 parseLxcMountPoint: function(value) {
280 if (!value) {
281 return;
282 }
283
284 var res = {};
285
286 var errors = false;
287 Ext.Array.each(value.split(','), function(p) {
288 if (!p || p.match(/^\s*$/)) {
289 return; // continue
290 }
291 var match_res = p.match(/^([a-z_]+)=(\S+)$/);
292 if (!match_res) {
293 if (!p.match(/\=/)) {
294 res.file = p;
295 return; // continue
296 }
297 errors = true;
298 return false; // break
299 }
300 var k = match_res[1];
301 if (k === 'volume') {
302 k = 'file';
303 }
304
305 if (Ext.isDefined(res[k])) {
306 errors = true;
307 return false; // break
308 }
309
310 var v = match_res[2];
311
312 res[k] = v;
313 });
314
315 if (errors || !res.file) {
316 return;
317 }
318
283b450e 319 var m = res.file.match(/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):/i);
4c1c0d5d
EK
320 if (m) {
321 res.storage = m[1];
322 res.type = 'volume';
323 } else if (res.file.match(/^\/dev\//)) {
324 res.type = 'device';
325 } else {
326 res.type = 'bind';
327 }
328
329 return res;
330 },
331
332 printLxcMountPoint: function(mp) {
333 var drivestr = mp.file;
334
335 Ext.Object.each(mp, function(key, value) {
336 if (!Ext.isDefined(value) || key === 'file' ||
337 key === 'type' || key === 'storage') {
338 return; // continue
339 }
340 drivestr += ',' + key + '=' + value;
341 });
342
343 return drivestr;
344 },
345
fcb64fe4
DM
346 parseStartup: function(value) {
347 if (value === undefined) {
348 return;
349 }
350
351 var res = {};
352
353 var errors = false;
354 Ext.Array.each(value.split(','), function(p) {
355 if (!p || p.match(/^\s*$/)) {
356 return; // continue
357 }
358
359 var match_res;
360
361 if ((match_res = p.match(/^(order)?=(\d+)$/)) !== null) {
362 res.order = match_res[2];
363 } else if ((match_res = p.match(/^up=(\d+)$/)) !== null) {
364 res.up = match_res[1];
365 } else if ((match_res = p.match(/^down=(\d+)$/)) !== null) {
366 res.down = match_res[1];
367 } else {
368 errors = true;
369 return false; // break
370 }
371 });
372
373 if (errors) {
374 return;
375 }
376
377 return res;
378 },
379
380 printStartup: function(startup) {
381 var arr = [];
382 if (startup.order !== undefined && startup.order !== '') {
383 arr.push('order=' + startup.order);
384 }
385 if (startup.up !== undefined && startup.up !== '') {
386 arr.push('up=' + startup.up);
387 }
388 if (startup.down !== undefined && startup.down !== '') {
389 arr.push('down=' + startup.down);
390 }
391
392 return arr.join(',');
393 },
394
395 parseQemuSmbios1: function(value) {
396 var res = {};
397
398 Ext.Array.each(value.split(','), function(p) {
399 var kva = p.split(/=/, 2);
400 res[kva[0]] = kva[1];
401 });
402
403 return res;
404 },
405
406 printQemuSmbios1: function(data) {
407
408 var datastr = '';
409
410 Ext.Object.each(data, function(key, value) {
84de645d 411 if (value === '') { return; }
fcb64fe4
DM
412 datastr += (datastr !== '' ? ',' : '') + key + '=' + value;
413 });
414
415 return datastr;
416 },
417
418 parseTfaConfig: function(value) {
419 var res = {};
420
421 Ext.Array.each(value.split(','), function(p) {
422 var kva = p.split(/=/, 2);
423 res[kva[0]] = kva[1];
424 });
425
426 return res;
4c1c0d5d
EK
427 },
428
429 parseQemuCpu: function(value) {
430 if (!value) {
431 return {};
432 }
433
434 var res = {};
435
436 var errors = false;
437 Ext.Array.each(value.split(','), function(p) {
438 if (!p || p.match(/^\s*$/)) {
439 return; // continue
440 }
fcb64fe4 441
4c1c0d5d
EK
442 if (!p.match(/=/)) {
443 if (Ext.isDefined(res['cpu'])) {
444 errors = true;
445 return false; // break
446 }
447 res.cputype = p;
448 return; // continue
449 }
450
451 var match_res = p.match(/^([a-z_]+)=(\S+)$/);
452 if (!match_res) {
453 errors = true;
454 return false; // break
455 }
456
457 var k = match_res[1];
458 if (Ext.isDefined(res[k])) {
459 errors = true;
460 return false; // break
461 }
462
463 res[k] = match_res[2];
464 });
465
466 if (errors || !res.cputype) {
467 return;
468 }
469
470 return res;
471 },
472
473 printQemuCpu: function(cpu) {
474 var cpustr = cpu.cputype;
475 var optstr = '';
476
477 Ext.Object.each(cpu, function(key, value) {
478 if (!Ext.isDefined(value) || key === 'cputype') {
479 return; // continue
480 }
481 optstr += ',' + key + '=' + value;
482 });
483
484 if (!cpustr) {
84de645d 485 if (optstr) {
4c1c0d5d 486 return 'kvm64' + optstr;
84de645d 487 }
4c1c0d5d
EK
488 return;
489 }
490
491 return cpustr + optstr;
492 },
fcb64fe4 493}});