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