]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
fix onlineHelp links for diskmanagement
[pve-manager.git] / www / manager6 / Utils.js
CommitLineData
b0a6d326
EK
1Ext.ns('PVE');
2
63b9faae
EK
3// avoid errors related to Accessible Rich Internet Applications
4// (access for people with disabilities)
0be88ae1 5// TODO reenable after all components are upgraded
63b9faae
EK
6Ext.enableAria = false;
7Ext.enableAriaButtons = false;
8Ext.enableAriaPanels = false;
9
b0a6d326 10// avoid errors when running without development tools
0be88ae1
DC
11if (!Ext.isDefined(Ext.global.console)) {
12 var console = {
0be88ae1 13 log: function() {}
b0a6d326
EK
14 };
15}
0be88ae1 16console.log("Starting PVE Manager");
b0a6d326
EK
17
18Ext.Ajax.defaultHeaders = {
19 'Accept': 'application/json'
20};
21
9fa2e36d 22Ext.define('PVE.Utils', { utilities: {
b0a6d326 23
9fa2e36d 24 // this singleton contains miscellaneous utilities
b0a6d326 25
0be88ae1 26 toolkit: undefined, // (extjs|touch), set inside Toolkit.js
b0a6d326 27
98a01af2
EK
28 bus_match: /^(ide|sata|virtio|scsi)\d+$/,
29
b0a6d326
EK
30 log_severity_hash: {
31 0: "panic",
32 1: "alert",
33 2: "critical",
34 3: "error",
35 4: "warning",
36 5: "notice",
37 6: "info",
38 7: "debug"
39 },
40
41 support_level_hash: {
42 'c': gettext('Community'),
43 'b': gettext('Basic'),
44 's': gettext('Standard'),
45 'p': gettext('Premium')
46 },
47
48 noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="http://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
49
50 kvm_ostypes: {
45d6c71a
TL
51 'Linux': [
52 { desc: '4.X/3.X/2.6 Kernel', val: 'l26' },
53 { desc: '2.4 Kernel', val: 'l24' }
54 ],
55 'Microsoft Windows': [
56 { desc: '10/2016', val: 'win10' },
57 { desc: '8.x/2012/2012r2', val: 'win8' },
58 { desc: '7/2008r2', val: 'win7' },
59 { desc: 'Vista/2008', val: 'w2k8' },
60 { desc: 'XP/2003', val: 'wxp' },
61 { desc: '2000', val: 'w2k' }
62 ],
63 'Solaris Kernel': [
64 { desc: '-', val: 'solaris'}
65 ],
66 'Other': [
67 { desc: '-', val: 'other'}
68 ]
b0a6d326
EK
69 },
70
428bc4a2
DC
71 get_health_icon: function(state, circle) {
72 if (circle === undefined) {
73 circle = false;
74 }
75
76 if (state === undefined) {
77 state = 'uknown';
78 }
79
80 var icon = 'faded fa-question';
81 switch(state) {
82 case 'good':
83 icon = 'good fa-check';
84 break;
85 case 'warning':
86 icon = 'warning fa-exclamation';
87 break;
88 case 'critical':
89 icon = 'critical fa-times';
90 break;
91 default: break;
92 }
93
94 if (circle) {
95 icon += '-circle';
96 }
97
98 return icon;
99 },
100
046e640c
DC
101 map_ceph_health: {
102 'HEALTH_OK':'good',
103 'HEALTH_WARN':'warning',
104 'HEALTH_ERR':'critical'
105 },
106
dfe6d184 107 render_ceph_health: function(healthObj) {
046e640c
DC
108 var state = {
109 iconCls: PVE.Utils.get_health_icon(),
110 text: ''
111 };
112
dfe6d184 113 if (!healthObj || !healthObj.status) {
046e640c
DC
114 return state;
115 }
116
dfe6d184 117 var health = PVE.Utils.map_ceph_health[healthObj.status];
046e640c
DC
118
119 state.iconCls = PVE.Utils.get_health_icon(health, true);
dfe6d184 120 state.text = healthObj.status;
046e640c
DC
121
122 return state;
123 },
124
45d6c71a
TL
125 get_kvm_osinfo: function(value) {
126 var info = { base: 'Other' }; // default
127 if (value) {
128 Ext.each(Object.keys(PVE.Utils.kvm_ostypes), function(k) {
129 Ext.each(PVE.Utils.kvm_ostypes[k], function(e) {
130 if (e.val === value) {
131 info = { desc: e.desc, base: k };
132 }
133 });
134 });
b0a6d326 135 }
45d6c71a
TL
136 return info;
137 },
138
139 render_kvm_ostype: function (value) {
140 var osinfo = PVE.Utils.get_kvm_osinfo(value);
141 if (osinfo.desc && osinfo.desc !== '-') {
142 return osinfo.base + ' ' + osinfo.desc;
143 } else {
144 return osinfo.base;
b0a6d326 145 }
b0a6d326
EK
146 },
147
148 render_hotplug_features: function (value) {
23d3881a 149 var fa = [];
b0a6d326
EK
150
151 if (!value || (value === '0')) {
ec99b1c3 152 return gettext('Disabled');
b0a6d326
EK
153 }
154
4dcca8a4
DC
155 if (value === '1') {
156 value = 'disk,network,usb';
157 }
158
b0a6d326
EK
159 Ext.each(value.split(','), function(el) {
160 if (el === 'disk') {
161 fa.push(gettext('Disk'));
162 } else if (el === 'network') {
163 fa.push(gettext('Network'));
164 } else if (el === 'usb') {
b9628aa5 165 fa.push('USB');
b0a6d326
EK
166 } else if (el === 'memory') {
167 fa.push(gettext('Memory'));
168 } else if (el === 'cpu') {
169 fa.push(gettext('CPU'));
170 } else {
171 fa.push(el);
172 }
173 });
174
175 return fa.join(', ');
176 },
177
1662ccdb
SI
178 render_qga_features: function(value) {
179 if (!value) {
180 return Proxmox.Utils.defaultText + ' (' + Proxmox.Utils.disabledText + ')';
181 }
182 var props = PVE.Parser.parsePropertyString(value, 'enabled');
183 if (!PVE.Parser.parseBoolean(props.enabled)) {
184 return Proxmox.Utils.disabledText;
185 }
186
187 delete props.enabled;
188 var agentstring = Proxmox.Utils.enabledText;
189
190 Ext.Object.each(props, function(key, value) {
191 var keystring = '' ;
192 agentstring += ', ' + key + ': ';
193
194 if (PVE.Parser.parseBoolean(value)) {
195 agentstring += Proxmox.Utils.enabledText;
196 } else {
197 agentstring += Proxmox.Utils.disabledText;
198 }
199 });
200
201 return agentstring;
202 },
203
17c71f27
DC
204 render_qemu_bios: function(value) {
205 if (!value) {
e7ade592 206 return Proxmox.Utils.defaultText + ' (SeaBIOS)';
17c71f27
DC
207 } else if (value === 'seabios') {
208 return "SeaBIOS";
209 } else if (value === 'ovmf') {
210 return "OVMF (UEFI)";
211 } else {
212 return value;
213 }
214 },
215
b0a6d326
EK
216 render_scsihw: function(value) {
217 if (!value) {
e7ade592 218 return Proxmox.Utils.defaultText + ' (LSI 53C895A)';
b0a6d326
EK
219 } else if (value === 'lsi') {
220 return 'LSI 53C895A';
221 } else if (value === 'lsi53c810') {
222 return 'LSI 53C810';
223 } else if (value === 'megasas') {
224 return 'MegaRAID SAS 8708EM2';
225 } else if (value === 'virtio-scsi-pci') {
49f5d1ab
EK
226 return 'VirtIO SCSI';
227 } else if (value === 'virtio-scsi-single') {
228 return 'VirtIO SCSI single';
b0a6d326
EK
229 } else if (value === 'pvscsi') {
230 return 'VMware PVSCSI';
231 } else {
232 return value;
233 }
234 },
235
236 // fixme: auto-generate this
237 // for now, please keep in sync with PVE::Tools::kvmkeymaps
238 kvm_keymaps: {
239 //ar: 'Arabic',
240 da: 'Danish',
0be88ae1
DC
241 de: 'German',
242 'de-ch': 'German (Swiss)',
243 'en-gb': 'English (UK)',
0a5b8747 244 'en-us': 'English (USA)',
b0a6d326
EK
245 es: 'Spanish',
246 //et: 'Estonia',
247 fi: 'Finnish',
0be88ae1
DC
248 //fo: 'Faroe Islands',
249 fr: 'French',
250 'fr-be': 'French (Belgium)',
b0a6d326
EK
251 'fr-ca': 'French (Canada)',
252 'fr-ch': 'French (Swiss)',
253 //hr: 'Croatia',
254 hu: 'Hungarian',
255 is: 'Icelandic',
0be88ae1 256 it: 'Italian',
b0a6d326
EK
257 ja: 'Japanese',
258 lt: 'Lithuanian',
259 //lv: 'Latvian',
0be88ae1 260 mk: 'Macedonian',
b0a6d326
EK
261 nl: 'Dutch',
262 //'nl-be': 'Dutch (Belgium)',
0be88ae1 263 no: 'Norwegian',
b0a6d326
EK
264 pl: 'Polish',
265 pt: 'Portuguese',
266 'pt-br': 'Portuguese (Brazil)',
267 //ru: 'Russian',
268 sl: 'Slovenian',
269 sv: 'Swedish',
270 //th: 'Thai',
271 tr: 'Turkish'
272 },
273
274 kvm_vga_drivers: {
275 std: gettext('Standard VGA'),
5ca366f2 276 vmware: gettext('VMware compatible'),
b0a6d326
EK
277 qxl: 'SPICE',
278 qxl2: 'SPICE dual monitor',
279 qxl3: 'SPICE three monitors',
280 qxl4: 'SPICE four monitors',
281 serial0: gettext('Serial terminal') + ' 0',
282 serial1: gettext('Serial terminal') + ' 1',
283 serial2: gettext('Serial terminal') + ' 2',
284 serial3: gettext('Serial terminal') + ' 3'
285 },
286
287 render_kvm_language: function (value) {
e7ade592
DC
288 if (!value || value === '__default__') {
289 return Proxmox.Utils.defaultText;
b0a6d326
EK
290 }
291 var text = PVE.Utils.kvm_keymaps[value];
292 if (text) {
293 return text + ' (' + value + ')';
294 }
295 return value;
296 },
297
298 kvm_keymap_array: function() {
f2782813 299 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
300 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
301 data.push([key, PVE.Utils.render_kvm_language(value)]);
302 });
303
304 return data;
305 },
306
3438c27e
DC
307 console_map: {
308 '__default__': Proxmox.Utils.defaultText + ' (HTML5)',
309 'vv': 'SPICE (remote-viewer)',
310 'html5': 'HTML5 (noVNC)',
4ace5c6f 311 'xtermjs': 'xterm.js'
3438c27e
DC
312 },
313
b0a6d326 314 render_console_viewer: function(value) {
3438c27e
DC
315 value = value || '__default__';
316 if (PVE.Utils.console_map[value]) {
317 return PVE.Utils.console_map[value];
b0a6d326 318 }
3438c27e 319 return value;
b0a6d326
EK
320 },
321
755b9083 322 console_viewer_array: function() {
3438c27e 323 return Ext.Array.map(Object.keys(PVE.Utils.console_map), function(v) {
755b9083
TL
324 return [v, PVE.Utils.render_console_viewer(v)];
325 });
326 },
327
b0a6d326
EK
328 render_kvm_vga_driver: function (value) {
329 if (!value) {
e7ade592 330 return Proxmox.Utils.defaultText;
b0a6d326
EK
331 }
332 var text = PVE.Utils.kvm_vga_drivers[value];
0be88ae1 333 if (text) {
b0a6d326
EK
334 return text + ' (' + value + ')';
335 }
336 return value;
337 },
338
339 kvm_vga_driver_array: function() {
f2782813 340 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
341 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
342 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
343 });
344
345 return data;
346 },
347
348 render_kvm_startup: function(value) {
349 var startup = PVE.Parser.parseStartup(value);
350
351 var res = 'order=';
352 if (startup.order === undefined) {
353 res += 'any';
354 } else {
355 res += startup.order;
356 }
357 if (startup.up !== undefined) {
358 res += ',up=' + startup.up;
359 }
360 if (startup.down !== undefined) {
361 res += ',down=' + startup.down;
362 }
363
364 return res;
365 },
366
b0a6d326
EK
367 extractFormActionError: function(action) {
368 var msg;
369 switch (action.failureType) {
370 case Ext.form.action.Action.CLIENT_INVALID:
371 msg = gettext('Form fields may not be submitted with invalid values');
372 break;
373 case Ext.form.action.Action.CONNECT_FAILURE:
374 msg = gettext('Connection error');
375 var resp = action.response;
376 if (resp.status && resp.statusText) {
377 msg += " " + resp.status + ": " + resp.statusText;
378 }
379 break;
380 case Ext.form.action.Action.LOAD_FAILURE:
381 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 382 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
383 break;
384 }
385 return msg;
386 },
387
b0a6d326 388 format_duration_short: function(ut) {
0be88ae1 389
b0a6d326 390 if (ut < 60) {
530ee6b7 391 return ut.toFixed(1) + 's';
b0a6d326
EK
392 }
393
394 if (ut < 3600) {
395 var mins = ut / 60;
530ee6b7 396 return mins.toFixed(1) + 'm';
b0a6d326
EK
397 }
398
399 if (ut < 86400) {
400 var hours = ut / 3600;
530ee6b7 401 return hours.toFixed(1) + 'h';
b0a6d326
EK
402 }
403
404 var days = ut / 86400;
530ee6b7 405 return days.toFixed(1) + 'd';
b0a6d326
EK
406 },
407
b0a6d326
EK
408 imagesText: gettext('Disk image'),
409 backupFileText: gettext('VZDump backup file'),
2c554952 410 vztmplText: gettext('Container template'),
b0a6d326 411 isoImageText: gettext('ISO image'),
2c554952 412 containersText: gettext('Container'),
b0a6d326 413
062a7f49
TL
414 storageSchema: {
415 dir: {
416 name: Proxmox.Utils.directoryText,
417 ipanel: 'DirInputPanel',
418 faIcon: 'folder'
419 },
420 lvm: {
421 name: 'LVM',
422 ipanel: 'LVMInputPanel',
423 faIcon: 'folder'
424 },
425 lvmthin: {
426 name: 'LVM-Thin',
427 ipanel: 'LvmThinInputPanel',
428 faIcon: 'folder'
429 },
430 nfs: {
431 name: 'NFS',
432 ipanel: 'NFSInputPanel',
433 faIcon: 'building'
434 },
435 cifs: {
436 name: 'CIFS',
437 ipanel: 'CIFSInputPanel',
438 faIcon: 'building'
439 },
440 glusterfs: {
441 name: 'GlusterFS',
442 ipanel: 'GlusterFsInputPanel',
443 faIcon: 'building'
444 },
445 iscsi: {
446 name: 'iSCSI',
447 ipanel: 'IScsiInputPanel',
448 faIcon: 'building'
449 },
450 sheepdog: {
451 name: 'Sheepdog',
452 ipanel: 'SheepdogInputPanel',
453 hideAdd: true,
454 faIcon: 'building'
455 },
456 rbd: {
457 name: 'RBD',
458 ipanel: 'RBDInputPanel',
062a7f49
TL
459 faIcon: 'building'
460 },
461 pveceph: {
462 name: 'RBD (PVE)',
0d1ac958
TL
463 ipanel: 'RBDInputPanel',
464 hideAdd: true,
062a7f49
TL
465 faIcon: 'building'
466 },
467 zfs: {
468 name: 'ZFS over iSCSI',
469 ipanel: 'ZFSInputPanel',
470 faIcon: 'building'
471 },
472 zfspool: {
473 name: 'ZFS',
474 ipanel: 'ZFSPoolInputPanel',
475 faIcon: 'folder'
476 },
477 drbd: {
478 name: 'DRBD',
479 hideAdd: true
480 }
481 },
482
3c23c025 483 format_storage_type: function(value, md, record) {
6760ab92 484 if (value === 'rbd' && record) {
0d1ac958 485 value = (record.get('monhost') ? 'rbd' : 'pveceph');
3c23c025 486 }
062a7f49
TL
487
488 var schema = PVE.Utils.storageSchema[value];
489 if (schema) {
490 return schema.name;
b0a6d326 491 }
062a7f49 492 return Proxmox.Utils.unknownText;
a3b8efb4
EK
493 },
494
ced1677b 495 format_ha: function(value) {
e7ade592 496 var text = Proxmox.Utils.noneText;
ced1677b
TL
497
498 if (value.managed) {
e7ade592 499 text = value.state || Proxmox.Utils.noneText;
ced1677b 500
e7ade592
DC
501 text += ', ' + Proxmox.Utils.groupText + ': ';
502 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
503 }
504
505 return text;
506 },
507
b0a6d326
EK
508 format_content_types: function(value) {
509 var cta = [];
510
511 Ext.each(value.split(',').sort(), function(ct) {
512 if (ct === 'images') {
513 cta.push(PVE.Utils.imagesText);
514 } else if (ct === 'backup') {
515 cta.push(PVE.Utils.backupFileText);
516 } else if (ct === 'vztmpl') {
517 cta.push(PVE.Utils.vztmplText);
518 } else if (ct === 'iso') {
519 cta.push(PVE.Utils.isoImageText);
520 } else if (ct === 'rootdir') {
521 cta.push(PVE.Utils.containersText);
522 }
523 });
524
525 return cta.join(', ');
526 },
527
528 render_storage_content: function(value, metaData, record) {
529 var data = record.data;
530 if (Ext.isNumber(data.channel) &&
531 Ext.isNumber(data.id) &&
532 Ext.isNumber(data.lun)) {
0be88ae1
DC
533 return "CH " +
534 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
535 " ID " + data.id + " LUN " + data.lun;
536 }
537 return data.volid.replace(/^.*:(.*\/)?/,'');
538 },
539
540 render_serverity: function (value) {
541 return PVE.Utils.log_severity_hash[value] || value;
542 },
543
544 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
545
546 if (!(record.data.uptime && Ext.isNumeric(value))) {
547 return '';
548 }
549
550 var maxcpu = record.data.maxcpu || 1;
551
552 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
553 return '';
554 }
0be88ae1 555
b0a6d326
EK
556 var per = value * 100;
557
558 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
559 },
560
561 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
562 /*jslint confusion: true */
563
564 if (!Ext.isNumeric(value)) {
565 return '';
566 }
567
e7ade592 568 return Proxmox.Utils.format_size(value);
b0a6d326
EK
569 },
570
946730cd
DC
571 render_bandwidth: function(value) {
572 if (!Ext.isNumeric(value)) {
573 return '';
574 }
575
e7ade592 576 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
577 },
578
3f633655
EK
579 render_timestamp_human_readable: function(value) {
580 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
581 },
582
ddd26302
DC
583 render_duration: function(value) {
584 if (value === undefined) {
585 return '-';
586 }
587 return PVE.Utils.format_duration_short(value);
588 },
589
0bfc799f
DC
590 calculate_mem_usage: function(data) {
591 if (!Ext.isNumeric(data.mem) ||
592 data.maxmem === 0 ||
593 data.uptime < 1) {
594 return -1;
595 }
596
597 return (data.mem / data.maxmem);
598 },
599
600 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
601 if (!Ext.isNumeric(value) || value === -1) {
602 return '';
603 }
604 if (value > 1 ) {
605 // we got no percentage but bytes
606 var mem = value;
607 var maxmem = record.data.maxmem;
608 if (!record.data.uptime ||
609 maxmem === 0 ||
610 !Ext.isNumeric(mem)) {
611 return '';
612 }
613
614 return ((mem*100)/maxmem).toFixed(1) + " %";
615 }
616 return (value*100).toFixed(1) + " %";
617 },
618
b0a6d326
EK
619 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
620
621 var mem = value;
622 var maxmem = record.data.maxmem;
0be88ae1 623
b0a6d326
EK
624 if (!record.data.uptime) {
625 return '';
626 }
627
628 if (!(Ext.isNumeric(mem) && maxmem)) {
629 return '';
630 }
631
728f1b97 632 return PVE.Utils.render_size(value);
b0a6d326
EK
633 },
634
0bfc799f
DC
635 calculate_disk_usage: function(data) {
636
637 if (!Ext.isNumeric(data.disk) ||
638 data.type === 'qemu' ||
639 (data.type === 'lxc' && data.uptime === 0) ||
640 data.maxdisk === 0) {
641 return -1;
642 }
643
644 return (data.disk / data.maxdisk);
645 },
646
647 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
648 if (!Ext.isNumeric(value) || value === -1) {
649 return '';
650 }
651
652 return (value * 100).toFixed(1) + " %";
653 },
654
b0a6d326
EK
655 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
656
657 var disk = value;
658 var maxdisk = record.data.maxdisk;
728f1b97 659 var type = record.data.type;
b0a6d326 660
728f1b97
DC
661 if (!Ext.isNumeric(disk) ||
662 type === 'qemu' ||
663 maxdisk === 0 ||
664 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
665 return '';
666 }
667
728f1b97 668 return PVE.Utils.render_size(value);
b0a6d326
EK
669 },
670
4dbc64a7
DC
671 get_object_icon_class: function(type, record) {
672 var status = '';
673 var objType = type;
674
675 if (type === 'type') {
676 // for folder view
677 objType = record.groupbyid;
678 } else if (record.template) {
679 // templates
680 objType = 'template';
681 status = type;
682 } else {
683 // everything else
684 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
685 }
686
4dbc64a7
DC
687 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
688 if (defaults && defaults.iconCls) {
689 var retVal = defaults.iconCls + ' ' + status;
690 return retVal;
b0a6d326
EK
691 }
692
4dbc64a7
DC
693 return '';
694 },
695
696 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
697
698 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 699
4dbc64a7 700 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 701 return fa + value;
b0a6d326
EK
702 },
703
b0a6d326
EK
704 render_support_level: function(value, metaData, record) {
705 return PVE.Utils.support_level_hash[value] || '-';
706 },
707
0be88ae1 708 render_upid: function(value, metaData, record) {
b0a6d326
EK
709 var type = record.data.type;
710 var id = record.data.id;
711
e7ade592 712 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
713 },
714
054ac1b8
DC
715 /* render functions for new status panel */
716
717 render_usage: function(val) {
718 return (val*100).toFixed(2) + '%';
719 },
720
721 render_cpu_usage: function(val, max) {
722 return Ext.String.format(gettext('{0}% of {1}') +
723 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
724 },
725
726 render_size_usage: function(val, max) {
bab64974
DC
727 if (max === 0) {
728 return gettext('N/A');
729 }
054ac1b8
DC
730 return (val*100/max).toFixed(2) + '% '+ '(' +
731 Ext.String.format(gettext('{0} of {1}'),
732 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
733 },
734
735 /* this is different for nodes */
736 render_node_cpu_usage: function(value, record) {
737 return PVE.Utils.render_cpu_usage(value, record.cpus);
738 },
739
740 /* this is different for nodes */
741 render_node_size_usage: function(record) {
742 return PVE.Utils.render_size_usage(record.used, record.total);
743 },
744
27809975
DC
745 render_optional_url: function(value) {
746 var match;
747 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 748 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
749 }
750 return value;
751 },
752
753 render_san: function(value) {
754 var names = [];
755 if (Ext.isArray(value)) {
756 value.forEach(function(val) {
757 if (!Ext.isNumber(val)) {
758 names.push(val);
759 }
760 });
761 return names.join('<br>');
762 }
763 return value;
764 },
765
aa0819a8 766 windowHostname: function() {
e7ade592 767 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
768 function(m, addr, offset, original) { return addr; });
769 },
0be88ae1 770
3438c27e
DC
771 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname) {
772 var dv = PVE.Utils.defaultViewer(consoles);
b0a6d326
EK
773 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
774 },
775
776 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 777 // kvm, lxc, shell, upgrade
b0a6d326 778
9e361643 779 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
780 throw "missing vmid";
781 }
782
783 if (!nodename) {
784 throw "no nodename specified";
785 }
786
c7218ab3
DC
787 if (viewer === 'html5') {
788 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
c6b2336c 789 } else if (viewer === 'xtermjs') {
e7ade592 790 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
791 } else if (viewer === 'vv') {
792 var url;
aa0819a8 793 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
794 if (vmtype === 'kvm') {
795 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
796 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
797 } else if (vmtype === 'lxc') {
798 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
799 PVE.Utils.openSpiceViewer(url, params);
800 } else if (vmtype === 'shell') {
801 url = '/nodes/' + nodename + '/spiceshell';
802 PVE.Utils.openSpiceViewer(url, params);
803 } else if (vmtype === 'upgrade') {
804 url = '/nodes/' + nodename + '/spiceshell';
805 params.upgrade = 1;
806 PVE.Utils.openSpiceViewer(url, params);
807 }
808 } else {
809 throw "unknown viewer type";
810 }
811 },
812
3438c27e
DC
813 defaultViewer: function(consoles) {
814
815 var allowSpice, allowXtermjs;
816
817 if (consoles === true) {
818 allowSpice = true;
819 allowXtermjs = true;
820 } else if (typeof consoles === 'object') {
821 allowSpice = consoles.spice;
4ace5c6f 822 allowXtermjs = !!consoles.xtermjs;
3438c27e 823 }
b0a6d326
EK
824 var vncdefault = 'html5';
825 var dv = PVE.VersionInfo.console || vncdefault;
3438c27e 826 if ((dv === 'vv' && !allowSpice) || (dv === 'xtermjs' && !allowXtermjs)) {
b0a6d326
EK
827 dv = vncdefault;
828 }
829
830 return dv;
831 },
832
c7218ab3 833 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 834 var url = Ext.urlEncode({
9e361643 835 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 836 novnc: 1,
b0a6d326
EK
837 vmid: vmid,
838 vmname: vmname,
16e64c97
DC
839 node: nodename,
840 resize: 'off'
b0a6d326
EK
841 });
842 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
843 nw.focus();
844 },
845
846 openSpiceViewer: function(url, params){
847
848 var downloadWithName = function(uri, name) {
849 var link = Ext.DomHelper.append(document.body, {
850 tag: 'a',
851 href: uri,
852 css : 'display:none;visibility:hidden;height:0px;'
853 });
854
855 // Note: we need to tell android the correct file name extension
856 // but we do not set 'download' tag for other environments, because
857 // It can have strange side effects (additional user prompt on firefox)
858 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
859 if (andriod) {
860 link.download = name;
861 }
862
863 if (link.fireEvent) {
864 link.fireEvent('onclick');
865 } else {
866 var evt = document.createEvent("MouseEvents");
867 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
868 link.dispatchEvent(evt);
869 }
870 };
871
e7ade592 872 Proxmox.Utils.API2Request({
b0a6d326
EK
873 url: url,
874 params: params,
875 method: 'POST',
876 failure: function(response, opts){
877 Ext.Msg.alert('Error', response.htmlStatus);
878 },
879 success: function(response, opts){
880 var raw = "[virt-viewer]\n";
881 Ext.Object.each(response.result.data, function(k, v) {
882 raw += k + "=" + v + "\n";
883 });
884 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
885 encodeURIComponent(raw);
0be88ae1 886
b0a6d326
EK
887 downloadWithName(url, "pve-spice.vv");
888 }
889 });
890 },
891
e3129443
DC
892 openTreeConsole: function(tree, record, item, index, e) {
893 e.stopEvent();
894 var nodename = record.data.node;
895 var vmid = record.data.vmid;
896 var vmname = record.data.name;
897 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 898 Proxmox.Utils.API2Request({
e3129443
DC
899 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
900 failure: function(response, opts) {
901 Ext.Msg.alert('Error', response.htmlStatus);
902 },
903 success: function(response, opts) {
ab9f0fe1 904 var allowSpice = !!response.result.data.spice;
e3129443
DC
905 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
906 }
907 });
908 } else if (record.data.type === 'lxc' && !record.data.template) {
909 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
910 }
911 },
912
fbd60cfd
DM
913 // test automation helper
914 call_menu_handler: function(menu, text) {
915
916 var list = menu.query('menuitem');
917
918 Ext.Array.each(list, function(item) {
919 if (item.text === text) {
920 if (item.handler) {
921 item.handler();
922 return 1;
923 } else {
924 return undefined;
925 }
926 }
927 });
928 },
929
685b7aa4
DC
930 createCmdMenu: function(v, record, item, index, event) {
931 event.stopEvent();
cc1a91be
DC
932 if (!(v instanceof Ext.tree.View)) {
933 v.select(record);
934 }
685b7aa4 935 var menu;
9bad05bd
DC
936 var template = !!record.data.template;
937 var type = record.data.type;
685b7aa4 938
9bad05bd
DC
939 if (template) {
940 if (type === 'qemu' || type == 'lxc') {
941 menu = Ext.create('PVE.menu.TemplateMenu', {
942 pveSelNode: record
943 });
944 }
945 } else if (type === 'qemu' ||
946 type === 'lxc' ||
947 type === 'node') {
948 menu = Ext.create('PVE.' + type + '.CmdMenu', {
949 pveSelNode: record,
c11ab8cb
DC
950 nodename: record.data.node
951 });
685b7aa4
DC
952 } else {
953 return;
954 }
955
956 menu.showAt(event.getXY());
e7ade592 957 },
9fa2e36d 958
fe4f00ad
TL
959 // helper for deleting field which are set to there default values
960 delete_if_default: function(values, fieldname, default_val, create) {
961 if (values[fieldname] === '' || values[fieldname] === default_val) {
962 if (!create) {
963 if (values['delete']) {
964 values['delete'] += ',' + fieldname;
965 } else {
966 values['delete'] = fieldname;
967 }
968 }
969
970 delete values[fieldname];
971 }
857b97a7
TL
972 },
973
974 loadSSHKeyFromFile: function(file, callback) {
975 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
976 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
977 // assume: 740*8 for max. 32kbit (5920 byte file)
978 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
979 if (file.size > 8192) {
980 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
981 return;
982 }
983 /*global
984 FileReader
985 */
986 var reader = new FileReader();
987 reader.onload = function(evt) {
988 callback(evt.target.result);
989 };
990 reader.readAsText(file);
abe824aa
DC
991 },
992
993 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
994
995 // types is either undefined (all busses), an array of busses, or a single bus
996 forEachBus: function(types, func) {
997 var busses = Object.keys(PVE.Utils.bus_counts);
998 var i, j, count, cont;
999
1000 if (Ext.isArray(types)) {
1001 busses = types;
1002 } else if (Ext.isDefined(types)) {
1003 busses = [ types ];
1004 }
1005
1006 // check if we only have valid busses
1007 for (i = 0; i < busses.length; i++) {
1008 if (!PVE.Utils.bus_counts[busses[i]]) {
1009 throw "invalid bus: '" + busses[i] + "'";
1010 }
1011 }
1012
1013 for (i = 0; i < busses.length; i++) {
1014 count = PVE.Utils.bus_counts[busses[i]];
1015 for (j = 0; j < count; j++) {
1016 cont = func(busses[i], j);
1017 if (!cont && cont !== undefined) {
1018 return;
1019 }
1020 }
1021 }
14a845bc
DC
1022 },
1023
1024 mp_counts: { mps: 10, unused: 10 },
1025
1026 forEachMP: function(func, includeUnused) {
1027 var i, cont;
1028 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1029 cont = func('mp', i);
1030 if (!cont && cont !== undefined) {
1031 return;
1032 }
1033 }
1034
1035 if (!includeUnused) {
1036 return;
1037 }
1038
1039 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1040 cont = func('unused', i);
1041 if (!cont && cont !== undefined) {
1042 return;
1043 }
1044 }
e7ade592
DC
1045 }
1046},
fe4f00ad 1047
9fa2e36d
EK
1048 singleton: true,
1049 constructor: function() {
1050 var me = this;
1051 Ext.apply(me, me.utilities);
685b7aa4 1052 }
e7ade592 1053
9fa2e36d 1054});
b0a6d326 1055