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