]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
d/control: bump versioned dependency for ifupdown2
[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
3b8f599b
DM
548 volume_is_qemu_backup: function(volid, format) {
549 return format === 'pbs-vm' || volid.match(':backup/vzdump-qemu-');
550 },
551
552 volume_is_lxc_backup: function(volid, format) {
553 return format === 'pbs-ct' || volid.match(':backup/vzdump-(lxc|openvz)-');
554 },
555
062a7f49
TL
556 storageSchema: {
557 dir: {
558 name: Proxmox.Utils.directoryText,
559 ipanel: 'DirInputPanel',
560 faIcon: 'folder'
561 },
562 lvm: {
563 name: 'LVM',
564 ipanel: 'LVMInputPanel',
565 faIcon: 'folder'
566 },
567 lvmthin: {
568 name: 'LVM-Thin',
569 ipanel: 'LvmThinInputPanel',
570 faIcon: 'folder'
571 },
572 nfs: {
573 name: 'NFS',
574 ipanel: 'NFSInputPanel',
575 faIcon: 'building'
576 },
577 cifs: {
578 name: 'CIFS',
579 ipanel: 'CIFSInputPanel',
580 faIcon: 'building'
581 },
582 glusterfs: {
583 name: 'GlusterFS',
584 ipanel: 'GlusterFsInputPanel',
585 faIcon: 'building'
586 },
587 iscsi: {
588 name: 'iSCSI',
589 ipanel: 'IScsiInputPanel',
590 faIcon: 'building'
591 },
4a4b2b6e
TL
592 cephfs: {
593 name: 'CephFS',
594 ipanel: 'CephFSInputPanel',
595 faIcon: 'building'
596 },
597 pvecephfs: {
598 name: 'CephFS (PVE)',
599 ipanel: 'CephFSInputPanel',
600 hideAdd: true,
601 faIcon: 'building'
602 },
062a7f49
TL
603 rbd: {
604 name: 'RBD',
605 ipanel: 'RBDInputPanel',
062a7f49
TL
606 faIcon: 'building'
607 },
608 pveceph: {
609 name: 'RBD (PVE)',
0d1ac958
TL
610 ipanel: 'RBDInputPanel',
611 hideAdd: true,
062a7f49
TL
612 faIcon: 'building'
613 },
614 zfs: {
615 name: 'ZFS over iSCSI',
616 ipanel: 'ZFSInputPanel',
617 faIcon: 'building'
618 },
619 zfspool: {
620 name: 'ZFS',
621 ipanel: 'ZFSPoolInputPanel',
622 faIcon: 'folder'
623 },
624 drbd: {
625 name: 'DRBD',
626 hideAdd: true
627 }
628 },
629
3c23c025 630 format_storage_type: function(value, md, record) {
4a4b2b6e
TL
631 if (value === 'rbd') {
632 value = (!record || record.get('monhost') ? 'rbd' : 'pveceph');
633 } else if (value === 'cephfs') {
634 value = (!record || record.get('monhost') ? 'cephfs' : 'pvecephfs');
3c23c025 635 }
062a7f49
TL
636
637 var schema = PVE.Utils.storageSchema[value];
638 if (schema) {
639 return schema.name;
b0a6d326 640 }
062a7f49 641 return Proxmox.Utils.unknownText;
a3b8efb4
EK
642 },
643
ced1677b 644 format_ha: function(value) {
e7ade592 645 var text = Proxmox.Utils.noneText;
ced1677b
TL
646
647 if (value.managed) {
e7ade592 648 text = value.state || Proxmox.Utils.noneText;
ced1677b 649
e7ade592
DC
650 text += ', ' + Proxmox.Utils.groupText + ': ';
651 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
652 }
653
654 return text;
655 },
656
b0a6d326 657 format_content_types: function(value) {
0e244a29
DC
658 return value.split(',').sort().map(function(ct) {
659 return PVE.Utils.contentTypes[ct] || ct;
660 }).join(', ');
b0a6d326
EK
661 },
662
663 render_storage_content: function(value, metaData, record) {
664 var data = record.data;
665 if (Ext.isNumber(data.channel) &&
666 Ext.isNumber(data.id) &&
667 Ext.isNumber(data.lun)) {
0be88ae1
DC
668 return "CH " +
669 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
670 " ID " + data.id + " LUN " + data.lun;
671 }
3b8f599b 672 return data.volid.replace(/^.*?:(.*?\/)?/,'');
b0a6d326
EK
673 },
674
675 render_serverity: function (value) {
676 return PVE.Utils.log_severity_hash[value] || value;
677 },
678
679 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
680
681 if (!(record.data.uptime && Ext.isNumeric(value))) {
682 return '';
683 }
684
685 var maxcpu = record.data.maxcpu || 1;
686
687 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
688 return '';
689 }
0be88ae1 690
b0a6d326
EK
691 var per = value * 100;
692
693 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
694 },
695
696 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
697 /*jslint confusion: true */
698
699 if (!Ext.isNumeric(value)) {
700 return '';
701 }
702
e7ade592 703 return Proxmox.Utils.format_size(value);
b0a6d326
EK
704 },
705
946730cd
DC
706 render_bandwidth: function(value) {
707 if (!Ext.isNumeric(value)) {
708 return '';
709 }
710
e7ade592 711 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
712 },
713
3f633655
EK
714 render_timestamp_human_readable: function(value) {
715 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
716 },
717
ddd26302
DC
718 render_duration: function(value) {
719 if (value === undefined) {
720 return '-';
721 }
722 return PVE.Utils.format_duration_short(value);
723 },
724
0bfc799f
DC
725 calculate_mem_usage: function(data) {
726 if (!Ext.isNumeric(data.mem) ||
727 data.maxmem === 0 ||
728 data.uptime < 1) {
729 return -1;
730 }
731
732 return (data.mem / data.maxmem);
733 },
734
735 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
736 if (!Ext.isNumeric(value) || value === -1) {
737 return '';
738 }
739 if (value > 1 ) {
740 // we got no percentage but bytes
741 var mem = value;
742 var maxmem = record.data.maxmem;
743 if (!record.data.uptime ||
744 maxmem === 0 ||
745 !Ext.isNumeric(mem)) {
746 return '';
747 }
748
749 return ((mem*100)/maxmem).toFixed(1) + " %";
750 }
751 return (value*100).toFixed(1) + " %";
752 },
753
b0a6d326
EK
754 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
755
756 var mem = value;
757 var maxmem = record.data.maxmem;
0be88ae1 758
b0a6d326
EK
759 if (!record.data.uptime) {
760 return '';
761 }
762
763 if (!(Ext.isNumeric(mem) && maxmem)) {
764 return '';
765 }
766
728f1b97 767 return PVE.Utils.render_size(value);
b0a6d326
EK
768 },
769
0bfc799f
DC
770 calculate_disk_usage: function(data) {
771
772 if (!Ext.isNumeric(data.disk) ||
773 data.type === 'qemu' ||
774 (data.type === 'lxc' && data.uptime === 0) ||
775 data.maxdisk === 0) {
776 return -1;
777 }
778
779 return (data.disk / data.maxdisk);
780 },
781
782 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
783 if (!Ext.isNumeric(value) || value === -1) {
784 return '';
785 }
786
787 return (value * 100).toFixed(1) + " %";
788 },
789
b0a6d326
EK
790 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
791
792 var disk = value;
793 var maxdisk = record.data.maxdisk;
728f1b97 794 var type = record.data.type;
b0a6d326 795
728f1b97
DC
796 if (!Ext.isNumeric(disk) ||
797 type === 'qemu' ||
798 maxdisk === 0 ||
799 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
800 return '';
801 }
802
728f1b97 803 return PVE.Utils.render_size(value);
b0a6d326
EK
804 },
805
4dbc64a7
DC
806 get_object_icon_class: function(type, record) {
807 var status = '';
808 var objType = type;
809
810 if (type === 'type') {
811 // for folder view
812 objType = record.groupbyid;
813 } else if (record.template) {
814 // templates
815 objType = 'template';
816 status = type;
817 } else {
818 // everything else
819 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
820 }
821
6284a48a
DC
822 if (record.lock) {
823 status += ' locked lock-' + record.lock;
824 }
825
4dbc64a7
DC
826 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
827 if (defaults && defaults.iconCls) {
828 var retVal = defaults.iconCls + ' ' + status;
829 return retVal;
b0a6d326
EK
830 }
831
4dbc64a7
DC
832 return '';
833 },
834
835 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
836
837 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 838
4dbc64a7 839 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 840 return fa + value;
b0a6d326
EK
841 },
842
b0a6d326
EK
843 render_support_level: function(value, metaData, record) {
844 return PVE.Utils.support_level_hash[value] || '-';
845 },
846
0be88ae1 847 render_upid: function(value, metaData, record) {
b0a6d326
EK
848 var type = record.data.type;
849 var id = record.data.id;
850
e7ade592 851 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
852 },
853
054ac1b8
DC
854 /* render functions for new status panel */
855
856 render_usage: function(val) {
857 return (val*100).toFixed(2) + '%';
858 },
859
860 render_cpu_usage: function(val, max) {
861 return Ext.String.format(gettext('{0}% of {1}') +
862 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
863 },
864
865 render_size_usage: function(val, max) {
bab64974
DC
866 if (max === 0) {
867 return gettext('N/A');
868 }
054ac1b8
DC
869 return (val*100/max).toFixed(2) + '% '+ '(' +
870 Ext.String.format(gettext('{0} of {1}'),
871 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
872 },
873
874 /* this is different for nodes */
875 render_node_cpu_usage: function(value, record) {
876 return PVE.Utils.render_cpu_usage(value, record.cpus);
877 },
878
879 /* this is different for nodes */
880 render_node_size_usage: function(record) {
881 return PVE.Utils.render_size_usage(record.used, record.total);
882 },
883
27809975
DC
884 render_optional_url: function(value) {
885 var match;
886 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 887 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
888 }
889 return value;
890 },
891
892 render_san: function(value) {
893 var names = [];
894 if (Ext.isArray(value)) {
895 value.forEach(function(val) {
896 if (!Ext.isNumber(val)) {
897 names.push(val);
898 }
899 });
900 return names.join('<br>');
901 }
902 return value;
903 },
904
6ad4be69
DC
905 render_full_name: function(firstname, metaData, record) {
906 var first = firstname || '';
907 var last = record.data.lastname || '';
908 return Ext.htmlEncode(first + " " + last);
909 },
910
2d41c7e6
TL
911 render_u2f_error: function(error) {
912 var ErrorNames = {
913 '1': gettext('Other Error'),
914 '2': gettext('Bad Request'),
915 '3': gettext('Configuration Unsupported'),
916 '4': gettext('Device Ineligible'),
917 '5': gettext('Timeout')
918 };
919 return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
920 },
921
aa0819a8 922 windowHostname: function() {
e7ade592 923 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
924 function(m, addr, offset, original) { return addr; });
925 },
0be88ae1 926
8eccc68f 927 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname, cmd) {
3438c27e 928 var dv = PVE.Utils.defaultViewer(consoles);
8eccc68f 929 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
930 },
931
8eccc68f 932 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname, cmd) {
9e361643 933 // kvm, lxc, shell, upgrade
b0a6d326 934
9e361643 935 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
936 throw "missing vmid";
937 }
938
939 if (!nodename) {
940 throw "no nodename specified";
941 }
942
c7218ab3 943 if (viewer === 'html5') {
8eccc68f 944 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname, cmd);
c6b2336c 945 } else if (viewer === 'xtermjs') {
8eccc68f 946 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
947 } else if (viewer === 'vv') {
948 var url;
aa0819a8 949 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
950 if (vmtype === 'kvm') {
951 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
952 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
953 } else if (vmtype === 'lxc') {
954 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
955 PVE.Utils.openSpiceViewer(url, params);
956 } else if (vmtype === 'shell') {
957 url = '/nodes/' + nodename + '/spiceshell';
958 PVE.Utils.openSpiceViewer(url, params);
959 } else if (vmtype === 'upgrade') {
960 url = '/nodes/' + nodename + '/spiceshell';
961 params.upgrade = 1;
962 PVE.Utils.openSpiceViewer(url, params);
8eccc68f
TM
963 } else if (vmtype === 'cmd') {
964 url = '/nodes/' + nodename + '/spiceshell';
965 params.cmd = cmd;
966 PVE.Utils.openSpiceViewer(url, params);
b0a6d326
EK
967 }
968 } else {
969 throw "unknown viewer type";
970 }
971 },
972
3438c27e
DC
973 defaultViewer: function(consoles) {
974
975 var allowSpice, allowXtermjs;
976
977 if (consoles === true) {
978 allowSpice = true;
979 allowXtermjs = true;
980 } else if (typeof consoles === 'object') {
981 allowSpice = consoles.spice;
4ace5c6f 982 allowXtermjs = !!consoles.xtermjs;
3438c27e 983 }
da9d14cd 984 var dv = PVE.VersionInfo.console || 'xtermjs';
f932cffa
DC
985 if (dv === 'vv' && !allowSpice) {
986 dv = (allowXtermjs) ? 'xtermjs' : 'html5';
987 } else if (dv === 'xtermjs' && !allowXtermjs) {
988 dv = (allowSpice) ? 'vv' : 'html5';
b0a6d326
EK
989 }
990
991 return dv;
992 },
993
8eccc68f 994 openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
af89f682
TL
995 let scaling = 'off';
996 if (Proxmox.Utils.toolkit !== 'touch') {
997 var sp = Ext.state.Manager.getProvider();
998 scaling = sp.get('novnc-scaling', 'off');
999 }
8eccc68f 1000 var url = Ext.Object.toQueryString({
9e361643 1001 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 1002 novnc: 1,
b0a6d326
EK
1003 vmid: vmid,
1004 vmname: vmname,
16e64c97 1005 node: nodename,
af89f682 1006 resize: scaling,
8eccc68f 1007 cmd: cmd
b0a6d326
EK
1008 });
1009 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
7af1ab47
DC
1010 if (nw) {
1011 nw.focus();
1012 }
b0a6d326
EK
1013 },
1014
1015 openSpiceViewer: function(url, params){
1016
1017 var downloadWithName = function(uri, name) {
1018 var link = Ext.DomHelper.append(document.body, {
1019 tag: 'a',
1020 href: uri,
1021 css : 'display:none;visibility:hidden;height:0px;'
1022 });
1023
1024 // Note: we need to tell android the correct file name extension
1025 // but we do not set 'download' tag for other environments, because
1026 // It can have strange side effects (additional user prompt on firefox)
1027 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1028 if (andriod) {
1029 link.download = name;
1030 }
1031
1032 if (link.fireEvent) {
1033 link.fireEvent('onclick');
1034 } else {
1035 var evt = document.createEvent("MouseEvents");
1036 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1037 link.dispatchEvent(evt);
1038 }
1039 };
1040
e7ade592 1041 Proxmox.Utils.API2Request({
b0a6d326
EK
1042 url: url,
1043 params: params,
1044 method: 'POST',
1045 failure: function(response, opts){
1046 Ext.Msg.alert('Error', response.htmlStatus);
1047 },
1048 success: function(response, opts){
1049 var raw = "[virt-viewer]\n";
1050 Ext.Object.each(response.result.data, function(k, v) {
1051 raw += k + "=" + v + "\n";
1052 });
1053 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1054 encodeURIComponent(raw);
0be88ae1 1055
b0a6d326
EK
1056 downloadWithName(url, "pve-spice.vv");
1057 }
1058 });
1059 },
1060
e3129443
DC
1061 openTreeConsole: function(tree, record, item, index, e) {
1062 e.stopEvent();
1063 var nodename = record.data.node;
1064 var vmid = record.data.vmid;
1065 var vmname = record.data.name;
1066 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 1067 Proxmox.Utils.API2Request({
e3129443
DC
1068 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1069 failure: function(response, opts) {
1070 Ext.Msg.alert('Error', response.htmlStatus);
1071 },
1072 success: function(response, opts) {
bd9537d7 1073 let conf = response.result.data;
54453c38 1074 var consoles = {
bd9537d7
TL
1075 spice: !!conf.spice,
1076 xtermjs: !!conf.serial,
54453c38
DC
1077 };
1078 PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
e3129443
DC
1079 }
1080 });
1081 } else if (record.data.type === 'lxc' && !record.data.template) {
1082 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1083 }
1084 },
1085
fbd60cfd
DM
1086 // test automation helper
1087 call_menu_handler: function(menu, text) {
1088
1089 var list = menu.query('menuitem');
1090
1091 Ext.Array.each(list, function(item) {
1092 if (item.text === text) {
1093 if (item.handler) {
1094 item.handler();
1095 return 1;
1096 } else {
1097 return undefined;
1098 }
1099 }
1100 });
1101 },
1102
685b7aa4
DC
1103 createCmdMenu: function(v, record, item, index, event) {
1104 event.stopEvent();
cc1a91be
DC
1105 if (!(v instanceof Ext.tree.View)) {
1106 v.select(record);
1107 }
685b7aa4 1108 var menu;
9bad05bd
DC
1109 var template = !!record.data.template;
1110 var type = record.data.type;
685b7aa4 1111
9bad05bd
DC
1112 if (template) {
1113 if (type === 'qemu' || type == 'lxc') {
1114 menu = Ext.create('PVE.menu.TemplateMenu', {
1115 pveSelNode: record
1116 });
1117 }
1118 } else if (type === 'qemu' ||
1119 type === 'lxc' ||
1120 type === 'node') {
1121 menu = Ext.create('PVE.' + type + '.CmdMenu', {
1122 pveSelNode: record,
c11ab8cb
DC
1123 nodename: record.data.node
1124 });
685b7aa4
DC
1125 } else {
1126 return;
1127 }
1128
1129 menu.showAt(event.getXY());
9f0b4e04 1130 return menu;
e7ade592 1131 },
9fa2e36d 1132
fe4f00ad
TL
1133 // helper for deleting field which are set to there default values
1134 delete_if_default: function(values, fieldname, default_val, create) {
1135 if (values[fieldname] === '' || values[fieldname] === default_val) {
1136 if (!create) {
1137 if (values['delete']) {
1138 values['delete'] += ',' + fieldname;
1139 } else {
1140 values['delete'] = fieldname;
1141 }
1142 }
1143
1144 delete values[fieldname];
1145 }
857b97a7
TL
1146 },
1147
1148 loadSSHKeyFromFile: function(file, callback) {
1149 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1150 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1151 // assume: 740*8 for max. 32kbit (5920 byte file)
1152 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1153 if (file.size > 8192) {
1154 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1155 return;
1156 }
1157 /*global
1158 FileReader
1159 */
1160 var reader = new FileReader();
1161 reader.onload = function(evt) {
1162 callback(evt.target.result);
1163 };
1164 reader.readAsText(file);
abe824aa
DC
1165 },
1166
8c4ec8c7
TL
1167 diskControllerMaxIDs: {
1168 ide: 4,
1169 sata: 6,
cf0d139e 1170 scsi: 31,
8c4ec8c7
TL
1171 virtio: 16,
1172 },
abe824aa
DC
1173
1174 // types is either undefined (all busses), an array of busses, or a single bus
1175 forEachBus: function(types, func) {
8c4ec8c7 1176 var busses = Object.keys(PVE.Utils.diskControllerMaxIDs);
abe824aa
DC
1177 var i, j, count, cont;
1178
1179 if (Ext.isArray(types)) {
1180 busses = types;
1181 } else if (Ext.isDefined(types)) {
1182 busses = [ types ];
1183 }
1184
1185 // check if we only have valid busses
1186 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1187 if (!PVE.Utils.diskControllerMaxIDs[busses[i]]) {
abe824aa
DC
1188 throw "invalid bus: '" + busses[i] + "'";
1189 }
1190 }
1191
1192 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1193 count = PVE.Utils.diskControllerMaxIDs[busses[i]];
abe824aa
DC
1194 for (j = 0; j < count; j++) {
1195 cont = func(busses[i], j);
1196 if (!cont && cont !== undefined) {
1197 return;
1198 }
1199 }
1200 }
14a845bc
DC
1201 },
1202
483bd394 1203 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1204
1205 forEachMP: function(func, includeUnused) {
1206 var i, cont;
1207 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1208 cont = func('mp', i);
1209 if (!cont && cont !== undefined) {
1210 return;
1211 }
1212 }
1213
1214 if (!includeUnused) {
1215 return;
1216 }
1217
1218 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1219 cont = func('unused', i);
1220 if (!cont && cont !== undefined) {
1221 return;
1222 }
1223 }
b945c7c1
TM
1224 },
1225
6c1d8ead 1226 hardware_counts: { net: 32, usb: 5, hostpci: 16, audio: 1, efidisk: 1, serial: 4, rng: 1 },
9d855398 1227
b945c7c1
TM
1228 cleanEmptyObjectKeys: function (obj) {
1229 var propName;
1230 for (propName in obj) {
1231 if (obj.hasOwnProperty(propName)) {
1232 if (obj[propName] === null || obj[propName] === undefined) {
1233 delete obj[propName];
1234 }
1235 }
1236 }
4616a55b
TM
1237 },
1238
1239 handleStoreErrorOrMask: function(me, store, regex, callback) {
1240
1241 me.mon(store, 'load', function (proxy, response, success, operation) {
1242
1243 if (success) {
1244 Proxmox.Utils.setErrorMask(me, false);
1245 return;
1246 }
1247 var msg;
1248
1249 if (operation.error.statusText) {
1250 if (operation.error.statusText.match(regex)) {
1251 callback(me, operation.error);
1252 return;
1253 } else {
1254 msg = operation.error.statusText + ' (' + operation.error.status + ')';
1255 }
1256 } else {
1257 msg = gettext('Connection error');
1258 }
1259 Proxmox.Utils.setErrorMask(me, msg);
1260 });
1261 },
1262
1263 showCephInstallOrMask: function(container, msg, nodename, callback){
1264 var regex = new RegExp("not (installed|initialized)", "i");
1265 if (msg.match(regex)) {
1266 if (Proxmox.UserName === 'root@pam') {
1267 container.el.mask();
1268 if (!container.down('pveCephInstallWindow')){
f992ef80 1269 var isInstalled = msg.match(/not initialized/i) ? true : false;
4616a55b
TM
1270 var win = Ext.create('PVE.ceph.Install', {
1271 nodename: nodename
1272 });
f992ef80 1273 win.getViewModel().set('isInstalled', isInstalled);
4616a55b
TM
1274 container.add(win);
1275 win.show();
1276 callback(win);
1277 }
1278 } else {
a7e8b87b
TL
1279 container.mask(Ext.String.format(gettext('{0} not installed.') +
1280 ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
4616a55b
TM
1281 }
1282 return true;
1283 } else {
1284 return false;
1285 }
49dfba72
DC
1286 },
1287
1288 propertyStringSet: function(target, source, name, value) {
1289 if (source) {
1290 if (value === undefined) {
1291 target[name] = source;
1292 } else {
1293 target[name] = value;
1294 }
1295 } else {
1296 delete target[name];
1297 }
bbc83309
DC
1298 },
1299
1300 updateColumns: function(container) {
f973c5b2
DC
1301 let mode = Ext.state.Manager.get('summarycolumns') || 'auto';
1302 let factor;
1303 if (mode !== 'auto') {
1304 factor = parseInt(mode, 10);
1305 if (Number.isNaN(factor)) {
1306 factor = 1;
1307 }
1308 } else {
1309 factor = container.getSize().width < 1400 ? 1 : 2;
1310 }
bbc83309
DC
1311
1312 if (container.oldFactor === factor) {
1313 return;
1314 }
1315
1316 let items = container.query('>'); // direct childs
1317 factor = Math.min(factor, items.length);
1318 container.oldFactor = factor;
1319
1320 items.forEach((item) => {
1321 item.columnWidth = 1 / factor;
1322 });
1323
1324 // we have to update the layout twice, since the first layout change
1325 // can trigger the scrollbar which reduces the amount of space left
1326 container.updateLayout();
1327 container.updateLayout();
1328 },
e65817a1
SR
1329
1330 forEachCorosyncLink: function(nodeinfo, cb) {
1331 let re = /(?:ring|link)(\d+)_addr/;
1332 Ext.iterate(nodeinfo, (prop, val) => {
1333 let match = re.exec(prop);
1334 if (match) {
1335 cb(Number(match[1]), val);
1336 }
1337 });
1338 },
e7ade592 1339},
fe4f00ad 1340
9fa2e36d
EK
1341 singleton: true,
1342 constructor: function() {
1343 var me = this;
1344 Ext.apply(me, me.utilities);
685b7aa4 1345 }
e7ade592 1346
9fa2e36d 1347});