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