]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
fixup: lxc/Resources: add missing trailing semicolon
[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 352 }
4f3e66d8
DC
353 var vga = PVE.Parser.parsePropertyString(value, 'type');
354 var text = PVE.Utils.kvm_vga_drivers[vga.type];
355 if (!vga.type) {
356 text = Proxmox.Utils.defaultText;
357 }
0be88ae1 358 if (text) {
b0a6d326
EK
359 return text + ' (' + value + ')';
360 }
361 return value;
362 },
363
364 kvm_vga_driver_array: function() {
f2782813 365 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
366 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
367 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
368 });
369
370 return data;
371 },
372
373 render_kvm_startup: function(value) {
374 var startup = PVE.Parser.parseStartup(value);
375
376 var res = 'order=';
377 if (startup.order === undefined) {
378 res += 'any';
379 } else {
380 res += startup.order;
381 }
382 if (startup.up !== undefined) {
383 res += ',up=' + startup.up;
384 }
385 if (startup.down !== undefined) {
386 res += ',down=' + startup.down;
387 }
388
389 return res;
390 },
391
b0a6d326
EK
392 extractFormActionError: function(action) {
393 var msg;
394 switch (action.failureType) {
395 case Ext.form.action.Action.CLIENT_INVALID:
396 msg = gettext('Form fields may not be submitted with invalid values');
397 break;
398 case Ext.form.action.Action.CONNECT_FAILURE:
399 msg = gettext('Connection error');
400 var resp = action.response;
401 if (resp.status && resp.statusText) {
402 msg += " " + resp.status + ": " + resp.statusText;
403 }
404 break;
405 case Ext.form.action.Action.LOAD_FAILURE:
406 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 407 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
408 break;
409 }
410 return msg;
411 },
412
b0a6d326 413 format_duration_short: function(ut) {
0be88ae1 414
b0a6d326 415 if (ut < 60) {
530ee6b7 416 return ut.toFixed(1) + 's';
b0a6d326
EK
417 }
418
419 if (ut < 3600) {
420 var mins = ut / 60;
530ee6b7 421 return mins.toFixed(1) + 'm';
b0a6d326
EK
422 }
423
424 if (ut < 86400) {
425 var hours = ut / 3600;
530ee6b7 426 return hours.toFixed(1) + 'h';
b0a6d326
EK
427 }
428
429 var days = ut / 86400;
530ee6b7 430 return days.toFixed(1) + 'd';
b0a6d326
EK
431 },
432
b0a6d326
EK
433 imagesText: gettext('Disk image'),
434 backupFileText: gettext('VZDump backup file'),
2c554952 435 vztmplText: gettext('Container template'),
b0a6d326 436 isoImageText: gettext('ISO image'),
2c554952 437 containersText: gettext('Container'),
b0a6d326 438
062a7f49
TL
439 storageSchema: {
440 dir: {
441 name: Proxmox.Utils.directoryText,
442 ipanel: 'DirInputPanel',
443 faIcon: 'folder'
444 },
445 lvm: {
446 name: 'LVM',
447 ipanel: 'LVMInputPanel',
448 faIcon: 'folder'
449 },
450 lvmthin: {
451 name: 'LVM-Thin',
452 ipanel: 'LvmThinInputPanel',
453 faIcon: 'folder'
454 },
455 nfs: {
456 name: 'NFS',
457 ipanel: 'NFSInputPanel',
458 faIcon: 'building'
459 },
460 cifs: {
461 name: 'CIFS',
462 ipanel: 'CIFSInputPanel',
463 faIcon: 'building'
464 },
465 glusterfs: {
466 name: 'GlusterFS',
467 ipanel: 'GlusterFsInputPanel',
468 faIcon: 'building'
469 },
470 iscsi: {
471 name: 'iSCSI',
472 ipanel: 'IScsiInputPanel',
473 faIcon: 'building'
474 },
475 sheepdog: {
476 name: 'Sheepdog',
477 ipanel: 'SheepdogInputPanel',
478 hideAdd: true,
479 faIcon: 'building'
480 },
481 rbd: {
482 name: 'RBD',
483 ipanel: 'RBDInputPanel',
062a7f49
TL
484 faIcon: 'building'
485 },
486 pveceph: {
487 name: 'RBD (PVE)',
0d1ac958
TL
488 ipanel: 'RBDInputPanel',
489 hideAdd: true,
062a7f49
TL
490 faIcon: 'building'
491 },
492 zfs: {
493 name: 'ZFS over iSCSI',
494 ipanel: 'ZFSInputPanel',
495 faIcon: 'building'
496 },
497 zfspool: {
498 name: 'ZFS',
499 ipanel: 'ZFSPoolInputPanel',
500 faIcon: 'folder'
501 },
502 drbd: {
503 name: 'DRBD',
504 hideAdd: true
505 }
506 },
507
3c23c025 508 format_storage_type: function(value, md, record) {
6760ab92 509 if (value === 'rbd' && record) {
0d1ac958 510 value = (record.get('monhost') ? 'rbd' : 'pveceph');
3c23c025 511 }
062a7f49
TL
512
513 var schema = PVE.Utils.storageSchema[value];
514 if (schema) {
515 return schema.name;
b0a6d326 516 }
062a7f49 517 return Proxmox.Utils.unknownText;
a3b8efb4
EK
518 },
519
ced1677b 520 format_ha: function(value) {
e7ade592 521 var text = Proxmox.Utils.noneText;
ced1677b
TL
522
523 if (value.managed) {
e7ade592 524 text = value.state || Proxmox.Utils.noneText;
ced1677b 525
e7ade592
DC
526 text += ', ' + Proxmox.Utils.groupText + ': ';
527 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
528 }
529
530 return text;
531 },
532
b0a6d326
EK
533 format_content_types: function(value) {
534 var cta = [];
535
536 Ext.each(value.split(',').sort(), function(ct) {
537 if (ct === 'images') {
538 cta.push(PVE.Utils.imagesText);
539 } else if (ct === 'backup') {
540 cta.push(PVE.Utils.backupFileText);
541 } else if (ct === 'vztmpl') {
542 cta.push(PVE.Utils.vztmplText);
543 } else if (ct === 'iso') {
544 cta.push(PVE.Utils.isoImageText);
545 } else if (ct === 'rootdir') {
546 cta.push(PVE.Utils.containersText);
547 }
548 });
549
550 return cta.join(', ');
551 },
552
553 render_storage_content: function(value, metaData, record) {
554 var data = record.data;
555 if (Ext.isNumber(data.channel) &&
556 Ext.isNumber(data.id) &&
557 Ext.isNumber(data.lun)) {
0be88ae1
DC
558 return "CH " +
559 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
560 " ID " + data.id + " LUN " + data.lun;
561 }
562 return data.volid.replace(/^.*:(.*\/)?/,'');
563 },
564
565 render_serverity: function (value) {
566 return PVE.Utils.log_severity_hash[value] || value;
567 },
568
569 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
570
571 if (!(record.data.uptime && Ext.isNumeric(value))) {
572 return '';
573 }
574
575 var maxcpu = record.data.maxcpu || 1;
576
577 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
578 return '';
579 }
0be88ae1 580
b0a6d326
EK
581 var per = value * 100;
582
583 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
584 },
585
586 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
587 /*jslint confusion: true */
588
589 if (!Ext.isNumeric(value)) {
590 return '';
591 }
592
e7ade592 593 return Proxmox.Utils.format_size(value);
b0a6d326
EK
594 },
595
946730cd
DC
596 render_bandwidth: function(value) {
597 if (!Ext.isNumeric(value)) {
598 return '';
599 }
600
e7ade592 601 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
602 },
603
3f633655
EK
604 render_timestamp_human_readable: function(value) {
605 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
606 },
607
ddd26302
DC
608 render_duration: function(value) {
609 if (value === undefined) {
610 return '-';
611 }
612 return PVE.Utils.format_duration_short(value);
613 },
614
0bfc799f
DC
615 calculate_mem_usage: function(data) {
616 if (!Ext.isNumeric(data.mem) ||
617 data.maxmem === 0 ||
618 data.uptime < 1) {
619 return -1;
620 }
621
622 return (data.mem / data.maxmem);
623 },
624
625 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
626 if (!Ext.isNumeric(value) || value === -1) {
627 return '';
628 }
629 if (value > 1 ) {
630 // we got no percentage but bytes
631 var mem = value;
632 var maxmem = record.data.maxmem;
633 if (!record.data.uptime ||
634 maxmem === 0 ||
635 !Ext.isNumeric(mem)) {
636 return '';
637 }
638
639 return ((mem*100)/maxmem).toFixed(1) + " %";
640 }
641 return (value*100).toFixed(1) + " %";
642 },
643
b0a6d326
EK
644 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
645
646 var mem = value;
647 var maxmem = record.data.maxmem;
0be88ae1 648
b0a6d326
EK
649 if (!record.data.uptime) {
650 return '';
651 }
652
653 if (!(Ext.isNumeric(mem) && maxmem)) {
654 return '';
655 }
656
728f1b97 657 return PVE.Utils.render_size(value);
b0a6d326
EK
658 },
659
0bfc799f
DC
660 calculate_disk_usage: function(data) {
661
662 if (!Ext.isNumeric(data.disk) ||
663 data.type === 'qemu' ||
664 (data.type === 'lxc' && data.uptime === 0) ||
665 data.maxdisk === 0) {
666 return -1;
667 }
668
669 return (data.disk / data.maxdisk);
670 },
671
672 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
673 if (!Ext.isNumeric(value) || value === -1) {
674 return '';
675 }
676
677 return (value * 100).toFixed(1) + " %";
678 },
679
b0a6d326
EK
680 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
681
682 var disk = value;
683 var maxdisk = record.data.maxdisk;
728f1b97 684 var type = record.data.type;
b0a6d326 685
728f1b97
DC
686 if (!Ext.isNumeric(disk) ||
687 type === 'qemu' ||
688 maxdisk === 0 ||
689 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
690 return '';
691 }
692
728f1b97 693 return PVE.Utils.render_size(value);
b0a6d326
EK
694 },
695
4dbc64a7
DC
696 get_object_icon_class: function(type, record) {
697 var status = '';
698 var objType = type;
699
700 if (type === 'type') {
701 // for folder view
702 objType = record.groupbyid;
703 } else if (record.template) {
704 // templates
705 objType = 'template';
706 status = type;
707 } else {
708 // everything else
709 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
710 }
711
4dbc64a7
DC
712 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
713 if (defaults && defaults.iconCls) {
714 var retVal = defaults.iconCls + ' ' + status;
715 return retVal;
b0a6d326
EK
716 }
717
4dbc64a7
DC
718 return '';
719 },
720
721 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
722
723 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 724
4dbc64a7 725 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 726 return fa + value;
b0a6d326
EK
727 },
728
b0a6d326
EK
729 render_support_level: function(value, metaData, record) {
730 return PVE.Utils.support_level_hash[value] || '-';
731 },
732
0be88ae1 733 render_upid: function(value, metaData, record) {
b0a6d326
EK
734 var type = record.data.type;
735 var id = record.data.id;
736
e7ade592 737 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
738 },
739
054ac1b8
DC
740 /* render functions for new status panel */
741
742 render_usage: function(val) {
743 return (val*100).toFixed(2) + '%';
744 },
745
746 render_cpu_usage: function(val, max) {
747 return Ext.String.format(gettext('{0}% of {1}') +
748 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
749 },
750
751 render_size_usage: function(val, max) {
bab64974
DC
752 if (max === 0) {
753 return gettext('N/A');
754 }
054ac1b8
DC
755 return (val*100/max).toFixed(2) + '% '+ '(' +
756 Ext.String.format(gettext('{0} of {1}'),
757 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
758 },
759
760 /* this is different for nodes */
761 render_node_cpu_usage: function(value, record) {
762 return PVE.Utils.render_cpu_usage(value, record.cpus);
763 },
764
765 /* this is different for nodes */
766 render_node_size_usage: function(record) {
767 return PVE.Utils.render_size_usage(record.used, record.total);
768 },
769
27809975
DC
770 render_optional_url: function(value) {
771 var match;
772 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 773 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
774 }
775 return value;
776 },
777
778 render_san: function(value) {
779 var names = [];
780 if (Ext.isArray(value)) {
781 value.forEach(function(val) {
782 if (!Ext.isNumber(val)) {
783 names.push(val);
784 }
785 });
786 return names.join('<br>');
787 }
788 return value;
789 },
790
aa0819a8 791 windowHostname: function() {
e7ade592 792 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
793 function(m, addr, offset, original) { return addr; });
794 },
0be88ae1 795
3438c27e
DC
796 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname) {
797 var dv = PVE.Utils.defaultViewer(consoles);
b0a6d326
EK
798 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
799 },
800
801 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 802 // kvm, lxc, shell, upgrade
b0a6d326 803
9e361643 804 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
805 throw "missing vmid";
806 }
807
808 if (!nodename) {
809 throw "no nodename specified";
810 }
811
c7218ab3
DC
812 if (viewer === 'html5') {
813 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
c6b2336c 814 } else if (viewer === 'xtermjs') {
e7ade592 815 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
816 } else if (viewer === 'vv') {
817 var url;
aa0819a8 818 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
819 if (vmtype === 'kvm') {
820 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
821 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
822 } else if (vmtype === 'lxc') {
823 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
824 PVE.Utils.openSpiceViewer(url, params);
825 } else if (vmtype === 'shell') {
826 url = '/nodes/' + nodename + '/spiceshell';
827 PVE.Utils.openSpiceViewer(url, params);
828 } else if (vmtype === 'upgrade') {
829 url = '/nodes/' + nodename + '/spiceshell';
830 params.upgrade = 1;
831 PVE.Utils.openSpiceViewer(url, params);
832 }
833 } else {
834 throw "unknown viewer type";
835 }
836 },
837
3438c27e
DC
838 defaultViewer: function(consoles) {
839
840 var allowSpice, allowXtermjs;
841
842 if (consoles === true) {
843 allowSpice = true;
844 allowXtermjs = true;
845 } else if (typeof consoles === 'object') {
846 allowSpice = consoles.spice;
4ace5c6f 847 allowXtermjs = !!consoles.xtermjs;
3438c27e 848 }
b0a6d326
EK
849 var vncdefault = 'html5';
850 var dv = PVE.VersionInfo.console || vncdefault;
3438c27e 851 if ((dv === 'vv' && !allowSpice) || (dv === 'xtermjs' && !allowXtermjs)) {
b0a6d326
EK
852 dv = vncdefault;
853 }
854
855 return dv;
856 },
857
c7218ab3 858 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 859 var url = Ext.urlEncode({
9e361643 860 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 861 novnc: 1,
b0a6d326
EK
862 vmid: vmid,
863 vmname: vmname,
16e64c97
DC
864 node: nodename,
865 resize: 'off'
b0a6d326
EK
866 });
867 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
868 nw.focus();
869 },
870
871 openSpiceViewer: function(url, params){
872
873 var downloadWithName = function(uri, name) {
874 var link = Ext.DomHelper.append(document.body, {
875 tag: 'a',
876 href: uri,
877 css : 'display:none;visibility:hidden;height:0px;'
878 });
879
880 // Note: we need to tell android the correct file name extension
881 // but we do not set 'download' tag for other environments, because
882 // It can have strange side effects (additional user prompt on firefox)
883 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
884 if (andriod) {
885 link.download = name;
886 }
887
888 if (link.fireEvent) {
889 link.fireEvent('onclick');
890 } else {
891 var evt = document.createEvent("MouseEvents");
892 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
893 link.dispatchEvent(evt);
894 }
895 };
896
e7ade592 897 Proxmox.Utils.API2Request({
b0a6d326
EK
898 url: url,
899 params: params,
900 method: 'POST',
901 failure: function(response, opts){
902 Ext.Msg.alert('Error', response.htmlStatus);
903 },
904 success: function(response, opts){
905 var raw = "[virt-viewer]\n";
906 Ext.Object.each(response.result.data, function(k, v) {
907 raw += k + "=" + v + "\n";
908 });
909 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
910 encodeURIComponent(raw);
0be88ae1 911
b0a6d326
EK
912 downloadWithName(url, "pve-spice.vv");
913 }
914 });
915 },
916
e3129443
DC
917 openTreeConsole: function(tree, record, item, index, e) {
918 e.stopEvent();
919 var nodename = record.data.node;
920 var vmid = record.data.vmid;
921 var vmname = record.data.name;
922 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 923 Proxmox.Utils.API2Request({
e3129443
DC
924 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
925 failure: function(response, opts) {
926 Ext.Msg.alert('Error', response.htmlStatus);
927 },
928 success: function(response, opts) {
ab9f0fe1 929 var allowSpice = !!response.result.data.spice;
e3129443
DC
930 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
931 }
932 });
933 } else if (record.data.type === 'lxc' && !record.data.template) {
934 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
935 }
936 },
937
fbd60cfd
DM
938 // test automation helper
939 call_menu_handler: function(menu, text) {
940
941 var list = menu.query('menuitem');
942
943 Ext.Array.each(list, function(item) {
944 if (item.text === text) {
945 if (item.handler) {
946 item.handler();
947 return 1;
948 } else {
949 return undefined;
950 }
951 }
952 });
953 },
954
685b7aa4
DC
955 createCmdMenu: function(v, record, item, index, event) {
956 event.stopEvent();
cc1a91be
DC
957 if (!(v instanceof Ext.tree.View)) {
958 v.select(record);
959 }
685b7aa4 960 var menu;
9bad05bd
DC
961 var template = !!record.data.template;
962 var type = record.data.type;
685b7aa4 963
9bad05bd
DC
964 if (template) {
965 if (type === 'qemu' || type == 'lxc') {
966 menu = Ext.create('PVE.menu.TemplateMenu', {
967 pveSelNode: record
968 });
969 }
970 } else if (type === 'qemu' ||
971 type === 'lxc' ||
972 type === 'node') {
973 menu = Ext.create('PVE.' + type + '.CmdMenu', {
974 pveSelNode: record,
c11ab8cb
DC
975 nodename: record.data.node
976 });
685b7aa4
DC
977 } else {
978 return;
979 }
980
981 menu.showAt(event.getXY());
e7ade592 982 },
9fa2e36d 983
fe4f00ad
TL
984 // helper for deleting field which are set to there default values
985 delete_if_default: function(values, fieldname, default_val, create) {
986 if (values[fieldname] === '' || values[fieldname] === default_val) {
987 if (!create) {
988 if (values['delete']) {
989 values['delete'] += ',' + fieldname;
990 } else {
991 values['delete'] = fieldname;
992 }
993 }
994
995 delete values[fieldname];
996 }
857b97a7
TL
997 },
998
999 loadSSHKeyFromFile: function(file, callback) {
1000 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1001 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1002 // assume: 740*8 for max. 32kbit (5920 byte file)
1003 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1004 if (file.size > 8192) {
1005 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1006 return;
1007 }
1008 /*global
1009 FileReader
1010 */
1011 var reader = new FileReader();
1012 reader.onload = function(evt) {
1013 callback(evt.target.result);
1014 };
1015 reader.readAsText(file);
abe824aa
DC
1016 },
1017
1018 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
1019
1020 // types is either undefined (all busses), an array of busses, or a single bus
1021 forEachBus: function(types, func) {
1022 var busses = Object.keys(PVE.Utils.bus_counts);
1023 var i, j, count, cont;
1024
1025 if (Ext.isArray(types)) {
1026 busses = types;
1027 } else if (Ext.isDefined(types)) {
1028 busses = [ types ];
1029 }
1030
1031 // check if we only have valid busses
1032 for (i = 0; i < busses.length; i++) {
1033 if (!PVE.Utils.bus_counts[busses[i]]) {
1034 throw "invalid bus: '" + busses[i] + "'";
1035 }
1036 }
1037
1038 for (i = 0; i < busses.length; i++) {
1039 count = PVE.Utils.bus_counts[busses[i]];
1040 for (j = 0; j < count; j++) {
1041 cont = func(busses[i], j);
1042 if (!cont && cont !== undefined) {
1043 return;
1044 }
1045 }
1046 }
14a845bc
DC
1047 },
1048
483bd394 1049 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1050
1051 forEachMP: function(func, includeUnused) {
1052 var i, cont;
1053 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1054 cont = func('mp', i);
1055 if (!cont && cont !== undefined) {
1056 return;
1057 }
1058 }
1059
1060 if (!includeUnused) {
1061 return;
1062 }
1063
1064 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1065 cont = func('unused', i);
1066 if (!cont && cont !== undefined) {
1067 return;
1068 }
1069 }
e7ade592
DC
1070 }
1071},
fe4f00ad 1072
9fa2e36d
EK
1073 singleton: true,
1074 constructor: function() {
1075 var me = this;
1076 Ext.apply(me, me.utilities);
685b7aa4 1077 }
e7ade592 1078
9fa2e36d 1079});
b0a6d326 1080