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