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