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