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