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