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