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