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