]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
gui: lxc/Resources: improve reload behaviour
[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': [
beb3c8a4 57 { desc: '10/2016/2019', val: 'win10' },
45d6c71a
TL
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
9c22da32 337 render_spice_enhancements: function(values) {
9c22da32
AL
338 let props = PVE.Parser.parsePropertyString(values);
339 if (Ext.Object.isEmpty(props)) {
3b0facc9 340 return Proxmox.Utils.noneText;
9c22da32
AL
341 }
342
343 let output = [];
344 if (PVE.Parser.parseBoolean(props.foldersharing)) {
345 output.push('Folder Sharing: ' + gettext('Enabled'));
346 }
347 if (props.videostreaming === 'all' || props.videostreaming === 'filter') {
348 output.push('Video Streaming: ' + props.videostreaming);
349 }
350 return output.join(', ');
351 },
352
b0a6d326
EK
353 // fixme: auto-generate this
354 // for now, please keep in sync with PVE::Tools::kvmkeymaps
355 kvm_keymaps: {
356 //ar: 'Arabic',
357 da: 'Danish',
0be88ae1
DC
358 de: 'German',
359 'de-ch': 'German (Swiss)',
360 'en-gb': 'English (UK)',
0a5b8747 361 'en-us': 'English (USA)',
b0a6d326
EK
362 es: 'Spanish',
363 //et: 'Estonia',
364 fi: 'Finnish',
0be88ae1
DC
365 //fo: 'Faroe Islands',
366 fr: 'French',
367 'fr-be': 'French (Belgium)',
b0a6d326
EK
368 'fr-ca': 'French (Canada)',
369 'fr-ch': 'French (Swiss)',
370 //hr: 'Croatia',
371 hu: 'Hungarian',
372 is: 'Icelandic',
0be88ae1 373 it: 'Italian',
b0a6d326
EK
374 ja: 'Japanese',
375 lt: 'Lithuanian',
376 //lv: 'Latvian',
0be88ae1 377 mk: 'Macedonian',
b0a6d326
EK
378 nl: 'Dutch',
379 //'nl-be': 'Dutch (Belgium)',
0be88ae1 380 no: 'Norwegian',
b0a6d326
EK
381 pl: 'Polish',
382 pt: 'Portuguese',
383 'pt-br': 'Portuguese (Brazil)',
384 //ru: 'Russian',
385 sl: 'Slovenian',
386 sv: 'Swedish',
387 //th: 'Thai',
388 tr: 'Turkish'
389 },
390
391 kvm_vga_drivers: {
392 std: gettext('Standard VGA'),
5ca366f2 393 vmware: gettext('VMware compatible'),
b0a6d326
EK
394 qxl: 'SPICE',
395 qxl2: 'SPICE dual monitor',
396 qxl3: 'SPICE three monitors',
397 qxl4: 'SPICE four monitors',
398 serial0: gettext('Serial terminal') + ' 0',
399 serial1: gettext('Serial terminal') + ' 1',
400 serial2: gettext('Serial terminal') + ' 2',
89ae1bb1
DC
401 serial3: gettext('Serial terminal') + ' 3',
402 virtio: 'VirtIO-GPU',
403 none: Proxmox.Utils.noneText
b0a6d326
EK
404 },
405
406 render_kvm_language: function (value) {
e7ade592
DC
407 if (!value || value === '__default__') {
408 return Proxmox.Utils.defaultText;
b0a6d326
EK
409 }
410 var text = PVE.Utils.kvm_keymaps[value];
411 if (text) {
412 return text + ' (' + value + ')';
413 }
414 return value;
415 },
416
417 kvm_keymap_array: function() {
f2782813 418 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
419 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
420 data.push([key, PVE.Utils.render_kvm_language(value)]);
421 });
422
423 return data;
424 },
425
3438c27e 426 console_map: {
da9d14cd 427 '__default__': Proxmox.Utils.defaultText + ' (xterm.js)',
3438c27e
DC
428 'vv': 'SPICE (remote-viewer)',
429 'html5': 'HTML5 (noVNC)',
4ace5c6f 430 'xtermjs': 'xterm.js'
3438c27e
DC
431 },
432
b0a6d326 433 render_console_viewer: function(value) {
3438c27e
DC
434 value = value || '__default__';
435 if (PVE.Utils.console_map[value]) {
436 return PVE.Utils.console_map[value];
b0a6d326 437 }
3438c27e 438 return value;
b0a6d326
EK
439 },
440
755b9083 441 console_viewer_array: function() {
3438c27e 442 return Ext.Array.map(Object.keys(PVE.Utils.console_map), function(v) {
755b9083
TL
443 return [v, PVE.Utils.render_console_viewer(v)];
444 });
445 },
446
b0a6d326
EK
447 render_kvm_vga_driver: function (value) {
448 if (!value) {
e7ade592 449 return Proxmox.Utils.defaultText;
b0a6d326 450 }
4f3e66d8
DC
451 var vga = PVE.Parser.parsePropertyString(value, 'type');
452 var text = PVE.Utils.kvm_vga_drivers[vga.type];
453 if (!vga.type) {
454 text = Proxmox.Utils.defaultText;
455 }
0be88ae1 456 if (text) {
b0a6d326
EK
457 return text + ' (' + value + ')';
458 }
459 return value;
460 },
461
462 kvm_vga_driver_array: function() {
f2782813 463 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
464 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
465 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
466 });
467
468 return data;
469 },
470
471 render_kvm_startup: function(value) {
472 var startup = PVE.Parser.parseStartup(value);
473
474 var res = 'order=';
475 if (startup.order === undefined) {
476 res += 'any';
477 } else {
478 res += startup.order;
479 }
480 if (startup.up !== undefined) {
481 res += ',up=' + startup.up;
482 }
483 if (startup.down !== undefined) {
484 res += ',down=' + startup.down;
485 }
486
487 return res;
488 },
489
b0a6d326
EK
490 extractFormActionError: function(action) {
491 var msg;
492 switch (action.failureType) {
493 case Ext.form.action.Action.CLIENT_INVALID:
494 msg = gettext('Form fields may not be submitted with invalid values');
495 break;
496 case Ext.form.action.Action.CONNECT_FAILURE:
497 msg = gettext('Connection error');
498 var resp = action.response;
499 if (resp.status && resp.statusText) {
500 msg += " " + resp.status + ": " + resp.statusText;
501 }
502 break;
503 case Ext.form.action.Action.LOAD_FAILURE:
504 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 505 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
506 break;
507 }
508 return msg;
509 },
510
b0a6d326 511 format_duration_short: function(ut) {
0be88ae1 512
b0a6d326 513 if (ut < 60) {
530ee6b7 514 return ut.toFixed(1) + 's';
b0a6d326
EK
515 }
516
517 if (ut < 3600) {
518 var mins = ut / 60;
530ee6b7 519 return mins.toFixed(1) + 'm';
b0a6d326
EK
520 }
521
522 if (ut < 86400) {
523 var hours = ut / 3600;
530ee6b7 524 return hours.toFixed(1) + 'h';
b0a6d326
EK
525 }
526
527 var days = ut / 86400;
530ee6b7 528 return days.toFixed(1) + 'd';
b0a6d326
EK
529 },
530
0e244a29
DC
531 contentTypes: {
532 'images': gettext('Disk image'),
533 'backup': gettext('VZDump backup file'),
534 'vztmpl': gettext('Container template'),
535 'iso': gettext('ISO image'),
aef28e04
DC
536 'rootdir': gettext('Container'),
537 'snippets': gettext('Snippets')
0e244a29 538 },
b0a6d326 539
062a7f49
TL
540 storageSchema: {
541 dir: {
542 name: Proxmox.Utils.directoryText,
543 ipanel: 'DirInputPanel',
544 faIcon: 'folder'
545 },
546 lvm: {
547 name: 'LVM',
548 ipanel: 'LVMInputPanel',
549 faIcon: 'folder'
550 },
551 lvmthin: {
552 name: 'LVM-Thin',
553 ipanel: 'LvmThinInputPanel',
554 faIcon: 'folder'
555 },
556 nfs: {
557 name: 'NFS',
558 ipanel: 'NFSInputPanel',
559 faIcon: 'building'
560 },
561 cifs: {
562 name: 'CIFS',
563 ipanel: 'CIFSInputPanel',
564 faIcon: 'building'
565 },
566 glusterfs: {
567 name: 'GlusterFS',
568 ipanel: 'GlusterFsInputPanel',
569 faIcon: 'building'
570 },
571 iscsi: {
572 name: 'iSCSI',
573 ipanel: 'IScsiInputPanel',
574 faIcon: 'building'
575 },
4a4b2b6e
TL
576 cephfs: {
577 name: 'CephFS',
578 ipanel: 'CephFSInputPanel',
579 faIcon: 'building'
580 },
581 pvecephfs: {
582 name: 'CephFS (PVE)',
583 ipanel: 'CephFSInputPanel',
584 hideAdd: true,
585 faIcon: 'building'
586 },
062a7f49
TL
587 rbd: {
588 name: 'RBD',
589 ipanel: 'RBDInputPanel',
062a7f49
TL
590 faIcon: 'building'
591 },
592 pveceph: {
593 name: 'RBD (PVE)',
0d1ac958
TL
594 ipanel: 'RBDInputPanel',
595 hideAdd: true,
062a7f49
TL
596 faIcon: 'building'
597 },
598 zfs: {
599 name: 'ZFS over iSCSI',
600 ipanel: 'ZFSInputPanel',
601 faIcon: 'building'
602 },
603 zfspool: {
604 name: 'ZFS',
605 ipanel: 'ZFSPoolInputPanel',
606 faIcon: 'folder'
607 },
608 drbd: {
609 name: 'DRBD',
610 hideAdd: true
611 }
612 },
613
3c23c025 614 format_storage_type: function(value, md, record) {
4a4b2b6e
TL
615 if (value === 'rbd') {
616 value = (!record || record.get('monhost') ? 'rbd' : 'pveceph');
617 } else if (value === 'cephfs') {
618 value = (!record || record.get('monhost') ? 'cephfs' : 'pvecephfs');
3c23c025 619 }
062a7f49
TL
620
621 var schema = PVE.Utils.storageSchema[value];
622 if (schema) {
623 return schema.name;
b0a6d326 624 }
062a7f49 625 return Proxmox.Utils.unknownText;
a3b8efb4
EK
626 },
627
ced1677b 628 format_ha: function(value) {
e7ade592 629 var text = Proxmox.Utils.noneText;
ced1677b
TL
630
631 if (value.managed) {
e7ade592 632 text = value.state || Proxmox.Utils.noneText;
ced1677b 633
e7ade592
DC
634 text += ', ' + Proxmox.Utils.groupText + ': ';
635 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
636 }
637
638 return text;
639 },
640
b0a6d326 641 format_content_types: function(value) {
0e244a29
DC
642 return value.split(',').sort().map(function(ct) {
643 return PVE.Utils.contentTypes[ct] || ct;
644 }).join(', ');
b0a6d326
EK
645 },
646
647 render_storage_content: function(value, metaData, record) {
648 var data = record.data;
649 if (Ext.isNumber(data.channel) &&
650 Ext.isNumber(data.id) &&
651 Ext.isNumber(data.lun)) {
0be88ae1
DC
652 return "CH " +
653 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
654 " ID " + data.id + " LUN " + data.lun;
655 }
656 return data.volid.replace(/^.*:(.*\/)?/,'');
657 },
658
659 render_serverity: function (value) {
660 return PVE.Utils.log_severity_hash[value] || value;
661 },
662
663 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
664
665 if (!(record.data.uptime && Ext.isNumeric(value))) {
666 return '';
667 }
668
669 var maxcpu = record.data.maxcpu || 1;
670
671 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
672 return '';
673 }
0be88ae1 674
b0a6d326
EK
675 var per = value * 100;
676
677 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
678 },
679
680 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
681 /*jslint confusion: true */
682
683 if (!Ext.isNumeric(value)) {
684 return '';
685 }
686
e7ade592 687 return Proxmox.Utils.format_size(value);
b0a6d326
EK
688 },
689
946730cd
DC
690 render_bandwidth: function(value) {
691 if (!Ext.isNumeric(value)) {
692 return '';
693 }
694
e7ade592 695 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
696 },
697
3f633655
EK
698 render_timestamp_human_readable: function(value) {
699 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
700 },
701
ddd26302
DC
702 render_duration: function(value) {
703 if (value === undefined) {
704 return '-';
705 }
706 return PVE.Utils.format_duration_short(value);
707 },
708
0bfc799f
DC
709 calculate_mem_usage: function(data) {
710 if (!Ext.isNumeric(data.mem) ||
711 data.maxmem === 0 ||
712 data.uptime < 1) {
713 return -1;
714 }
715
716 return (data.mem / data.maxmem);
717 },
718
719 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
720 if (!Ext.isNumeric(value) || value === -1) {
721 return '';
722 }
723 if (value > 1 ) {
724 // we got no percentage but bytes
725 var mem = value;
726 var maxmem = record.data.maxmem;
727 if (!record.data.uptime ||
728 maxmem === 0 ||
729 !Ext.isNumeric(mem)) {
730 return '';
731 }
732
733 return ((mem*100)/maxmem).toFixed(1) + " %";
734 }
735 return (value*100).toFixed(1) + " %";
736 },
737
b0a6d326
EK
738 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
739
740 var mem = value;
741 var maxmem = record.data.maxmem;
0be88ae1 742
b0a6d326
EK
743 if (!record.data.uptime) {
744 return '';
745 }
746
747 if (!(Ext.isNumeric(mem) && maxmem)) {
748 return '';
749 }
750
728f1b97 751 return PVE.Utils.render_size(value);
b0a6d326
EK
752 },
753
0bfc799f
DC
754 calculate_disk_usage: function(data) {
755
756 if (!Ext.isNumeric(data.disk) ||
757 data.type === 'qemu' ||
758 (data.type === 'lxc' && data.uptime === 0) ||
759 data.maxdisk === 0) {
760 return -1;
761 }
762
763 return (data.disk / data.maxdisk);
764 },
765
766 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
767 if (!Ext.isNumeric(value) || value === -1) {
768 return '';
769 }
770
771 return (value * 100).toFixed(1) + " %";
772 },
773
b0a6d326
EK
774 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
775
776 var disk = value;
777 var maxdisk = record.data.maxdisk;
728f1b97 778 var type = record.data.type;
b0a6d326 779
728f1b97
DC
780 if (!Ext.isNumeric(disk) ||
781 type === 'qemu' ||
782 maxdisk === 0 ||
783 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
784 return '';
785 }
786
728f1b97 787 return PVE.Utils.render_size(value);
b0a6d326
EK
788 },
789
4dbc64a7
DC
790 get_object_icon_class: function(type, record) {
791 var status = '';
792 var objType = type;
793
794 if (type === 'type') {
795 // for folder view
796 objType = record.groupbyid;
797 } else if (record.template) {
798 // templates
799 objType = 'template';
800 status = type;
801 } else {
802 // everything else
803 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
804 }
805
6284a48a
DC
806 if (record.lock) {
807 status += ' locked lock-' + record.lock;
808 }
809
4dbc64a7
DC
810 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
811 if (defaults && defaults.iconCls) {
812 var retVal = defaults.iconCls + ' ' + status;
813 return retVal;
b0a6d326
EK
814 }
815
4dbc64a7
DC
816 return '';
817 },
818
819 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
820
821 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 822
4dbc64a7 823 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 824 return fa + value;
b0a6d326
EK
825 },
826
b0a6d326
EK
827 render_support_level: function(value, metaData, record) {
828 return PVE.Utils.support_level_hash[value] || '-';
829 },
830
0be88ae1 831 render_upid: function(value, metaData, record) {
b0a6d326
EK
832 var type = record.data.type;
833 var id = record.data.id;
834
e7ade592 835 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
836 },
837
054ac1b8
DC
838 /* render functions for new status panel */
839
840 render_usage: function(val) {
841 return (val*100).toFixed(2) + '%';
842 },
843
844 render_cpu_usage: function(val, max) {
845 return Ext.String.format(gettext('{0}% of {1}') +
846 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
847 },
848
849 render_size_usage: function(val, max) {
bab64974
DC
850 if (max === 0) {
851 return gettext('N/A');
852 }
054ac1b8
DC
853 return (val*100/max).toFixed(2) + '% '+ '(' +
854 Ext.String.format(gettext('{0} of {1}'),
855 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
856 },
857
858 /* this is different for nodes */
859 render_node_cpu_usage: function(value, record) {
860 return PVE.Utils.render_cpu_usage(value, record.cpus);
861 },
862
863 /* this is different for nodes */
864 render_node_size_usage: function(record) {
865 return PVE.Utils.render_size_usage(record.used, record.total);
866 },
867
27809975
DC
868 render_optional_url: function(value) {
869 var match;
870 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 871 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
872 }
873 return value;
874 },
875
876 render_san: function(value) {
877 var names = [];
878 if (Ext.isArray(value)) {
879 value.forEach(function(val) {
880 if (!Ext.isNumber(val)) {
881 names.push(val);
882 }
883 });
884 return names.join('<br>');
885 }
886 return value;
887 },
888
6ad4be69
DC
889 render_full_name: function(firstname, metaData, record) {
890 var first = firstname || '';
891 var last = record.data.lastname || '';
892 return Ext.htmlEncode(first + " " + last);
893 },
894
2d41c7e6
TL
895 render_u2f_error: function(error) {
896 var ErrorNames = {
897 '1': gettext('Other Error'),
898 '2': gettext('Bad Request'),
899 '3': gettext('Configuration Unsupported'),
900 '4': gettext('Device Ineligible'),
901 '5': gettext('Timeout')
902 };
903 return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
904 },
905
aa0819a8 906 windowHostname: function() {
e7ade592 907 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
908 function(m, addr, offset, original) { return addr; });
909 },
0be88ae1 910
8eccc68f 911 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname, cmd) {
3438c27e 912 var dv = PVE.Utils.defaultViewer(consoles);
8eccc68f 913 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
914 },
915
8eccc68f 916 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname, cmd) {
9e361643 917 // kvm, lxc, shell, upgrade
b0a6d326 918
9e361643 919 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
920 throw "missing vmid";
921 }
922
923 if (!nodename) {
924 throw "no nodename specified";
925 }
926
c7218ab3 927 if (viewer === 'html5') {
8eccc68f 928 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname, cmd);
c6b2336c 929 } else if (viewer === 'xtermjs') {
8eccc68f 930 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
931 } else if (viewer === 'vv') {
932 var url;
aa0819a8 933 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
934 if (vmtype === 'kvm') {
935 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
936 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
937 } else if (vmtype === 'lxc') {
938 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
939 PVE.Utils.openSpiceViewer(url, params);
940 } else if (vmtype === 'shell') {
941 url = '/nodes/' + nodename + '/spiceshell';
942 PVE.Utils.openSpiceViewer(url, params);
943 } else if (vmtype === 'upgrade') {
944 url = '/nodes/' + nodename + '/spiceshell';
945 params.upgrade = 1;
946 PVE.Utils.openSpiceViewer(url, params);
8eccc68f
TM
947 } else if (vmtype === 'cmd') {
948 url = '/nodes/' + nodename + '/spiceshell';
949 params.cmd = cmd;
950 PVE.Utils.openSpiceViewer(url, params);
b0a6d326
EK
951 }
952 } else {
953 throw "unknown viewer type";
954 }
955 },
956
3438c27e
DC
957 defaultViewer: function(consoles) {
958
959 var allowSpice, allowXtermjs;
960
961 if (consoles === true) {
962 allowSpice = true;
963 allowXtermjs = true;
964 } else if (typeof consoles === 'object') {
965 allowSpice = consoles.spice;
4ace5c6f 966 allowXtermjs = !!consoles.xtermjs;
3438c27e 967 }
da9d14cd 968 var dv = PVE.VersionInfo.console || 'xtermjs';
f932cffa
DC
969 if (dv === 'vv' && !allowSpice) {
970 dv = (allowXtermjs) ? 'xtermjs' : 'html5';
971 } else if (dv === 'xtermjs' && !allowXtermjs) {
972 dv = (allowSpice) ? 'vv' : 'html5';
b0a6d326
EK
973 }
974
975 return dv;
976 },
977
8eccc68f 978 openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
af89f682
TL
979 let scaling = 'off';
980 if (Proxmox.Utils.toolkit !== 'touch') {
981 var sp = Ext.state.Manager.getProvider();
982 scaling = sp.get('novnc-scaling', 'off');
983 }
8eccc68f 984 var url = Ext.Object.toQueryString({
9e361643 985 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 986 novnc: 1,
b0a6d326
EK
987 vmid: vmid,
988 vmname: vmname,
16e64c97 989 node: nodename,
af89f682 990 resize: scaling,
8eccc68f 991 cmd: cmd
b0a6d326
EK
992 });
993 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
7af1ab47
DC
994 if (nw) {
995 nw.focus();
996 }
b0a6d326
EK
997 },
998
999 openSpiceViewer: function(url, params){
1000
1001 var downloadWithName = function(uri, name) {
1002 var link = Ext.DomHelper.append(document.body, {
1003 tag: 'a',
1004 href: uri,
1005 css : 'display:none;visibility:hidden;height:0px;'
1006 });
1007
1008 // Note: we need to tell android the correct file name extension
1009 // but we do not set 'download' tag for other environments, because
1010 // It can have strange side effects (additional user prompt on firefox)
1011 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1012 if (andriod) {
1013 link.download = name;
1014 }
1015
1016 if (link.fireEvent) {
1017 link.fireEvent('onclick');
1018 } else {
1019 var evt = document.createEvent("MouseEvents");
1020 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1021 link.dispatchEvent(evt);
1022 }
1023 };
1024
e7ade592 1025 Proxmox.Utils.API2Request({
b0a6d326
EK
1026 url: url,
1027 params: params,
1028 method: 'POST',
1029 failure: function(response, opts){
1030 Ext.Msg.alert('Error', response.htmlStatus);
1031 },
1032 success: function(response, opts){
1033 var raw = "[virt-viewer]\n";
1034 Ext.Object.each(response.result.data, function(k, v) {
1035 raw += k + "=" + v + "\n";
1036 });
1037 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1038 encodeURIComponent(raw);
0be88ae1 1039
b0a6d326
EK
1040 downloadWithName(url, "pve-spice.vv");
1041 }
1042 });
1043 },
1044
e3129443
DC
1045 openTreeConsole: function(tree, record, item, index, e) {
1046 e.stopEvent();
1047 var nodename = record.data.node;
1048 var vmid = record.data.vmid;
1049 var vmname = record.data.name;
1050 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 1051 Proxmox.Utils.API2Request({
e3129443
DC
1052 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1053 failure: function(response, opts) {
1054 Ext.Msg.alert('Error', response.htmlStatus);
1055 },
1056 success: function(response, opts) {
bd9537d7 1057 let conf = response.result.data;
54453c38 1058 var consoles = {
bd9537d7
TL
1059 spice: !!conf.spice,
1060 xtermjs: !!conf.serial,
54453c38
DC
1061 };
1062 PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
e3129443
DC
1063 }
1064 });
1065 } else if (record.data.type === 'lxc' && !record.data.template) {
1066 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1067 }
1068 },
1069
fbd60cfd
DM
1070 // test automation helper
1071 call_menu_handler: function(menu, text) {
1072
1073 var list = menu.query('menuitem');
1074
1075 Ext.Array.each(list, function(item) {
1076 if (item.text === text) {
1077 if (item.handler) {
1078 item.handler();
1079 return 1;
1080 } else {
1081 return undefined;
1082 }
1083 }
1084 });
1085 },
1086
685b7aa4
DC
1087 createCmdMenu: function(v, record, item, index, event) {
1088 event.stopEvent();
cc1a91be
DC
1089 if (!(v instanceof Ext.tree.View)) {
1090 v.select(record);
1091 }
685b7aa4 1092 var menu;
9bad05bd
DC
1093 var template = !!record.data.template;
1094 var type = record.data.type;
685b7aa4 1095
9bad05bd
DC
1096 if (template) {
1097 if (type === 'qemu' || type == 'lxc') {
1098 menu = Ext.create('PVE.menu.TemplateMenu', {
1099 pveSelNode: record
1100 });
1101 }
1102 } else if (type === 'qemu' ||
1103 type === 'lxc' ||
1104 type === 'node') {
1105 menu = Ext.create('PVE.' + type + '.CmdMenu', {
1106 pveSelNode: record,
c11ab8cb
DC
1107 nodename: record.data.node
1108 });
685b7aa4
DC
1109 } else {
1110 return;
1111 }
1112
1113 menu.showAt(event.getXY());
9f0b4e04 1114 return menu;
e7ade592 1115 },
9fa2e36d 1116
fe4f00ad
TL
1117 // helper for deleting field which are set to there default values
1118 delete_if_default: function(values, fieldname, default_val, create) {
1119 if (values[fieldname] === '' || values[fieldname] === default_val) {
1120 if (!create) {
1121 if (values['delete']) {
1122 values['delete'] += ',' + fieldname;
1123 } else {
1124 values['delete'] = fieldname;
1125 }
1126 }
1127
1128 delete values[fieldname];
1129 }
857b97a7
TL
1130 },
1131
1132 loadSSHKeyFromFile: function(file, callback) {
1133 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1134 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1135 // assume: 740*8 for max. 32kbit (5920 byte file)
1136 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1137 if (file.size > 8192) {
1138 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1139 return;
1140 }
1141 /*global
1142 FileReader
1143 */
1144 var reader = new FileReader();
1145 reader.onload = function(evt) {
1146 callback(evt.target.result);
1147 };
1148 reader.readAsText(file);
abe824aa
DC
1149 },
1150
1151 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
1152
1153 // types is either undefined (all busses), an array of busses, or a single bus
1154 forEachBus: function(types, func) {
1155 var busses = Object.keys(PVE.Utils.bus_counts);
1156 var i, j, count, cont;
1157
1158 if (Ext.isArray(types)) {
1159 busses = types;
1160 } else if (Ext.isDefined(types)) {
1161 busses = [ types ];
1162 }
1163
1164 // check if we only have valid busses
1165 for (i = 0; i < busses.length; i++) {
1166 if (!PVE.Utils.bus_counts[busses[i]]) {
1167 throw "invalid bus: '" + busses[i] + "'";
1168 }
1169 }
1170
1171 for (i = 0; i < busses.length; i++) {
1172 count = PVE.Utils.bus_counts[busses[i]];
1173 for (j = 0; j < count; j++) {
1174 cont = func(busses[i], j);
1175 if (!cont && cont !== undefined) {
1176 return;
1177 }
1178 }
1179 }
14a845bc
DC
1180 },
1181
483bd394 1182 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1183
1184 forEachMP: function(func, includeUnused) {
1185 var i, cont;
1186 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1187 cont = func('mp', i);
1188 if (!cont && cont !== undefined) {
1189 return;
1190 }
1191 }
1192
1193 if (!includeUnused) {
1194 return;
1195 }
1196
1197 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1198 cont = func('unused', i);
1199 if (!cont && cont !== undefined) {
1200 return;
1201 }
1202 }
b945c7c1
TM
1203 },
1204
1205 cleanEmptyObjectKeys: function (obj) {
1206 var propName;
1207 for (propName in obj) {
1208 if (obj.hasOwnProperty(propName)) {
1209 if (obj[propName] === null || obj[propName] === undefined) {
1210 delete obj[propName];
1211 }
1212 }
1213 }
4616a55b
TM
1214 },
1215
1216 handleStoreErrorOrMask: function(me, store, regex, callback) {
1217
1218 me.mon(store, 'load', function (proxy, response, success, operation) {
1219
1220 if (success) {
1221 Proxmox.Utils.setErrorMask(me, false);
1222 return;
1223 }
1224 var msg;
1225
1226 if (operation.error.statusText) {
1227 if (operation.error.statusText.match(regex)) {
1228 callback(me, operation.error);
1229 return;
1230 } else {
1231 msg = operation.error.statusText + ' (' + operation.error.status + ')';
1232 }
1233 } else {
1234 msg = gettext('Connection error');
1235 }
1236 Proxmox.Utils.setErrorMask(me, msg);
1237 });
1238 },
1239
1240 showCephInstallOrMask: function(container, msg, nodename, callback){
1241 var regex = new RegExp("not (installed|initialized)", "i");
1242 if (msg.match(regex)) {
1243 if (Proxmox.UserName === 'root@pam') {
1244 container.el.mask();
1245 if (!container.down('pveCephInstallWindow')){
f992ef80 1246 var isInstalled = msg.match(/not initialized/i) ? true : false;
4616a55b
TM
1247 var win = Ext.create('PVE.ceph.Install', {
1248 nodename: nodename
1249 });
f992ef80 1250 win.getViewModel().set('isInstalled', isInstalled);
4616a55b
TM
1251 container.add(win);
1252 win.show();
1253 callback(win);
1254 }
1255 } else {
a7e8b87b
TL
1256 container.mask(Ext.String.format(gettext('{0} not installed.') +
1257 ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
4616a55b
TM
1258 }
1259 return true;
1260 } else {
1261 return false;
1262 }
e7ade592
DC
1263 }
1264},
fe4f00ad 1265
9fa2e36d
EK
1266 singleton: true,
1267 constructor: function() {
1268 var me = this;
1269 Ext.apply(me, me.utilities);
685b7aa4 1270 }
e7ade592 1271
9fa2e36d 1272});
b0a6d326 1273